Advertisement
Guest User

Maze algorithm in Clojure

a guest
Jan 20th, 2011
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. (ns maze.core)
  2.  
  3. (def *size* 20)
  4.  
  5. (defn walk [start]
  6. (iterate (fn [[x y]]
  7. (first (shuffle (filter (fn [lst]
  8. (every? (fn [k]
  9. (< -1 k *size*))
  10. lst))
  11. [[(dec x) y] [(inc x) y] [x (dec y)] [x (inc y)]]))))
  12. start))
  13.  
  14. (defn terminating-walk [remaining [pt & rest] acc]
  15. (if (remaining pt)
  16. (recur remaining rest (cons pt acc))
  17. [pt acc]))
  18.  
  19. (defn cleanup-path [lst acc prev]
  20. (if-let [[pt & rest] lst]
  21. (if (prev pt)
  22. (reverse acc)
  23. (recur rest (cons pt acc) (conj prev pt)))
  24. (reverse acc)))
  25.  
  26. (defn add-path [cells link path]
  27. (reduce (fn [cells [[x1 y1 :as pt1] [x2 y2 :as pt2]]]
  28. (assoc-in cells
  29. [(if (> (+ x1 y1) (+ x2 y2))
  30. pt2
  31. pt1)
  32. (if (= y1 y2)
  33. 0
  34. 1)]
  35. false))
  36. (into cells
  37. (for [pt path]
  38. [pt [true true]]))
  39. (map vector (cons link path) path)))
  40.  
  41. (defn maze [cells]
  42. (let [remaining (set (for [x (range *size*)
  43. y (range *size*)
  44. :when (not (cells [x y]))]
  45. [x y]))]
  46. (if (seq remaining)
  47. (let [[link path] (terminating-walk remaining (walk (first (shuffle remaining))) nil)
  48. path (cleanup-path path nil #{})]
  49. (recur (add-path cells link path)))
  50. cells)))
  51.  
  52. (defn draw-maze [cells]
  53. (dotimes [_ *size*] (print "+-"))
  54. (print "+")
  55. (newline)
  56. (dotimes [y *size*]
  57. (print "|")
  58. (dotimes [x *size*]
  59. (print " ")
  60. (if ((cells [x y]) 0)
  61. (print "|")
  62. (print " ")))
  63. (newline)
  64. (dotimes [x *size*]
  65. (print "+")
  66. (if ((cells [x y]) 1)
  67. (print "-")
  68. (print " ")))
  69. (print "+")
  70. (newline)))
  71.  
  72. ;; user> (draw-maze (maze {[(rand-int *size*) (rand-int *size*)] [true true]}))
  73. ;; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  74. ;; | | | | | | |
  75. ;; + + +-+-+ +-+ + +-+-+-+-+ + + +-+-+-+-+ +
  76. ;; | | | | | | | | | | | | | | |
  77. ;; + + +-+ +-+ + + + + + +-+ + +-+-+ +-+-+ +
  78. ;; | | | | | | | | | |
  79. ;; + +-+ +-+ +-+-+ +-+ + +-+-+-+ + + +-+-+ +
  80. ;; | | | | | | | | | | | |
  81. ;; + +-+ + +-+ +-+ + +-+ + +-+ +-+ + +-+-+ +
  82. ;; | | | | | | | | | |
  83. ;; +-+ +-+-+ + +-+-+ +-+-+-+ +-+ + +-+ +-+ +
  84. ;; | | | | | | | | |
  85. ;; +-+-+ + + + +-+ +-+-+ + +-+-+ +-+ +-+ + +
  86. ;; | | | | | | | | | | |
  87. ;; +-+-+-+-+-+ + +-+-+ +-+-+-+ +-+ + + +-+-+
  88. ;; | | | | | | | | | |
  89. ;; +-+-+-+-+ + +-+ +-+ +-+-+ +-+ +-+-+-+ +-+
  90. ;; | | | | | | | | | | |
  91. ;; +-+ + +-+-+-+ +-+-+-+ +-+ + + +-+-+ +-+ +
  92. ;; | | | | | | | | | | |
  93. ;; + +-+ +-+ +-+ + + +-+-+ + + +-+ + +-+ +-+
  94. ;; | | | | | | | | | |
  95. ;; + + + + +-+-+-+ +-+ + +-+ + +-+ +-+-+ + +
  96. ;; | | | | | | | | | | | | | |
  97. ;; + + +-+ + +-+-+ + + + +-+-+ + +-+ + + + +
  98. ;; | | | | | | | | | | | |
  99. ;; + +-+ + +-+ +-+-+ + +-+-+ + +-+ + + +-+-+
  100. ;; | | | | | | | | | | | | |
  101. ;; + + +-+-+-+-+-+ +-+ +-+ +-+-+-+ +-+-+ +-+
  102. ;; | | | | | | | | | | | |
  103. ;; + + + +-+ +-+ +-+ +-+-+ + + + +-+ + + + +
  104. ;; | | | | | | | | | | | |
  105. ;; + +-+-+-+ + + +-+ +-+ + +-+ +-+-+ +-+-+-+
  106. ;; | | | | | | | | | | | | |
  107. ;; +-+ +-+ +-+ +-+-+ +-+ +-+ +-+ + + + +-+ +
  108. ;; | | | | | | | | | |
  109. ;; + +-+-+ +-+-+ +-+ + +-+-+ +-+ + + +-+-+ +
  110. ;; | | | | | | | | | |
  111. ;; + + + + + +-+ + +-+ +-+ +-+ + + +-+-+ + +
  112. ;; | | | | | | | | | |
  113. ;; +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement