Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Created August 27, 2013 08:22
Show Gist options
  • Save ikitommi/6351028 to your computer and use it in GitHub Desktop.
Save ikitommi/6351028 to your computer and use it in GitHub Desktop.
(ns tron.kekkonen)
(defn addv [a b] (mapv + a b))
(defn turn-left [[dx dy]] [dy (- dx)])
(defn turn-right [[dx dy]] [(- dy) dx])
(defn left? [old-dir new-dir] (= (turn-right new-dir) old-dir))
(defn president
[turn & [primary-goal :as original-goals]]
(fn [look {:keys [pos dir goals] :or {dir (or primary-goal [0 1])
goals original-goals}}]
(letfn
[(ok? [new-dir & [dont-turn]]
(let [new-pos (addv pos new-dir)]
(when-not (look new-pos)
{:pos new-pos :dir new-dir :goals (map (if dont-turn identity turn) goals)})))]
(or
(some ok? goals)
(ok? dir true)
(ok? (turn-left dir))
(ok? (turn-right dir))))))
(def mannerheim (president identity [0 -1]))
(def kekkonen (president turn-left [0 -1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment