Created
May 20, 2012 13:01
-
-
Save smasty/2758041 to your computer and use it in GitHub Desktop.
The Game of Life in PHP
This file contains hidden or 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 | |
/** | |
* The Game of Life - a PHP simulator of the famous game. | |
* Early development state. | |
* | |
* Copyright 2011 Martin Srank (http://smasty.net) | |
* | |
* This source file is subject to the MIT license: | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the „Software“), to | |
* deal in the Software without restriction, including without limitation the | |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is furnished | |
* to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED „AS IS“, WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
* | |
*/ | |
// Glider | |
$game = new GameOfLife(41); | |
$game->table[3][5] = 1; | |
$game->table[4][5] = 1; | |
$game->table[5][5] = 1; | |
$game->table[4][3] = 1; | |
$game->table[5][4] = 1; | |
echo $game->runSimulation(1000, 25); | |
/** | |
* The Game of Life | |
*/ | |
class GameOfLife { | |
public $table, $table2 = array(); | |
private $size = 48; | |
public function __construct($size = null){ | |
if($size !== null) | |
$this->size = $size; | |
$this->createTable($size); | |
} | |
public function createTable($size){ | |
for($i = 1; $i <= $size; $i++) | |
for($j = 1; $j <= $size; $j++) | |
$this->table[$i][$j] = $this->table2[$i][$j] = 0; | |
} | |
public function nextCellState($x, $y){ | |
$cell = $this->table[$x][$y]; | |
$neighbours = 0; | |
for($i = $x-1; $i <= $x+1; $i++) | |
for($j = $y-1; $j <= $y+1; $j++) | |
if($this->table[$this->coord($i)][$this->coord($j)] == 1 && !($i == $x && $j == $y)) | |
$neighbours++; | |
if($cell) | |
return ($neighbours == 2 || $neighbours == 3) ? 1 : 0; | |
else | |
return ($neighbours == 3) ? 1 : 0; | |
} | |
public function paintState(){ | |
for($i = 1; $i <= $this->size; $i++){ | |
for($j = 1; $j <= $this->size; $j++) | |
echo (bool) $this->table[$i][$j] ? "x" : " "; | |
echo "\n"; | |
} | |
} | |
public function regenerate(){ | |
for($i = 1; $i <= $this->size; $i++) | |
for($j = 1; $j <= $this->size; $j++) | |
$this->table2[$i][$j] = $this->nextCellState($i, $j); | |
$this->table = $this->table2; | |
} | |
public function runSimulation($generations, $delay = 200){ | |
for($n = 1; $n <= $generations; $n++){ | |
$this->paintState(); | |
usleep($delay * 1e3); | |
$this->regenerate(); | |
} | |
} | |
public function coord($n){ | |
if($n == 0) return $this->size; | |
if($n == $this->size+1) return 1; | |
return $n; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment