help with tic tac toe program

This is a discussion on help with tic tac toe program within the C++ Programming forums, part of the General Programming Boards category; Hi i'm trying to do a program that allows two users or one to play a game of tic tac ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    Unhappy help with tic tac toe program

    Hi i'm trying to do a program that allows two users or one to play a game of tic tac toe....my program goes well until someone wins and the program keeps asking for the next X or O to be entered...could someone help me?


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      char game[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};  
      char choice;
      
      int row, column, count=0, winner=0;
      
      cout<<"Lets play a game of TicTacToe! Here's the starting game board: "<<endl;
      
      for(int i=0; i<3; i++)
      {
         for(int j=0; j<3; j++)
    	 {
    		 cout << game[i][j]<<"  ";
    	 }
    	 cout<<endl;
      }
     
      do
      {
    	cout<<"Are you X or O? Please enter a captial X or O:  ";
        cin>>choice;
    
    	cout<<"Enter the row and column to place your "<<choice<<".  ";
    	cin>>row>>column;
    	
    	game[row][column]=choice;
    
    	count+=1;
    
    	for(int i=0; i<3; i++)
    	{
    		for(int j=0; j<3; j++)
    		{
    			cout << game[i][j]<<"  ";
    		}
    		
    		cout<<endl;
    	}
    
    	if(game[0][0]=='X' && game[0][1]=='X' && game[0][2]=='X')
    	{
    		winner=1;
    
    		cout<<game[0][0]<<" wins!!"<<endl;
    	}
    	
    	else if(game[1][0]=='X' && game[1][1]=='X' && game[1][2]=='X')
    	{
    		winner=1;
    		 
    		cout<<game[1][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[2][0]='X' && game[2][1]=='X' && game[2][2]=='X')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][0]=='X' && game[1][0]=='X' && game[2][0]=='X')
    	{
    		winner=1;
    
    		cout <<game[0][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][1]=='X' && game[1][1]=='X' && game[2][1]=='X')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    	
    	else if(game[0][2]=='X' && game[1][2]=='X' && game[2][2]=='X')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][0]=='X' && game[1][1]=='X' && game[2][2]=='X')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][2]=='X' && game[1][1]=='X' && game[2][0]=='X')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	 else if(game[0][0]=='O' && game[0][1]=='O' && game[0][2]=='O')
    	{
    		winner=1;
    
    		cout<<game[0][0]<<" wins!!"<<endl;
    	}
    	
    	else if(game[1][0]=='O' && game[1][1]=='O' && game[1][2]=='O')
    	{
    		winner=1;
    		 
    		cout<<game[1][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[2][0]=='O' && game[2][1]=='O' && game[2][2]=='O')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][0]=='O' && game[1][0]=='O' && game[2][0]=='O')
    	{
    		winner=1;
    
    		cout <<game[0][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][1]=='O' && game[1][1]=='O' && game[2][1]=='O')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    	
    	else if(game[0][2]=='O' && game[1][2]=='O' && game[2][2]=='O')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][0]=='O' && game[1][1]=='O' && game[2][2]=='O')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    
    	else if(game[0][2]=='O' && game[1][1]=='O' && game[2][0]=='O')
    	{
    		winner=1;
    
    		cout <<game[2][0]<<" wins!!"<<endl;
    	}
    	
    	else if(count>8)
    	  cout<<"There is no winner";
    
      }while(winner==0 && count<8);
    
      
      return 0;
    }

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    A lot of else-if here! I have not scrutinized your code to closely but you may need this:

    Code:
    while( winner== 0 || count > 8 );   /*You may not be satisfying both conditionals here? If this is met you have a DRAW!  */
    Last edited by slingerland3g; 12-14-2009 at 01:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  2. Check tic tac toe winner
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 11:41 AM
  3. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  4. My First c++ program : TIC TAC TOE !
    By renderstream in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2004, 03:58 PM
  5. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 07:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21