I'm making a game of tic tack toe but I'm trying to get it to check if someone has already played in the spot or not but I just can't seem to figure out a simple way to it. what it does as of now is that it replaces whatever is in the spot even if the other player already played in there
Here is my code.
#include <iostream>
using namespace std;
char game_state[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
void print_state()
{
system("cls");
cout << "Tic Tac Toe v2.6" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << " "<< game_state[i][j] << " ";
if (j == 0 or j == 1)
cout <<"| ";
}
cout << endl;
if (i == 0 or i == 1)
cout <<"-------------" << endl;
}
}
void take_turn()
{
int a;
cout << "Press the number of the field: ";
cin >> a;
if (a == 1)
game_state[0][0] = player;
else if (a == 2)
game_state[0][1] = player;
else if (a == 3)
game_state[0][2] = player;
else if (a == 4)
game_state[1][0] = player;
else if (a == 5)
game_state[1][1] = player;
else if (a == 6)
game_state[1][2] = player;
else if (a == 7)
game_state[2][0] = player;
else if (a == 8)
game_state[2][1] = player;
else if (a == 9)
game_state[2][2] = player;
}
void TogglePlayer()
{
if (player == 'X')
player = 'O';
else
player = 'X';
}
char check_winner()
{
//check if first player is winner
if (game_state[0][0] == 'X' && game_state[0][1] == 'X' && game_state[0][2] == 'X')
return 'X';
if (game_state[1][0] == 'X' && game_state[1][1] == 'X' && game_state[1][2] == 'X')
return 'X';
if (game_state[2][0] == 'X' && game_state[2][1] == 'X' && game_state[2][2] == 'X')
return 'X';