Trying to fill an array with enums

Hi. I'm trying to fill the array with enums, why aren't the if else statements responding?

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include<iostream>

using namespace std;

int main()
{
	int theBoardPos[8];
	
	enum boardState {empty, x,o};
	
	int i;
	
	int choice;

	cout << "Empty, x or o?\n";
	cin >> choice;
	switch (choice)
	{
		case empty:
		{
		for (i=0;i<8;i++)
		{
		theBoardPos[empty];
		}
		break;
		}
	
		case x:
		{
		for (i=0;i<8;i++)
		{
		theBoardPos[x];
		}
		break;
		}	
	
		case o:
		{
		for (i=0;i<8;i++)
		{
		theBoardPos[o];
		}
		break;
		}
}

	
		/*for (i=0;i<8;i++)
		{
		cout << theBoardPos[i];
		}
		*/
		cout << "\n";
	
		if (theBoardPos[0]==empty)
		{
		cout << "|-|-|-|\n";
		cout << "|-|-|-|\n";
		cout << "|-|-|-|\n";
		}
		else if(theBoardPos[0]==x)
		{
		cout << "|x|x|x|\n";
		cout << "|x|x|x|\n";
		cout << "|x|x|x|\n";
		}
		else if(theBoardPos[0]==o)
		{
		cout << "|0|0|0|\n";
		cout << "|0|0|0|\n";
		cout << "|0|0|0|\n";
		}
		else
		{
		cout << "Incorrect board data.\n";
		}
}
theBoardPos[empty];
That doesn't do anything. It's probably the same as theBoardPos[0]; and it does nothing.

theBoardPos[x];
That doesn't do anything. It's probably the same as theBoardPos[1]; and it does nothing.

theBoardPos[o];
That doesn't do anything. It's probably the same as theBoardPos[2]; and it does nothing.
Last edited on
i think you want
theBoardPos[i] = x; //example

I really like to put enums in a namespace and even moreso if you use a name like 'x' in it that could be locally overwritten... this is a recipe for frustration without the namespace.

if this is tictactoe this is really messed up. eg if you had an x in one corner, you wouldnt print a board with 9 xs. you only print 1 x, for that location.


Last edited on
Hi I'm trying to get the if else statement to respond with your suggestion but its still going wrong and not responding, how can I fix

#include<iostream>

using namespace std;

int main()
{
int theBoardPos[8];

enum boardState {empty, x,o};

int i;

int choice;

cout << "Empty, x or o?\n";
cin >> choice;
switch (choice)
{
case empty:
{
for (i=0;i<8;i++)
{
theBoardPos[i]=empty;
}
break;
}

case x:
{
for (i=0;i<8;i++)
{
theBoardPos[i]=x;
}
break;
}

case o:
{
for (i=0;i<8;i++)
{
theBoardPos[i]=o;
}
break;
}
}


/*for (i=0;i<8;i++)
{
cout << theBoardPos[i];
}
*/
cout << "\n";

if (theBoardPos[0]==empty)
{
cout << "|-|-|-|\n";
cout << "|-|-|-|\n";
cout << "|-|-|-|\n";
}
else if(theBoardPos[0]==x)
{
cout << "|x|x|x|\n";
cout << "|x|x|x|\n";
cout << "|x|x|x|\n";
}
else if(theBoardPos[0]==o)
{
cout << "|0|0|0|\n";
cout << "|0|0|0|\n";
cout << "|0|0|0|\n";
}
else
{
cout << "Incorrect board data.\n";
}
}


so it prints out the correct board when I make the choice, thanks.
well, let me preach on it lol.
if this is indeed tic-tac-toe, that board has 9 locations, not 8. so that is bug #1.

now, second thing, c++ enums have a flaw ... you can't print the enum's NAME value directly (that only exists in the CODE).
so you need to help it. Here is a simple help for your situation.

third, you need to loop to print the board.

putting all that together, and giving some code snippets to serve only as examples:

int board[9]{};
char letterz[] = "-XO"; //dash is empty, you could use space or whatever here?
...//blah blah

for(int dx = 0, int rows = 0; rows < 3; rows++)
{
for(int cols = 0; cols < 3; cols ++)
cout << "|"<<letterz[board[dx++]];

cout <<"|" << endl; // not in the inner loop.
}

you can do it in a single loop but its harder to understand so I left it in double loop form. Ideally you would do it in the single for efficiency, but lets get it working first.

The point is that you need to print a value of (blank, x, o) for each board location, not hard-coded constants (as you do currently) when printing the board. And because you can't print enums (they are integers) by name, you have to do a lookup table to what the name is to relate the enum value back to the board's stored value. (this is a poor design, by the way, lose the enum and make board an array of character would eliminate a lot of complexity here). If the board were char, you could just set it to 'x' or 'o' or '-' directly, see?




Last edited on
Topic archived. No new replies allowed.