Created
May 31, 2015 03:13
-
-
Save hhutch/46b9d4e7fa2b44861683 to your computer and use it in GitHub Desktop.
Conrad Barski's (http://pastebin.com/dd5ccDkP#) implementation of http://weblog.jamisbuck.org/2011/1/20/maze-generation-wilson-s-algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]})) | |
;; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
;; | | | | | | | | |
;; + + +-+-+ +-+ + +-+-+-+-+ + + +-+-+-+-+ + | |
;; | | | | | | | | | | | | | | | | |
;; + + +-+ +-+ + + + + + +-+ + +-+-+ +-+-+ + | |
;; | | | | | | | | | | | |
;; + +-+ +-+ +-+-+ +-+ + +-+-+-+ + + +-+-+ + | |
;; | | | | | | | | | | | | | |
;; + +-+ + +-+ +-+ + +-+ + +-+ +-+ + +-+-+ + | |
;; | | | | | | | | | | | |
;; +-+ +-+-+ + +-+-+ +-+-+-+ +-+ + +-+ +-+ + | |
;; | | | | | | | | | | |
;; +-+-+ + + + +-+ +-+-+ + +-+-+ +-+ +-+ + + | |
;; | | | | | | | | | | | | |
;; +-+-+-+-+-+ + +-+-+ +-+-+-+ +-+ + + +-+-+ | |
;; | | | | | | | | | | | |
;; +-+-+-+-+ + +-+ +-+ +-+-+ +-+ +-+-+-+ +-+ | |
;; | | | | | | | | | | | | |
;; +-+ + +-+-+-+ +-+-+-+ +-+ + + +-+-+ +-+ + | |
;; | | | | | | | | | | | | |
;; + +-+ +-+ +-+ + + +-+-+ + + +-+ + +-+ +-+ | |
;; | | | | | | | | | | | |
;; + + + + +-+-+-+ +-+ + +-+ + +-+ +-+-+ + + | |
;; | | | | | | | | | | | | | | | |
;; + + +-+ + +-+-+ + + + +-+-+ + +-+ + + + + | |
;; | | | | | | | | | | | | | |
;; + +-+ + +-+ +-+-+ + +-+-+ + +-+ + + +-+-+ | |
;; | | | | | | | | | | | | | | |
;; + + +-+-+-+-+-+ +-+ +-+ +-+-+-+ +-+-+ +-+ | |
;; | | | | | | | | | | | | | |
;; + + + +-+ +-+ +-+ +-+-+ + + + +-+ + + + + | |
;; | | | | | | | | | | | | | |
;; + +-+-+-+ + + +-+ +-+ + +-+ +-+-+ +-+-+-+ | |
;; | | | | | | | | | | | | | | |
;; +-+ +-+ +-+ +-+-+ +-+ +-+ +-+ + + + +-+ + | |
;; | | | | | | | | | | | |
;; + +-+-+ +-+-+ +-+ + +-+-+ +-+ + + +-+-+ + | |
;; | | | | | | | | | | | |
;; + + + + + +-+ + +-+ +-+ +-+ + + +-+-+ + + | |
;; | | | | | | | | | | | |
;; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment