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");
}

This code :

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();
	}

Can be reduced to this :

int index = positionHuman - 'A';
int row = 0;
int col = 0;
if( index < 3 ){
  row = 0;
  col = index % 3;
}
else if( index< 6){
  row = 1;
  col = index  % 3;
}
else {
row = 2;
col = index% 3;
}

The same goes for your placePiecePc function.
It could be shorter but my brain is not working right now. You should have used 1d array to simplify things.

If you wan't to check if a position is occupied, you can do the same thing I did above. Find its corresponding row, and column and check if it containes 'X' or 'O', if not then its free.

EDIT:
you can substitute this code for the above as well.

int index = humanPosition - 'A';
int row = 0;
int col =  index % 3;
while( index/3  >= 1 ){
    row++;
    index /= 3;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.