Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageekguy/f6e78ae0c6c8bbd0b92b to your computer and use it in GitHub Desktop.
Save mageekguy/f6e78ae0c6c8bbd0b92b to your computer and use it in GitHub Desktop.
east oriented game of life
<?php
namespace gol;
interface cell
{
function isAtAbscissaAndOrdinateInView($x, $y, view $view);
function isAtAbscissaAndOrdinateInGrid($x, $y, grid $grid);
function cellNeighborIs(cell $cell);
function cellNeighborIsDead();
function cellNeighborIsAlive();
}
interface grid
{
function cellIs(cell $cell);
function cellAtAbscissaAndOrdinateIs($x, $y, cell $cell);
function cellNeedCellNeighborAtAbscissaAndOrdinate(cell $cell, $x, $y);
function viewIs(view $view);
}
interface view
{
function gridIs(grid $grid);
function cellIsAliveAtAbscissaAndOrdinate($x, $y);
function cellIsDeadAtAbscissaAndOrdinate($x, $y);
}
interface controller
{
function nextGridIs(grid $grid);
}
interface dictionary
{
function controllerNeedNextGridOfGrid(controller $controller, grid $grid);
function nextGridOfGridIs(grid $grid, grid $nextGrid);
}
namespace gol\cell;
use
gol
;
class classic implements gol\cell
{
private
$e,
$s
;
function __construct($e)
{
$this->e = $e == true;
}
function isAtAbscissaAndOrdinateInView($x, $y, gol\view $view)
{
! $this->e
?
$view->cellIsDeadAtAbscissaAndOrdinate($x, $y)
:
$view->cellIsAliveAtAbscissaAndOrdinate($x, $y)
;
return $this;
}
function isAtAbscissaAndOrdinateInGrid($x, $y, gol\grid $grid)
{
$cell = new self($this->e);
$grid
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x - 1, $y - 1)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x, $y - 1)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x + 1, $y - 1)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x - 1, $y)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x + 1, $y)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x - 1, $y + 1)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x, $y + 1)
->cellNeedCellNeighborAtAbscissaAndOrdinate($cell, $x + 1, $y + 1)
;
$cell->e = $cell->s == 3 || ($cell->e && $cell->s == 2);
$cell->s = null;
return $cell;
}
function cellNeighborIs(gol\cell $cell)
{
! $this->e
?
$cell->cellNeighborIsDead()
:
$cell->cellNeighborIsAlive()
;
return $this;
}
function cellNeighborIsDead()
{
return $this;
}
function cellNeighborIsAlive()
{
$this->s++;
return $this;
}
}
namespace gol\dictionary;
use
gol
;
class phpArray implements gol\dictionary
{
private
$grids,
$nextGrids
;
function __construct()
{
$this->grids = [];
$this->nextGrids = [];
}
function controllerNeedNextGridOfGrid(gol\controller $controller, gol\grid $grid)
{
$key = array_search($grid, $this->grids);
if ($key === false)
{
$grid->noNextGridInDictionary($this);
$key = array_search($grid, $this->grids);
}
$controller->nextGridIs($this->nextGrids[$key]);
return $this;
}
function nextGridOfGridIs(gol\grid $grid, gol\grid $nextGrid)
{
$this->grids[] = $grid;
$this->nextGrids[] = $nextGrid;
return $this;
}
}
namespace gol\grid;
use
gol
;
class custom implements gol\grid
{
private
$size,
$cells
;
function __construct($size, gol\cell $cell)
{
$this->size = $size;
$this->cells = [];
$this->cellIs($cell);
}
function cellIs(gol\cell $cell)
{
return $this->callableIs(function($x, $y) use ($cell) {
$this->cellAtAbscissaAndOrdinateIs($x, $y, $cell);
}
);
}
function cellAtAbscissaAndOrdinateIs($x, $y, gol\cell $cell)
{
if ($x >= 0 && $y >= 0 && $x < $this->size && $y < $this->size)
{
$this->cells[$x][$y] = $cell;
}
return $this;
}
function cellNeedCellNeighborAtAbscissaAndOrdinate(gol\cell $cell, $x, $y)
{
! isset($this->cells[$x][$y])
?
$cell->cellNeighborIsDead()
:
$this->cells[$x][$y]->cellNeighborIs($cell)
;
return $this;
}
function viewIs(gol\view $view)
{
$this->callableIs(function($x, $y) use ($view) {
$this->cells[$x][$y]->isAtAbscissaAndOrdinateInView($x, $y, $view);
}
);
return $this;
}
function noNextGridInDictionary(gol\dictionary $dictionary)
{
$grid = clone $this;
$this->callableIs(function($x, $y) use ($grid) {
$grid->cellAtAbscissaAndOrdinateIs($x, $y, $this->cells[$x][$y]->isAtAbscissaAndOrdinateInGrid($x, $y, $this));
}
);
$dictionary->nextGridOfGridIs($this, $grid);
return $this;
}
private function callableIs(callable $callable)
{
for ($x = 0; $x < $this->size; $x++)
{
for ($y = 0; $y < $this->size; $y++)
{
$callable($x, $y);
}
}
return $this;
}
}
namespace gol\view;
use
gol
;
class cli implements gol\view
{
function gridIs(gol\grid $grid)
{
$grid->viewIs($this);
return $this;
}
function cellIsAliveAtAbscissaAndOrdinate($x, $y)
{
echo 'Cell is alive at ' . $x . ',' . $y . PHP_EOL;
return $this;
}
function cellIsDeadAtAbscissaAndOrdinate($x, $y)
{
echo 'Cell is dead at ' . $x . ',' . $y . PHP_EOL;
return $this;
}
}
namespace gol\controller;
use
gol
;
class dictionary implements gol\controller
{
private
$view,
$dictionary
;
function __construct(gol\view $view, gol\dictionary $dictionary)
{
$this->view = $view;
$this->dictionary = $dictionary;
}
function gridHasLifetime(gol\grid $grid, $lifetime)
{
$this->grid = $grid;
while ($lifetime--)
{
$this->dictionary = $this->dictionary->controllerNeedNextGridOfGrid($this, $this->grid);
}
return $this;
}
function nextGridIs(gol\grid $grid)
{
$this->view->gridIs($grid);
$this->grid = $grid;
return $this;
}
}
namespace gol;
(new controller\dictionary(new view\cli, new dictionary\phpArray))
->gridHasLifetime(
(new grid\custom(9, new cell\classic(false)))
->cellAtAbscissaAndOrdinateIs(1, 2, new cell\classic(true))
->cellAtAbscissaAndOrdinateIs(2, 2, new cell\classic(true))
->cellAtAbscissaAndOrdinateIs(3, 2, new cell\classic(true))
,
6
)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment