Yet another... Tic Tac Toe.

So I have a tic-tac-toe game for my Intro to C++ class that I have almost completed. I have two issues that I cannot seem to figure out for the life of me.
The board displays as such:
1 2 3
4 5 6
7 8 9

Each player enters a number 1-9 to assign their space. This mostly works except I cannot get it to reject double-digit numbers. Instead, it uses a turn for each digit, which is my first problem.

My second problem is the if statement to end the game when a player has won: it breaks the loop of the game in the first turn and declares a winner improperly. Any help or insight in either of these would be fantastic and greatly appreciated!

int main ()
{
char gb[3][3];
char p; // player input
char XorO;
bool playerx = true; // true is X, false is O
int num_moves = 0;

ini_game(gb);

while (1)
{
display_game(gb);
cout << endl;

if (playerx)
XorO = 'X';
else
XorO = 'O';
do
{
cout << "Player " << XorO << " move: ";
cin >> p;
if (avail_spot(gb, p, playerx))
{
assign (gb, p, playerx);
num_moves++;
}
else
{
cout << "Invalid move on " << p << ", try again." << endl;
}
} while ((p >= '1' && p<= '9') && (avail_spot(gb, p, playerx)));
if (playerx)
playerx = false;
else
playerx = true;

// break loops
if (hasWon(gb));
{
display_game(gb);
cout << "Player " << XorO << " won!\n";
break;
}
if (num_moves == 9)
{
display_game(gb);
cout << endl << "Cat's game!\n";
break;
}
}
return 0;
}

bool hasWon (const char gb[][3])
{
for(int i = 0; i < 3; i++)
{
if(((gb[i][0] + gb[i][1] + gb[i][2]) == 264) || ((gb[i][0] + gb[i][1] + gb[i][2]) == 237))
{
return true;
}
else if(((gb[0][i] + gb[1][i] + gb[2][i]) == 264) || ((gb[0][i] + gb[1][i] + gb[2][i]) == 237))
{
return true;
}
}
if (((gb[0][0] + gb[1][1] + gb[2][2]) == 264) || ((gb[0][0] + gb[1][1] + gb[2][2]) == 237))
{
return true;
}
else if (((gb[0][3] + gb[1][1] + gb[3][0]) == 264) || ((gb[0][3] + gb[1][1] + gb[3][0]) == 237))
{
return true;
}
else
{
return false;
}
}
:s try to use code tags the <> button or
[code][/code]
.
It is also hard to tell what the problem is because of the ill-formated program try to use proper indentation.
1
2
3
4
5
6
7
8
9
10
int main()
{
    loop()
    {
        if()
        {
            stuff
        {
    }
}


as far as question one goes I think you are missing the avail spot function and the input of where the player is moving to. All I see is the input for which person is moving not where they are moving to.

Question 2 I have no idea what the 2d-array gb is supposed to be....and your if/else if/else statements are very confusing imo.

Also it looks like the second set of if/else if statements are useless because you are already going through those cases in the for loop except for the [0][3] one which is invalid at your 9th to last line. else if (((gb[0][3] + gb[1][1] + gb[3][0]) == 264) || ((gb[0][3] + gb[1][1] + gb[3][0]) == 237))
Topic archived. No new replies allowed.