Question

So, basically, I've been writing this Game of Life PHP script. My output is wack and I can't figure it out! The whole scheme consists of a 2-dimensional array called $world in which each value corresponds to a 2-state cell that can be 1 or 0(alive or dead). Each cell has 8 neighbors. The rules to compute the next state of the system are as follows:

  1. If you have 2-3 live neighbors(1's), you're alive the next turn.
  2. Otherwise you're dead.

My implementation is as follows: (note: $j_minus, $i_plus, etc refers to $j-1, etc but takes the edges into account)

for($i=0;$i<$size;$i++)
{ 
    for($j=0;$j<$size;$j++) 
    {

            if( ($world[$j_minus][$i] + $world[$j_plus][$i] + $world[$j][$i_minus] + $world[$j][$i_plus]+$world[$j_minus][$i_minus]+$world[$j_minus][$i_plus]+$world[$j_plus][$i_minus]+$world[$j_plus][$i_plus]) > 3 )
            {
                $new_world[$j][$i]=0;

            }

            else if( ($world[$j_minus][$i] + $world[$j_plus][$i] + $world[$j][$i_minus] + $world[$j][$i_plus]+$world[$j_minus][$i_minus]+$world[$j_minus][$i_plus]+$world[$j_plus][$i_minus]+$world[$j_plus][$i_plus])>= 2 )
            {
                $new_world[$j][$i]=1;

            }
            else {$new_world[$j][$i]=0;}
        }

}

After these rules have been applied, making $new_world the new state of the system, I go to print the array to the screen with this:

for($i=0;$i<$size;$i++)
{ 
    for($j=0;$j<$size;$j++) 
    {

        echo $new_world[$i][$j]." ";
    }
        echo "</p>";
}

What I get, regardless of the initial state of $world, is either a completely stagnant state full of lines and big blocks or an oscillation between 2-3 such states. The rules don't seem to be applying correctly!

Was it helpful?

Solution

In the code you show nothing looks wrong... you could combine the if statements into one but still minor detail. Therefore, look and make sure that the $j_minus,$i_right, etc. values are actually what you expect. Make sure that at no point is newworld = world (it does't look like that here but we can't see the loop that this lives in where you update world). As suggested work out an example by hand and you should see where your program goes wrong. Finally make sure that this isn't the right answer... some times the life stagnates in the game of life.

OTHER TIPS

Walk through it on paper and see how it's turning out. You should be able to tell after a few loops where it's not calculating correctly.

From a pure readability point of view, I think you would be better writing it like this:

for ($i=0; $i < $size; $i++) {

    for ($j = 0; $j < $size; $j++) {

        $liveNeighbours = $world[$j_minus][$i]
                        + $world[$j_plus][$i]
                        + $world[$j][$i_minus]
                        + $world[$j][$i_plus]
                        + $world[$j_minus][$i_minus]
                        + $world[$j_minus][$i_plus]
                        + $world[$j_plus][$i_minus]
                        + $world[$j_plus][$i_plus];

        if ($liveNeighbours == 2 || $liveNeighbours == 3) {
            $new_world[$j][$i] = 1;
        } else {
            $new_world[$j][$i] = 0;
        }

    }

}

Spacing your code correctly can very much help you spot errors in the logic. I cannot tell you exactly what the problem is, because you are not showing us how you have created the $j_plus etc variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top