Code always does code of if statement when it should also do else if.
Dec 4, 2014 at 12:10pm UTC
So I'm trying to make this tic tac toe game change player after one player makes his move. To do this I wrote the player = 2 line of code at the end of if(player = 1) statement. But the program seems to think that player still equals 1 and never changes turn to player 2 (never does the else if statement). What is wrong with my code?
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
void game()
{
int field;
if (player = 1)
{
cout<<"Player O turn" <<endl;
}
else
{
cout<<"Player X turn" <<endl;
}
cin>>field;
if (player = 1)
{
switch (field)
{
case 1: gameBoard[0][0] = 'O' ; break ;
case 2: gameBoard[0][1] = 'O' ; break ;
case 3: gameBoard[0][2] = 'O' ; break ;
case 4: gameBoard[1][0] = 'O' ; break ;
case 5: gameBoard[1][1] = 'O' ; break ;
case 6: gameBoard[1][2] = 'O' ; break ;
case 7: gameBoard[2][0] = 'O' ; break ;
case 8: gameBoard[2][1] = 'O' ; break ;
case 9: gameBoard[2][2] = 'O' ; break ;
}
displayBoard();
player = 2;
}
else if (player = 2)
{
switch (field)
{
case 1: gameBoard[0][0] = 'X' ; break ;
case 2: gameBoard[0][1] = 'X' ; break ;
case 3: gameBoard[0][2] = 'X' ; break ;
case 4: gameBoard[1][0] = 'X' ; break ;
case 5: gameBoard[1][1] = 'X' ; break ;
case 6: gameBoard[1][2] = 'X' ; break ;
case 7: gameBoard[2][0] = 'X' ; break ;
case 8: gameBoard[2][1] = 'X' ; break ;
case 9: gameBoard[2][2] = 'X' ; break ;
}
displayBoard();
player = 1;
}
game();
}
Dec 4, 2014 at 12:24pm UTC
= is assigment
== is comparison
Dec 4, 2014 at 12:37pm UTC
thanks, it's working now.
Topic archived. No new replies allowed.