diff --git a/distance-grid.lisp b/distance-grid.lisp new file mode 100644 index 0000000..c7df3fb --- /dev/null +++ b/distance-grid.lisp @@ -0,0 +1,15 @@ +(in-package :mazes) + +(defclass distance-grid (grid) + ((distances :initform nil :accessor distances)) + (:documentation "A subclass of GRID that displays distance values of the cells.")) + +(defmethod contents-of ((g grid) (c cell)) + (let ((dist (distances g)) + (chars "0123456789abcdef")) + (if (and (not (null dist)) (< (gethash c dist) (length chars))) + (format nil "~a" (subseq chars (gethash c dist) (1+ (gethash c dist)))) + "*"))) + +(defun make-distance-grid (rows cols) + (make-instance 'distance-grid :rows rows :cols cols)) diff --git a/grid.lisp b/grid.lisp index b87d724..ac6c2c9 100644 --- a/grid.lisp +++ b/grid.lisp @@ -38,12 +38,19 @@ (defgeneric size (grid) (:documentation "Return the size of the grid")) +(defgeneric contents-of (grid cell) + (:documentation "Default value of text for a cell to print out. To be overridden by subclasses of GRID.")) + (defgeneric to-string (grid) (:documentation "Return ASCII string of the maze.")) (defgeneric get-cell (grid row cell) (:documentation "Return a cell at position (row,col)")) +(defmethod contents-of ((g grid) (c cell)) + (declare (ignore g c)) + (format nil " ")) + (defmethod to-string ((obj grid)) (let ((body "") @@ -60,7 +67,7 @@ (dotimes (c (grid-cols obj)) (setf sell (aref (grid-matrix obj) r c)) (when (null sell) (setf sell (make-cell -1 -1))) - (setf body " ") ; three spaces + (setf body (concatenate 'string " " (contents-of obj sell) " ")) ; three spaces (if (linked-p sell :other-cell (cell-east sell)) (setf east-boundary " ") (setf east-boundary "|")) diff --git a/mazes.asd b/mazes.asd index efd9403..7282078 100644 --- a/mazes.asd +++ b/mazes.asd @@ -11,4 +11,5 @@ (:file "grid") (:file "binary-tree") (:file "side-winder") - (:file "distances"))) + (:file "distances") + (:file "distance-grid"))) diff --git a/mazes.lisp b/mazes.lisp index a464c74..4fa3781 100644 --- a/mazes.lisp +++ b/mazes.lisp @@ -34,7 +34,6 @@ (defgeneric neighbors (cell) (:documentation "List of cells that adjoin this cell.")) - (defgeneric distances (cell) (:documentation "Caluclate distance of each cell from root cell"))