Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
85
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.  
  31. (defn walk-robot [robot state]
  32.   (let [new-pos (vec (map + (:position robot) (:direction robot)))]
  33.     (validate-position new-pos (:grid state))
  34.     (let [new-state
  35.       (create-game-state
  36.         (place-content (:position robot) nil (:grid state))
  37.         (remove #(= (:position robot) (:position %)) (:robots state)))]
  38.       (create-robot new-pos new-state))
  39.   ))
  40.    
  41.  
  42. (defn create-robot [position state]
  43.   (create-game-state
  44.     (place-content position :robot (:grid state))
  45.     (conj (:robots state) (hash-map :position position :direction [0 1]))
  46.   )
  47. )
  48.  
  49. (defn create-game-state [grid robots]
  50.   {:grid grid
  51.    :robots robots})
  52.  
  53. (def initial-state (create-game-state (create-grid [2 2]) []))
  54. (as-> initial-state state
  55.   (create-robot [1 0] state)
  56.   (do (println state "\n") state)
  57.   (walk-robot (first (:robots state)) state)
  58.   (do (println state "\n") state)
  59. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement