Added lot of junk laying around on harvie.cz
[mirrors/Programs.git] / php / game-of-life / golife.php
diff --git a/php/game-of-life/golife.php b/php/game-of-life/golife.php
new file mode 100644 (file)
index 0000000..fa509ae
--- /dev/null
@@ -0,0 +1,154 @@
+<?php\r
+//Game of life 0.3 (2D Cellular automata example implementation)\r
+//Coded: Harvie 2oo7\r
+/*\r
+ *  The Rules (THX2John Conway):\r
+ *    For a space that is 'populated':\r
+ *      Each cell with one or no neighbors dies, as if by loneliness. \r
+ *      Each cell with four or more neighbors dies, as if by overpopulation. \r
+ *      Each cell with two or three neighbors survives. \r
+ *    For a space that is 'empty' or 'unpopulated':\r
+ *     Each cell with three neighbors becomes populated. \r
+ *\r
+ *  http://www.bitstorm.org/gameoflife/           -Online\offline java based version\r
+ *  http://www.bitstorm.org/gameoflife/lexicon/   -Nice lexicon of lifes, that you can test on this simulator\r
+ *\r
+ *  $life contains array of strings, where every character is one cell\r
+ *  there is also function to load life from file (for more lifes see lexicon)\r
+ *  i included two lifes life.txt - simple small life and glider.txt - the most classical of all, it become symbol of hackership\r
+ *\r
+ *  for better experience please resize console window to size of life, that you are running.\r
+ *\r
+ *  PHP+AsciiART == Rox!!!\r
+ */\r
+\r
+///FUNCTIONS////////////////////////////////////////////////////\r
+\r
+//\r
+function life_load($file) {\r
+  $life = file($file);\r
+  foreach ($life as $lnum => $line) {\r
+    $life[$lnum] = trim($line);\r
+  }\r
+  return $life;\r
+}\r
+\r
+//\r
+function life_randomize($lines, $chars) {\r
+  $pop = $GLOBALS["populated"];\r
+  $unp = $GLOBALS["unpopulated"];\r
+  $life = "";\r
+  $i = 0;\r
+  while($lines > $i) {\r
+    $line = "";\r
+    $ichars = $chars;\r
+    while($ichars > 0) {\r
+      $line = $line.rand(0,1);\r
+      $ichars--;\r
+    }\r
+    $line = str_replace("0", $unp, $line);\r
+    $line = str_replace("1", $pop, $line);\r
+    $life[$i] = $line;\r
+    $i++;\r
+  }\r
+  return $life;\r
+}\r
+\r
+//\r
+function life_print($life) {\r
+  foreach($life as $line) {\r
+      $line = str_replace($GLOBALS["populated"], $GLOBALS["prpop"], $line);\r
+      $line = str_replace($GLOBALS["unpopulated"], $GLOBALS["prunp"], $line);\r
+      echo($line."|\r\n");\r
+  }\r
+}\r
+\r
+//\r
+function life_neighs($life, $line, $char) {\r
+  @$neighs = "";\r
+  @$neighs = $neighs.$life[$line][$char+1];\r
+  @$neighs = $neighs.$life[$line][$char-1];\r
+  @$neighs = $neighs.$life[$line+1][$char+1];\r
+  @$neighs = $neighs.$life[$line+1][$char-1];\r
+  @$neighs = $neighs.$life[$line+1][$char];\r
+  @$neighs = $neighs.$life[$line-1][$char+1];\r
+  @$neighs = $neighs.$life[$line-1][$char-1];\r
+  @$neighs = $neighs.$life[$line-1][$char];\r
+  return $neighs;\r
+}\r
+\r
+//\r
+function life_neighs_num($life, $line, $char) {\r
+  $neighs = life_neighs($life, $line, $char);\r
+  $neighs = ereg_replace("(\.| |0|_|-)", "", $neighs); //Unpopulated chars: . 0_-\r
+  $neighs = strlen($neighs);\r
+  //echo($neighs); //Debug\r
+  return $neighs;\r
+}\r
+\r
+//\r
+function life_next($life) {\r
+\r
+  $pop = $GLOBALS["populated"];\r
+  $unp = $GLOBALS["unpopulated"];\r
+  $old_life = $life;\r
+  \r
+  foreach($life as $lnum => $line) {\r
+    //echo($lnum); //Debug\r
+    for($i = 0;$i < strlen($line);$i++) {\r
+      $neigh_num =  0;\r
+      $neigh_num = life_neighs_num($old_life, $lnum, $i);\r
+      //echo($lnum."-".$i."=".$neigh_num."\n"); //Debug\r
+      if($old_life[$lnum][$i] == $pop) {\r
+          if($neigh_num == 2 || $neigh_num == 3) { $life[$lnum][$i] = $pop; } else { $life[$lnum][$i] = $unp; }\r
+        }\r
+      else\r
+        {\r
+          if($neigh_num == 3) { $life[$lnum][$i] = $pop; }\r
+        }\r
+    }\r
+  }\r
+  return $life;\r
+  \r
+}\r
+\r
+//\r
+function cls() {\r
+  //Clear screen\r
+  for($i = 0; $i < 20;$i++) echo("\r\n");\r
+}\r
+\r
+///CODE///////////////////////////////////////////////////////////////////////////////\r
+\r
+//Settings\r
+$life = life_load("spacefiller.cells"); //Load life\r
+//$life = life_randomize(30, 60); //Randomize life\r
+$populated = "#"; //File format populated\r
+$unpopulated = "."; //File format unpopulated\r
+$prpop = "Û"; //chr(219); //Print chars populated\r
+$prunp = " "; //Print chars unpopulated\r
+$sleep = 1; //sleep in seconds\r
+$usleep = 200000; //sleep in microseconds\r
+\r
+/*\r
+$i = 0;\r
+while($i < 220) { echo($i."-".chr($i)."\n"); $i++; }\r
+*/\r
+\r
+//Run\r
+$backup_life = $life;\r
+$old_life = "";\r
+$generation = 1;\r
+while($old_life != $life) { //While moving\r
+  $old_life = $life;\r
+  echo("Generation: ".$generation."\r\n");\r
+  life_print($life);\r
+    //cls(); //Shifting\r
+  $life = life_next($life);\r
+  //sleep($sleep);\r
+  //usleep($usleep);\r
+  $generation++;\r
+}\r
+\r
+\r
+?>\r
This page took 0.128879 seconds and 4 git commands to generate.