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