#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;
}

This posting is similar to one posted a day or two ago related to a 4x4 Bingo. My recommendations are the same to you!

You are doing a 3x3, so use a bit array!

0   1    2                0x001   0x002   0x004
3   4    5         so   0x008   0x010    0x020
6   7    8                0x040   0x080    0x100

When a slot is chosen set a bit
MyPick = 3
b |= 1 << MyPick
So b = 0x008 ; 000001000

Then to do your pattern match...
Second Row pattern

000
XXX                       = 0x38
000

So to match your pattern

if ( (b & 0x38) == 0x38) pattern found


You can insert the pattern codes { 0x38, etc. }
into a table lookup to save space!

This should drastically reduce the size of your code!

This article has been dead for over six months. Start a new discussion instead.