Tic Tac Toe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cl2020
    New Member
    • May 2010
    • 2

    Tic Tac Toe

    I have a code. It is running after built. However, when running it only played only one person and ended. The intention of making it is to make two players playing tic tac toe. Where do I go wrong in this code.

    Code:
    //Timing.
    #include<iostream>
    using namespace std;
    
    int check( char g[3][3], int r, int c, int p)
    {
    	if ( g[r][c] !='*')
    	{
    		cout << "\n\tSorry, wrong entry.";
    		return 0;
    	}
    
    	char ch;
    	if (p == 0)
    		ch= 'X';
    
    	if (p == 1)
    		ch= 'O';
    
    	g[r][c] = ch;
    	
    	cout << "\n\n";
    	for ( int i = 0; i < 3; i++)
    	{
    		for (int j = 0; j < 3; j++ )
    			cout << "   " << g[i][j];
    
    		cout << "\n";
    	}
    
    	if(//Row Section
    		(( g [0][0]==ch) && (g[0][1]==ch)
    		&& (g[0][2]==ch)) || ((g[1][0]==ch)
    		&& (g[1][1]==ch) && (g[1][2]==ch))
    		||((g[2][0]==ch) && (g[2][1]==ch)
    		&& (g[2][2]==ch))||
    		//Column Section
    		((g[0][0]==ch) && (g[1][0]==ch)
    		&& (g[2][0]==ch))|| ((g[0][1]==ch)
    		&& (g[1][1]==ch) && (g[2][1]==ch))||
    		((g[0][2]==ch)&& (g[1][2]==ch)&&
    						(g[2][2]==ch))||
    		//Diagonal Sections 
    		((g[0][0]==ch)&&(g[1][1]==ch)&&
    		(g[2][2]==ch))||((g[0][2]==ch)&&
    			(g[1][1]==ch)&& (g[2][0]==ch))
    	)
    		return -1;
    
    	return 1;
    }
    //Main Execution 
    int main( )
    {
    	char game[3][3];
    	int row, col;
    	int count=0;
    	int result;
    	int end;
    
    	cout << "\t\t\tTic-Tac-Toe Game\n";
    
    	for (int i = 0; i < 3; i++)
    		for (int j = 0; j < 3; j++)
    			game [i][j]='*';
    			
    	do
    	{
    		cout << "\n\n\tPlayer " << (( count % 2));
    		cout << "\n\tEnter row:  ";
    		cin >> row;
    
    			cout << "\tEnter column:  ";
    			cin >> col;
    
    		//Play!
    			result=check(game, row-1, col-1, count % 2);
    
    			if ( result == -1)
    			{
    				cout << "\n\n\t Player " << (( count % 2) + 1)
    					<< "Wins! Congratulaions =) \n";
    				break;
    
    		}
    	}
    	while (end == 0);
    
    	return 0;
    }
    Last edited by Banfa; Jun 10 '10, 08:15 AM. Reason: code and been pasted twice
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Check what you are doing with the variable count in main.

    Comment

    Working...