I'm currently making an ultra simple tictactoe program but I've run into a slight issue. I've created a gameboard using a 2-D array of characters (not a crazy fan of using chars for this, but figured it was the easiest way to enable me to enter X's and O's into the array to output on the screen).
In the PlayerOneMove() function, I prompt the user to enter the cell (1-9) where he would like to place his game piece. The input is stored as a char. I then pass his cell choice the MakeMove() function, which checks if the character the user entered is currently stored in the array. However, the function always returns false.
I realize this is an issue with the compiler seeing the input as an integer and not matching the character that is in the array (because if I replace the elements in the array with letters and then input a letter it finds the match), but I'm not sure how to solve this. I've tried adding the ASCII value to the character and changing the char input to an integer input and then static casting it to a char to no avail. Any help would be appreciated before I decide to just completely annihilate the character array!!
class TicTacToe {
private:
char board[3][3] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
public:
void DrawBoard();
void PlayerOneMove();
bool MakePlay(char, char);
};
void TicTacToe::PlayerOneMove() {
char cell;
cout << "Player One, please select a cell: ";
cin >> cell;
while (!MakePlay(cell, 'O')) {
cout << "That cell is unavailable. Please select another cell: ";
cin >> cell;
}
}
bool TicTacToe::MakePlay(char cell, char player) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row][col] == cell) {
board[row][col] = player;
return true;
}
else return false;
}
}
}
int main() {
TicTacToe game;
game.DrawBoard();
game.PlayerOneMove();
return 0;
}
example: https://i.stack.imgur.com/sGFjZ.jpg