Skip to content

Commit

Permalink
Debugging maze.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
ndpar committed Jun 18, 2016
1 parent d3bcaa0 commit 978e611
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cljprog/maze.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
(merge-with into index {a [b] b [a]}))
{} (map seq walls))
start-loc (rand-nth (keys paths))]
;(println "Start:" start-loc)
(loop [walls walls
unvisited (disj (set (keys paths)) start-loc)]
;(println "Unvisited:" unvisited)
(if-let [loc (when-let [s (seq unvisited)] (rand-nth s))]
(let [walk (iterate (comp rand-nth paths) loc)
steps (zipmap (take-while unvisited walk) (next walk))]
;(println "Loc:" loc "Steps:" steps)
(recur (reduce disj walls (map set steps))
(reduce disj unvisited (keys steps))))
walls))))
Expand Down Expand Up @@ -45,5 +48,7 @@
.pack
(.setVisible true)))

; (ns cljprog.maze)
; (load "maze")
; (draw 3 3 #{#{[1 0] [1 1]} #{[2 1] [1 1]} #{[0 0] [0 1]} #{[1 1] [1 2]}})
; (draw 40 40 (maze (grid 40 40)))
48 changes: 48 additions & 0 deletions test/cljprog/maze_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(ns cljprog.maze-test
(:use clojure.test cljprog.maze))

(deftest merge-test
(is (= {:c [1] :a {1 2} :b [4]}
(merge {:a {0 1} :b [2 3]}
{:a {1 2} :b [4]}
{:c [1]}))))

(deftest into-test
(is (= #{1 2 3 4}
(into #{1 2 3} #{2 3 4})))
(is (= [1 2 3 4]
(into [1 2] [3 4])))
(is (= '(4 3 1 2)
(into '(1 2) '(3 4)))))

(deftest merge-with-test
(is (= {:c [1] :a {0 1 1 2} :b [2 3 4]}
(merge-with into
{:a {0 1} :b [2 3]}
{:a {1 2} :b [4]}
{:c [1]}))))

(deftest seq-test
(let [[a b] '(1 2)]
(is (and (= a 1) (= b 2))))
(is (= '(([1 1] [0 1]) ([1 1] [1 2]))
(map seq #{#{[0 1] [1 1]} #{[1 1] [1 2]}})))) ; seq of seqs

#_(deftest walk-test
(let [paths {[0 0] [[1 0] [0 1]]
[1 0] [[0 0] [1 1]]
[0 1] [[0 0] [1 1]]
[1 1] [[0 1] [1 0]]}]
(print (take 5 (iterate (comp rand-nth paths) [0 0])))))

(deftest zipmap-test
(let [numbers (iterate inc 0)
pairs (zipmap (take-while #(< % 10) numbers) (next numbers))]
(is (= pairs {0 1, 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10}))))

(deftest map-set-test
(is (= '(#{1 2} #{3 4})
(map set {1 2 3 4}))))

#_(deftest maze-test
(println (maze (grid 5 5))))

0 comments on commit 978e611

Please sign in to comment.