Browse Source

Factor out mapv-if helper function

Thomas Dy 8 years ago
parent
commit
3df70c0af5
1 changed files with 24 additions and 23 deletions
  1. 24 23
      src/bombnet/game.clj

+ 24 - 23
src/bombnet/game.clj

@@ -47,6 +47,9 @@
 (defn with-action [typ]
   (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]
   "Returns a vector of valid new player positions"
   (let [{:keys [board players bombs]} state
@@ -69,21 +72,21 @@
 (defn perform-movement [initial-state]
   (loop [state initial-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)
           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)
         proposed-state
         (recur (assoc state :players valid-players))))))
@@ -117,16 +120,14 @@
            bomb (first exploding)
            other-bombs (filter #(not= bomb %) bombs)
            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
          (-> state
              (assoc :players new-players)