Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; GRID
  2. (defn create-tile [position]
  3.   {:position position
  4.    :content nil})
  5.  
  6. (defn create-grid [[size-x size-y]]
  7.   (mapv
  8.     (fn [x] (mapv (fn [y] (create-tile [x y])) (range size-y)))
  9.     (range size-x)))
  10.  
  11. (defn position-in-grid? [[pos-x pos-y] grid]
  12.   (and
  13.     (>= pos-x 0)
  14.     (>= pos-y 0)
  15.     (< pos-x (count grid))
  16.     (< pos-y (count (grid pos-x)))))
  17.  
  18. (defn validate-position [position grid]
  19.   (if (not (position-in-grid? position grid))
  20.     (throw (AssertionError. (str "Invalid position " position)))))
  21.  
  22. (defn place-content [position content grid]
  23.   (validate-position position grid)
  24.   (assoc grid (first position)
  25.     (assoc
  26.       (grid (first position))
  27.       (second position)
  28.       {:position position :content content})))
  29.  
  30. ;; ROBOTS
  31. (defn create-robot [position state]
  32.   (create-game-state
  33.     (place-content position :robot (:grid state))
  34.     (conj (:robots state) (hash-map :position position :direction [0 1]))
  35.   )
  36. )
  37.  
  38. (defn walk-robot [robot state]
  39.   (let [new-pos (vec (map + (:position robot) (:direction robot)))]
  40.     (validate-position new-pos (:grid state))
  41.     (create-robot
  42.       new-pos
  43.       (create-game-state
  44.         (place-content (:position robot) nil (:grid state))
  45.         (remove #(= (:position robot) (:position %)) (:robots state))))))
  46.  
  47. ;; STATE
  48. (defn create-game-state [grid robots]
  49.   {:grid grid
  50.    :robots robots})
  51.  
  52. (as-> (create-game-state (create-grid [2 2]) []) state
  53.   (create-robot [1 0] state)
  54.   (do (println state "\n") state)
  55.   (walk-robot (first (:robots state)) state)
  56.   (do (println state "\n") state)
  57. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement