This is a simple tictactoe game using Constructor member function

I have a problem of outputing if someone won the game or tied.

Everything seems working fine.

Can anyone mind check for me? I tried to find the problem for so long.

Thank you.

#include <iostream>
#include <iomanip>
using namespace std;

// Constructor
class gameInfo
{
    private:
        static char square[3][3];
        char turn; //The current player (1 or 0)
        char winner;

        int row; // Variables that holds the number of the row and column selected by the players
        int column;

        bool validmove; // Required variables to check if a move is valid or not
        bool validrow;
        bool validcolumn;

    public:
        gameInfo()
        {
            turn = '1';
            winner =' ';
        }


void displayBoard() // Draw the board
    {
    cout << setw(6) << "1" << setw(4) << "2" << setw(4) << "3" << endl << "  -------------" << endl;

    for(int i=1;i<4;i++)
        {
        cout << " " << i << " | " << square[i-1][0] << " | " << square[i-1][1] << " | " << square[i-1][2] << " " << endl << "  -------------" << endl;
        }
    }


void getWinner()
    {
        while (winner ==  ' ' )
        {

        //Check for an eventual winner of for a tie
        for(int i=0; i<3; i++)
            {
            if(square[i][0] == square[i][1] && square[i][1] == square[i][2] && square[i][0] != ' ')
                {
                winner = square[i][0];
                }
            }

        for(int i=0; i<3; i++)
            {
            if(square[0][i] == square[1][i] && square[1][i] == square[2][i] && square[0][i] != ' ')
                {
                winner = square[0][i];
                }
            }

            if(square[0][0] == square[1][1] && square[1][1] == square[2][2] && square[0][0] != ' ')
                {
                winner = square[0][0];
                }

            if(square[0][2] == square[1][1] && square[1][1] == square[2][0] && square[0][2] != ' ')
                {
                winner = square[0][2];
                }

            if(square[0][0] == square[0][1] && square[0][1] == square[0][2] && square[0][2] == square[1][0] && square[1][0] == square[1][1] && square[1][1] == square[1][2] && square[1][2] == square[2][0] && square[2][0] == square[2][1] && square[2][1] == square[2][2] && square[0][0] != ' ')
                {
                winner = 't';
                }

        cout << endl;



    // If somebody won 1 or 0
    if (winner == '1' || winner == 'O')
    {


    // Display congratulations message
        cout << "Congratulations! Player ";

        if (winner == '1')
            {
            cout << 1;
            }
        else
            {
            cout << 2;
            }

        cout << " won the Game!" << endl;
        break;
    }

    else if(winner == 't')
        {
        cout << "Tie!" << endl; // Display a message if it`s tie
        break;
        }


    while(winner == ' ')
        {   
        cout << "Player ";

    if(turn == '1')
    {
    cout << 1;
    }

    else
    {
    cout << 2;
    }

    cout << "'s turn:" << endl;



validmove = false; //Loop until the player selects a valid square
while(!validmove)
    {


validrow = false;
while(!validrow) //Loop until the player selects a valid row
    {
    cout << "Row: ";
    cin >> row;

    if(row == 1 || row == 2 || row == 3)
        {
            validrow = true;
        }

    else
        {
            cout << endl << "Invalid row!" << endl;
        }
    }


validcolumn = false;
while(!validcolumn) //Loop until the player selects a valid column
    {
    cout << "Column: ";
    cin >> column;

    if(column == 1 || column == 2 || column == 3)
        {
        validcolumn = true;
        }

    else
        {
        cout << endl << "Invalid column!" << endl;
        }
    }


if(square[row-1][column-1] == ' ') //Change the turn to the next player
    {
    square[row-1][column-1] = turn;
    system("cls");
    displayBoard();
    validmove = true;

if(turn == '1')
        {
        turn = 'O';
        }
else
        {
        turn = '1';
        }
    }

else //If the selected square is occupied display a message and loop again
{
cout << "The selected square is occupied!" << endl
    << "Select again:" << endl;
}

}
}
}
}
};

char gameInfo::square[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};


int main()
{
    gameInfo game;
    game.displayBoard();
    game.getWinner();


return 0;
}

Edited 4 Years Ago by jdh1231

The problem is that winner will never have the '1' or '0', it will have character that has ascii value 1 or 0
Have a look at ascii table

Edited 4 Years Ago by Marcusflint

ascii value of integers start from 48
48 for 0, 49 for 1,50 for 2 ...
so just add 48 wherever necessary

for example it would be something like this
winner = square[i][0]; + 48 ;

The problem is that winner will never have the '1' or '0', it will have character that has ascii value 1 or 0

Huh? '1' has an ascii value of 48, 1 does not. Unless I missed something I don't see that as a problem with that program as long as the OP consistently uses '0' and '1' instead of 0 and 1.

i beg your pardon, mistook square array as an integer array, my mistake, sorry

Huh? '1' has an ascii value of 48

in my prev post, it is 49

ascii value of integers start from 48
48 for 0, 49 for 1,50 for 2 ...

Edited 4 Years Ago by Marcusflint

This article has been dead for over six months. Start a new discussion instead.