Skip to content

Instantly share code, notes, and snippets.

@laurentpetit
Created September 7, 2011 11:38
Show Gist options
  • Save laurentpetit/1200343 to your computer and use it in GitHub Desktop.
Save laurentpetit/1200343 to your computer and use it in GitHub Desktop.
(ns ^{:doc "Conway's Game of Life."}
game-of-life)
;; Core game of life's algorithm functions
(defn neighbours
"Given a cell natural identifier, returns the natural identifiers
of the neighbours of the cell.
In this implementation, the natural identifier of the cell is its [x, y]
coordinates."
[[x y]]
(for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])]
[(+ dx x) (+ dy y)]))
(defn step
"Given a set of living cells (representing the state of the game at step N-1),
computes the new set of living cells (representing the state of the game at step N)
by applying the usual rules.
Implementation detail:
- (frequencies ...) returns a map of entries [cell n] where cell
is a cell natural identifier, n the number of living cells (at step N-1) around it.
- (for ...) is a list comprehension which, for each cell surrounded by n living cells,A
computes whether the cell survives (let cell be part of the list) or dies."
[cells]
(set (for [[cell n] (frequencies (mapcat neighbours cells))
:when (or (= n 3) (and (= n 2) (cells cell)))]
cell)))
;; Utility methods for displaying game on a text terminal
(defn print-board
"Prints a board (up to w cells as width, h cells as height) on *out*"
[board w h]
(doseq [x (range (inc w)) y (range (inc h))]
(if (= y 0) (print "\n"))
(print (if (board [x y]) "[X]" " . "))))
(defn display-grids
"Prints on *out* a sequence of boards representing the states of the game at each step"
[grids w h]
(doseq [board grids]
(print-board board w h)
(print "\n")))
;; Launches an example board
(def
^{:doc "board represents the initial state of the game, e.g. a set of living cells"}
board #{[2 1] [2 2] [2 3]})
(display-grids (take 3 (iterate step board)) 5 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment