Created
December 16, 2016 04:44
-
-
Save bolivier/2aee20766d222fe86edb35dda7eb8edb to your computer and use it in GitHub Desktop.
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
(ns MyBot3 | |
(:require [game] | |
[io]) | |
(:gen-class)) | |
(def bot-name "MyBot3") | |
(declare map-to-adjacent-sites get-direction-to-nearest-edge) | |
(defn max-out-direction | |
"return direction that would set strength above production value of site" | |
[game-map site] | |
(reduce (fn [best-choice direction] | |
(let [potential-site (game/adjacent-site game-map site direction)] | |
(if (< (* 2 (:production potential-site)) (+ (:strength potential-site) (:strength site))) | |
direction | |
best-choice))) | |
:still | |
game/cardinal-directions)) | |
(defn single-piece-move "Should return a [site direction] pair for a single site" | |
[game-map [my-site adjacent-sites]] | |
(let [best-direction-to-go (get-direction-to-nearest-edge game-map my-site) | |
site-to-go-to (game/adjacent-site game-map my-site best-direction-to-go) | |
i-own-best-site (= (:owner my-site) (:owner site-to-go-to)) | |
production-comparison (>= (:strength my-site) (* 4 (:production my-site))) | |
adjacent-direction-to-max-out (max-out-direction game-map my-site) | |
strength-test (>= (:strength my-site ) | |
(:strength site-to-go-to))] | |
[my-site (cond | |
(or strength-test | |
(and i-own-best-site production-comparison)) | |
best-direction-to-go | |
:else :still)])) | |
(defn make-great-guess "Guess making function. | |
my-sites is my sites associated the adjacent tiles. | |
Should return a list of [site direction] pairs" | |
[game-map my-sites] | |
(map #(single-piece-move game-map %) my-sites)) | |
(defn distance-to-edge-in-direction [game-map site direction] | |
(let [me (:owner site)] | |
(loop [distance 0 test-site (game/adjacent-site game-map site direction)] | |
(if (and | |
(not= site test-site) | |
(= me (:owner test-site))) | |
(recur (+ 1 distance) (game/adjacent-site game-map test-site direction)) | |
distance)))) | |
(defn get-direction-to-nearest-edge [game-map site] | |
(:direction | |
(reduce (fn [closest-direction test-direction] | |
(let [distance (distance-to-edge-in-direction game-map site test-direction) | |
is-closer (<= distance (:distance closest-direction))] | |
(if is-closer | |
{:direction test-direction :distance distance} | |
closest-direction))) | |
{:distance 100 :direction :north} | |
game/cardinal-directions))) | |
(defn carefully-considered-moves | |
"Takes a 2D vector of sites and returns a list of [site, direction] pairs" | |
[my-id game-map] | |
(let [my-sites (->> game-map | |
flatten | |
(filter #(= (:owner %) my-id)))] | |
(make-great-guess game-map (map-to-adjacent-sites game-map my-sites)))) | |
(defn map-to-adjacent-sites [game-map my-sites] | |
(reduce (fn [acc site] | |
(assoc acc site (vec (map #(game/adjacent-site game-map site %) game/cardinal-directions)))) | |
{} | |
my-sites)) | |
(defn -main [] | |
(let [{:keys [my-id productions width height game-map]} (io/get-init!)] | |
;; Do any initialization you want with the starting game-map before submitting the bot-name | |
(println bot-name) | |
(doseq [turn (range)] | |
(let [game-map (io/create-game-map width height productions (io/read-ints!))] | |
(io/send-moves! (carefully-considered-moves my-id game-map)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment