Hi.I am making a Tic-Tac-Toe game in C++. So far my program can display the board but it doesn't display the turns of Human and CPU on the board. I am attaching my code below to have a look.
while (true) {
human_turn();
Computer_turn();
check_wins();
}
}
void Game::draw_board()
{
//Nested for loop to loop through the rows and columns of matrix
for (int i = 0; i < Grid_size; i++) {
for (int j = 0; j < Grid_size; j++) {
void Game::check_wins()
{
const char*winning_moves[8] = {
"123",
"456",
"789",
"147",
"258",
"369",
"159",
"753"
};
for (int i = 0; i < 8; i++) {
bool winner = true;
char prev_grid = '0';
const char*winning_move = winning_moves[i];
for (int index = 0; index < GRID_SIZE; index++) {
char character = winning_move[index];
int entered_num = character - '0';
int grid_space = entered_num - 1;
int row = grid_space / GRID_SIZE;
int col = grid_space % GRID_SIZE;
char grid_char = matrix[row][col];
if (prev_grid == '0') {
prev_grid = grid_char;
}
else if (prev_grid == grid_char) {
continue;
}
else {
winner = false;
break;
}
}
if (winner) {
cout << "*****************We have a Winner" << endl;
cout << "Looks like someone won!!!" << prev_grid << endl;
break;
}
}
}
void Game::Computer_turn()
{
while (true) {
int computer_choice = (rand() % 9) + 1;
int row = (computer_choice - 1) / 3;
int col = (computer_choice - 1) % 3;
char grid_pos = matrix[row][col];
if (grid_pos == 'X' || grid_pos == 'Y') {
continue;
}
else {
cout << "The computer has made a move" << computer_choice << endl;
matrix[row][col] = 'O';
break;
}
}
}
void Game::human_turn()
{
string input;
while (true) {
cout << "Where would you like to place your turn?" << endl;
getline(cin, input);
if (input != "") {
char entered = input.c_str()[0];
if (entered >= '1' && entered <= ' 9') {
int entered_num = entered - '0';
int index = entered_num - 1;
int row = index / 3;
int col = index % 3;
char grid_pos = matrix[row][col];
if (grid_pos == 'X' || grid_pos == 'O') {
cout << "The position in the grid is already taken. Please try again" << endl;
}
else {
matrix[row][col] = 'X';
break;
}
}
else {
cout << "Enter a value between 1-9" << endl;
}
}
else {
cout << "Enter something" << endl;
}
}
}
Appreciate it if you could respond to my message. Thank you.
Your loop is flawed. If the human wins, it still allows the computer to make a move. It would be better to use a bool to track whose turn it is. Something like this:
void Game::Run_game()
{
bool human = false;
char state; // state of the board
do {
human = !human; // switch players
draw_board();
if (human) {
human_turn();
} else {
Computer_turn();
}
state = check_wins();
} while (state == ' ');
draw_board();
switch (state) {
case'X':
cout << "Player wins!\n";
break;
case'O':
cout << "Computer wins!\n";
break;
case'T':
cout << "Tie game\n";
}
}
This assumes that you change check_wins to return a char:
- ' ' if nobody has won and the game can continue
- 'X' if the human wins
- 'O' if the computer wins
- 'T' if the game is a tie (all squares taken and no winner