Newer
Older
mazes / side-winder.lisp
(in-package :mazes)

(defclass sidewinder ()
  ((grid :initarg :grid :reader sidewinder-grid)))

(defmethod initialize-instance :after ((sw sidewinder) &key)
    (let* ((grid (sidewinder-grid sw))
           (matrix (grid-matrix grid))
           (rows (grid-rows grid))
           (cols (grid-cols grid))
           (run ())
           (cell ())
           (member ())
           (at-eastern-boundary nil)
           (at-northern-boundary nil)
           (should-close-out nil))
      (dotimes (r rows)
        (setf run ())
        (dotimes (c cols)
          (setf cell (aref matrix r c))
          (setf run (append run `(,cell)))
          (if (null (cell-east cell))
              (setf at-eastern-boundary t)
              (setf at-eastern-boundary nil))
          (if (null (cell-north cell))
              (setf at-northern-boundary t)
              (setf at-northern-boundary nil))

          (setf should-close-out 
                (or at-eastern-boundary 
                    (equal 0
                           (and (not at-northern-boundary) (random 2)))))
          (if should-close-out
              (progn
                (setf member (elt run (random (length run))))
                (when (cell-north member) (link member :neighbor-cell (cell-north member)))
                (setf run ()))
              ; else 
              (link cell :neighbor-cell (cell-east cell)))))
      grid))

(defun make-sidewinder (grid)
  (make-instance 'sidewinder :grid grid))