|
@@ -47,6 +47,9 @@
|
|
(defn with-action [typ]
|
|
(defn with-action [typ]
|
|
(fn [p] (= typ (get-in p [:action :type]))))
|
|
(fn [p] (= typ (get-in p [:action :type]))))
|
|
|
|
|
|
|
|
+(defn ^:private mapv-if [pred f coll]
|
|
|
|
+ (mapv (fn [e] (if (pred e) (f e) e)) coll))
|
|
|
|
+
|
|
(defn check-positions [state]
|
|
(defn check-positions [state]
|
|
"Returns a vector of valid new player positions"
|
|
"Returns a vector of valid new player positions"
|
|
(let [{:keys [board players bombs]} state
|
|
(let [{:keys [board players bombs]} state
|
|
@@ -69,21 +72,21 @@
|
|
(defn perform-movement [initial-state]
|
|
(defn perform-movement [initial-state]
|
|
(loop [state initial-state]
|
|
(loop [state initial-state]
|
|
(let [{players :players} state
|
|
(let [{players :players} state
|
|
- wants-to-move (with-action "move")
|
|
|
|
- to-move (vec (map (fn [p]
|
|
|
|
- (let [p2 (assoc p :prev-pos (get p :pos))]
|
|
|
|
- (if (wants-to-move p2)
|
|
|
|
- (dissoc (move-player p2 (get-in p2 [:action :dir])) :action)
|
|
|
|
- p)))
|
|
|
|
- players))
|
|
|
|
|
|
+ to-move (mapv-if
|
|
|
|
+ (with-action "move")
|
|
|
|
+ (fn [p] (-> p
|
|
|
|
+ (assoc :prev-pos (:pos p))
|
|
|
|
+ (move-player (get-in p [:action :dir]))
|
|
|
|
+ (dissoc :action)))
|
|
|
|
+ players)
|
|
proposed-state (assoc state :players to-move)
|
|
proposed-state (assoc state :players to-move)
|
|
valid-moves (check-positions proposed-state)
|
|
valid-moves (check-positions proposed-state)
|
|
- valid-players (map (fn [p]
|
|
|
|
- (if (and
|
|
|
|
- (not (valid-moves (:id p)))
|
|
|
|
- (wants-to-move p))
|
|
|
|
- (dissoc p :action)
|
|
|
|
- p)) players)]
|
|
|
|
|
|
+ valid-players (mapv-if
|
|
|
|
+ #(and
|
|
|
|
+ (not (valid-moves (:id %)))
|
|
|
|
+ ((with-action "move") %))
|
|
|
|
+ #(dissoc % :action)
|
|
|
|
+ players)]
|
|
(if (= players valid-players)
|
|
(if (= players valid-players)
|
|
proposed-state
|
|
proposed-state
|
|
(recur (assoc state :players valid-players))))))
|
|
(recur (assoc state :players valid-players))))))
|
|
@@ -117,16 +120,14 @@
|
|
bomb (first exploding)
|
|
bomb (first exploding)
|
|
other-bombs (filter #(not= bomb %) bombs)
|
|
other-bombs (filter #(not= bomb %) bombs)
|
|
explosion (explode-bomb board bomb)
|
|
explosion (explode-bomb board bomb)
|
|
- new-bombs (mapv (fn [bomb]
|
|
|
|
- (if (explosion (:pos bomb))
|
|
|
|
- (assoc bomb :counter 0)
|
|
|
|
- bomb))
|
|
|
|
- other-bombs)
|
|
|
|
- new-players (mapv (fn [player]
|
|
|
|
- (if (explosion (:pos player))
|
|
|
|
- (assoc player :dead? true)
|
|
|
|
- player))
|
|
|
|
- players)]
|
|
|
|
|
|
+ new-bombs (mapv-if
|
|
|
|
+ #(explosion (:pos %))
|
|
|
|
+ #(assoc % :counter 0)
|
|
|
|
+ other-bombs)
|
|
|
|
+ new-players (mapv-if
|
|
|
|
+ #(explosion (:pos %))
|
|
|
|
+ #(assoc % :dead? true)
|
|
|
|
+ players)]
|
|
(recur
|
|
(recur
|
|
(-> state
|
|
(-> state
|
|
(assoc :players new-players)
|
|
(assoc :players new-players)
|