Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; GRID
- (defn create-tile [position content]
- {:position position :content content})
- (defn create-grid [[size-x size-y]]
- (mapv
- (fn [x] (mapv (fn [y] (create-tile [x y] nil)) (range size-y)))
- (range size-x)))
- (defn position-in-grid? [grid [pos-x pos-y]]
- (and
- (>= pos-x 0)
- (>= pos-y 0)
- (< pos-x (count grid))
- (< pos-y (count (grid pos-x)))))
- (defn validate-position [grid position]
- (if (not (position-in-grid? grid position))
- (throw (AssertionError. (str "Invalid position " position)))))
- (defn place-content [grid position content]
- (validate-position grid position)
- (assoc-in grid position (create-tile position content)))
- ;; ROBOT
- (def directions
- [[0 1] [1 0] [0 -1] [-1 0]])
- (defn create-robot [position direction]
- {:position position :direction direction})
- (defn move-robot [{:keys [position direction] :as robot}]
- (let [new-pos (vec (map + position direction))]
- (assoc robot :position new-pos)))
- ;; STATE
- (defn create-game-state [grid robots]
- {:grid grid :robots robots})
- (defn place-robot-in-state [state {position :position :as robot}]
- (-> state
- (assoc-in [:grid (first position) (second position)] (create-tile position :robot))
- (assoc-in [:robots] (conj (:robots state) robot))))
- (defn remove-robot-from-state [state {position :position :as robot}]
- (let [robot-index (.indexOf (:robots state) robot)
- robots (:robots state)
- new-robots-list (concat (subvec robots 0 robot-index) (subvec robots (inc robot-index)))]
- (-> state
- (assoc-in [:grid (first position) (second position)] (create-tile position nil))
- (assoc-in [:robots] (vec new-robots-list)))))
- (defn move-robot-in-state [state {old-pos :position :as robot}]
- (let [new-robot (move-robot robot) new-pos (:position new-robot)]
- (validate-position (:grid state) new-pos)
- (-> state
- (remove-robot-from-state robot)
- (place-robot-in-state (move-robot robot)))))
- ;; GAME-LOOP
- (as-> (create-game-state (create-grid [2 2]) []) state
- (do (println state "\n") state)
- (place-robot-in-state state (create-robot [0 0] (first directions)))
- (do (println state "\n") state)
- ;; (remove-robot-from-state state (first (:robots state)))
- ;; (do (println state "\n") state)
- (move-robot-in-state state (first (:robots state)))
- (do (println state "\n") state)
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement