Another TicTacToe problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wizardRahl
    New Member
    • Oct 2006
    • 40

    #1

    Another TicTacToe problem

    Hello,
    I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users to play tic-tac-toe. The program needs to have turns alternately from player X and player O.
    I have defined the board as char board[9]. The real problem is that in my loop that I have set up it's not giving me a chance to input each player's positions. I get a compilation error saying:
    "Error E2377 lab4.cpp 31: If statement missing ) in function main()"

    I've double and triple check my closed parenthesis and it seems to be correct, yet I have been staring at the screen for a while and it might need a fresh set of eyes.

    Also, I dont have to draw a board of anything, just display it like so:
    1 2 3
    4 5 6
    7 8 9
    Players then choose the position they would like and it holds and marks the value for them. I haven't started with the postion, but would like advice on how to do so. I know that you will have to start the sequence with 0 and work up to 8 instead of 1-9.

    Any direction or advice would be most welcomed and appreciated.
    Here's what I have so far.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int Xpos, Opos, turn;
    	char board[9];
    	
    	cout << "\nPlease enter a number corresponding to the board\n"
    	     << "to mark your spot. 3 in a row wins the game.\n";
    	cout << "              Good Luck!                      \n\n";
    
    	
    	cout << "\n1 2 3\n"
    		  << "4 5 6\n"
    		  << "7 8 9\n";
    
    
    
    	for (int turn = 1; turn < 10; turn++)			//checks turn of player
    	   	{
    	do
    	{
    		if (turn !(% 2) == 0)   //*heres where i get the error*
    		
    		{	cout << "Player X, your move.\n"		
    		
    			 cin >> board[Xpos];								//Player X input
    		}	  
    	    else
    			  		 												
    		 	  {	cout << "Player O, your move\n"
    			
    				        << board[Xpos] << endl;			//Updated board
    			  
    			     cin >> board[Opos];						//Player O input
    				  cout << board[Opos];						//Updated board
    			  }
    
         }
    	 while (turn < 10);
     			}
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    should line
    if (turn !(% 2) == 0) //*heres where i get the error*

    be
    if (!(turn % 2) == 0) //*heres where i get the error*

    also you have ; missing off the end of the following line
    { cout << "Player X, your move.\n"

    Comment

    • wizardRahl
      New Member
      • Oct 2006
      • 40

      #3
      Nice! That helped... =)

      Now I need some direction on how to update the board. Say if player X goes first and chooses 1, when player O's turn is up, the board should display:

      X 2 3
      4 5 6
      7 8 9.
      and so and and so forth.

      Any direction -> most thankful!

      Comment

      • wizardRahl
        New Member
        • Oct 2006
        • 40

        #4
        In other words, where I have in my code " cin >> board[Xpos]; and cin >> board[Opos]; " I need it to display the updated board each time the loop iterates.

        I'm not sure how to display the captured value of the array and the board at the same time.
        Thanks

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You need to do the following
          • The array board needs to be initialise to something, I would suggest the characters '1', '2', '3' ... '9' in the relevent locations to make displaying the board easy.
          • The code line
            cin >> board[Xpos];
            and all similar lines are problematic because you never initialise Xpos so you are righting to a random array location that is likely to be out of bounds and cause some kind of exception.

            You need to

            cin >> Xpos;
            board[Xpos-1] = 'X'; // -1 because locations number 1-9 and array is 0-8
          • Re-Printing the board is easy
            cout << "Current Board\n";
            cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
            cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;
            cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;

          Comment

          • wizardRahl
            New Member
            • Oct 2006
            • 40

            #6
            Thanks,
            I have a foreign teacher and she sometimes doesn't understand questions well, especially since we have to email them (online class).
            She said that we could just do cout << to display the numbers. I was confused because like you said, Xpos didn't really mean anything since there was no array initialized.

            Thanks again... that makes loads of sense.

            LordRahl

            Comment

            • wizardRahl
              New Member
              • Oct 2006
              • 40

              #7
              Ok.. sorry to keep bugging all of you, but I've almost got it. Actually, I'm fairly sure I know how to do that last part also. It's to generate the win or tie message.

              Before I write it all, I going to check to make sure this is correct.
              Would the win message be an "if" or a "bool"?

              I think you could do an if something like:

              If
              board[0]&&board[3]&&board[6] = 'X' cout << "Player x wins the game!.

              Something to that extent, using all the combos.

              Is this the most efficient way? I'm still very new to arrays and just wanted to check before I write a good size IF statement that is hopeless.

              Thanks in advance,
              LordRahl

              Comment

              • wizardRahl
                New Member
                • Oct 2006
                • 40

                #8
                Alright... here's an update. Hopefully this is correct and I'm just missing a ) or ; or something.
                The trouble is that I get a couple lines (basically the same code) that says that it has no effect. I've marked it with //**
                Here it is:
                Code:
                 #include <iostream>
                using namespace std;
                
                int main()
                {
                	int Xpos, Opos, turn;
                	char board[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
                	
                	cout << "\nPlease enter a number correspoding to the board\n"
                	     << "to mark your spot. 3 in a row wins the game.\n";
                	cout << "              Good Luck!                      \n\n";
                
                	cout << "Current Board:\n\n";
                cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
                cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;  //displays the current board
                cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;
                
                 
                 
                  do
                	{
                	for (turn = 1; turn < 10; turn++)			//checks turn of player
                	   
                
                		if (!(turn % 2) == 0)
                		
                		{	
                	  	cout << "\nPlayer X, your move.\n";		
                		
                			 cin >> Xpos;
                			 board[Xpos-1] = 'X';
                			 
                			 if			 
                 		((board[0]&&board[3]&&board[6] == 'X')||(board[1]&&board[4]&&board[7] == 'X')||
                		(board[2]&&board[5]&&board[8] == 'X')||(board[0]&&board[4]&&board[8] == 'X')||
                		(board[2]&&board[4]&&board[6] == 'X'));  //*says code has no effect
                 		
                			cout << "Congrats! Player X wins the game!";	
                			 
                			 
                			 			cout << "\nCurrent Board\n";
                cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
                cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;  //displays the current board
                cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;
                
                			 
                		}	  
                	    else
                			  		 												
                		 	  {	
                			 
                			  cout << "\nPlayer O, your move\n";
                			
                			
                				        			  
                			     cin >> Opos;
                				  board[Opos-1] = 'O';
                				 
                				  if
                	((board[0]&&board[3]&&board[6] == 'O')||(board[1]&&board[4]&&board[7] == 'O')||
                	(board[2]&&board[5]&&board[8] == 'O')||(board[0]&&board[4]&&board[8] == 'O')||
                	(board[2]&&board[4]&&board[6] == 'O'));//*says code has no effect
                 			
                			cout << "Congrats! Player O wins the game!";	
                				  
                				  
                				  	cout << "\nCurrent Board\n";
                cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
                cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;  //displays the current board
                cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;
                
                
                				 
                				}
                
                     }
                	 while (turn < 10);
                 
                	 	cout << "It's a tie!";						//displays tie message 
                  return 0;
                 }
                Any direction.. most appreciated.

                Comment

                • wizardRahl
                  New Member
                  • Oct 2006
                  • 40

                  #9
                  / bump
                  Ok I got the previous problem fixed.
                  Now, I need a way to end the program if a player wins. In its current state, the program will display the win message, but will keep on going until all 9 turns are complete, and then display that the game was a tie.
                  If I were to do an IF statement at the end, all 9 turns would still pass and it would be possible for both players to win. I guess what I'm really asking if there is a way to end or exit the program in the middle of your code.
                  It gives me an "unreachabl e code" error everytime i try to do a "return 0;" in the middle which makes sense. I just need to know if there is another way to end the program once the if statement has been fulfilled in "mid-code".

                  Thanks again.

                  Comment

                  • wizardRahl
                    New Member
                    • Oct 2006
                    • 40

                    #10
                    /bump

                    ??

                    /bump

                    Comment

                    Working...