Guest User


a guest
Jul 5th, 2015
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns cmaze.maze
  2.   (require [clojure.set :as set])
  3.   (use cmaze.cell))
  5. (defrecord Maze [cells size])
  7. (defn- create-maze-cell [index size]
  8.   "Creates a cell depending on its location in the maze."
  9.   (let [is-only-cell (= size 1)
  10.         [is-corner-cell corner] (m-corner-cell? index size)
  11.         [is-wall-cell side] (m-wall-cell? index size)]
  12.     (cond (true? is-only-cell) (create-cell)
  13.           (true? is-corner-cell) (create-corner-cell corner)
  14.           (true? is-wall-cell) (create-wall-cell side)
  15.           :else (create-unbound-cell))))
  17. (defn create-maze [size]
  18.   "Create a maze sizeXsize large."
  19.   (Maze.
  20.     (let [total-cells (* size size)]
  21.       (map-indexed (fn [idx _] (create-maze-cell idx size))
  22.         (range total-cells)))
  23.     size))
  25. (defn get-cell [maze idx dir]
  26.   "Returns the neighboring cell of the cell at point idx. The direction defines which neighbor to return."
  27.   (let [{cells :cells size :size} maze]
  28.     (when (> idx (* size size)) (throw (IllegalArgumentException. (format "Index %s exceeds size of maze." idx))))
  29.     (case dir
  30.       :west (nth cells (dec idx))
  31.       :east (nth cells (inc idx))
  32.       :north (nth cells (- idx size))
  33.       :south (nth cells (+ idx size)))))
  35. (defn- is-open? [cell dir]
  36.   (contains? (:open-dirs cell) dir))
  38. (defn- link-cell [maze idx cell]
  39.   (let [{valid-dirs :valid-dirs open-dirs :open-dirs} cell
  40.         dirs-to-open (reduce (fn [dirs-to-open dir]
  41.                                (case dir
  42.                                  :west (if (is-open? (get-cell maze idx :west) :east) (conj dirs-to-open :west) dirs-to-open)
  43.                                  :east (if (is-open? (get-cell maze idx :east) :west) (conj dirs-to-open :east) dirs-to-open)
  44.                                  :north (if (is-open? (get-cell maze idx :north) :south) (conj dirs-to-open :north) dirs-to-open)
  45.                                  :south (if (is-open? (get-cell maze idx :south) :north) (conj dirs-to-open :south) dirs-to-open)))
  46.                        #{}
  47.                        valid-dirs)]
  48.     (assoc cell :open-dirs (set/union open-dirs dirs-to-open))))
  50. (defn- link-cells [maze]
  51.   (->Maze
  52.     (map-indexed (partial link-cell maze) (:cells maze))
  53.     (:size maze)))
  55. (defn traverse [maze f]
  56.   "Allows manipulation of a maze by iterating over each cell from top-left to bottom-right."
  57.   (let [{cells :cells size :size} maze]
  58.       (->Maze
  59.         (map (fn [cell] (f cell)) cells)
  60.         size)))
  62. (defn generate-maze-layout [maze f]
  63.   "Given function f, applies f to each cell in the maze starting at the top-left."
  64.   (link-cells (traverse maze f)))
  66. (defn binary-tree [cell]
  67.   (let [valid-dirs (:valid-dirs cell)]
  68.     (cond
  69.       (and (contains? valid-dirs :north) (contains? valid-dirs :east)) (assoc cell :open-dirs #{:east})
  70.       (contains? valid-dirs :north) (assoc cell :open-dirs #{:north})
  71.       (contains? valid-dirs :east) (assoc cell :open-dirs #{:east})
  72.       :else cell)))
RAW Paste Data