Skip to content

Instantly share code, notes, and snippets.

@ckirkendall
Created August 15, 2012 17:52
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 ckirkendall/3361937 to your computer and use it in GitHub Desktop.
Save ckirkendall/3361937 to your computer and use it in GitHub Desktop.
optimization of create-rep
(defn move [direction {:keys x y :as piece}]
(case direction
:down (assoc piece :y (inc y))
:right (assoc piece :x (inc x))
:left (assoc piece :x (dec x))
:up (assoc piece :y (dec y))
piece))
(defn handle-input [k game-state]
(let [filt-func #(= "player" (:entity-name %))
[start [player] end] (partition-by filt-func game-state)]
(if player
(concat start end [(move k player)])
game-state))
(defn create-rep [game-state]
(let [xy-map (reduce #(assoc %1 [(:x %2) (:y %2)] (:display %2)) {} game-state)
board (for [y (range 0 24)
x (range 0 80)]
(let [sqr (get [x y] xy-map)]
(if sqr sqr " ")))]
(doseq [line (partition 80 board)]
(apply str line))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment