Hello all,
Been working on this assignment and I am running into this problem I cannot seem to solve.
Basically in this assignment I have to design a Tic - Tac - Toe array where the user will input the size of the array (has to be a perfect square other than 9) and I have to do this by declaring a pointer using malloc and then using pointer arithmetic to perform the various calculations.
The code that follows is still incomplete but it seems that in function players() when I try to assign a value to *TTT_Ptr on the line *((TTT_Ptr)+numx-1)= -1; it only works the first time around, on the subsequent passes nothing seems to work and I cannot figure out why.
Can anyone point out where my mistake is? The code is following please focus on the players() function. Nervermind the rest unless it is related to the error.
Thank you in advance.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void display(void);
int ifWon(void);
void players(void);
/* Define one Integer for Array Size and
another one as a pointer to the Array */
int TTT_Size, *TTT_Ptr, SQR_Size;
main()
{
int i,total;
char Continue='y';
/* Take input of the array size */
while ((SQR_Size* SQR_Size)!=TTT_Size || TTT_Size!=9){
printf("\n Enter the size of the Tic-Tac-Toe Array : ");
scanf("%d",&TTT_Size);
SQR_Size = sqrt(TTT_Size);
if ((SQR_Size* SQR_Size)!=TTT_Size || TTT_Size==9){
printf("\n Sorry, the number has to be a perfect square\n");
printf("\n other than 9. Please try again! \n");
TTT_Size--;}
else
break;
}
TTT_Ptr = (int*) malloc(TTT_Size*(sizeof(int)));
do {
for (i=0;i<=TTT_Size;i++)
*((TTT_Ptr)+i) = i+1;
display();
do {
players();
if(ifWon()==1){
printf("\n\nPlayer 1 WINS !!!\n");
break;
}
if(ifWon()==0){
for(i=0,total=0;i<3*3;i++)
total += *(TTT_Ptr+i);
/* check if game drawn resulting in 5 X and 4 O like
x o x
x o o
o x x
*/
if(total==(-1*(5)+-2*4))
break;
}
/* playerO();
display(); */
if(ifWon()==2){
printf("\n\nPlayer 2 WINS !!!\n");
break;
}
if(ifWon()==0){
for(i=0,total=0;i<3*3;i++)
total+= *(TTT_Ptr+i);
/* check if game drawn resulting in 5 X and 4 O like
x o x
x o o
o x x
*/
if(total==(-1*(5)+-2*4))
break;
}
} while(ifWon()==0);
if(ifWon()==0)
printf("\nNobody WON!!!");
printf("\n\nPress n/N to quit, any other key to continue :");
getchar();
scanf("%c",&Continue);
} while((Continue!='N')&&(Continue!='n'));{
free(TTT_Ptr);
return 0;}
}
/* ----------- Start Display Function ---------------------------------------*/
void display(void){
int i,j,line_jump=SQR_Size-1;
printf("\n\nCurrent Contents of Tic Tac Toe\n\n");
for(i=0;i<TTT_Size;i++){
if((*(TTT_Ptr)+i)==-1)
printf(" X");
else if ((*(TTT_Ptr)+i)==-2)
printf(" O");
else
printf("%3d",(i+1));
if(i==line_jump)
{printf("\n");
line_jump = line_jump+SQR_Size;}
}
}
/* ifWon returns 1 if player X wins, return 2 if
player O wins otherwise return 0 */
int ifWon(void){
int i,j,k,sumInARow,sumInLDiagonal=0,sumInRDiagonal=0;
int sum[3];
for(k=0;k<3;k++)
sum[k]=0;
for(i=0;i<3;i++){ /* repeat for each row */
sumInARow=0;
for(j=0;j<3;j++){ /* repeat for each column */
sumInARow+=*(TTT_Ptr+j);
if(i==j) /* diagonal formed with [0][0],[1][1],[2][2] */
sumInLDiagonal+=*(TTT_Ptr+j);
if((i+j)==(2))/* diagonal formed with [0][2],[1][1],[2][0] */
sumInRDiagonal+=(*(TTT_Ptr+i)+ *(TTT_Ptr+j));
for(k=0;k<j;k++); /* move tot4 to point to current column*/
sum[k]= *(TTT_Ptr+k); /* add the sum for current column */
}
if(sumInARow==-1*3) /* if all X in this row */
return 1; /* Player X wins */
if(sumInARow==-2*3) /* if all O in this row */
return 2; /* Player O wins */
}
/* tot3 is a 3 element array, where each element stores
the sum of numbers in that column */
for(i=0;i<3;i++){ /* repeat for each column */
if(sum[i]==-1*3) /* if all X in this column*/
return 1; /* Player X wins */
if(sum[i]==-2*3) /* if all O in this column*/
return 2; /* Player O wins */
}
if((sumInLDiagonal==-1*3)||(sumInRDiagonal==-1*3)) /* if all X in any of the diagonal*/
return 1; /* Player X wins */
if((sumInLDiagonal==-2*3)||(sumInRDiagonal==-2*3)) /* if all O in any of the diagonal*/
return 2; /* player O wins */
return 0;
}
/* ------------------------ Players Function ------------------------------*/
void players(){
int numx,numo,i;
while((numx>=1) && (numx<= SQR_Size * SQR_Size) && (numx==*(TTT_Ptr-1)));
{/* Repeat until a number b/w 1 to TTT_Size is entered */
printf("\n\n PLAYER 1: Enter a number (from 1 to %d)",TTT_Size);
printf (" to put an X : ");
scanf("%d",&numx);
for(i=1;i<numx;i++);
*((TTT_Ptr)+numx-1)= -1;
/* *(TTT_Ptr+i-1)=-1;*/ /* -1 marks an entry 'X'*/
}
display();
while((numo>=1) && (numo<= SQR_Size * SQR_Size) && (numo==*(TTT_Ptr-1)));
{ /* Repeat until a number b/w 1 to TTT_Size is entered */
printf("\n\n PLAYER 2: Enter a number (from 1 to %d) )",TTT_Size);
printf ("to put an O : ");
scanf("%d",&numo);
for(i=1;i<numo;i++);
*((TTT_Ptr)+numo -1)= -2;/* -2 marks an entry as 'O' */
}
display();
}