Page 1 of 1

I need help! Conway game of life PHP version

Posted: Fri Aug 12, 2011 2:27 am
by darkhao
Life!

Write a program to simulate a simplified version of Conway’s Game of Life. The Game Of Life is a zero player game which starts from an initial (randomly generated) 2D array configuration and then evolves without any user input.

Assume there is no user input for this simulation. The simulation begins with a 16 x 16 2D array, which represents life. Each cell in the 2D array can be in one of two states: live or dead. Every cell in the 2D array can only interact with its surrounding 8 neighbours.

In the beginning, the program should randomly generate “dead” or “live” values for the initial configuration of the 2D array. Next, the program should generate a random number between 5 and 20, inclusive, which will be the number of times the simulation should run. For example, when 5 is randomly generated, the program should compute and display the next 5 steps of the 2D array configuration.

For each step of the simulation, the following rules must be applied to each cell:

• Any “live” cell that has less than 2 “live” neighbour cells becomes “dead” due to under-population.
• Any “live” cell that has 2 or 3 “live” neighbour cells remains “live”.
• Any “live” cell that has more than 3 “live” neighbour cells becomes “dead” due to over-crowding.
• Any “dead” cell that has exactly 3 “live” neighbour cells becomes “live” again due to reproduction.

Thus the simulation will run as follows:
• Show the initial configuration of the 2D array with the cells either “live” or “dead”
• Show the random number generated indicating the number of steps it will run
• For each step:
o Show the next configuration of the 2D array with the cells either “live” or “dead” based on the rules above.

At the end of the simulation game, the program should print the following statistics:
• Total number of initial “live” cells.
• Total number of initial “dead” cells.
• Total number of cells that went from “live” to “dead” after all the steps.
• Total number of cells that went from “dead” to “live” after all the steps.

The program must, at least, include the following methods with the correct input parameters and output types:

i. initializeFirstConfiguration, which will take as input an empty 2D array and randomly assign “live” or “dead” states to all the cells in the array

ii. generateNextConfiguration, which will accept as input the current configuration 2D array and the next configuration 2D array. It will process the current configuration 2D array by applying the rules to generate the new values in the next configuration array.

Additional info
• When finding the 8 neighbours’ values of each cell, if a cell does not have 8 neighbours (e.g. for a corner cell), then the program should just find only for those existing neighbours.

• Therefore (ii) should be as follows:

(ii) generateNextConfiguration, which will accept as input at least the current configuration 2D array and any other data as needed. It will process the current configuration 2D array by applying the rules to generate the new values in the next configuration array.

i hope you guys can help with my project

Re: I need help! Conway game of life PHP version

Posted: Fri Aug 12, 2011 10:38 am
by McInfo
This appears to be a homework assignment, which means your instructor believes you are capable of completing the task. You won't find a shrink-wrapped solution here, but you will find guidance if you ask questions about particular points. Show that you are willing to make an effort, and we will be happy to help you.

What plan do you have so far? Do you need help with planning an overall design? Have you started to write code? What is the first thing you are struggling with?

Re: I need help! Conway game of life PHP version

Posted: Sat Aug 13, 2011 1:24 am
by darkhao

Code: Select all

<?php
$board = array();
for ($i = 0; $i < 16; $i++)
{
	for ($j = 0; $j < 16; $j++)
	{
	     $board[$i][$j] = rand(0,1);
	}
}
for ($i = 0; $i < 16; $i++)
{
	for ($j = 0; $j < 16; $j++)
	{
		print $board[$i][$j] . " ";
	}

	print "<br/>";
}
?>
i am stuck here :(

Re: I need help! Conway game of life PHP version

Posted: Sat Aug 13, 2011 1:29 am
by darkhao
i was told that i needed 2 for loop test for 8 ifelse

Re: I need help! Conway game of life PHP version

Posted: Sun Aug 14, 2011 2:52 pm
by McInfo
Your specification asks you to write a function to load the array with random values:
darkhao wrote:i. initializeFirstConfiguration, which will take as input an empty 2D array and randomly assign “live” or “dead” states to all the cells in the array
It seems to want the array passed by reference. You already have written the body of the function:

Code: Select all

$board = array();
for ($i = 0; $i < 16; $i++)
{
        for ($j = 0; $j < 16; $j++)
        {
             $board[$i][$j] = rand(0,1);
        }
}
You just need to move that into a function definition.

This code might go in a printBoard function:

Code: Select all

for ($i = 0; $i < 16; $i++)
{
        for ($j = 0; $j < 16; $j++)
        {
                print $board[$i][$j] . " ";
        }

        print "<br/>";
}
Instead of using magic numbers like 16, you can add meaning to them by defining constants. For example, you might define BOARD_WIDTH and BOARD_HEIGHT.

The board array has two dimensions. The first dimension is the row number; the second is the column number. That makes counting the number of living neighbors not too difficult. Simply subtract or add one to move to an adjacent row or column. This table shows (row, column) offsets relative to the center cell.

Code: Select all

+----------+----------+----------+
| (-1, -1) | (-1,  0) | (-1,  1) |
+----------+----------+----------+
| ( 0, -1) | ( 0,  0) | ( 0,  1) |
+----------+----------+----------+
| ( 1, -1) | ( 1,  0) | ( 1,  1) |
+----------+----------+----------+
For example, the cell above and right of $board[5][8] is $board[5-1][8+1] or $board[4][9].

The simplest way to write the generateNextConfiguration code is, for every cell, count the number of neighbors. You might write a countCellsNear function. If a cell has fewer than two or more than three neighbors, it dies. If it has exactly three neighbors, it lives. When determining the next generation, you need to write to a copy of the array to avoid overwriting cells before neighboring cells have read the old value.

Re: I need help! Conway game of life PHP version

Posted: Tue Aug 16, 2011 5:47 am
by darkhao
Thank for the help
anyway i have not been taught "Instead of using magic numbers like 16, you can add meaning to them by defining constants. For example, you might define BOARD_WIDTH and BOARD_HEIGHT."

is it possible to be done by using if else?

Re: I need help! Conway game of life PHP version

Posted: Tue Aug 16, 2011 10:22 pm
by McInfo
If you haven't learned about constants, they are unnecessary for this assignment; so don't worry about that suggestion.