gameoflife

This module provides functions for Game of Life.

Basic usage

Blinker example is here.

import gameoflife
from os import sleep

var board: Board = @[
  @[dead, dead, dead, dead, dead],
  @[dead, dead, dead, dead, dead],
  @[dead, live, live, live, dead],
  @[dead, dead, dead, dead, dead],
  @[dead, dead, dead, dead, dead],
]

while true:
  board.nextStep()
  board.print
  echo "-----------------------------------"
  sleep(100)

See also:

Types

Board = seq[seq[uint8]]

Consts

dead = 0'u8
live = 1'u8

Procs

proc getNeighbourCells(board: Board; x, y: int): seq[uint8] {...}{.raises: [], tags: [].}
Returns neighbour cells. Not included x, y cell.

Examples:

let board = @[@[live, dead, live, live], @[dead, live, live, live],
           @[live, live, live, dead], @[live, live, live, dead]]
doAssert board.getNeighbourCells(x = 0, y = 0) == @[dead, dead, live]
proc isReproduction(self: uint8; livingCellCount: int): bool {...}{.raises: [], tags: [].}
Returns cell is reproduction. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Examples:

doAssert dead.isReproduction(2) == false
doAssert dead.isReproduction(3)
proc isGeneration(self: uint8; livingCellCount: int): bool {...}{.raises: [], tags: [].}
Returns cell is generation. Any live cell with two or three live neighbours lives on to the next generation.

Examples:

doAssert live.isGeneration(1) == false
doAssert live.isGeneration(2)
doAssert live.isGeneration(3)
proc isUnderpopulation(self: uint8; livingCellCount: int): bool {...}{.raises: [], tags: [].}
Returns cell is underpopulation. Any live cell with fewer than two live neighbours dies, as if by underpopulation.

Examples:

doAssert live.isUnderpopulation(0)
doAssert live.isUnderpopulation(1)
doAssert live.isUnderpopulation(2) == false
proc isOverpopulation(self: uint8; livingCellCount: int): bool {...}{.raises: [], tags: [].}
Returns cell is overpopulation. Any live cell with more than three live neighbours dies, as if by overpopulation.

Examples:

doAssert live.isOverpopulation(3) == false
doAssert live.isOverpopulation(4)
doAssert live.isOverpopulation(5)
proc nextStep(board: var Board) {...}{.raises: [Exception], tags: [].}
Check all cells and update board.

Examples:

var board: Board = @[@[dead, dead, dead, dead, dead], @[dead, dead, dead, dead, dead],
                 @[dead, live, live, live, dead], @[dead, dead, dead, dead, dead],
                 @[dead, dead, dead, dead, dead]]
board.nextStep
doAssert board ==
    @[@[dead, dead, dead, dead, dead], @[dead, dead, live, dead, dead],
      @[dead, dead, live, dead, dead], @[dead, dead, live, dead, dead],
      @[dead, dead, dead, dead, dead]]
proc print(board: Board) {...}{.raises: [], tags: [].}
Print board to stdout.

Examples:

var board: Board = @[@[dead, dead, dead, dead, dead], @[dead, dead, dead, dead, dead],
                 @[dead, live, live, live, dead], @[dead, dead, dead, dead, dead],
                 @[dead, dead, dead, dead, dead]]
board.print
## Output:
## |0|0|0|0|0|
## |0|0|0|0|0|
## |0|1|1|1|0|
## |0|0|0|0|0|
## |0|0|0|0|0|