I am writing this tic tac toe program using classes. I wrote this function called makeAMove(). This function is supposed to validate the move of the player, but it keeps displaying that it's an invalid move and it's asking the player to reenter. There's probably a few logical errors, but I am just not seeing them. Can anyone help? It's a class that uses an object called Cell board[ROW][COL] and another object called Player players[2]. The markers are either X or O, while the iVal is the initial value of the board ( each cell on the board has a number from 1 - 9 ). Included in here is the winGame() and the switchPlayer() for your reference. While I think my winGame() function is ok, I know I can do this using sums. In other words, each cell has in internal marker like -1 or 1 and if the sum of the rows, columns, etc, is -3 or 3, the internal marker corresponding to the player will output a winner. If you have any insight on how to do this, feel free to share, otherwise, my winGame is fine.
int TicTacToe::switchPlayer(){
if ( currentPlayer == 0)
currentPlayer = 1;
else if ( currentPlayer == 1 )
currentPlayer = 0;
return (currentPlayer);
}
//
//method to make a move
//
void TicTacToe::makeAMove(){
char move;
int turns = 1;
do{
cout << "Player " << (getCurrentPlayer() + 1) << " make a move." << endl;
cin >> move;
for ( int i = 0; i < ROW; i++ ){
for ( int j = 0; j < COL; j++ ){
//This is where my problem begins
if ( board[i][j].getiVal() == move ){
board[i][j].setiVal( players[currentPlayer].getMarker());
DrawBoard();
switchPlayer();
turns++;
}
else if ( board[i][j].getiVal() == 'X' || board[i][j].getiVal() == 'O'){
cout << "Invalid move, please reenter. " << endl;
cin >> move;
}//the else if statement always outputs:x
}
}
} while ( turns <= 9 || !winGame() );
cout << "Congratulations " << ( getCurrentPlayer() + 1 ) << "you won the game!" << endl;
}
bool TicTacToe::winGame(){
bool validate = false;
int k = 0;
for ( int i = 0; i < ROW; i++ ){
//check column wins
if ( board[0][i].getMarker() == board[1][i].getMarker() && board[1][i].getMarker() == board[2][i].getMarker()){
players[currentPlayer].setNumWin(k++);
validate = true;
break;
}
//check row wins
else if ( board[i][0].getMarker() == board[i][1].getMarker() && board[i][1].getMarker() == board[i][2].getMarker()){
players[currentPlayer].setNumWin(k++);
validate = true;
break;
}
}
if( board[0][0].getMarker() == board[1][1].getMarker() && board[1][1].getMarker() == board[2][2].getMarker()){
players[currentPlayer].setNumWin(k++);
validate = true;
}
else if ( board[0][2].getMarker() == board[1][1].getMarker() && board[1][1].getMarker() == board[2][0].getMarker()){
players[currentPlayer].setNumWin(k++);
validate = true;
}
return (validate);
}
//This is a sample run of the program
+--+--+--+
|1 |2 |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
1
+--+--+--+
|O |2 |3 |
+--+--+--+
|4 |5 |6 |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 2 make a move.
5
Invalid move, please reenter.
6
+--+--+--+
|O |2 |3 |
+--+--+--+
|4 |5 |X |
+--+--+--+
|7 |8 |9 |
+--+--+--+
Player 1 make a move.
Aucun commentaire:
Enregistrer un commentaire