2 //Game of life 0.3 (2D Cellular automata example implementation)
5 * The Rules (THX2John Conway):
6 * For a space that is 'populated':
7 * Each cell with one or no neighbors dies, as if by loneliness.
8 * Each cell with four or more neighbors dies, as if by overpopulation.
9 * Each cell with two or three neighbors survives.
10 * For a space that is 'empty' or 'unpopulated':
11 * Each cell with three neighbors becomes populated.
13 * http://www.bitstorm.org/gameoflife/ -Online\offline java based version
14 * http://www.bitstorm.org/gameoflife/lexicon/ -Nice lexicon of lifes, that you can test on this simulator
16 * $life contains array of strings, where every character is one cell
17 * there is also function to load life from file (for more lifes see lexicon)
18 * i included two lifes life.txt - simple small life and glider.txt - the most classical of all, it become symbol of hackership
20 * for better experience please resize console window to size of life, that you are running.
22 * PHP+AsciiART == Rox!!!
25 ///FUNCTIONS////////////////////////////////////////////////////
28 function life_load($file) {
30 foreach ($life as $lnum => $line) {
31 $life[$lnum] = trim($line);
37 function life_randomize($lines, $chars) {
38 $pop = $GLOBALS["populated"];
39 $unp = $GLOBALS["unpopulated"];
46 $line = $line.rand(0,1);
49 $line = str_replace("0", $unp, $line);
50 $line = str_replace("1", $pop, $line);
58 function life_print($life) {
59 foreach($life as $line) {
60 $line = str_replace($GLOBALS["populated"], $GLOBALS["prpop"], $line);
61 $line = str_replace($GLOBALS["unpopulated"], $GLOBALS["prunp"], $line);
67 function life_neighs($life, $line, $char) {
69 @$neighs = $neighs.$life[$line][$char+
1];
70 @$neighs = $neighs.$life[$line][$char-1];
71 @$neighs = $neighs.$life[$line+
1][$char+
1];
72 @$neighs = $neighs.$life[$line+
1][$char-1];
73 @$neighs = $neighs.$life[$line+
1][$char];
74 @$neighs = $neighs.$life[$line-1][$char+
1];
75 @$neighs = $neighs.$life[$line-1][$char-1];
76 @$neighs = $neighs.$life[$line-1][$char];
81 function life_neighs_num($life, $line, $char) {
82 $neighs = life_neighs($life, $line, $char);
83 $neighs = ereg_replace("(\.| |0|_|-)", "", $neighs); //Unpopulated chars: . 0_-
84 $neighs = strlen($neighs);
85 //echo($neighs); //Debug
90 function life_next($life) {
92 $pop = $GLOBALS["populated"];
93 $unp = $GLOBALS["unpopulated"];
96 foreach($life as $lnum => $line) {
97 //echo($lnum); //Debug
98 for($i = 0;$i < strlen($line);$i++
) {
100 $neigh_num = life_neighs_num($old_life, $lnum, $i);
101 //echo($lnum."-".$i."=".$neigh_num."\n"); //Debug
102 if($old_life[$lnum][$i] == $pop) {
103 if($neigh_num == 2 ||
$neigh_num == 3) { $life[$lnum][$i] = $pop; } else { $life[$lnum][$i] = $unp; }
107 if($neigh_num == 3) { $life[$lnum][$i] = $pop; }
118 for($i = 0; $i < 20;$i++
) echo("\r\n");
121 ///CODE///////////////////////////////////////////////////////////////////////////////
124 $life = life_load("spacefiller.cells"); //Load life
125 //$life = life_randomize(30, 60); //Randomize life
126 $populated = "#"; //File format populated
127 $unpopulated = "."; //File format unpopulated
128 $prpop = "Û"; //chr(219); //Print chars populated
129 $prunp = " "; //Print chars unpopulated
130 $sleep = 1; //sleep in seconds
131 $usleep = 200000; //sleep in microseconds
135 while($i < 220) { echo($i."-".chr($i)."\n"); $i++; }
139 $backup_life = $life;
142 while($old_life != $life) { //While moving
144 echo("Generation: ".$generation."\r\n");
147 $life = life_next($life);
This page took 0.359353 seconds and 4 git commands to generate.