Last active
November 18, 2019 17:57
-
-
Save malcolmsparks/eadcec1da2785c4f864b70c7fcf6693e to your computer and use it in GitHub Desktop.
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 juxt.play | |
(:require [clojure.set :as set]) | |
) | |
;;#{[0 1] [1 1]} | |
;;what about if we're at [1 1] | |
(defn neighbours [world cell] | |
(set/intersection | |
world | |
(set | |
(for [col-delta [-1 0 1] | |
row-delta [-1 0 1] | |
:when (not (and | |
(zero? col-delta) | |
(zero? row-delta)))] | |
[(+ col-delta (first cell)) (+ row-delta (second cell))] | |
)))) | |
(defn new-status-of-cell? [world cell] | |
(case | |
(count | |
(neighbours world cell)) | |
(0 1) :off | |
(2 3) :on | |
:off | |
)) | |
(defn step [world] | |
;; Visit every cell in our world across a 3x3 grid | |
(set | |
(for [col (range 10) | |
row (range 10) | |
:when (= :on (new-status-of-cell? world [col row]))] | |
[col row]))) | |
(defn render-smiley-row [row] | |
(apply str | |
(for [i (range 10)] | |
(if (contains? (set row) i) | |
#_"☺" "X" | |
"-") | |
))) | |
(defn render-world [world] | |
(do | |
(doseq [line | |
(map render-smiley-row | |
(let [groups (group-by second world)] | |
(for [row (range 10) | |
] | |
(map first (get groups row)) | |
)))] | |
(println line)) | |
(println))) | |
(defn game-of-life [world] | |
(doseq [world (iterate step world)] | |
(render-world world) | |
(Thread/sleep 1000) | |
) | |
) | |
(game-of-life #{[0 1] [1 1] [2 2] [1 2]}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment