/game-of-life

Implementation of the Conway's game of life in Clojure

Primary LanguageClojure

game-of-life

this project

A Clojure implementation of the famous Conway's game of life.

It's implemented in 60 x 60 matrix and displays in multicolor using Quil.

How to run it locally

lein run

How to require the cellular automaton framework

(require '[zone.lambda.game.board :as b :refer [DEAD ALIVE]])

Result

game of life animated image

rules of the game

(defn neighbour-cells [block]
  (concat (subvec (vec block) 0 4) (subvec (vec  block) 5 9)))
(defn neighbour-count [block]
  (count (filter #(= % ALIVE) (neighbour-cells block))))
(defn is-alive? [board i]
  (= (get board i) ALIVE))
(defn coords2state [board coords]
  (map #(get board %) coords))

(defn parse-block [board block-coords]
  (let [i (get block-coords 4)
        ncount (neighbour-count (coords2state board block-coords))]
    (if (is-alive? board i)
      (cond
        (< ncount 2) DEAD
        (or (= ncount 2) (= ncount 3)) ALIVE
        (> ncount 3) DEAD)
      (if (= ncount 3)
        ALIVE
        DEAD))))

(defn apply-rules [board]
  (mapv (fn [i]
          (let [mov (partial b/move i)] (parse-block board [(mov -1 -1) (mov 0 -1) (mov +1 -1)
                                                            (mov -1 0) i           (mov +1 0)
                                                            (mov -1 +1) (mov 0 +1) (mov +1 +1)])))
        (range (* b/column-nb b/raw-nb))))

(defn game-of-life-step [{:keys [board] :as state}]
  (reset! b/arena board)
  (let [new-board (apply-rules board)]
    {:board new-board}))

if you want to modify the colour

modify the code and re-run lein run

if you want to modify the initial state

modify the code and re-run lein run

if you want to modify the rules

modify the code and re-run lein run

or use Klipse for an interactive session :)

Reference

Klipse library

License

Copyright © 2016 Mathieu Gauthron

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.