Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns maze.core)
- (def *size* 20)
- (defn walk [start]
- (iterate (fn [[x y]]
- (first (shuffle (filter (fn [lst]
- (every? (fn [k]
- (< -1 k *size*))
- lst))
- [[(dec x) y] [(inc x) y] [x (dec y)] [x (inc y)]]))))
- start))
- (defn terminating-walk [remaining [pt & rest] acc]
- (if (remaining pt)
- (recur remaining rest (cons pt acc))
- [pt acc]))
- (defn cleanup-path [lst acc prev]
- (if-let [[pt & rest] lst]
- (if (prev pt)
- (reverse acc)
- (recur rest (cons pt acc) (conj prev pt)))
- (reverse acc)))
- (defn add-path [cells link path]
- (reduce (fn [cells [[x1 y1 :as pt1] [x2 y2 :as pt2]]]
- (assoc-in cells
- [(if (> (+ x1 y1) (+ x2 y2))
- pt2
- pt1)
- (if (= y1 y2)
- 0
- 1)]
- false))
- (into cells
- (for [pt path]
- [pt [true true]]))
- (map vector (cons link path) path)))
- (defn maze [cells]
- (let [remaining (set (for [x (range *size*)
- y (range *size*)
- :when (not (cells [x y]))]
- [x y]))]
- (if (seq remaining)
- (let [[link path] (terminating-walk remaining (walk (first (shuffle remaining))) nil)
- path (cleanup-path path nil #{})]
- (recur (add-path cells link path)))
- cells)))
- (defn draw-maze [cells]
- (dotimes [_ *size*] (print "+-"))
- (print "+")
- (newline)
- (dotimes [y *size*]
- (print "|")
- (dotimes [x *size*]
- (print " ")
- (if ((cells [x y]) 0)
- (print "|")
- (print " ")))
- (newline)
- (dotimes [x *size*]
- (print "+")
- (if ((cells [x y]) 1)
- (print "-")
- (print " ")))
- (print "+")
- (newline)))
- ;; user> (draw-maze (maze {[(rand-int *size*) (rand-int *size*)] [true true]}))
- ;; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- ;; | | | | | | |
- ;; + + +-+-+ +-+ + +-+-+-+-+ + + +-+-+-+-+ +
- ;; | | | | | | | | | | | | | | |
- ;; + + +-+ +-+ + + + + + +-+ + +-+-+ +-+-+ +
- ;; | | | | | | | | | |
- ;; + +-+ +-+ +-+-+ +-+ + +-+-+-+ + + +-+-+ +
- ;; | | | | | | | | | | | |
- ;; + +-+ + +-+ +-+ + +-+ + +-+ +-+ + +-+-+ +
- ;; | | | | | | | | | |
- ;; +-+ +-+-+ + +-+-+ +-+-+-+ +-+ + +-+ +-+ +
- ;; | | | | | | | | |
- ;; +-+-+ + + + +-+ +-+-+ + +-+-+ +-+ +-+ + +
- ;; | | | | | | | | | | |
- ;; +-+-+-+-+-+ + +-+-+ +-+-+-+ +-+ + + +-+-+
- ;; | | | | | | | | | |
- ;; +-+-+-+-+ + +-+ +-+ +-+-+ +-+ +-+-+-+ +-+
- ;; | | | | | | | | | | |
- ;; +-+ + +-+-+-+ +-+-+-+ +-+ + + +-+-+ +-+ +
- ;; | | | | | | | | | | |
- ;; + +-+ +-+ +-+ + + +-+-+ + + +-+ + +-+ +-+
- ;; | | | | | | | | | |
- ;; + + + + +-+-+-+ +-+ + +-+ + +-+ +-+-+ + +
- ;; | | | | | | | | | | | | | |
- ;; + + +-+ + +-+-+ + + + +-+-+ + +-+ + + + +
- ;; | | | | | | | | | | | |
- ;; + +-+ + +-+ +-+-+ + +-+-+ + +-+ + + +-+-+
- ;; | | | | | | | | | | | | |
- ;; + + +-+-+-+-+-+ +-+ +-+ +-+-+-+ +-+-+ +-+
- ;; | | | | | | | | | | | |
- ;; + + + +-+ +-+ +-+ +-+-+ + + + +-+ + + + +
- ;; | | | | | | | | | | | |
- ;; + +-+-+-+ + + +-+ +-+ + +-+ +-+-+ +-+-+-+
- ;; | | | | | | | | | | | | |
- ;; +-+ +-+ +-+ +-+-+ +-+ +-+ +-+ + + + +-+ +
- ;; | | | | | | | | | |
- ;; + +-+-+ +-+-+ +-+ + +-+-+ +-+ + + +-+-+ +
- ;; | | | | | | | | | |
- ;; + + + + + +-+ + +-+ +-+ +-+ + + +-+-+ + +
- ;; | | | | | | | | | |
- ;; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement