Jump to content


Need To Simplify A Code..

php tictactoe

  • Please log in to reply
2 replies to this topic

#1 gauravtips



  • New Members
  • Pip
  • 1 posts

Posted 31 October 2012 - 01:28 AM

I have following code block for a tic-tac-toe's game. Please help me simplifying it.

        $grid[0]=array(' ',' ',' ');
        $grid[1]=array(' ',' ',' ');
        $grid[2]=array(' ',' ',' ');
        //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.

#2 shlumph


    Advanced Member

  • Members
  • PipPipPip
  • 576 posts
  • LocationNew Hampshire

Posted 15 November 2012 - 12:44 PM

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.
whether you think you can, or can't, you're right -HF
Visit my website or github

#3 txmedic03


    Advanced Member

  • Members
  • PipPipPip
  • 313 posts
  • LocationCall, TX, USA

Posted 20 November 2012 - 10:48 AM

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?


I can't stop you from doing something silly, but at least I can help you do it right.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime