Added lot of junk laying around on harvie.cz
[mirrors/Programs.git] / php / game-of-life / golife.php
1 <?php
2 //Game of life 0.3 (2D Cellular automata example implementation)
3 //Coded: Harvie 2oo7
4 /*
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.
12 *
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
15 *
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
19 *
20 * for better experience please resize console window to size of life, that you are running.
21 *
22 * PHP+AsciiART == Rox!!!
23 */
24
25 ///FUNCTIONS////////////////////////////////////////////////////
26
27 //
28 function life_load($file) {
29 $life = file($file);
30 foreach ($life as $lnum => $line) {
31 $life[$lnum] = trim($line);
32 }
33 return $life;
34 }
35
36 //
37 function life_randomize($lines, $chars) {
38 $pop = $GLOBALS["populated"];
39 $unp = $GLOBALS["unpopulated"];
40 $life = "";
41 $i = 0;
42 while($lines > $i) {
43 $line = "";
44 $ichars = $chars;
45 while($ichars > 0) {
46 $line = $line.rand(0,1);
47 $ichars--;
48 }
49 $line = str_replace("0", $unp, $line);
50 $line = str_replace("1", $pop, $line);
51 $life[$i] = $line;
52 $i++;
53 }
54 return $life;
55 }
56
57 //
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);
62 echo($line."|\r\n");
63 }
64 }
65
66 //
67 function life_neighs($life, $line, $char) {
68 @$neighs = "";
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];
77 return $neighs;
78 }
79
80 //
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
86 return $neighs;
87 }
88
89 //
90 function life_next($life) {
91
92 $pop = $GLOBALS["populated"];
93 $unp = $GLOBALS["unpopulated"];
94 $old_life = $life;
95
96 foreach($life as $lnum => $line) {
97 //echo($lnum); //Debug
98 for($i = 0;$i < strlen($line);$i++) {
99 $neigh_num = 0;
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; }
104 }
105 else
106 {
107 if($neigh_num == 3) { $life[$lnum][$i] = $pop; }
108 }
109 }
110 }
111 return $life;
112
113 }
114
115 //
116 function cls() {
117 //Clear screen
118 for($i = 0; $i < 20;$i++) echo("\r\n");
119 }
120
121 ///CODE///////////////////////////////////////////////////////////////////////////////
122
123 //Settings
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
132
133 /*
134 $i = 0;
135 while($i < 220) { echo($i."-".chr($i)."\n"); $i++; }
136 */
137
138 //Run
139 $backup_life = $life;
140 $old_life = "";
141 $generation = 1;
142 while($old_life != $life) { //While moving
143 $old_life = $life;
144 echo("Generation: ".$generation."\r\n");
145 life_print($life);
146 //cls(); //Shifting
147 $life = life_next($life);
148 //sleep($sleep);
149 //usleep($usleep);
150 $generation++;
151 }
152
153
154 ?>
This page took 0.496621 seconds and 4 git commands to generate.