Last active
August 1, 2017 08:07
-
-
Save pelensky/364be4188c9aaf8397c60ef0964af833 to your computer and use it in GitHub Desktop.
Game Of Life Logic for an Infinite World in Clojure
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 game-of-life.life) | |
(def fewest-neighbours-to-stay-living 2) | |
(def most-neighbours-to-stay-living 3) | |
(def exact-neighbours-to-regenerate 3) | |
(def neighbours | |
[ [-1 1] [0 1] [1 1] | |
[-1 0] [1 0] | |
[-1 -1] [0 -1] [1 -1]]) | |
(defn- update-coordinate [cell neighbour] | |
[(+ (get cell 0) (get neighbour 0)) (+ (get cell 1) (get neighbour 1))]) | |
(defn find-all-neighbours [cell] | |
(map #(update-coordinate cell %) neighbours)) | |
(defn find-non-living-neighbours [cell grid] | |
(clojure.set/difference (set (find-all-neighbours cell)) (set grid))) | |
(defn find-all-non-living-neighbours [grid] | |
(reduce into #{} | |
(map #(find-non-living-neighbours % grid) grid))) | |
(defn count-live-neighbours [cell grid] | |
(count (clojure.set/intersection (set grid) (set (find-all-neighbours cell))))) | |
(defn live-on? [cell grid] | |
(let [live-neighbours (count-live-neighbours cell grid)] | |
(if (and (>= live-neighbours fewest-neighbours-to-stay-living) (<= live-neighbours most-neighbours-to-stay-living)) | |
true))) | |
(defn regenerate? [cell grid] | |
(if (= exact-neighbours-to-regenerate (count-live-neighbours cell grid)) | |
true)) | |
(defn evolve [grid] | |
(let [regenerated (clojure.set/select #(regenerate? % grid) (find-all-non-living-neighbours grid)) | |
living (clojure.set/select #(live-on? % grid) (set grid))] | |
(clojure.set/union living regenerated))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment