Search the Community
Showing results for tags 'tictactoe'.
-
I have following code block for a tic-tac-toe's game. Please help me simplifying it. $grid=array(); $grid[0]=array(' ',' ',' '); $grid[1]=array(' ',' ',' '); $grid[2]=array(' ',' ',' '); $user='X'; $com='O'; $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] The code is working fine but need to reduce it.