Browse files

It works! It plays! It wins!

  • Loading branch information...
1 parent 0059c57 commit 174a49bd4a87d0e2b6fb3ddac38b5eb416b27bfc @dvrensk dvrensk committed Jan 15, 2013
Showing with 52 additions and 0 deletions.
  1. +52 −0 src/gomoku/team_david.clj
View
52 src/gomoku/team_david.clj
@@ -0,0 +1,52 @@
+(ns gomoku.team-david)
+
+(defn get-all-positions [w h]
+ (for [x (range w) y (range h)]
+ [x y]))
+
+(defn is-cell-occupied? [state pos]
+ (contains? (set (map :pos state)) pos))
+
+(def dirs
+ (for [x (range -1 2)
+ y (range -1 2)
+ :when (not (and (= 0 x) (= 0 y)))]
+ [x y]))
+
+(defn add-pos [p1 p2]
+ (map + p1 p2))
+
+(defn neighbours2 [position]
+ (map #(add-pos position %) dirs))
+(defn neighbours1 [positions acc]
+ (if (empty? positions)
+ (let [res (map #(into [] %) acc)]
+ ;; (print res)
+ res)
+ (recur (rest positions) (concat acc (neighbours2 (first positions))))))
+(defn neighbours [positions]
+ (neighbours1 positions ()))
+
+
+(defn get-move [state board-width board-height player]
+ (let [all-positions (get-all-positions board-width board-height)
+ possible-moves (remove #(is-cell-occupied? state %) all-positions)
+ occupied-positions (remove #(not (is-cell-occupied? state %)) all-positions)]
+ (if (empty? possible-moves)
+ nil
+ (if (empty? occupied-positions)
+ [10 10]
+ (let [all-neighbours (neighbours occupied-positions)
+ available-neighbours
+ (remove #(not (contains? (set possible-moves) %))
+ all-neighbours)]
+ ;; (println possible-moves)
+ ;; (println "hej")
+ ;; (println available-neighbours)
+ (first available-neighbours))))))
+
+(defn slow-move [s w h p]
+ (Thread/sleep (+ 10 (rand-int 100)))
+ (let [pos (get-move s w h p)]
+ ;; (print pos)
+ pos))

0 comments on commit 174a49b

Please sign in to comment.