Newer
Older
mazes / binary-tree.lisp
(in-package :mazes)

(defclass binary-tree ()
  ((grid :initarg :grid :reader binary-tree-grid)))

(defmethod initialize-instance :after ((bt binary-tree) &key)
  (let* ((grid (binary-tree-grid bt))
           (matrix (grid-matrix grid))
           (rows (grid-rows grid))
           (cols (grid-cols grid))
           (neighbors ())
           (cell ()))
      (dotimes (r rows)
        (dotimes (c cols)
          (setf neighbors ())
          (setf cell (aref matrix r c))
          (when (cell-north cell) (push (cell-north cell) neighbors))
          (when (cell-east cell) (push (cell-east cell) neighbors))
          (when (> (length neighbors) 0)
            (link cell :neighbor-cell (elt neighbors (random (length neighbors)))))))
      grid))

(defun make-binary-tree (grid)
  (make-instance 'binary-tree :grid grid))