Created
January 31, 2012 21:10
-
-
Save anonymous/1712937 to your computer and use it in GitHub Desktop.
London Clojure User Group - Jan 2012 - Dreadnought
This file contains 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
(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