Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2012 21:10
Show Gist options
  • Save anonymous/1712937 to your computer and use it in GitHub Desktop.
Save anonymous/1712937 to your computer and use it in GitHub Desktop.
London Clojure User Group - Jan 2012 - Dreadnought
(defn adjacent? [[x1 y1] [x2 y2]]
(or
(and (= x1 x2) (= (Math/abs (- (int y1) (int y2))) 1))
(and (= y1 y2) (= (Math/abs (- (int x1) (int x2))) 1))
))
(defn is-good-move? [hits move]
(not (empty? (filter (fn [[candidate hit]] (adjacent? candidate hit)) (map vector (repeat move) hits)))))
(defn choices [shots]
(clojure.set/difference
(set (for [row ["A" "B" "C" "D" "E" "F" "G" "H" "I" "J"]
column (vec (range 1 11))]
(reduce str [row column])))
(set shots)))
(defn next-shot
"Where do you want to attack next?"
[{:keys [shots hits]} opponent-context]
(let [moves (choices shots)]
(or
(first (filter (partial is-good-move? hits) moves))
(first moves))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment