Skip to content

Commit 09d31d6

Browse files
authored
Merge pull request #160 from tmarble/add-clojure-starter-kit
Updates the Clojure starter kit
2 parents c54ef74 + 215b09c commit 09d31d6

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

starter_kits/Clojure/hlt/lib/hlt/game_map.clj

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[hlt.input :refer [read-line-ints]]
44
[hlt.direction :refer [invert]]
55
[hlt.position :refer [new-position abs-position sub-position
6-
position? directional-offset]]
6+
get-position directional-offset]]
77
[hlt.map-cell :as map-cell :refer [read-map-cells]]
88
[hlt.direction :as direction]))
99

@@ -15,10 +15,7 @@
1515
(defn at
1616
"Get cell at position"
1717
[position-or-entity]
18-
(let [position (if (position? position-or-entity)
19-
position-or-entity
20-
(:position position-or-entity))]
21-
(get-in @game [:game-map :map-cells position])))
18+
(get-in @game [:game-map :map-cells (get-position position-or-entity)]))
2219

2320
(defn is-occupied?
2421
"Is cell occupied?"
@@ -47,8 +44,8 @@
4744
"A method that computes the Manhattan distance between two locations,
4845
and accounts for the toroidal wraparound."
4946
[source target]
50-
(let [source (normalize source)
51-
target (normalize target)
47+
(let [source (-> source get-position normalize)
48+
target (-> target get-position normalize)
5249
[x y] (abs-position (sub-position source target))
5350
{:keys [width height]} (game-map)]
5451
(+ (min x (- width x)) (min y (- height y)))))
@@ -57,8 +54,8 @@
5754
"Returns where in the cardinality spectrum the target is from source.
5855
e.g.: North, East; South, West; etc."
5956
[source target]
60-
(let [[sx sy] source
61-
[tx ty] target
57+
(let [[sx sy] (-> source get-position)
58+
[tx ty] (-> target get-position)
6259
cx (if (> tx sx)
6360
direction/east
6461
(if (< tx sx)
@@ -75,8 +72,8 @@
7572
if the source and destination are the same."
7673
[source destination]
7774
(let [{:keys [width height]} (game-map)
78-
source (normalize source)
79-
destination (normalize destination)
75+
source (-> source get-position normalize)
76+
destination (-> destination get-position normalize)
8077
[dx dy] (abs-position (sub-position destination source))
8178
[cx cy] (get-target-direction source destination)
8279
m0 (if-not (zero? dx)
@@ -100,8 +97,9 @@
10097
without colliding with other entities. Returns a direction of
10198
still if no such move exists."
10299
[ship destination]
103-
(let [ship-position (:position ship)
104-
unsafe-moves (get-unsafe-moves ship-position destination)
100+
(let [ship-position (get-position ship)
101+
destination-position (get-position destination)
102+
unsafe-moves (get-unsafe-moves ship-position destination-position)
105103
dir (reduce
106104
(fn [_ direction]
107105
(if (is-empty?

starter_kits/Clojure/hlt/lib/hlt/position.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
[x]
4141
(and (vector? x) (= 2 (count x))))
4242

43+
(defn get-position
44+
"Gets a position from an entity -or- returns the arg if it is a position"
45+
[position-or-entity]
46+
(if (or (map? position-or-entity) (not (position? position-or-entity)))
47+
(:position position-or-entity)
48+
position-or-entity))
49+
4350
(defn new-position
4451
"Create a new position"
4552
[x y]

starter_kits/Clojure/project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[com.taoensso/timbre "4.10.0"]
44
[org.clojure/data.json "0.2.6"]]
55
:source-paths ["hlt/lib" "hlt/app"]
6+
:test-paths ["test"]
67
:main MyBot
78
:aot [MyBot]
89
:uberjar-name "MyBot.jar")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns hlt.game-map-test
2+
(:require [clojure.test :refer :all]
3+
[hlt.ship :as ship]
4+
[hlt.player :as player]
5+
[hlt.shipyard :as shipyard]
6+
[hlt.game-map :refer :all]))
7+
8+
(deftest test-naive-navigate
9+
(with-redefs [game-map (fn [] {:width 32 :height 32})]
10+
(let [ship {::ship/id 1 ::player/id 0 :position [8 10] :halite 100}
11+
shipyard {::shipyard/id -1 ::player/id 0 :position [8 16]}]
12+
(is (= [0 1] (naive-navigate ship shipyard))))))

0 commit comments

Comments
 (0)