Need to simplify a code block

I have following code block for a tic-tac-toe's game. Please help me simplifying 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
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
		$ran=0;
		//For user
		if(($grid[0][1]==$grid[0][2])&&$grid[0][1]!=' '&&$grid[0][0]==' '){$ran=1;}
		if(($grid[1][0]==$grid[2][0])&&$grid[1][0]!=' '&&$grid[0][0]==' '){$ran=1;}
		if(($grid[1][1]==$grid[2][2])&&$grid[1][1]!=' '&&$grid[0][0]==' '){$ran=1;}
		if(($grid[0][0]==$grid[0][2])&&$grid[0][0]!=' '&&$grid[0][1]==' '){$ran=2;}
		if(($grid[1][1]==$grid[2][1])&&$grid[1][1]!=' '&&$grid[0][1]==' '){$ran=2;}
		if(($grid[0][0]==$grid[0][1])&&$grid[0][0]!=' '&&$grid[0][2]==' '){$ran=3;}
		if(($grid[1][1]==$grid[2][0])&&$grid[1][1]!=' '&&$grid[0][2]==' '){$ran=3;}
		if(($grid[1][2]==$grid[2][2])&&$grid[1][2]!=' '&&$grid[0][2]==' '){$ran=3;}
		if(($grid[0][0]==$grid[2][0])&&$grid[0][0]!=' '&&$grid[1][0]==' '){$ran=4;}
		if(($grid[1][1]==$grid[1][2])&&$grid[1][1]!=' '&&$grid[1][0]==' '){$ran=4;}
		if(($grid[0][0]==$grid[2][2])&&$grid[0][0]!=' '&&$grid[1][1]==' '){$ran=5;}
		if(($grid[0][1]==$grid[2][1])&&$grid[0][1]!=' '&&$grid[1][1]==' '){$ran=5;}
		if(($grid[0][2]==$grid[2][0])&&$grid[0][2]!=' '&&$grid[1][1]==' '){$ran=5;}
		if(($grid[1][0]==$grid[1][2])&&$grid[1][0]!=' '&&$grid[1][1]==' '){$ran=5;}
		if(($grid[0][2]==$grid[2][2])&&$grid[0][2]!=' '&&$grid[1][2]==' '){$ran=6;}
		if(($grid[1][0]==$grid[1][1])&&$grid[1][0]!=' '&&$grid[1][2]==' '){$ran=6;}
		if(($grid[0][0]==$grid[1][0])&&$grid[0][0]!=' '&&$grid[2][0]==' '){$ran=7;}
		if(($grid[0][2]==$grid[1][1])&&$grid[0][2]!=' '&&$grid[2][0]==' '){$ran=7;}
		if(($grid[2][1]==$grid[2][2])&&$grid[2][1]!=' '&&$grid[2][0]==' '){$ran=7;}
		if(($grid[0][1]==$grid[1][1])&&$grid[0][1]!=' '&&$grid[2][1]==' '){$ran=8;}
		if(($grid[2][0]==$grid[2][2])&&$grid[2][0]!=' '&&$grid[2][1]==' '){$ran=8;}
		if(($grid[0][0]==$grid[1][1])&&$grid[0][0]!=' '&&$grid[2][2]==' '){$ran=9;}
		if(($grid[0][2]==$grid[1][2])&&$grid[0][2]!=' '&&$grid[2][2]==' '){$ran=9;}
		if(($grid[2][0]==$grid[2][1])&&$grid[2][0]!=' '&&$grid[2][2]==' '){$ran=9;}
		//For computer
		if(($grid[0][1]==$grid[0][2])&&$grid[0][1]==$com&&$grid[0][0]==' '){$ran=1;}
		if(($grid[1][0]==$grid[2][0])&&$grid[1][0]==$com&&$grid[0][0]==' '){$ran=1;}
		if(($grid[1][1]==$grid[2][2])&&$grid[1][1]==$com&&$grid[0][0]==' '){$ran=1;}
		if(($grid[0][0]==$grid[0][2])&&$grid[0][0]==$com&&$grid[0][1]==' '){$ran=2;}
		if(($grid[1][1]==$grid[2][1])&&$grid[1][1]==$com&&$grid[0][1]==' '){$ran=2;}
		if(($grid[0][0]==$grid[0][1])&&$grid[0][0]==$com&&$grid[0][2]==' '){$ran=3;}
		if(($grid[1][2]==$grid[2][2])&&$grid[1][2]==$com&&$grid[0][2]==' '){$ran=3;}
		if(($grid[1][1]==$grid[2][0])&&$grid[1][1]==$com&&$grid[0][2]==' '){$ran=3;}
		if(($grid[1][1]==$grid[1][2])&&$grid[1][1]==$com&&$grid[1][0]==' '){$ran=4;}
		if(($grid[0][0]==$grid[2][0])&&$grid[0][0]==$com&&$grid[1][0]==' '){$ran=4;}
		if(($grid[1][0]==$grid[1][2])&&$grid[1][0]==$com&&$grid[1][1]==' '){$ran=5;}
		if(($grid[0][1]==$grid[2][1])&&$grid[0][1]==$com&&$grid[1][1]==' '){$ran=5;}
		if(($grid[0][0]==$grid[2][2])&&$grid[0][0]==$com&&$grid[1][1]==' '){$ran=5;}
		if(($grid[0][2]==$grid[2][0])&&$grid[0][2]==$com&&$grid[1][1]==' '){$ran=5;}
		if(($grid[1][0]==$grid[1][1])&&$grid[1][0]==$com&&$grid[1][2]==' '){$ran=6;}
		if(($grid[0][2]==$grid[2][2])&&$grid[0][2]==$com&&$grid[1][2]==' '){$ran=6;}
		if(($grid[2][1]==$grid[2][2])&&$grid[2][1]==$com&&$grid[2][0]==' '){$ran=7;}
		if(($grid[0][0]==$grid[1][0])&&$grid[0][0]==$com&&$grid[2][0]==' '){$ran=7;}
		if(($grid[0][2]==$grid[1][1])&&$grid[0][2]==$com&&$grid[2][0]==' '){$ran=7;}
		if(($grid[2][0]==$grid[2][2])&&$grid[2][0]==$com&&$grid[2][1]==' '){$ran=8;}
		if(($grid[0][1]==$grid[1][1])&&$grid[0][1]==$com&&$grid[2][1]==' '){$ran=8;}
		if(($grid[0][1]==$grid[1][1])&&$grid[0][1]==$com&&$grid[2][1]==' '){$ran=8;}
		if(($grid[2][0]==$grid[2][1])&&$grid[2][0]==$com&&$grid[2][2]==' '){$ran=9;}
		if(($grid[0][2]==$grid[1][2])&&$grid[0][2]==$com&&$grid[2][2]==' '){$ran=9;}
		if(($grid[0][0]==$grid[1][1])&&$grid[0][0]==$com&&$grid[2][2]==' '){$ran=9;}
		return $ran;


Here grid is an 2d array of game's grid and ran is the next position of computer's move. 1 for [0][0], 2 for [0][1],....,8 for [2][1] and 9 for [2][2]
Default value of grid elements is ' ' and $com stands for computer's cue.
If I understand this correctly, you are checking whether 2 spots are taken up in any row, col or diagonal.

This can be simplified to checking the adjacent spot. So for corner spots such as [0][0], [0][2] etc check the adjacent spot in the row col and diagonal. For spots such as [0][1], [1][2], [1][2], and [1][0] you only need to check the centre spot because the others have been checked when you did the corners.

Adjacency can be achieved by adding or subtracting 1 from the Row or Col variables.

So the whole thing can be done with nested for loops and an adjacency function. Same thing for checking for a win.

Hope this helps.

BTW this is the first time I have seen anyone using $ as part of a variable name. It's not illegal or anything - just unusual.
I will try it. Actually it is a php's code...
Topic archived. No new replies allowed.