Last active
August 29, 2015 14:21
-
-
Save wouterj/a232644f4050282d5f08 to your computer and use it in GitHub Desktop.
Game of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function life($i){foreach($i as$j=>$a)foreach($a as$k=>$b){$l=array_sum([($r=!!@$i[$x=$y=$j-1])&&$i[$x][$k],($R=!!@$i[$y+=2])&&$i[$y][$k],($u=$i[$j])&&($c=isset($u[$z=$k-1]))&&$u[$z],$c&&$r&&$i[$x][$z],$c&&$R&&$i[$y][$z],($C=isset($u[$z+=2]))&&$u[$z],$C&&$r&&$i[$x][$z],$C&&$R&&$i[$y][$z]]);$o[$j][$k]=$b?(!($l<2||$l>3)):($l==3);}return$o;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include '1-life.php'; | |
// util | |
function show($world) { | |
foreach ($world as $r) { | |
foreach ($r as $c) { | |
echo ($c ? 'x' : ' '); | |
} | |
echo PHP_EOL; | |
} | |
} | |
// create world (30x30) is almost the max the | |
// terminal can cope with at a reasonable speed. | |
$world = []; | |
$size = 30; | |
for ($s = 0; $s < $size; $s++) { | |
for ($ss = 0; $ss < $size; $ss++) { | |
$world[$s][$ss] = floor(pow(lcg_value(), 40)*2); | |
} | |
} | |
// output first generation | |
echo 'Generation 1'.PHP_EOL, | |
show($world), | |
PHP_EOL; | |
// outputting all further generations | |
for ($g = 1; true; $g++) { | |
sleep(.7); | |
$prevWorld = $world; | |
$world = life($world); | |
echo chr(27)."[".($size + 2)."A\r", | |
'# Generation '.$g.PHP_EOL, | |
show($world), | |
PHP_EOL; | |
if ($prevWorld == $world) { | |
exit; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function life($i) { | |
// $i: input world | |
// $o: output world | |
foreach ($i as $j => $a) | |
// $a: row of world | |
foreach($a as $k => $b) { | |
// $l: living neighbours | |
$l = array_sum([ | |
// calculate living cells (true = 1 and false = 0 when using the + operator) | |
// !!@ behaves like isset() when you know the item won't be a falsey value (a filled array isn't considered falsey) | |
// $r: not first row | |
// above middle | |
($r = !!@$i[$x = $y = $j-1]) && $i[$x][$k], | |
// $R: not last row | |
// $y = $j+1 = ($j-1) + 2 = $x + 2 | |
// below middle | |
($R = !!@$i[$y += 2]) && $i[$y][$k], | |
// $c: not first column | |
// left | |
($u = $i[$j]) && ($c = isset($u[$z = $k-1])) && $u[$z], | |
// above left | |
$c && $r && $i[$x][$z], | |
// below left | |
$c && $R && $i[$y][$z], | |
// $C: not last column | |
// right | |
($C = isset($u[$z+=2])) && $u[$z], | |
// above right | |
$C && $r && $i[$x][$z], | |
// below right | |
$C && $R && $i[$y][$z] | |
]); | |
$o[$j][$k] = $b | |
// a living cell remains alive if it has 2 or 3 living neighbours | |
? (!($l < 2 || $l > 3)) | |
// a dead cell becomes alive if it has exactly 3 living neighbours | |
: ($l == 3) | |
; | |
} | |
return $o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment