 (defn rand-int-range [min max]
   (+ (rand-int (- max min)) min))
 
+(defn room-bounds-to-center
+  [[x1 y1 x2 y2]]
+  [(int (/ (+ x1 x2) 2))
+   (int (/ (+ y1 y2) 2))])
+  
 (defn random-place
   "Create a grid of random rooms with corridors connecting them and doors
    where corridors connect to rooms."

        lines    (map (fn [line] (map last (group-by first line))) lines-xy)]
    lines))

(defn cellsxy-to-ascii [cells-xy]
  "Convert a grid of cells into a list of string that can be rendered."
  (let [contents (map (fn [line] (clojure.string/join
                                      (map (fn [[x y cell]]
                                        (if (nil? cell)
                                            \ 
                                            (case (cell :type)
                                              :floor \.
                                              :vertical-wall \|
                                              :horizontal-wall \-
                                              \?)))
                                       line))) cells-xy)]
    contents))

(defn merge-cells
  "When merging cells, given their types, determine
   the type of the resulting cell."
  [cell1 cell2]
  (case (cell1 :type)
    :floor (case (cell2 :type)
             :floor :floor
             :horizontal-wall :floor
             :vertical-wall :floor
             :corridor :close-door
             :close-door :floor
             :up-stairs :up-stairs
             :down-stairs :down-stairs
             :nil :floor)
    :horizontal-wall (case (cell2 :type)
             :floor :floor
             :horizontal-wall :horizontal-wall
             :vertical-wall :horizontal-wall
             :corridor :close-door
             :close-door :close-door
             :up-stairs :up-stairs
             :down-stairs :down-stairs
             :nil :horizontal-wall)
    :vertical-wall (case (cell2 :type)
             :floor :floor
             :horizontal-wall :horizontal-wall
             :vertical-wall :vertical-wall
             :corridor :close-door
             :close-door :close-door
             :up-stairs :up-stairs
             :down-stairs :down-stairs
             :nil :vertical-wall)
    :corridor (case (cell2 :type)
             :floor :floor
             :horizontal-wall :close-door
             :vertical-wall :close-door
             :corridor :corridor
             :close-door :close-door
             :up-stairs :up-stairs
             :down-stairs :down-stairs
             :nil :corridor)
    :close-door (case (cell2 :type)
             :floor :floor
             :horizontal-wall :close-door
             :vertical-wall :close-door
             :corridor :corridor
             :close-door :close-door
             :up-stairs :up-stairs
             :down-stairs :down-stairs
             :nil :close-door)
    :up-stairs (case (cell2 :type)
             :floor :up-stairs
             :horizontal-wall :up-stairs
             :vertical-wall :up-stairs
             :corridor :up-stairs
             :close-door :up-stairs
             :nil :up-stairs)
    :down-stairs (case (cell2 :type)
             :floor :down-stairs 
             :horizontal-wall :down-stairs 
             :vertical-wall :down-stairs 
             :corridor :down-stairs 
             :close-door :down-stairs 
             :nil :down-stairs)
    :nil (cell2 :type)))

(defn merge-with-canvas
  "Merge a grid of `[x y cell]` into an existing grid. The result is a merged grid

        g            (graph-to-minimum-spanning-tree fcg)
        more-edges   (additional-edges min-width min-height room-centers g)
        g            (concat g more-edges)
        ;; set of room centers that are junction rooms
        junction-rooms (find-junction-rooms g)
        ;; merge room cells to canvas, skipping junction rooms
        cells        (reduce (fn [cells room-bound]
                               (println "room-bound" room-bound)
                               (if (contains? junction-rooms (room-bounds-to-center room-bound))
                                 cells
                                 (let [room-cellsxy (apply room-to-cellsxy room-bound)]
                                   (println "room-cellsxy" room-cellsxy)
                                   (apply merge-with-canvas cells room-cellsxy))))
                             (canvas width height)
                             room-bounds)
        ;; reduce over g, merging corridor cells to canvas
        cells        (reduce (fn [cells [start goal]]
                               (let [points (make-corridor-points cells room-centers-to-room-bounds junction-rooms start goal)]
                                 (println "corridor points" points)
                                 (apply merge-with-canvas cells (points-to-corridor points))))
                             cells
                             g)
        upstairs     (conj [(first room-centers)] {:type :up-stairs})
        downstairs   (conj [(last room-centers)] {:type :down-stairs})]
