By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Another TicTacToe problem

P: 40
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.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int Xpos, Opos, turn;
  7.     char board[9];
  8.  
  9.     cout << "\nPlease enter a number corresponding to the board\n"
  10.          << "to mark your spot. 3 in a row wins the game.\n";
  11.     cout << "              Good Luck!                      \n\n";
  12.  
  13.  
  14.     cout << "\n1 2 3\n"
  15.           << "4 5 6\n"
  16.           << "7 8 9\n";
  17.  
  18.  
  19.  
  20.     for (int turn = 1; turn < 10; turn++)            //checks turn of player
  21.            {
  22.     do
  23.     {
  24.         if (turn !(% 2) == 0)   //*heres where i get the error*
  25.  
  26.         {    cout << "Player X, your move.\n"        
  27.  
  28.              cin >> board[Xpos];                                //Player X input
  29.         }      
  30.         else
  31.  
  32.                {    cout << "Player O, your move\n"
  33.  
  34.                         << board[Xpos] << endl;            //Updated board
  35.  
  36.                  cin >> board[Opos];                        //Player O input
  37.                   cout << board[Opos];                        //Updated board
  38.               }
  39.  
  40.      }
  41.      while (turn < 10);
  42.              }
  43. }
  44.  
  45.  
Nov 1 '06 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 1,510
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"
Nov 1 '06 #2

P: 40
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!
Nov 1 '06 #3

P: 40
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
Nov 1 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
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;
Nov 1 '06 #5

P: 40
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
Nov 2 '06 #6

P: 40
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
Nov 2 '06 #7

P: 40
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:
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int Xpos, Opos, turn;
  7.     char board[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  8.  
  9.     cout << "\nPlease enter a number correspoding to the board\n"
  10.          << "to mark your spot. 3 in a row wins the game.\n";
  11.     cout << "              Good Luck!                      \n\n";
  12.  
  13.     cout << "Current Board:\n\n";
  14. cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
  15. cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;  //displays the current board
  16. cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;
  17.  
  18.  
  19.  
  20.   do
  21.     {
  22.     for (turn = 1; turn < 10; turn++)            //checks turn of player
  23.  
  24.  
  25.         if (!(turn % 2) == 0)
  26.  
  27.         {    
  28.           cout << "\nPlayer X, your move.\n";        
  29.  
  30.              cin >> Xpos;
  31.              board[Xpos-1] = 'X';
  32.  
  33.              if             
  34.          ((board[0]&&board[3]&&board[6] == 'X')||(board[1]&&board[4]&&board[7] == 'X')||
  35.         (board[2]&&board[5]&&board[8] == 'X')||(board[0]&&board[4]&&board[8] == 'X')||
  36.         (board[2]&&board[4]&&board[6] == 'X'));  //*says code has no effect
  37.  
  38.             cout << "Congrats! Player X wins the game!";    
  39.  
  40.  
  41.                          cout << "\nCurrent Board\n";
  42. cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
  43. cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;  //displays the current board
  44. cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;
  45.  
  46.  
  47.         }      
  48.         else
  49.  
  50.                {    
  51.  
  52.               cout << "\nPlayer O, your move\n";
  53.  
  54.  
  55.  
  56.                  cin >> Opos;
  57.                   board[Opos-1] = 'O';
  58.  
  59.                   if
  60.     ((board[0]&&board[3]&&board[6] == 'O')||(board[1]&&board[4]&&board[7] == 'O')||
  61.     (board[2]&&board[5]&&board[8] == 'O')||(board[0]&&board[4]&&board[8] == 'O')||
  62.     (board[2]&&board[4]&&board[6] == 'O'));//*says code has no effect
  63.  
  64.             cout << "Congrats! Player O wins the game!";    
  65.  
  66.  
  67.                       cout << "\nCurrent Board\n";
  68. cout << board[0] << ' ' << board[1] << ' ' << board[2] << endl;
  69. cout << board[3] << ' ' << board[4] << ' ' << board[5] << endl;  //displays the current board
  70. cout << board[6] << ' ' << board[7] << ' ' << board[8] << endl;
  71.  
  72.  
  73.  
  74.                 }
  75.  
  76.      }
  77.      while (turn < 10);
  78.  
  79.          cout << "It's a tie!";                        //displays tie message 
  80.   return 0;
  81.  }
  82.  
  83.  
Any direction.. most appreciated.
Nov 3 '06 #8

P: 40
/ 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 "unreachable 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.
Nov 3 '06 #9

P: 40
/bump

??

/bump
Nov 3 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.