Firstly, you have some obvious code duplication for player1 and player2's turns. You should make a function that takes in the current player and does their turn.
Your loop stops after player2's turn because at that point both player1 and player2 are equal to 1. So, both loops
while (player1 == 0)
and
while (player2 == 0)
are getting skipped over and thus your main loop is infinite looping. You need to reset player1 and player2 to 0 after their respective turn.
Also, your case statements are incorrect. If the player chooses a space that's already taken, it will run the next case (since the else conditions don't have a
break
statement). By the way, the
break
statements in a
case
make the execution break from the switch statement,
not the surrounding loop.
Your case statements should look like this (using case 1 of player1 as an example):
1 2 3 4 5 6 7 8 9 10 11 12
|
case 1:
if (board[0] == 'a')
{
board[0] = 'O';
player2 = 1;
}
else
{
cout << "That space is already taken";
player2 = 0;
}
break;
|
And you may consider having a
default
case to handle if the player selects a number besides 1-9.