Tick tack toe

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';

if (game_state[0][0] == 'X' && game_state[1][0] == 'X' && game_state[2][0] == 'X')
return 'X';
if (game_state[0][1] == 'X' && game_state[1][1] == 'X' && game_state[2][1] == 'X')
return 'X';
if (game_state[0][2] == 'X' && game_state[1][2] == 'X' && game_state[2][2] == 'X')
return 'X';

if (game_state[0][0] == 'X' && game_state[1][1] == 'X' && game_state[2][2] == 'X')
return 'X';
if (game_state[2][0] == 'X' && game_state[1][1] == 'X' && game_state[0][2] == 'X')
return 'X';

//check if second player is winner
if (game_state[0][0] == 'O' && game_state[0][1] == 'O' && game_state[0][2] == 'O')
return 'O';
if (game_state[1][0] == 'O' && game_state[1][1] == 'O' && game_state[1][2] == 'O')
return 'O';
if (game_state[2][0] == 'O' && game_state[2][1] == 'O' && game_state[2][2] == 'O')
return 'O';

if (game_state[0][0] == 'O' && game_state[1][0] == 'O' && game_state[2][0] == 'O')
return 'O';
if (game_state[0][1] == 'O' && game_state[1][1] == 'O' && game_state[2][1] == 'O')
return 'O';
if (game_state[0][2] == 'O' && game_state[1][2] == 'O' && game_state[2][2] == 'O')
return 'O';

if (game_state[0][0] == 'O' && game_state[1][1] == 'O' && game_state[2][2] == 'O')
return 'O';
if (game_state[2][0] == 'O' && game_state[1][1] == 'O' && game_state[0][2] == 'O')
return 'O';

return '/';
}
int main()
{
print_state();
while (1)
{
print_state();
if (check_winner() == 'X')
{
cout << "X wins!" << endl;
break;
}
else if (check_winner() == 'O')
{
cout << "O wins!" << endl;
break;
}
TogglePlayer();
}
system("pause");
return 0;
}
Why don't you check the field before you write to it?
1
2
3
4
5
6
7
8
9
10
11
if (a == 1)
{
  if(game_state[0][0] != ' X' &&  game_state[0][0] != ' O')
  {
    game_state[0][0]= player;
  }
  else
  {
     // show error
  }
}

Maybe create a function to check if the field is empty.
Topic archived. No new replies allowed.