One person Tic-Tac-Toe game

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.

Thanks for the help! I really appreciate this.
Last edited on
Firstly please use the [code]The Code goes here[/code]

Could you post the exact error message?
closed account (DSLq5Di1)
Indentation please. http://en.wikipedia.org/wiki/Indent_style

..and if I had to guess, I'd say you have one too many curly braces.
Last edited on
..and if I had to guess, I'd say you have one too many curly braces.

At least in this snippet, there is no extra curly brace.
Last edited on
closed account (DSLq5Di1)
Just an oddly formed do while loop it seems,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
    }
    else if (whoWon ( ) == 2)
    {
        cout << "The computer has won the game!" << endl;
    }
    else
    {
        cout << "It's a tie game!" << endl;
    }
    system ("PAUSE");
}
Okay, thanks. The exact error message, if anyone still needs to know, is "error C2061: syntax error : identifier 'showBoard'".

I counted the curly brackets twice, and that's not it. I also tried moving the do while loop, and that just gave me more problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 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;
	   }
	   else if (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.
Last edited on
Here, it's on codepad now. I'd never heard of it before, so thanks.

http://codepad.org/7GZ4Wjwt
closed account (DSLq5Di1)
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?
Topic archived. No new replies allowed.