[Welcome to Sensei's Library!]

StartingPoints
ReferenceSection
About


Referenced by
TheCodeSoFar

 

The Code So Far - Position
    Keywords: Software

A position is a board and some stones:

 (defclass Position ()
   ((Board :accessor Board :initarg Board)
    (Stones :accessor Stones :initarg Stones)))

Making an empty position is easy. Actually, the Board should really decide the size of the Stones array, that way the code could support non-square Gobans.

 (defun new-position (sz)
   (make-instance 'Position
 		 'Board (make-Board sz)
 		 'Stones (make-array (* sz sz) :initial-element *Empty* )))

To copy a position, share the board and copy the stones:

 (defmethod copy ((p Position))
   (make-instance 'Position
 		 'Board (Board p)
 		 'Stones (copy-seq (Stones p))))

A few utilities:

 (defmethod all-points ((p Position))
   (all-points (Board p)))
 (defmethod at ((b Position) (p Point))
   (aref (Stones b) (index p)))
 (defmethod (setf at) ((s Stone) (b Position) (p Point))
   (setf (aref (Stones b) (index p)) s))
 (defmethod empty? ((b Position) (p Point))
   (eq (at b p) *Empty*))
 (defmethod point ((b Position) l)
   (b-at (Board b) (car l) (cdr l)))

Make a new position by adding a stone to an existing position (note that this is just putting a stone down: we make no claims about the vaildity of play in a real game.) The old position is unaffected:

 (defmethod add ((b Position) (s Stone) (p Point))
   (let* ((nb (copy b)))
     (setf (at nb p) s)
     nb))

Add a stone at each point in l:

 (defmethod add-stones ((b Position) (s Stone) l)
   (if (null l)
       b
     (add (add-stones b s (cdr l)) s (point b (car l)))))

Make a position from a list of black stones and a list of white stones:

 (defun make-position (sz black white)
   (add-stones (add-stones (new-position sz) *White* white)
 	      *Black* black))

Print a Position in ASCII:

 (defmethod printb ((b Position))
   (let ((sz (Size (Board b))))
     (loop for r from 0 to (1- sz) do
 	  (loop for c from 0 to (1- sz) do
 		(format t "~C " (PChar (at b (b-at (Board b) r c))))
 		finally (format t "~%" )))))


This is a copy of the living page "The Code So Far - Position" at Sensei's Library.
(OC) 2004 the Authors, published under the OpenContent License V1.0.