help tic tac toe

why does the funcion isFree does not work.
It is suppose to check if a position in the array has an x or o.
the array is a char array and my code is quite long so i tried to shorten it.


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
char position[3][3];

void searchmove(char playermove, char position[3][3])
{
	// Looks through the two dimentional array
	for(int i = 1; i < 4; i++)
	{
		for(int t = 1; t < 4; t++)
		{
			if(position[i][t] == playermove)
			{
				isFree(i, t, position,playermove); // Work in progress
				position[i][t] = 'X';
			}
		}
	} 
}

 void isFree (int m, int n, char position[3][3],char playermove)
{
	cout << m << " + " << n << endl;
	while(position[m][n] == 'O' || position[m][n] == 'X')
	{
		cout << "re-enter already taken \n";
		cin >> playermove;
	}
}
The input doesn't do anything. If you're trying to change playermove, you need to pass it by reference using the '&' symbol.
sorry i don't understand. The function searchmove will give an X if the position wanted but when isFree goes through the while condition

while(position[m][n] == 'O' || position[m][n] == 'X')

does not work
Last edited on
for(int i = 1; i < 4; i++) doesn't work. Array indices go from 0 to array.length - 1
Well, Imo isFree should be a bool returning function. And should just check and return bool. :D I would structure it like this:

1
2
3
4
bool isFree(int x, int y, char position[3][3]) //note that 3,3 is really 0,1,2,3 and is four spots
{
         return (position[x][y] == ' ');
}


This checks if the player's choice is occupied. Returns true if empty, returns false otherwise.
thats not the issue that works fine and the position goes through the function fine.
but when it goes to the isFree function the while statement does not check properly. i added a cout to check that and it looks in the correct array but the condition in the while does not function.
I'm telling you, the for loop doesn't work. What is position[3][3]? It's not in the array, so it's going to be something you didn't expect. You'll get in trouble if you try to modify that.
Topic archived. No new replies allowed.