added tips to lamer.bash help
[mirrors/Programs.git] / php / game-of-life / golife.php
CommitLineData
8de51304
H
1<?php\r
2//Game of life 0.3 (2D Cellular automata example implementation)\r
3//Coded: Harvie 2oo7\r
4/*\r
5 * The Rules (THX2John Conway):\r
6 * For a space that is 'populated':\r
7 * Each cell with one or no neighbors dies, as if by loneliness. \r
8 * Each cell with four or more neighbors dies, as if by overpopulation. \r
9 * Each cell with two or three neighbors survives. \r
10 * For a space that is 'empty' or 'unpopulated':\r
11 * Each cell with three neighbors becomes populated. \r
12 *\r
13 * http://www.bitstorm.org/gameoflife/ -Online\offline java based version\r
14 * http://www.bitstorm.org/gameoflife/lexicon/ -Nice lexicon of lifes, that you can test on this simulator\r
15 *\r
16 * $life contains array of strings, where every character is one cell\r
17 * there is also function to load life from file (for more lifes see lexicon)\r
18 * i included two lifes life.txt - simple small life and glider.txt - the most classical of all, it become symbol of hackership\r
19 *\r
20 * for better experience please resize console window to size of life, that you are running.\r
21 *\r
22 * PHP+AsciiART == Rox!!!\r
23 */\r
24\r
25///FUNCTIONS////////////////////////////////////////////////////\r
26\r
27//\r
28function life_load($file) {\r
29 $life = file($file);\r
30 foreach ($life as $lnum => $line) {\r
31 $life[$lnum] = trim($line);\r
32 }\r
33 return $life;\r
34}\r
35\r
36//\r
37function life_randomize($lines, $chars) {\r
38 $pop = $GLOBALS["populated"];\r
39 $unp = $GLOBALS["unpopulated"];\r
40 $life = "";\r
41 $i = 0;\r
42 while($lines > $i) {\r
43 $line = "";\r
44 $ichars = $chars;\r
45 while($ichars > 0) {\r
46 $line = $line.rand(0,1);\r
47 $ichars--;\r
48 }\r
49 $line = str_replace("0", $unp, $line);\r
50 $line = str_replace("1", $pop, $line);\r
51 $life[$i] = $line;\r
52 $i++;\r
53 }\r
54 return $life;\r
55}\r
56\r
57//\r
58function life_print($life) {\r
59 foreach($life as $line) {\r
60 $line = str_replace($GLOBALS["populated"], $GLOBALS["prpop"], $line);\r
61 $line = str_replace($GLOBALS["unpopulated"], $GLOBALS["prunp"], $line);\r
62 echo($line."|\r\n");\r
63 }\r
64}\r
65\r
66//\r
67function life_neighs($life, $line, $char) {\r
68 @$neighs = "";\r
69 @$neighs = $neighs.$life[$line][$char+1];\r
70 @$neighs = $neighs.$life[$line][$char-1];\r
71 @$neighs = $neighs.$life[$line+1][$char+1];\r
72 @$neighs = $neighs.$life[$line+1][$char-1];\r
73 @$neighs = $neighs.$life[$line+1][$char];\r
74 @$neighs = $neighs.$life[$line-1][$char+1];\r
75 @$neighs = $neighs.$life[$line-1][$char-1];\r
76 @$neighs = $neighs.$life[$line-1][$char];\r
77 return $neighs;\r
78}\r
79\r
80//\r
81function life_neighs_num($life, $line, $char) {\r
82 $neighs = life_neighs($life, $line, $char);\r
83 $neighs = ereg_replace("(\.| |0|_|-)", "", $neighs); //Unpopulated chars: . 0_-\r
84 $neighs = strlen($neighs);\r
85 //echo($neighs); //Debug\r
86 return $neighs;\r
87}\r
88\r
89//\r
90function life_next($life) {\r
91\r
92 $pop = $GLOBALS["populated"];\r
93 $unp = $GLOBALS["unpopulated"];\r
94 $old_life = $life;\r
95 \r
96 foreach($life as $lnum => $line) {\r
97 //echo($lnum); //Debug\r
98 for($i = 0;$i < strlen($line);$i++) {\r
99 $neigh_num = 0;\r
100 $neigh_num = life_neighs_num($old_life, $lnum, $i);\r
101 //echo($lnum."-".$i."=".$neigh_num."\n"); //Debug\r
102 if($old_life[$lnum][$i] == $pop) {\r
103 if($neigh_num == 2 || $neigh_num == 3) { $life[$lnum][$i] = $pop; } else { $life[$lnum][$i] = $unp; }\r
104 }\r
105 else\r
106 {\r
107 if($neigh_num == 3) { $life[$lnum][$i] = $pop; }\r
108 }\r
109 }\r
110 }\r
111 return $life;\r
112 \r
113}\r
114\r
115//\r
116function cls() {\r
117 //Clear screen\r
118 for($i = 0; $i < 20;$i++) echo("\r\n");\r
119}\r
120\r
121///CODE///////////////////////////////////////////////////////////////////////////////\r
122\r
123//Settings\r
124$life = life_load("spacefiller.cells"); //Load life\r
125//$life = life_randomize(30, 60); //Randomize life\r
126$populated = "#"; //File format populated\r
127$unpopulated = "."; //File format unpopulated\r
128