How to display turns of CPU and Human on board in Tic-Tac-Toe

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.

#include "pch.h"
#include "Game.h"
#include<iostream>
#include<string>
using namespace std;
Game::Game()
{
matrix[0][0] = '1';
matrix[0][1] = '2';
matrix[0][2] = '3';
matrix[1][0] = '4';
matrix[1][1] = '5';
matrix[1][2] = '6';
matrix[2][0] = '7';
matrix[2][1] = '8';
matrix[2][2] = '9';
Grid_size = 1;
}

void Game::Run_game()
{
draw_board();

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++) {

cout << " " << matrix[0][0] << " | " << matrix[0][1] << " | " << matrix[0][2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << matrix[1][0] << " | " << matrix[1][1] << " | " << matrix[1][2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << matrix[2][0] << " | " << matrix[2][1] << " | " << matrix[2][2] << endl;

cout << " | | " << endl;

}
}
}

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.
Please use [.code][/.code] beacon. (without dot).

To solve your problem, just put :
std::cout<<"[The opponent you want] turn."<<std::endl;
Before each turn function inside run_game();

Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

Thank you for your feedback.
Topic archived. No new replies allowed.