0

```
#include<stdio.h>
// Global variable declaration and initialization. can be used every where in the program body.
static char grid[3][3] ={{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}
};
void Print()//--- to print the grid. prints the grid.
{
int i, j;
printf("\t-------------\n");
for(i = 0; i < 3; i++)
{
printf("\t");
for(j = 0; j < 3; j++)
{
printf("| %c ",grid[i][j]);
}
printf("|\n\t-------------\n");
}
}
void UserLocationInput() // ---- this accepts the row and column. user plays to win.
{
int row, coll;
X:printf("Enter Row and coll number.\n");
scanf("%d%d", &row, &coll);
if((row < 0 || row > 2) || (coll < 0 || coll > 2))
{
printf("Enter valid row and coll b/n (0 and 2)\n");
goto X;
}
if(grid[row][coll] == ' ')
{
grid[row][coll] = '0';
printf("\nyou move to [%d,%d]\n",row, coll);
}
else
{
printf("Sorry!! [%d,%d] is already alocated!\n",row, coll);
goto X;
}
}
int Move()// ---- move acording 1 & 2. This func returns 1 if it made a move 0 otherwise. computer blocks the player.
{
int i, j;
for(i = 0; i < 3; i++)//for Winning Movement.
{
if(grid[i][0] == grid[i][1] && grid[i][2] == ' ' && grid[i][0] == '*')
{
grid[i][2] = '*';
printf("\nComputer moves to [%d,2]\n",i);
return 1;
}
else if(grid[i][0] == grid[i][2] && grid[i][1] == ' ' && grid[i][0] == '*')
{
grid[i][1] = '*';
printf("\nComputer moves to [%d,1]\n",i);
return 1;
}
else //changed
{
if(grid[i][1] == grid[i][2] && grid[i][0] == ' ' && grid[i][1] == '*')
{
grid[i][0] = '*';
printf("\nComputer moves to [%d,%d]\n",i,0);
return 1;
}
}
}
for(i = 0; i < 3; i++)// for Defense Movements.
{
if(grid[i][0] == grid[i][1] && grid[i][2] == ' ' && grid[i][0] == '0')
{
grid[i][2] = '*';
printf("\nComputer moves to [%d,2]\n",i);
return 1;
}
else if(grid[i][0] == grid[i][2] && grid[i][1] == ' ' && grid[i][0] == '0')
{
grid[i][1] = '*';
printf("\nComputer moves to [%d,1]\n",i);
return 1;
}
else if(grid[i][1] == grid[i][2] && grid[i][0] == ' ' && grid[i][1] == '0')
{
grid[i][0] = '*';
printf("\nComputer moves to [%d,0]\n",i);
return 1;
}
}
return 0;
}
void MoveAttack()// move base on procedures 3 & 4 computer moves to win the game.
{
int i, j;
if(grid[0][0] != '0' && grid[0][1] != '0' && grid[0][2] != '0')
{
if(grid[0][0] == ' ')
{
grid[0][0] = '*';
printf("\nComputer moves to [%d,%d]\n",0,0);
}
else if(grid[0][1] == ' ')
{
grid[0][1] = '*';
printf("\nComputer moves to [%d,%d]\n",0,1);
}
else if(grid[0][2] == ' ')
{
grid[0][2] = '*';
printf("\nComputer moves to [%d,%d]\n",0,2);
}
}
else if(grid[1][0] != '0' && grid[1][1] != '0' && grid[1][2] != '0')
{
if(grid[1][0] == ' ')
{
grid[1][0] = '*';
printf("\nComputer moves to [%d,%d]\n",1,0);
}
else if(grid[1][1] == ' ')
{
grid[1][1] = '*';
printf("\nComputer moves to [%d,%d]\n",1,1);
}
else if(grid[1][2] == ' ')
{
grid[1][2] = '*';
printf("\nComputer moves to [%d,%d]\n",1,2);
}
}
else if(grid[2][0] != '0' && grid[2][1] != '0' && grid[2][2] != '0' )
{
if(grid[2][0] == ' ')
{
grid[2][0] = '*';
printf("\nComputer moves to [%d,%d]\n",2,0);
}
else if(grid[2][1] == ' ')
{
grid[2][1] = '*';
printf("\nComputer moves to [%d,%d]\n",2,1);
}
else if(grid[2][2] == ' ')
{
grid[2][2] = '*';
printf("\nComputer moves to [%d,%d]\n",2,2);
}
}
else
{
for(i = 0; i < 3; i++)
{
if(grid[i][0] == ' ' )
{
grid[i][0] = '*';
printf("\nComputer moves to [%d,%d]\n",i,0);
break;
}
else if( grid[i][1] == ' ' )
{
grid[i][1] = '*';
printf("\nComputer moves to [%d,%d]\n",i,1);
break;
}
else if(grid[i][2] == ' ')
{
grid[i][2] = '*';
printf("\nComputer moves to [%d,%d]\n",i,2);
break;
}
}
}
}
int CheckStatus()//check stutes.. returns 1 if one wins or if it is imposible to for any to win.
{
int i, j;
for(i = 0; i < 3; i++)
{
if(grid[i][0] == grid[i][1] && grid[i][2] == grid[i][1] && grid[i][0] == '*')
{
printf("\nOh!!. Computer Won!!\n");
return 1;
}
else if(grid[i][0] == grid[i][1] && grid[i][2] == grid[i][1] && grid[i][0] == '0')
{
printf("\nCongr!!. You Won!!\n");
return 1;
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
/*
* Checking all possibilities that can happen
* a game drawn conditions.
*/
if(((grid[0][0] != ' ' && grid[0][1] != ' ') || (grid[0][0] != ' ' && grid[0][2] != ' ') || (grid[0][1] != ' ' && grid[0][2] != ' '))
&& ((grid[1][0] != ' ' && grid[1][1] != ' ') || (grid[1][0] != ' ' && grid[1][2] != ' ') || (grid[1][1] != ' ' && grid[1][2] != ' '))
&& ((grid[2][0] != ' ' && grid[2][1] != ' ') || (grid[2][0] != ' ' && grid[2][2] != ' ') || (grid[2][1] != ' ' && grid[2][2] != ' ')))
{
if((grid[0][0] != grid[0][1] && grid[1][0] != grid[1][1] && grid[2][0] != grid[2][1])
|| (grid[0][0] != grid[0][2] && grid[1][0] != grid[1][2] && grid[2][0] != grid[2][2])
|| (grid[0][1] != grid[0][2] && grid[1][1] != grid[1][2] && grid[2][1] != grid[2][2]))
{
printf("\n Game is drawn!\n");
return 1;
}
else return 0;
}
else
{
return 0;
}
}
}
}
int main()
{
printf("You are player 'Nought' == [0]\nThe computer is player 'Cross' == [*]\n\n");
int i = 0, j = 0;
Print();
int x=0;
while(x=0)// infinite loop. if check stutus returns true(1) loop breakes and exits.
{
UserLocationInput();
Print();
if(Move() == 1)
{
Print();
if(CheckStatus() == 1)
{break;}
else{continue;}
}
MoveAttack();
Print();
if(CheckStatus() == 1)
{break;}
else{continue;}
}// End loop.
return 0;
}
```