Skip to content

Instantly share code, notes, and snippets.

@babo
Created September 7, 2010 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babo/568977 to your computer and use it in GitHub Desktop.
Save babo/568977 to your computer and use it in GitHub Desktop.
(ns pw.bot
(:gen-class :main true :prefix "-")
(:use [clojure.string :only (trim split-lines)]
pw.planetwars))
;; Helpers for your bot
(defn my-strongest-planet
[pw]
((first (sort-by #(> (:num-ships %1) (:num-ships %2))
(my-planets pw))) :planet-id))
(defn weakest-enemy-planet
[pw]
((first (sort-by #(< (:num-ships %1) (:num-ships %2))
(enemy-planets pw))) :planet-id))
(defn ihalf [x] (int (/ x 2)))
;; Your Robot
(defn do-turn [pw]
(cond
;; Do nothing if a fleet is in flight
(pos? (count (my-fleets pw))) nil
;; Else send half your ships from your strongest planets
;; to your enemy's weakest planet
:else (let [source (my-strongest-planet pw)
dest (weakest-enemy-planet pw)]
(if (and (pos? source) (pos? dest))
(issue-order source dest
(ihalf ((get-planet pw source) :num-ships)))))))
;; utility functions
(defn- go? [s] (= (apply str (take 2 s)) "go"))
(defn- take-turn
[f pw]
(f (parse-game-state pw)) ;; run your bot (f) on parsed pw
(finish-turn)) ;; say go
;; Main IO loop
(defn -main [& args]
(try
(loop [line (read-line) pw ""]
(cond (go? line) (if-not (empty? pw)
(do
(take-turn do-turn pw)
(recur (read-line) ""))
(do
(finish-turn)
(recur (read-line) "")))
:else (recur (read-line)
(apply str (concat pw line "\n")))))
(catch Exception e
(java.lang.System/exit 1)))
(java.lang.System/exit 0))
(defproject clj-pwars "1.0.0-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]]
:dev-dependencies [[swank-clojure "1.2.1"]]
:aot [pw.bot pw.bot pw.planetwars]
:main pw.bot
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment