vendredi 18 décembre 2015

Logical Mistakes, Checking for a Winner in Tic Tac Toe

Alright, so I am incredibly close to finishing this program. I understand why my program wasn't making a move and I was able to fix it, but now I am trying to check for a winner. I realize that my winGame(); function should be in some sort of while or do while loop to end the game. But, when I was trying to do a little debugging to sort some things out, I realize something unsettling. It always says that it's a draw, even when it's not supposed to be. These are such minor things that I am ashamed to not understand and I would really like some assistance on how I can fix it. Also, I know there's supposed to be a while or do while loop to end the game if there's a win. I'm just unsure where to put it, so if you have any suggestions, please let me know.

*Note that in my valid move function there's a little array, I plan on making it a static const array. My get functions return the value in the name (e.g. getIval() returns the initial value of the cell object), while my set functions just assign the values appropriately.

bool TicTacToe::validMove(char move){
        char options[9] = { '1','2', '3', '4','5','6','7', '8','9' };
        bool validate = false;
                for ( int i = 0; i < 9; i++ ){
                        if ( move == options[i]){
                                validate = true;
                            }
                    }

            return ( validate );
    }
void TicTacToe::setMove( char move ){
        for ( int i = 0; i < ROW; i++ ){
                for ( int j = 0; j < COL; j++ ){
                        if ( board[i][j].getiVal() == move ){
                                board[i][j].setiVal( players[currentPlayer].getMarker() );
                                switchPlayer();
                                break;
                        }
                }
        }
}
void TicTacToe::makeAMove(){
        char move;
        int turns = 1;
        bool validate = true;


        do{
                cout << "Player " << (getCurrentPlayer() + 1) << " make a move." << endl;
                cin >> move;

                if ( validMove( move ) ){
                        if ( turns > 4 ){
                                cout << "Nested if-else statement." << endl;
                                winGame();
                                setMove( move );
                        }
                        else
                                setMove(move);
                }
                else{
                        cout << "Invalid Move. Please reenter." << endl;
                        cin >> move;
                }

                DrawBoard();
                turns++;

        } while ( turns <= 9 );

}
bool TicTacToe::winGame(){
        cout << "Calling winGame() "  << endl;
        bool validate = false;
        int k = 0;
        for ( int i = 0; i < COL; i++ ){
                //check column wins
                if ( board[0][i].getMarker() == board[1][i].getMarker() && board[1][i].getMarker() == board[2][i].getMarker() && board[2][i].getMarker() != (' ')){
                        cout << "Column win " << endl;
                        validate = true;
                        break;
                }
                //check row wins
                 else if ( board[i][0].getMarker() == board[i][1].getMarker() && board[i][1].getMarker() == board[i][2].getMarker() && board[i][2].getMarker() !=  (' ')){
                        cout << "Row win." << endl;
                        validate = true;
                        break;
                }
        }

        if( board[0][0].getMarker() == board[1][1].getMarker() && board[1][1].getMarker() == board[2][2].getMarker() && board[2][2].getMarker() != (' ')){
                cout << "Diagonal 1" << endl;
                validate = true;
        }
        else if ( board[0][2].getMarker() == board[1][1].getMarker() && board[1][1].getMarker() == board[2][0].getMarker() && board[2][0].getMarker() != (' ') ){
                cout << "Diagonal 2 " << endl;
                validate = true;
        }
        else{
                cout << "It's a draw!" << endl;
                validate = true;
        }

        return (validate);
}

Here is a sample run of the program for your reference.

    //sample run
+--+--+--+
|1 |2 |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
1

+--+--+--+
|X |2 |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 2 make a move.
2

+--+--+--+
|X |O |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
3

+--+--+--+
|X |O |X |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 2 make a move.
5

+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
7
Nested if-else statement.
Calling winGame()
It's a draw!

+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|X |8 |9 |
+--+--+--+
Player 2 make a move.
8
Nested if-else statement.
Calling winGame()
It's a draw!

+--+--+--+
|X |O |X |
+--+--+--+
|4 |O |6 |
+--+--+--+
|X |O |9 |
+--+--+--+

Aucun commentaire:

Enregistrer un commentaire