Created
February 12, 2014 22:18
-
-
Save AndyStewart/8965703 to your computer and use it in GitHub Desktop.
Game Of Life Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns default.core-test | |
| (:use midje.sweet)) | |
| (defn initial-seed [width height] | |
| (let [board #{}] | |
| (into board (for [x (range width) | |
| y (range height)] | |
| {:x x :y y})))) | |
| (defn in-height-range[cell other-cell] | |
| (and | |
| (>= (:y other-cell) (- (:y cell) 1)) | |
| (<= (:y other-cell) (+ (:y cell) 1)))) | |
| (defn in-width-range[cell other-cell] | |
| (and | |
| (>= (:x other-cell) (- (:x cell) 1)) | |
| (<= (:x other-cell) (+ (:x cell) 1)))) | |
| (defn not-same-cell[cell other-cell] | |
| (or | |
| (not= (:x cell) (:x other-cell)) | |
| (not= (:y cell) (:y other-cell)))) | |
| (defn is-neighbour[cell other-cell] | |
| (and | |
| (in-height-range cell other-cell) | |
| (in-width-range cell other-cell) | |
| (not-same-cell cell other-cell))) | |
| (defn number-of-neighbours [live-cells cell] | |
| (count (filter (fn [inner-cell] (is-neighbour cell inner-cell)) live-cells))) | |
| (defn is-alive[live-cells] | |
| (fn [cell] | |
| (>= (number-of-neighbours live-cells cell) 2))) | |
| (defn evolve [live-cells] | |
| (filter (is-alive live-cells) live-cells)) | |
| (facts "About game of lifes initial board" | |
| (fact "Initial board should contain 1 live cell at the centre" | |
| (count (initial-seed 1 1)) => 1 | |
| (initial-seed 1 1) => (contains {:x 0 :y 0})) | |
| (fact "Initial board should contain all live cells" | |
| (count (initial-seed 2 2)) => 4 | |
| (initial-seed 2 2) => (contains {:x 0 :y 0}) | |
| (initial-seed 2 2) => (contains {:x 0 :y 0}))) | |
| (facts "About evolving game of lifes board" | |
| (fact "board with 1 cell should contain no cells" | |
| (count (evolve(initial-seed 1 1))) => 0) | |
| (fact "board with 2 cell should contain no cells" | |
| (count (evolve(initial-seed 1 2))) => 0) | |
| (fact "board with 3 horizontal cells should contain one cell in the centre" | |
| (count (evolve(initial-seed 3 1))) => 1 | |
| (evolve(initial-seed 3 1)) => (contains {:x 1 :y 0})) | |
| (fact "board with 3 vertical cells should contain one cell in the centre" | |
| (count (evolve(initial-seed 1 3))) => 1 | |
| (evolve(initial-seed 1 3)) => (contains {:x 0 :y 1})) | |
| (fact "board with 3 vertical cells should contain one cell in the centre" | |
| (count (evolve(initial-seed 1 3))) => 1 | |
| (evolve(initial-seed 1 3)) => (contains {:x 0 :y 1})) | |
| (fact "board with 4 cells in a square all should live" | |
| (count (evolve(initial-seed 2 2))) => 4 | |
| (evolve(initial-seed 2 2)) => (contains {:x 0 :y 0}) | |
| (evolve(initial-seed 2 2)) => (contains {:x 1 :y 1})) | |
| (fact "board with 4x3 cells should produce the beehive" | |
| (count (evolve(initial-seed 4 3))) => 12 | |
| (evolve(initial-seed 4 3)) => (contains {:x 1 :y 0}) | |
| (evolve(initial-seed 4 3)) => (contains {:x 2 :y 0}) | |
| (evolve(initial-seed 4 3)) => (contains {:x 0 :y 1}) | |
| (evolve(initial-seed 4 3)) => (contains {:x 3 :y 1}) | |
| (evolve(initial-seed 4 3)) => (contains {:x 1 :y 2}) | |
| (evolve(initial-seed 4 3)) => (contains {:x 2 :y 2})) | |
| ; (fact "board with 3x3 with horizontal line evolves to vertical line" | |
| ; (count (evolve [{:x 0 :y 0} {:x 1 :y 0} {:x 2 :y 0}])) => 3 | |
| ; (evolve [{:x 0 :y 0} {:x 1 :y 0} {:x 2 :y 0}]) => (contains {:x 1 :y 0} {:x 1 :y 1} {:x 1 :y 2})) | |
| ) | |
| (facts "Locating number-of-neighbours" | |
| (fact "should count vertical neighbours" | |
| (number-of-neighbours [{:x 1 :y 1} {:x 1 :y 2}] {:x 1 :y 2}) => 1 | |
| (number-of-neighbours [{:x 1 :y 2} {:x 1 :y 1}] {:x 1 :y 1}) => 1 | |
| (number-of-neighbours [{:x 1 :y 2}] {:x 1 :y 2}) => 0) | |
| (fact "should count horizontal neighbours" | |
| (number-of-neighbours [{:x 1 :y 1} {:x 2 :y 1}] {:x 1 :y 1}) => 1 | |
| (number-of-neighbours [{:x 1 :y 1} {:x 2 :y 1}] {:x 2 :y 1}) => 1 | |
| (number-of-neighbours [{:x 1 :y 2}] {:x 1 :y 2}) => 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment