From a7379f2fbd9e2bc77e49046cfcaab2fc79f11087 Mon Sep 17 00:00:00 2001 From: David Vrensk Date: Tue, 15 Jan 2013 20:18:37 +0100 Subject: [PATCH] An agent with a cleverer strategy! --- src/gomoku/team_david.clj | 5 ++- src/gomoku/team_david2.clj | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/gomoku/team_david2.clj diff --git a/src/gomoku/team_david.clj b/src/gomoku/team_david.clj index 0ceb8e5..adccea6 100644 --- a/src/gomoku/team_david.clj +++ b/src/gomoku/team_david.clj @@ -28,6 +28,9 @@ (neighbours1 positions ())) +(defn middle-of-board [a b] + [(int (/ a 2)) (int (/ b 2))]) + (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) @@ -35,7 +38,7 @@ (if (empty? possible-moves) nil (if (empty? occupied-positions) - [10 10] + (middle-of-board board-width board-height) (let [all-neighbours (neighbours occupied-positions) available-neighbours (remove #(not (contains? (set possible-moves) %)) diff --git a/src/gomoku/team_david2.clj b/src/gomoku/team_david2.clj new file mode 100644 index 0000000..cd73190 --- /dev/null +++ b/src/gomoku/team_david2.clj @@ -0,0 +1,64 @@ +(ns gomoku.team-david2 + (:require [gomoku.gameplay :as gameplay])) + +(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 positions-with-chain-length [candidates state player] + (let [moves (gameplay/get-positions-of-moves-for-player state player)] + (for [pos candidates + dir dirs] + [pos (gameplay/nr-of-moves-in-dir (cons pos moves) pos dir)]))) + +(defn middle-of-board [a b] + [(int (/ a 2)) (int (/ b 2))]) + +(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) + (middle-of-board board-width board-height) + (let [all-neighbours (neighbours occupied-positions) + available-neighbours + (remove #(not (contains? (set possible-moves) %)) + all-neighbours) + positions-with-length + (positions-with-chain-length available-neighbours state player) + positions-sorted (map first (sort-by last positions-with-length))] + ;; (println positions-with-length) + ;; (println "hej") + ;; (println positions-sorted) + (last positions-sorted)))))) + +(defn slow-move [s w h p] + (Thread/sleep (+ 10 (rand-int 100))) + (let [pos (get-move s w h p)] + ;; (print pos) + pos)) \ No newline at end of file