LTClipp
LTClipp

Reputation: 534

Variable not being set to correct value

I am making a Tic Tac Toe game. At the end of the game if one of the two human players wins, it is supposed to say "Player [1 or 2] has won!" It does this by checking what value ('X' or 'O') the winning row/column/diagonal is and corresponds it to what player it is (player 1 chooses X or O as their symbol). However, it ALWAYS thinks that player 1 has won regardless of who actually won.

Weird thing is, I can have the program cout both playerwon and BSTAT.getsquarestatus("A1") and player_won will ALWAYS output 'X' and BSTAT.getsquarestatus("A1") will output the correct answer, whether it's X or O.

Question: Why is player_won always being set to X? It is supposed to be set to the value of the player who won, but for some reason it is always set to X even if the winning player's value is 'O'.

If you need more information, please let me know. Thanks for your help!

char player1_mark;
char player_won;

class Boardstatus
{

char A1;

public:

    char getsquarestatus( string square )
{
  // enumerator basically changes A1-C3 to a number 1-9
  // not an actual enumerator, just a function
    int squarenumber = enumerator( square );
    switch ( squarenumber )
    {
    case 1:
        return A1;
        break;
    }
}
private:

    //This function acts similar to an enumerator, but instead returns a number 1-9 based
    //on what string x is entered so that the switch cases in get and setsquarestatus will
    //function properly (they require int or char to work)
    int enumerator( string x )
    {
       if ( x == "A1" )
       {
           return 1;
       }
    };

    bool checkwin( Boardstatus& BSTAT)
    {
        //Row 'A' checkwin
        if (BSTAT.getsquarestatus("A1")!= ' ' && BSTAT.getsquarestatus("A1")==BSTAT.getsquarestatus("A2") && BSTAT.getsquarestatus("A2")==BSTAT.getsquarestatus("A3"))
        {
            //****This is where something is wrong****
            player_won = BSTAT.getsquarestatus("A1");
            return true;
        }
    }

int main()
{         
Boardstatus BSTAT;  
if ( player_won = player1_mark )
    {
        cout << "Player 1 has won!";
    }
else
    {
        cout << "Player 2 has won!";
    }
}

Upvotes: 1

Views: 217

Answers (1)

Steve Fallows
Steve Fallows

Reputation: 6424

This: if ( player_won = player1_mark ) is setting the value of player_won

You need == for comparison, not assignment.

Upvotes: 1

Related Questions