Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:00:56+00:00 2026-05-27T12:00:56+00:00

So, basically, I’ve been writing this Game of Life PHP script. My output is

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:00:57+00:00Added an answer on May 27, 2026 at 12:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I want to do it this: a pdb file contains a location
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically, I would like a brief explanation of how I can access a SQL
Basically I am trying to restart a service from a php web page. Here
Basically from a database I am getting data that is formatted like this nameofproject101
Basically I'm attempting to add rows to a table, I need to do this
Basically I am writing a simple bit of code to increment every one second
Basically this function is meant to store the height value of the element that
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.
Basically I have a loop incrementing i, and I want to do this: var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.