Skip to content

Instantly share code, notes, and snippets.

@pelensky
Created August 1, 2017 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelensky/7bc9c6795b7784cf19de999da98429a7 to your computer and use it in GitHub Desktop.
Save pelensky/7bc9c6795b7784cf19de999da98429a7 to your computer and use it in GitHub Desktop.
Game Of Life Display for an Infinite World in Clojure
(ns game-of-life.display)
(defn- current-x [coordinates]
(get (first coordinates) 0))
(defn- current-y [coordinates]
(get (first coordinates) 1))
(defn- remaining [coordinates]
into [] (rest coordinates))
(defn- updated [grid coordinates]
(assoc-in grid [(current-y coordinates) (current-x coordinates)] "*"))
(defn- highest [letter position coordinates]
(inc (max letter (apply max (map #(get % position) coordinates)))))
(defn- empty-grid-correct-size [coordinates grid x y]
(into grid (conj (repeat (highest y 1 coordinates) (into grid (repeat (highest x 0 coordinates) " "))))))
(defn- format-rows [grid]
(for [row grid]
(clojure.string/join "" row)))
(defn- format-grid [grid]
(clojure.string/join "\n" grid))
(defn create-grid [coordinates grid x y]
(if (empty? coordinates)
(format-grid (format-rows grid))
(if (empty? grid)
(recur coordinates (empty-grid-correct-size coordinates grid x y) (highest x 0 coordinates) (highest y 1 coordinates) )
(recur (remaining coordinates) (updated grid coordinates) x y) )))
(defn display [coordinates]
(println (create-grid coordinates [] 0 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment