I'm trying to create a tic-tac-toe game for a beginner C++ class. I've got a working two-player game, but when I try to make it a one-player game, with the second player the computer, something's going wrong. I had a lot of problems earlier, but I've fixed all but one. I'm getting a syntax error message, saying my something's wrong with my identifier. I had that same bit of code in the same place for the two-person game. I've tried moving it, deleting and retyping it, deleting the other one, but each time, I simply get more error messages than before. Here's the relevant bit of code- If more is needed, I can add it.
I'm getting the error for the second place where it says: showBoard ( );
while (whoWon ( ) == 0 && Total_Moves < 9)
{
// Do this until the player chooses a valid move
do
{
// Show the board
showBoard ( );
// Tell which player to move
if (Whose_Turn == 1)
{
cout << Player_1_Name << ": It's your turn." << endl;
cout << "Enter the number of the spot where you'd like to move." << endl;
// Get the move
cin >> Move;
while (moveIsValid (Move) != true);
// Add 1 to Total_Moves
Total_Moves++;
}
else
{
cout << "It's the computer's turn, now. Please wait." << endl;
int Cpu_Move;
while (moveIsValid (Move) != true);
// Add 1 to Total_Moves
Total_Moves++;
}
// Change whose turn it is
switch (Whose_Turn)
{
case (1):
{
Board[Move] = 'x';
Whose_Turn = 2;
break;
}
case (2):
{
Board[Move] = 'o';
Whose_Turn = 1;
}
}
}
// Show the board
// This is where I'm having the problem
showBoard ( );
if (whoWon ( ) == 1)
{
cout << Player_1_Name << " has won the game!" << endl;
}
else if (whoWon ( ) == 2)
{
cout << "The computer has won the game!" << endl;
}
else
{
cout << "It's a tie game!" << endl;
}
system ("PAUSE");
}
This is most of the main function of the program. I tried to narrow it down, but since I really don't have any more ideas about how to fix it, or even really what's wrong, I didn't want to leave out the entire reason for the error, or any silly mistakes of that sort. If I did somehow leave something crucial out, please let me know, I'll add it in.
while (whoWon ( ) == 0 && Total_Moves < 9) // Moved further down.
{
// Do this until the player chooses a valid move
do{
// Show the board
showBoard ( );
// Tell which player to move
if (Whose_Turn == 1)
{
cout << Player_1_Name << ": It's your turn." << endl;
cout << "Enter the number of the spot where you'd like to move." << endl;
// Get the move
cin >> Move;
while (moveIsValid (Move) != true);
// Add 1 to Total_Moves
Total_Moves++;
}
else
{
cout << "It's the computer's turn, now. Please wait." << endl;
int Cpu_Move;
while (moveIsValid (Move) != true);
// Add 1 to Total_Moves
Total_Moves++;
}
// Change whose turn it is
switch (Whose_Turn)
{
case (1):
{
Board[Move] = 'x';
Whose_Turn = 2;
break;
}
case (2):
{
Board[Move] = 'o';
Whose_Turn = 1;
}
}
} while (whoWon ( ) == 0 && Total_Moves < 9);
// Show the board
// This is where I'm having the problem
showBoard ( );
if (whoWon ( ) == 1)
{
cout << Player_1_Name << " has won the game!" << endl;
}
elseif (whoWon ( ) == 2)
{
cout << "The computer has won the game!" << endl;
}
else
{
cout << "It's a tie game!" << endl;
}
system ("PAUSE");
}
while (whoWon ( ) == 0 && Total_Moves < 9)
{
// Do this until the player chooses a valid move
do
{
// Show the board
showBoard ( );
// Tell which player to move
if (Whose_Turn == 1)
{
cout << Player_1_Name << ": It's your turn." << endl;
cout << "Enter the number of the spot where you'd like to move." << endl;
// Get the move
cin >> Move;
while (moveIsValid (Move) != true);
// Add 1 to Total_Moves
Total_Moves++;
}
else
{
cout << "It's the computer's turn, now. Please wait." << endl;
int Cpu_Move;
while (moveIsValid (Move) != true);
// Add 1 to Total_Moves
Total_Moves++;
}
// Change whose turn it is
switch (Whose_Turn)
{
case (1):
{
Board[Move] = 'x';
Whose_Turn = 2;
break;
}
case (2):
{
Board[Move] = 'o';
Whose_Turn = 1;
}
}
}
// Show the board
// This is where the problem is
showBoard ();
if (whoWon ( ) == 1)
{
cout << Player_1_Name << " has won the game!" << endl;
}
elseif (whoWon ( ) == 2)
{
cout << "The computer has won the game!" << endl;
}
else
{
cout << "It's a tie game!" << endl;
}
system ("PAUSE");
}
Try putting the entire code on some site like codepad or pastebin.
It is not possible to get the error without looking at the code.
EDIT:
The error occurs if there is a identifier being used before it is declared, though, the fact that it is not showing an error for line 7 suggests that it has been declared.
http://codepad.org/SGuVOBcB
Your while loop should be functioning as intended now, though you'll get stuck in an infinite loop when it is the computers turn.. is that work in progress?