Issues with my nested if statement

Hi guys
I'm new to programming and for one of my assignments I have been asked to write a noughts and crosses program that never loses. I have decided to tackle this using lots of if statements, and for one particular move I have written the following code:

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
if (gameboard[2][1]!=" "){
			cout<<"The computer has placed its "<<Computer.type<<"  on the space "<<"0 0"<<endl;
			gameboard[0][0]=Computer.type;
		} 
		else if (gameboard[1][2]!=" "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 0"<<endl;
			gameboard[0][0]=Computer.type;
		}
		else if (gameboard[1][0]!= " "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
			gameboard[2][2]=Computer.type;
		}
		else if (gameboard[0][1]!= " "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
			gameboard[2][2]=Computer.type;
		}
		else if (gameboard [0][0]!=" "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
			gameboard[2][2]=Computer.type;
		}
		else if (gameboard [2][0]!=" "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 2"<<endl;
			gameboard[0][2]=Computer.type;
		}
		else if (gameboard [2][2]!=" "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 0"<<endl;
			gameboard[0][0]=Computer.type;
		}
		else if (gameboard [0][2]!=" "){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 0"<<endl;
			gameboard[2][0]=Computer.type;
		}	

where Computer.type is a char that denotes whether the computer is X or O, gameboard is a 2d array that contains the board, and empty spaces are denoted in the board by " ". My issue is that the code compiles correctly; however, when I execute the program no matter what condition I enter the code executes whatever expression is in the first if statement; even if that condition is not true. I've spent quite a long time on this and I can't see the problem.

I would be most grateful if someone could provide any helpful advice
Last edited on
How is gameboard defined?

I'm going to take a guess here, that it is an array in which each element is of type char. Right so far - remember I'm guessing?

In that case, this comparison is not valid: if (gameboard[2][1]!=" ")
why? Because you are comparing a single character on the left with a string on the right.

If my psychic powers have correctly identified the problem, then you need to change the double quotes to single quotes throughout.
if (gameboard[2][1] != ' ')

You might also find the code easier to read if instead of typing the literal ' ' or " " each time, instead use a constant, such as
const char empty = ' ';
and then put if (gameboard[2][1] != empty)
Hi Chervil

Thanks for your reply. I did originally have it defined as

char gameboard[3][3]

However, when i try to compile the code the entire program crashes; hence i defined it as an array of strings instead. I know i end up setting the elements of the arrays to a character (such as Computer.type). But it seems to work, so I left it. The problem still exists though, and its happening in other parts of the code as well; in some of the if statements, only the first result is ever triggered, regardless of the conditions.

By the way, many thanks for the tip with cleaning up the code, if you have any more advice it would be much appreciated.
At this stage I'm a bit puzzled. I suspect the original code you mention where you had char gameboard[3][3] may have been along the right lines, but contained some error. I'd be tempted to return to that version, and see whether the forum members here can help resolve that problem.

But still, what is the current definition of gameboard?

Are you using c-strings, or c++ std::string ?
Depending on which you have, the solution will be different.

For example, if you are using c-strings, you need to compare using function strcmp(), otherwise just testing using the != or == operators won't work.
Last edited on
I am using c++ std::string, and the current definition of gameboard is as a string. However, i think this may not be my problem; I think there may be other more obvious mistakes i have made. If its okay i'm going to post all the if statements i have written so far(its still a work in progress) and if possible could you take a look at it to see if there are any glaring mistakes? Thanks

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

//the first move is made by user

if (Player1.start=="true"||Player1.start=="True"){
		cout<<"Where would you like to place your "<<Player1.type<<endl;
		cin>>n>>m;
		gameboard[n][m]=Player1.type;
		displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
	}
	
	//the firt move is made by Computer
	
	else {
		int p=0,q=0;
		cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"1 1"<<endl;
		gameboard[1][1]=Computer.type;
		displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
	
		//player move
		
		cout<<"Where would you like to place your "<<Player1.type<<endl;
		cin>>p>>q;
		//if (gameboard[p][q]!=" "){
		//cout<<"Oops! This space is already taken! Please try somewhere else!"<<endl;
		//cin>>p>>q;
		//}
		gameboard[p][q]=Player1.type;
		displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
	
		//computer move		
		
		if (gameboard[2][1]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 0"<<endl;
			gameboard[0][0]=Computer.type;
			displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
			
			//player move
			
			cout<<"Where would you like to place your "<<Player1.type<<endl;
			cin>>p>>q;
			gameboard[p][q]=Player1.type;
			displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
			
			//computer move
			
			if (gameboard[2][2]==empty){
				cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
				gameboard[2][2]=Computer.type;
				displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
				cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
				cout<<"THE END"<<endl;
			}
			else {
				cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 2"<<endl;
				gameboard[0][2]=Computer.type;
				displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
				
				//player move
				
				cout<<"Where would you like to place your "<<Player1.type<<endl;
				cin>>p>>q;
				gameboard[p][q]=Player1.type;
				displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
				
				//computer move
				
				if (gameboard[1][0]==empty){
					cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"1 0"<<endl;
					gameboard[1][0]=Computer.type;
					displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
					cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
					cout<<"THE END"<<endl;
				}
				else if (gameboard[0][2]==empty){
					cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 2"<<endl;
					gameboard[0][2]=Computer.type;
					displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
					cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
					cout<<"THE END"<<endl;
				}
			}
		}
		
		else if (gameboard[1][2]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 0"<<endl;
			gameboard[0][0]=Computer.type;
			displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
			
			//player move
			
			cout<<"Where would you like to place your "<<Player1.type<<endl;
			cin>>p>>q;
			gameboard[p][q]=Player1.type;
			displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
			
			//computer move
			
			if (gameboard[2][2]==empty){
				cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
				gameboard[2][2]=Computer.type;
				displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
				cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
				cout<<"THE END"<<endl;
			}
			else {
				cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 0"<<endl;
				gameboard[2][0]=Computer.type;
				displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
				
				//player move
				
				cout<<"Where would you like to place your "<<Player1.type<<endl;
				cin>>p>>q;
				gameboard[p][q]=Player1.type;
				displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
				if (gameboard[0][1]==empty){
					cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 1"<<endl;
					gameboard[0][1]=Computer.type;
					displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
					cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
					cout<<"THE END"<<endl;
				}
				else if (gameboard[2][0]==empty){
					cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 0"<<endl;
					gameboard[2][0]=Computer.type;
					displayboard(gameboard[0][0],gameboard[1][0], gameboard[2][0], gameboard[0][1], gameboard[1][1], gameboard[2][1],gameboard[0][2], gameboard[1][2], gameboard[2][2]);
					cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
					cout<<"THE END"<<endl;
				}
			}
		}
		else if (gameboard[1][0]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
			gameboard[2][2]=Computer.type;
		}
		else if (gameboard[0][1]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
			gameboard[2][2]=Computer.type;
		}
		else if (gameboard [0][0]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 2"<<endl;
			gameboard[2][2]=Computer.type;
		}
		else if (gameboard [2][0]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 2"<<endl;
			gameboard[0][2]=Computer.type;
		}
		else if (gameboard [2][2]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 0"<<endl;
			gameboard[0][0]=Computer.type;
		}
		else if (gameboard [0][2]!=empty){
			cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 0"<<endl;
			gameboard[2][0]=Computer.type;
		}	
}
}
Well, I haven't got an answer to the original question yet.
What I do see from the sample of code posted above is this line:
1
2
3
displayboard(gameboard[0][0], gameboard[1][0], gameboard[2][0],
             gameboard[0][1], gameboard[1][1], gameboard[2][1],
             gameboard[0][2], gameboard[1][2], gameboard[2][2]);


Now it seems hardly worthwhile using an array, if you are going to list every individual element in the function call.

It should look something like this, I think:
displayboard(gameboard);

and the corresponding function definition would look something like this:
1
2
3
4
void displayboard(std::string board[][3])
{
    // whatever code needed here
}


(though I still feel it would be simpler to use type char rather than std::string throughout).
thanks for pointing that out; I knew there was something that could have made that easier! As for my original problem, it still exists and even though I have finished writing the code (for when the computer starts the game off)
the problem still exists. Here is a small part of the code:

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
         else {
				cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"2 0"<<endl;
				gameboard[2][0]=Computer.type;
				displayboard(gameboard);				
				//player move
				
				cout<<"Where would you like to place your "<<Player1.type<<endl;
				cin>>p>>q;
				gameboard[p][q]=Player1.type;
				displayboard(gameboard);				
				
				//computer move
				
				if (gameboard[1][0]==empty){
					cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"1 0"<<endl;
					gameboard[1][0]=Computer.type;
					displayboard(gameboard);	
					cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
					cout<<"THE END"<<endl;
				}
				else if (gameboard[0][2]==empty){
					cout<<"The computer has placed its "<<Computer.type<<" on the space "<<"0 2"<<endl;
					gameboard[0][2]=Computer.type;
					displayboard(gameboard);					
					cout<<"Computer Wins!!! Unluckly, mere mortal!"<<endl;
					cout<<"THE END"<<endl;
				}


When I executed the code, it gets to the else statement correctly, but then when it gets to the nested if statement, regardless of the condition it always skips to the else if condition and continue from there. Why???? I'm getting frustrated but I'm sure I'm doing something wrong
Topic archived. No new replies allowed.