The Game of Life is a   cellular automaton   devised by the British mathematician   John Horton Conway   in 1970.   It is the best-known example of a cellular automaton.

Conway's game of life is described   here:

A cell   C   is represented by a   1   when alive,   or   0   when dead,   in an   m-by-m   (or m×m)   square array of cells.

We calculate   N   - the sum of live cells in C's   eight-location neighbourhood,   then cell   C   is alive or dead in the next generation based on the following table:

   C   N                 new C
   1   0,1             ->  0  # Lonely
   1   4,5,6,7,8       ->  0  # Overcrowded
   1   2,3             ->  1  # Lives
   0   3               ->  1  # It takes three to give birth!
   0   0,1,2,4,5,6,7,8 ->  0  # Barren

Assume cells beyond the boundary are always dead.

The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players.   One interacts with the Game of Life by creating an initial configuration and observing how it evolves.



Task

Although you should test your implementation on more complex examples such as the   glider   in a larger universe,   show the action of the blinker   (three adjoining cells in a row all alive),   over three generations, in a 3 by 3 grid.



References

   Its creator John Conway, explains   the game of life.   Video from numberphile on youtube.
   John Conway   Inventing Game of Life   - Numberphile video.


Related task

   Langton's ant   - another well known cellular automaton.


/**
* States of a cell. A cell is either [ALIVE] or [DEAD].
* The state contains its [symbol] for printing.
*/
class State {
  const State(this.symbol);
 
  static final ALIVE = const State('#');
  static final DEAD = const State(' ');
 
  final String symbol;
}
 
/**
* The "business rule" of the game. Depending on the count of neighbours,
* the [cellState] changes.
*/
class Rule {
  Rule(this.cellState);
 
  reactToNeighbours(int neighbours) {
    if (neighbours == 3) {
      cellState = State.ALIVE;
    } else if (neighbours != 2) {
      cellState = State.DEAD;
    }
  }
 
  var cellState;
}
 
/**
* A coordinate on the [Grid].
*/
class Point {
  const Point(this.x, this.y);
 
  operator +(other) => new Point(x + other.x, y + other.y);
 
  final int x;
  final int y;
}
 
/**
* List of the relative indices of the 8 cells around a cell.
*/
class Neighbourhood {
  List<Point> points() {
    return [
      new Point(LEFT, UP), new Point(MIDDLE, UP), new Point(RIGHT, UP),
      new Point(LEFT, SAME), new Point(RIGHT, SAME),
      new Point(LEFT, DOWN), new Point(MIDDLE, DOWN), new Point(RIGHT, DOWN)
    ];
  }
 
  static final LEFT = -1;
  static final MIDDLE = 0;
  static final RIGHT = 1;
  static final UP = -1;
  static final SAME = 0;
  static final DOWN = 1;
}
 
/**
* The grid is an endless, two-dimensional [field] of cell [State]s.
*/
class Grid {
  Grid(this.xCount, this.yCount) {
    _field = new Map();
    _neighbours = new Neighbourhood().points();
  }
 
  set(point, state) {
    _field[_pos(point)] = state;
  }
 
  State get(point) {
    var state = _field[_pos(point)];
    return state != null ? state : State.DEAD;
  }
 
  int countLiveNeighbours(point) =>
    _neighbours.filter((offset) => get(point + offset) == State.ALIVE).length;
 
  _pos(point) => '${(point.x + xCount) % xCount}:${(point.y + yCount) % yCount}';
 
  print() {
    var sb = new StringBuffer();
    iterate((point) { sb.add(get(point).symbol); }, (x) { sb.add("\n"); });
    return sb.toString();
  }
 
  iterate(eachCell, [finishedRow]) {
    for (var x = 0; x < xCount; x++) {
      for (var y = 0; y < yCount; y++) {
         eachCell(new Point(x, y));
      }
      if(finishedRow != null) {
        finishedRow(x);
      }
    }
  }
 
  final xCount, yCount;
  List<Point> _neighbours;
  Map<String, State> _field;
}
 
/**
* The game updates the [grid] in each step using the [Rule].
*/
class Game {
  Game(this.grid);
 
  tick() {
    var newGrid = createNewGrid();
 
    grid.iterate((point) {
      var rule = new Rule(grid.get(point));
      rule.reactToNeighbours(grid.countLiveNeighbours(point));
      newGrid.set(point, rule.cellState);
    });
 
    grid = newGrid;
  }
 
  createNewGrid() => new Grid(grid.xCount, grid.yCount);
 
  printGrid() => print(grid.print());
 
  Grid grid;
}
 
main() {
  // Run the GoL with a blinker.
  runBlinker();
}
 
runBlinker() {
  var game = new Game(createBlinkerGrid());
 
  for(int i = 0; i < 3; i++) {
    game.printGrid();
    game.tick();
  }
  game.printGrid();
}
 
createBlinkerGrid() {
  var grid = new Grid(4, 4);
  loadBlinker(grid);
  return grid;
}
 
loadBlinker(grid) => blinkerPoints().forEach((point) => grid.set(point, State.ALIVE));
 
blinkerPoints() => [new Point(0, 1), new Point(1, 1), new Point(2, 1)];