Last active
December 19, 2015 14:38
-
-
Save bruceadams/5970280 to your computer and use it in GitHub Desktop.
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 mazejam.core | |
(:require [clojure.set :as set])) | |
(defn to-json | |
[maze] | |
(clojure.pprint/cl-format | |
true | |
"[~{[~{~3d~^, ~}]~^,\n ~}]~%" | |
maze)) | |
(defn uncarved | |
[x-size y-size] | |
(vec (for [x (range x-size)] (vec (for [y (range y-size)] 0))))) | |
(comment | |
(to-json (uncarved 2 2)) | |
(to-json (uncarved 9 9)) | |
) | |
(defn points | |
[x-size y-size] | |
(for [x (range 0 x-size) y (range 0 y-size)] [x y])) | |
(comment | |
(points 3 3) | |
) | |
(defn valid-cell? [[x y] x-size y-size] | |
(when (and (>= x 0) (>= y 0) | |
(< x x-size) (< y y-size)) | |
[x y])) | |
(defn neighbors | |
"For a maze of size (x-size, y-size) return the coordinates of all | |
neighbors of cell (x,y)." | |
[[x y] [x-size y-size]] | |
(let [all | |
[[(dec x) y] [(inc x) y] | |
[x (dec y)] [x (inc y)]]] | |
(filter #(valid-cell? % x-size y-size) all))) | |
(comment | |
(neighbors [1 1] [4 4]) | |
(neighbors [0 0] [4 4]) | |
(neighbors [3 3] [4 4]) | |
) | |
(defn uncarved-neighbors | |
[[x y] point-list discards maze-size] | |
(set/difference | |
(set (neighbors [x y] maze-size)) | |
(set (concat point-list discards)))) | |
(defn maze-size | |
[maze] | |
[(count (first maze)) (count maze)]) | |
(defn add-value | |
[maze [x y] v] | |
(assoc maze y | |
(assoc (nth maze y) x (+ v (nth (nth maze y) x))))) | |
(def north 1) | |
(def south 2) | |
(def east 4) | |
(def west 8) | |
(defn link-points | |
[maze p1 p2] | |
(if (= (first p1) (first p2)) | |
(add-value (add-value maze p1 south) p2 north) | |
(add-value (add-value maze p1 east) p2 west))) | |
(defn connect | |
"Returns a new maze with the given points connected." | |
[p1 p2 maze] | |
(let [[p1 p2] (sort [p1 p2])] | |
(link-points maze p1 p2))) | |
(comment | |
(to-json (connect [0 0] [0 1] (uncarved 3 3))) | |
(to-json (connect [2 0] [1 0] (uncarved 3 3))) | |
) | |
(defn carve-maze | |
[maze point-list discards] | |
(if (empty? point-list) | |
maze | |
(let [point (first point-list) | |
ucn (uncarved-neighbors | |
point point-list discards (maze-size maze)) | |
other (first ucn)] | |
(prn point other ucn) | |
(prn point-list) | |
(to-json maze) | |
(if (empty? ucn) | |
(recur maze (rest point-list) (concat [point] discards)) | |
(recur (connect point other maze) | |
(concat [other] point-list) | |
discards))))) | |
(defn build-maze | |
"Return a maze of the given size." | |
[x-size y-size] | |
(let [point-list [[0 0]] | |
maze (uncarved x-size y-size)] | |
(carve-maze maze point-list []))) | |
(comment | |
(to-json | |
(build-maze 5 5)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment