//
you're reading...
Uncategorized

Conway’s Game of Life in Clojure

Conway’s Laws for the Game of Life

  • The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by overcrowding.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Determine a Representation

Use a representation that facilitates the definition of step function that maps one configuration state to next according to Conway’s rules. We will make use of frequencies from clojure.core, and apply frequencies to the collection of neighbors of each cell in the configuration.


(frequencies '([0 0] [0 1] [0 0] [0 0])) ;=> {[0 0] 3, [0 1] 1}

(defn nhbrs [[x y]]
  (for [dx [-1 0 1] 
        dy (if (zero? dx) [-1 1] [-1 0 1])]
        [(+ dx x) (+ dy y)]))

(nhbrs [1 1]) ;=>  ([0 0] [0 1] [0 2] [1 0] [1 2] [2 0] [2 1] [2 2])
(nhbrs  [0 0]);=> ([-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1])

(frequencies (mapcat nhbrs (nhbrs  [1 1])))
{[2 2] 2, [0 0] 2, [2 -1] 2, [1 0] 4, [2 3] 2, [3 3] 1, [-1 0] 2, [1 1] 8, [-1 3] 1, [3 0] 2, [-1 2] 2, [3 -1] 1, [-1 -1] 1, [1 3] 3, [0 3] 2, [1 -1] 3, [0 2] 2, [-1 1] 3, [2 0] 2, [3 1] 3, [2 1] 4, [0 -1] 2, [1 2] 4, [3 2] 2, [0 1] 4}

; those cells in the universe that have exactly 3 nhbrs in the config box around [0 0]
(let [config (nhbrs [0 0])] 
  (for  [[loc num-nbr]     
         (frequencies (mapcat nhbrs config)) 
             :when (= num-nbr 3)  ] 
                 loc))
; => ([0 -2] [0 2] [2 0] [-2 0])

; those cells in config-box that have exactly 2 nhbrs in config box around [0 0]
(let [config (nhbrs [0 0])] 
    (for [[loc num-nbr] 
        (frequencies (mapcat nhbrs config )) 
        :when (and  (= num-nbr 2) (contains? (set  config) loc))]  loc)) 
; => ([1 1] [-1 -1] [1 -1] [-1 1])

(defn step [cells]
  (set (for [[loc n] 
       (frequencies (mapcat nhbrs cells))
             :when (or (= n 3) (and (= n 2) (contains? cells loc)))]
                loc)))

(step #{[1 0] [1 1] [1 2]}) ;=>  #{[1 1] [2 1] [0 1]}

;; Let’s see how it behaves with the “blinker” configuration:

(def board #{[1 0] [1 1] [1 2]})

(take 5 (iterate step board))
;=>  (#{[1 0] [1 1] [1 2]} #{[2 1] [1 1] [0 1]} #{[1 0] [1 1] [1 2]} #{[2 1] [1 1] [0 1]} #{[1 0] [1 1] [1 2]})

;;And....It oscillates as expected!
;; Let’s see how it behaves with the “glider” configuration:

(def glider-bd #{[2 1] [2 2] [0 1] [1 2] [2 0]})
(last (take 5 (iterate step glider-bd)))

;=> #{[3 2] [3 3] [2 3] [1 2] [3 1]}
; so after 5 steps the glider has shifted up 1 in both x and y

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: