Nested if statements in C++

I'm writing a program and having a little trouble with the if statements in it... is this legal?
1
2
3
4
5
6
7
8
9
10
if(Board[1][1]==1){
		if(Board[2][2]==1) return 1;
		if(Board[2][1]==1) return 2;
		if(Board[2][0]==1) return 3;
		if(Board[1][2]==1) return 4;
		if(Board[1][0]==1) return 6;
		if(Board[0][2]==1) return 7;
		if(Board[0][1]==1) return 8;
		if(Board[0][0]==1) return 9;
	}

Meaning to say, if [1][1] && [2][2] in a nested if rather than all jumbled in the parentheses...

Thanks,

endusr000
Yes but say [2][2] and [0][0] equal 1. It will return 1 and that's it. Maybe that's not a problem but yes that's legal.
... hmm, it goes into a infinite loop. I'll include the whole function so you can see. It looks when it's supposto block or win... Thanks again,

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
int Computer::compIntel(int Board[3][3]){
	/* Put the Brain here */
	int num, count; // for use with selecting random moves after block and win
	count=5; num=0;

	/* Check if you (computer) can win */
	if((Board[1][1]==2) && (Board[2][2]==2)) return 1;
	if((Board[1][1]==2) && (Board[2][1]==2)) return 2;
	if((Board[1][1]==2) && (Board[2][0]==2)) return 3;
	if((Board[1][1]==2) && (Board[1][2]==2)) return 4;
	if((Board[1][1]==2) && (Board[1][0]==2)) return 6;
	if((Board[1][1]==2) && (Board[0][2]==2)) return 7;
	if((Board[1][1]==2) && (Board[0][1]==2)) return 8;
	if((Board[1][1]==2) && (Board[0][0]==2)) return 9;

	if((Board[0][0]==2) && (Board[0][2]==2)) return 2;
	if((Board[0][0]==2) && (Board[0][1]==2)) return 3;
	if((Board[0][0]==2) && (Board[2][0]==2)) return 4;
	if((Board[0][0]==2) && (Board[2][2]==2)) return 5;
	if((Board[0][0]==2) && (Board[1][0]==2)) return 7;

	if((Board[2][0]==2) && (Board[1][0]==2)) return 1;
	if((Board[2][0]==2) && (Board[0][2]==2)) return 5;
	if((Board[2][0]==2) && (Board[2][2]==2)) return 8;
	if((Board[2][0]==2) && (Board[2][1]==2)) return 9;

	if((Board[0][2]==2) && (Board[0][1]==2)) return 1;
	if((Board[0][2]==2) && (Board[2][2]==2)) return 6;
	if((Board[0][2]==2) && (Board[1][2]==2)) return 9;

	if((Board[1][2]==2) && (Board[2][2]==2)) return 3;
	if((Board[1][2]==2) && (Board[0][1]==2)) return 5;

	if((Board[2][1]==2) && (Board[0][1]==2)) return 5;
	if((Board[2][1]==2) && (Board[2][2]==2)) return 7;

	/* Check if a block is nessecary */

	if((Board[1][1]==1) && (Board[2][2]==1)) return 1;
	if((Board[1][1]==1) && (Board[2][1]==1)) return 2;
	if((Board[1][1]==1) && (Board[2][0]==1)) return 3;
	if((Board[1][1]==1) && (Board[1][2]==1)) return 4;
	if((Board[1][1]==1) && (Board[1][0]==1)) return 6;
	if((Board[1][1]==1) && (Board[0][2]==1)) return 7;
	if((Board[1][1]==1) && (Board[0][1]==1)) return 8;
	if((Board[1][1]==1) && (Board[0][0]==1)) return 9;

	if((Board[0][0]==1) && (Board[0][2]==1)) return 2;
	if((Board[0][0]==1) && (Board[0][1]==1)) return 3;
	if((Board[0][0]==1) && (Board[2][0]==1)) return 4;
	if((Board[0][0]==1) && (Board[2][2]==1)) return 5;
	if((Board[0][0]==1) && (Board[1][0]==1)) return 7;

	if((Board[2][0]==1) && (Board[1][0]==1)) return 1;
	if((Board[2][0]==1) && (Board[0][2]==1)) return 5;
	if((Board[2][0]==1) && (Board[2][2]==1)) return 8;
	if((Board[2][0]==1) && (Board[2][1]==1)) return 9;

	if((Board[0][2]==1) && (Board[0][1]==1)) return 1;
	if((Board[0][2]==1) && (Board[2][2]==1)) return 6;
	if((Board[0][2]==1) && (Board[1][2]==1)) return 9;
	
	if((Board[1][2]==1) && (Board[2][2]==1)) return 3;
	if((Board[1][2]==1) && (Board[0][1]==1)) return 5;

	if((Board[2][1]==1) && (Board[0][1]==1)) return 5;
	if((Board[2][1]==1) && (Board[2][2]==1)) return 7;
	
	/* Check and move to conrers and middle */
	if(Board[0][0]) count--;
	if(Board[1][1]) count--;
	if(Board[0][2]) count--;
	if(Board[2][0]) count--;
	if(Board[2][2]) count--;

	srand(time(NULL));
	num = (rand() % count);

	if(!num) return 1;
	if(num==1) return 3;
	if(num==2) return 5;
	if(num==3) return 7;
	if(num==4) return 9;

	/* If all else fails, pick an open space NOTE: was getting a fail with the for loop
	 trying returns so it picks something, moveMe returns false if it didn't work*/
	return 1; return 2; return 3; return 4; return 5; return 6; return 7; return 8; return 9;
}


OR -

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
int Computer::compIntel(int Board[3][3]){
	/* Put the Brain here */
	int num, count; // for use with selecting random moves after block and win
	count=5; num=0;

	/* Check if you (computer) can win */
	if(Board[1][1]==2){
		if(Board[2][2]==2) return 1;
		if(Board[2][1]==2) return 2;
		if(Board[2][0]==2) return 3;
		if(Board[1][2]==2) return 4;
		if(Board[1][0]==2) return 6;
		if(Board[0][2]==2) return 7;
		if(Board[0][1]==2) return 8;
		if(Board[0][0]==2) return 9;
	}
	if(Board[0][0]==2){
		if(Board[0][2]==2) return 2;
		if(Board[0][1]==2) return 3;
		if(Board[2][0]==2) return 4;
		if(Board[2][2]==2) return 5;
		if(Board[1][0]==2) return 7;
	}
	if(Board[2][0]==2){
		if(Board[1][0]==2) return 1;
		if(Board[0][2]==2) return 5;
		if(Board[2][2]==2) return 8;
		if(Board[2][1]==2) return 9;
	}
	if(Board[0][2]==2){
		if(Board[0][1]==2) return 1;
		if(Board[2][2]==2) return 6;
		if(Board[1][2]==2) return 9;
	}
	if(Board[1][2]==2){
		if(Board[2][2]==2) return 3;
		if(Board[0][1]==2) return 5;
	}
	if(Board[2][1]==2){
		if(Board[0][1]==2) return 5;
		if(Board[2][2]==2) return 7;
	}


	/* Check if a block is nessecary */
	if(Board[1][1]==1){
		if(Board[2][2]==1) return 1;
		if(Board[2][1]==1) return 2;
		if(Board[2][0]==1) return 3;
		if(Board[1][2]==1) return 4;
		if(Board[1][0]==1) return 6;
		if(Board[0][2]==1) return 7;
		if(Board[0][1]==1) return 8;
		if(Board[0][0]==1) return 9;
	}
	if(Board[0][0]==1){
		if(Board[0][2]==1) return 2;
		if(Board[0][1]==1) return 3;
		if(Board[2][0]==1) return 4;
		if(Board[2][2]==1) return 5;
		if(Board[1][0]==1) return 7;
	}
	if(Board[2][0]==1){
		if(Board[1][0]==1) return 1;
		if(Board[0][2]==1) return 5;
		if(Board[2][2]==1) return 8;
		if(Board[2][1]==1) return 9;
	}
	if(Board[0][2]==1){
		if(Board[0][1]==1) return 1;
		if(Board[2][2]==1) return 6;
		if(Board[1][2]==1) return 9;
	}
	if(Board[1][2]==1){
		if(Board[2][2]==1) return 3;
		if(Board[0][1]==1) return 5;
	}
	if(Board[2][1]==1){
		if(Board[0][1]==1) return 5;
		if(Board[2][2]==1) return 7;
	}
	
	/* Check and move to conrers and middle */
	if(Board[0][0]) count--;
	if(Board[1][1]) count--;
	if(Board[0][2]) count--;
	if(Board[2][0]) count--;
	if(Board[2][2]) count--;

	srand(time(NULL));
	num = (rand() % count);

	if(!num) return 1;
	if(num==1) return 3;
	if(num==2) return 5;
	if(num==3) return 7;
	if(num==4) return 9;

	/* If all else fails, pick an open space NOTE: was getting a fail with the for loop
	 trying returns so it picks something, moveMe returns false if it didn't work*/
	return 1; return 2; return 3; return 4; return 5; return 6; return 7; return 8; return 9;
}


enduser000
I understand how your function works and it looks fine to me. (You probably could have saved some typing with for loops). I don't understand what you mean when you say "it looks".

If it reaches line 101 it will return 1 so the other returns are redundant.

Maybe post the loop? I don't see any reason for it in the function you posted.
Okay,
I got it to work... and here was the problem, I was using ==1 and ==2 instead of =='1' and =='2'. What's the difference?
'1', '2' are chars. Though in the parameter you say Board[][] is an array of ints. So that doesn't make sense to me. Maybe someone else knows.

As for chars

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{ 
     char letter;
     letter = 'H'; // is different that letter = H (as in a variable)
     cout << letter << endl;
     return 0;
}
This difference is x==1 and x==2 compare to see if x is equal to the numerical value 1 and the numerical value 2, where x=='1' and x=='2' compare to see if x is equal to the CHARACTER value 1 and 2. All digits have an ascii representation so they can be represented as characters. 1's Ascii representions is 49 decimal, and 2's is 50 decimal. Since board is a matrix of integers the following code fragments
Board[x][x]=='1'
Board[x][x]=='2'
will really turn out to be
Board[x][x]==49
Board[x][x]==50
in effect as the compiler will perform an implicit type case to the right type. So if this causes your code to work, then perhaps Board should be a matrix of characters, not integers
Topic archived. No new replies allowed.