Created
September 17, 2011 14:34
-
-
Save smat/1223988 to your computer and use it in GitHub Desktop.
Conway's Game of Life
This file contains 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 test.life | |
(:use [clojure.test]) | |
(:use [midje.sweet]) | |
) | |
(unfinished ) | |
(defn neighbours [cell] | |
(map (fn [offset] (map + cell offset)) [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1] ])) | |
(defn is-dying? [cell generation] | |
(let [living-neighbours (count (filter (set generation) (neighbours cell)))] | |
(not (or (= 2 living-neighbours) (= 3 living-neighbours))))) | |
(defn is-newborn? [cell generation] | |
(= 3 (count (filter (set generation) (neighbours cell))))) | |
(defn bordering [generation] | |
(remove (set generation) (set (mapcat neighbours generation)))) | |
(defn newborns [generation] | |
(filter (fn [cell] (is-newborn? cell generation)) (bordering generation))) | |
(defn survivors [generation] | |
(remove (fn [cell] (is-dying? cell generation)) generation)) | |
(defn tick [generation] | |
(concat (survivors generation) (newborns generation))) | |
(defn generations [generation] | |
(rest (reductions (fn [x y] (tick x)) generation (repeat 1)))) | |
(deftest wrapper | |
(fact "integration test" | |
(tick [[2 1] [1 1] [0 1]]) => (just [[1 2] [1 1] [1 0]] :in-any-order)) | |
(fact "integration test" | |
(tick (tick [[2 1] [1 1] [0 1]])) => (just [[2 1] [1 1] [0 1]] :in-any-order)) | |
(fact "A cell should have 8 neighbours" | |
(neighbours [1 1]) => (just [[2 2] [1 2] [0 2] | |
[2 1] [0 1] | |
[2 0] [1 0] [0 0]] :in-any-order)) | |
(fact "A cell lives if it has three neighbours" | |
(is-dying? ...living-cell... [...living-cell... ...first-living-neighbour... ...second-living-neighbour... ...third...]) => false | |
(provided | |
(neighbours ...living-cell...) => [...first-living-neighbour... ...second-living-neighbour... ...third...])) | |
(fact "A cell lives if it has two neighbours" | |
(is-dying? ...living-cell... [...living-cell... ...first-living-neighbour... ...second-living-neighbour...]) => false | |
(provided | |
(neighbours ...living-cell...) => [...first-living-neighbour... ...second-living-neighbour...])) | |
(fact "A cell is not born if it does not 3 living neighbours" | |
(is-newborn? ...dead-cell... [...living-cell...]) => false | |
(provided | |
(neighbours ...dead-cell...) => [...living-cell... ...another-dead-cell... ...third-dead-cell...])) | |
(fact "A cell will be born if it has 3 living neighbours" | |
(is-newborn? ...dead-cell... [...living-cell... ...2-living-cell... ...3-living-cell...]) => true | |
(provided | |
(neighbours ...dead-cell...) => [...living-cell... ...2-living-cell... ...3-living-cell...])) | |
(fact "Bordering cells are neighbourcells that are dead" | |
(bordering [...alive-cell... ...another-alive-cell...]) => [...dead-cell...] | |
(provided | |
(neighbours ...alive-cell...) => [...another-alive-cell... ...dead-cell...] | |
(neighbours ...another-alive-cell...) => [...alive-cell... ...dead-cell...])) | |
(fact "Newborn cells can only be bordering cells" | |
(newborns ...generation...) => [...newborn-cell...] | |
(provided | |
(bordering ...generation...) => [...border-cell... ...newborn-cell...] | |
(is-newborn? ...newborn-cell... ...generation...) => true | |
(is-newborn? ...border-cell... ...generation...) => false)) | |
(fact "Survivors are the cells that are not dying" | |
(survivors [...dying-cell... ...normal-cell...]) => [...normal-cell...] | |
(provided | |
(is-dying? ...dying-cell... [...dying-cell... ...normal-cell...]) => true | |
(is-dying? ...normal-cell... [...dying-cell... ...normal-cell...]) => false)) | |
(fact "Each tick should remove dying cells and add newborn cells" | |
(tick [...dying-cell... ...normal-cell...]) => [...normal-cell... ...newborn-cell...] | |
(provided | |
(survivors [...dying-cell... ...normal-cell...]) => [...normal-cell...] | |
(newborns [...dying-cell... ...normal-cell...]) => [...newborn-cell...])) | |
(fact "Should create new generation for each tick" | |
(second (generations ...initial-generation...)) => ...second-generation... | |
(provided | |
(tick ...initial-generation...) => ...first-generation... | |
(tick ...first-generation...) => ...second-generation...)) | |
) (run-tests) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment