Checking Arrays

I need help checking to see if the following array has any empty spaces or if it is full via function call
int Board[9] = " "; // Sets all 9 spaces to " "

if it is full, I want to return that to the main function

I tried something like:

for(auto i = 0; i < 9; i++))
{
if(Board[i] = ' ')
++count;
}

Where if, count is greater than 1 there is still empty spaces and thus the program continues to run, however this doesn't seem to work.

Let me know if anyone has any questions in regards to this problem as far as purpose or code clarity goes.
Last edited on
Well, first off when making a comparison you need a double equals sign. Right now you are doing an assignment.
if (Board[i] == ' ')

More importantly, your Board array is of type int, right now you are checking it for a single character, which I assume your compiler is implicitly casting that to an int with its ascii value (32). So your code right now is just setting all 9 elements of your array to 32.

I'm not entirely clear on what you are trying to accomplish, are you trying to see if the array contains any empty elements? If so, with it being an int array, you need to either set a default "empty" value such as 0, and checking its values against that. Otherwise it will just be "garbage" numbers since you never set values for the array.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Main

#include <iostream>


using namespace std;

void Display_Board(char *Board);
bool Check_Board(char *Board);
void Make_Move(int move,char *Board);


int main()
{
	bool Turn;
	char Board[9] = " ";
	Display_Board(Board);
	Check_Board(Board);
	bool Game_is_On = true;
	do
	{

		
		int Move;
		cout << "Make your move, please: " << endl << endl;
		cin >> Move;
		Make_Move(Move, Board);
		Display_Board(Board);
		if (Check_Board(Board) == false)
			Game_is_On == false;
		
	} while (Game_is_On == true);

	cout << "You've won the game!";

	system("pause");
	return 0;
}

void Make_Move(int Move, char *Board)
{
	Board[Move] = 'X';
}


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
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

void Display_Board(char *Board)
{
	cout << endl; 
	cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
	cout << "---------" << endl;
	cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
	cout << "---------" << endl;
	cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
}

bool Check_Board(char *Board)
{
	int count = 9;
	for (auto i = 0; i < 9; i++)
	{
		if (Board[i] == ' ')
		{
			count--;
		}
	}

	if (count == 0)
	{
		return false;
	}
	else

		return true;
	


}


That's most of the code revolving around it,
I figured out the one equals sign, that was a typo in the original code.

I apologize for any "bad programming practice" -- as of right now I'm trying to right this program entirely on my own, without copying it from another source.
Ah ok I think I see what you're trying to do, the goal is to fill up all the spaces and you win? If that's the case then you just need to change your if statement in Check_Board to
if (Board[i] == 'X')

Also on line 30, you are doing a comparison instead of an assignment, it needs to be:
Game_is_On = false;
Close, it's the beginning of a tic-tac-toe game, just slowly building it piece by piece. I need to eventually add checks to determine computer turn or human turn etc.

so if
if (Board[i] == 'X') will work to check if the board has all 'X' then why won't it work when checking if it has all ' '?

When I run the program and fill all spaces, the program continues to run, which at this point in the design process I do not want to happen.
Did you remember to fix the problem on line 30 as well? I thought you wanted to end the game once the board was filled with X's, if you want it to work for ' ' then you had most of it right. Here is the part that is messing up the ' ' comparison.

On line 16:
char Board[9] = " ";

This is like initializing a string, you're giving a char array an empty string, so all it's doing is setting Board[0] = ' ', the rest of the elements are getting defaulted to 0, which in ascii is the null character. So what you need to do is create your Board array as follows:
char Board[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};

Or:
1
2
3
char Board[9];
for (int i = 0; i < 9; i++)
  Board[i] = ' ';
That works a lot better, but it still won't end when it is full >:(

It's gotta be something to do with checking the board

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool Check_Board(char *Board)
{
	int count = 0;
	for (auto i = 0; i < 9; i++)
	{
		if (Board[i] != ' ')
			count++;
		
		else
			return false;
	}
	if (count > 0)
	{
		return true;
	}

}
This works for me, I just copied your code and made a few tweaks in Check_board and in the do while loop

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

void Display_Board(char *Board)
{
	cout << endl; 
	cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
	cout << "---------" << endl;
	cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
	cout << "---------" << endl;
	cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
}

bool Check_Board(char *Board)
{
	for (int i = 0; i < 9; i++)
	{
		if (Board[i] == ' ')
			return true;
	}
	
	return false;
}

void Make_Move(int Move, char *Board)
{
	Board[Move] = 'X';
}

int main()
{
	bool Turn;
	char Board[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
	Display_Board(Board);
	Check_Board(Board);
	bool Game_is_On = true;
	do
	{
		int Move;
		cout << "Make your move, please: " << endl << endl;
		cin >> Move;
		Make_Move(Move, Board);
		Display_Board(Board);
		if (Check_Board(Board) == false)
			Game_is_On = false;
		
	} while (Game_is_On == true);

	cout << "You've won the game!";

	return 0;
}
Topic archived. No new replies allowed.