gauravtips Posted October 31, 2012 Share Posted October 31, 2012 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. Link to comment https://forums.phpfreaks.com/topic/270103-need-to-simplify-a-code/ Share on other sites More sharing options...
shlumph Posted November 15, 2012 Share Posted November 15, 2012 The only difference I see between the //For User block and the //For computer block is $com. So, to start, you can wrap this block into it's own function and call it twice. After that, I'd look at simplifying this new function. Link to comment https://forums.phpfreaks.com/topic/270103-need-to-simplify-a-code/#findComment-1392704 Share on other sites More sharing options...
txmedic03 Posted November 20, 2012 Share Posted November 20, 2012 I think you just broke my brain with all those if statements. I was totally unprepared for that. If I am understanding the code correctly, your arrays are referring to the blocks on a tic-tac-toe board in the fashion that you described and your code is basically running through all the possible winning combinations of 3 items and in the user case you are trying to block any of their potential wins and in the second half you take any victory by completing 3 in-a-row for the computer. After all possible scenarios are considered then $ran is the block that would be the most logical next move for the computer, right? Okay, assuming that I am correct about all that, I would say there has to be a way to use a loop perhaps and start grouping the boxes up. There is a definite pattern to your movement through the different combinations and perhaps you could capitalize on that with a for loop that incremented integers to select the different blocks and compare the various solutions. I can understand the desire to shrink the code and make it look clean and efficient, but I question whether it is really worth it if the code is currently working and you are not suffering from massive inefficiency. What would potentially shaving a few lines of code off really do for you? Link to comment https://forums.phpfreaks.com/topic/270103-need-to-simplify-a-code/#findComment-1393814 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.