Skip to content

Instantly share code, notes, and snippets.

@lenstr
Created November 12, 2012 03:50
Show Gist options
  • Save lenstr/4057420 to your computer and use it in GitHub Desktop.
Save lenstr/4057420 to your computer and use it in GitHub Desktop.
game-of-life
(ns game-of-life.core
(:require [clojure.string :as str]
[clojure.core.reducers :as r]))
(comment
" Rules
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:
1. Any live cell with fewer than two live neighbours dies,
as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell,
as if by reproduction.
The initial pattern constitutes the seed of the system.
The first generation is created by applying the above rules simultaneously
to every cell in the seed—births and deaths occur simultaneously,
and the discrete moment at which this happens is sometimes called a tick
(in other words, each generation is a pure function of the preceding one).
The rules continue to be applied repeatedly to create further generations.
http://en.wikipedia.org/wiki/Conway's_Game_of_Life
")
(def glider-gun "
.
.... .
. .... .
. . . . ..
.. . .. .... ..
.. . .. ....
. .. .
. .
.
")
(def live-cell \x)
(def empty-cell \ )
;; ---------HELPERS------------------------------------------------
(defn ->xy [i [w h]]
[(mod i w) (int (/ i w))])
(defn xy-> [[x y] [w h]]
(+ (* y w) x))
(defn make-vector-2d
([size] (make-vector-2d size false))
([[w h] value] (vec (repeat (* w h) value))))
(defn string->vector-2d [string [w h :as size]]
(let [lines (str/split string #"\n")]
(->> (for [line lines]
(-> (map #(= % \.) line)
(concat (repeat (- w (count line)) false))))
(apply concat)
(#(concat % (repeat (* w (- h (count lines))) false)))
(vec))))
(defn vector-2d->string [vector-2d [w h :as size]]
(->> (->> (partition w (map #(if % live-cell empty-cell) vector-2d))
(map str/join))
(str/join "\n")))
(defn string->board [string size]
[(string->vector-2d string size) size])
(defn board->string [[data size]]
(vector-2d->string data size))
(defn print-board [board]
(println (board->string board)))
;; ----------------------------------------------------------------
(defn guard-bounds [[w h] [x y]]
(letfn [(guard [c lim]
(cond (neg? c) (+ lim c)
(>= c lim) (- c lim)
:else c))]
[(guard x w) (guard y h)]))
(defn count-neighbours
([board pos] (count-neighbours [[-1 -1] [0 -1] [1 -1]
[-1 0] [1 0]
[-1 1] [0 1] [1 1]] board pos))
([deltas [data size] pos]
(reduce + (r/map (fn [new-pos]
(if (data (xy-> (guard-bounds size new-pos) size)) 1 0))
(r/map #(map + pos %) deltas)))))
(defn is-alive [[data size :as board] pos]
(case (count-neighbours board pos)
3 true
2 (data (xy-> pos size))
false))
(defn next-generation [[data size :as board]]
[(vec (map-indexed (fn [i v] (is-alive board (->xy i size))) data))
size])
(defn run-game []
(loop [generation (string->board glider-gun [70 30])]
(Thread/sleep 20)
(print-board generation)
(recur (next-generation generation))))
(defn -main []
(run-game))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment