I need help to make a function that checks to see if a position is already occupied by an X or O. The game just checks to see if the random pc spot is the same as that chosen by the user on their last turn. I'm trying to get it yo check to see if the spot is occupied from a previous turn of the pc or human.
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
char positionHuman, positionPC;
char grid [3][3] = {{'A','B','C'},
{'D','E','F'},
{'G','H','I'}};
void showgrid()
{
for(int row=0; row <3; row++)
{
for (int col=0; col<3;col++)
{
cout << grid [row][col];
cout << " ";
}cout <<endl;
}
}
void move()
{
cout << "Where would you like to place your piece?"<<endl;
cin >> positionHuman;
positionHuman = (toupper(positionHuman));
}
void placepiece ()
{
move();
if (positionHuman == 'A')
{
grid [0][0] = 'X';
showgrid();
}
else if (positionHuman == 'B')
{
grid [0][1] = 'X';
showgrid();
}
else if (positionHuman == 'C')
{
grid [0][2] = 'X';
showgrid();
}
else if (positionHuman == 'D')
{
grid [1][0] = 'X';
showgrid();
}
else if (positionHuman == 'E')
{
grid [1][1] = 'X';
showgrid();
}
else if (positionHuman == 'F')
{
grid [1][2] = 'X';
showgrid();
}
else if (positionHuman == 'G')
{
grid [2][0] = 'X';
showgrid();
}
else if (positionHuman == 'H')
{
grid [2][1] = 'X';
showgrid();
}
else if (positionHuman == 'I')
{
grid [2][2] = 'X';
showgrid();
}
}
void placepiecePC ()
{
char randchar = rand() % 9 + 'A';
while (randchar == positionHuman)
{
if (randchar == positionHuman)
{
randchar = rand() % 9 + 'A';
}
}
if (randchar == 'A')
{
grid [0][0] = 'O';
showgrid();
}
else if (randchar == 'B')
{
grid [0][1] = 'O';
showgrid();
}
else if (randchar == 'C')
{
grid [0][2] = 'O';
showgrid();
}
else if (randchar == 'D')
{
grid [1][0] = 'O';
showgrid();
}
else if (randchar == 'E')
{
grid [1][1] = 'O';
showgrid();
}
else if (randchar == 'F')
{
grid [1][2] = 'O';
showgrid();
}
else if (randchar == 'G')
{
grid [2][0] = 'O';
showgrid();
}
else if (randchar == 'H')
{
grid [2][1] = 'O';
showgrid();
}
else if (randchar == 'I')
{
grid [2][2] = 'O';
showgrid();
}
}
int main ()
{
srand (time(0));
cout << "\t\t\t\tTIC-TAC-TOE"<<endl;
cout << "XOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO"<<endl;
cout << "This is one player only."<<endl;
cout << "You are X. PC is O."<<endl;
showgrid ();
for (int x=1; x<=9; x++)
{
placepiece ();
cout <<endl<<"COMPUTER TURN"<<endl;
placepiecePC ();
cout <<endl;
}
system ("pause");
}