friedbrice/elm-teaching-tools/1.0.0

NOTE: the latest version is 1.0.1

ElmTeachingTools.Labs.Life

Conway's Game of Life.

For details, see the Wikipedia article, https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life.

Basics

type alias Cell =
( Int, Int )

A cell is a position in an infinite grid.

= Dead
| Alive

A cell is either Alive or Dead.

type alias Board =
Cell -> CellStatus

Conceptually, a board is an infinite grid of cells, each of which is either alive or dead.

We model this concept in our program using a function. Specifically, a function that outputs a CellStatus when you input a Cell.

If you have a Board and Cell, you can find the status of that Cell by plugging it into the Board.

If you need to create a board, you do so by writing a function whose variable is a Cell and that returns a CellStatus.

Rules

type alias LifeRules =
{ nextStatus : Int -> CellStatus -> CellStatus
, livingNeighbors : Board -> Cell -> Int
, nextBoard : Board -> Board
}

The rules we need to describe in order to make a working Life game can be modeled by these three functions:

  • nextStatus

Given the number of living neighbors and the current cell status, what should the cell's next status be? Use the rules listed in the Wikipedia article to write this function.

  • livingNeighbors

Given the current board and a cell, how many living neighbors does that cell have? You'll need a tiny bit of geometry to write this function.

  • nextBoard

Given the current board, what should the next board be? You should use your livingNeighbors and nextStatus functions to write this function.

Running

runLife : LifeRules -> Game State Event

To complete this lab, import this library into your own Elm file, create an appropriate LifeRules record, and use it in runLife.

import ElmTeachingTools.Labs.Life exposing (..)


myLifeRules = { ... }


main = runLife myLifeRules