2008-06-03 16:08:30 +00:00
|
|
|
(in-package :shape-board)
|
|
|
|
|
|
|
|
(defclass shape-board (basic-board)
|
2008-06-18 00:46:32 +00:00
|
|
|
((shape-board
|
|
|
|
:initform nil
|
|
|
|
:accessor shape-board)
|
|
|
|
(shape-sizes
|
|
|
|
:initform nil
|
|
|
|
:accessor shape-sizes)
|
|
|
|
(shapes-points
|
|
|
|
:initform nil
|
|
|
|
:accessor shapes-points)
|
|
|
|
(next-shape-id
|
|
|
|
:initform 0
|
|
|
|
:accessor next-shape-id)))
|
2008-06-03 16:08:30 +00:00
|
|
|
|
2008-06-18 00:46:32 +00:00
|
|
|
(defmethod initialize-instance :after ((board shape-board) &key from-board)
|
|
|
|
(if (eql from-board nil)
|
|
|
|
(progn
|
|
|
|
(setf (shape-board board) (make-2d-board (boardsize board) nil))
|
2008-06-19 06:16:51 +00:00
|
|
|
(setf (shape-sizes board) (make-array 1 :fill-pointer 0 :adjustable t))
|
|
|
|
(setf (shapes-points board) (make-array 1 :fill-pointer 0 :adjustable t)))
|
2008-06-18 00:46:32 +00:00
|
|
|
(progn
|
|
|
|
(setf (shape-board board) (copy-2d-board (shape-board from-board)))
|
|
|
|
(setf (shape-sizes board) (copy-array (shape-sizes from-board)))
|
2008-06-19 06:16:51 +00:00
|
|
|
(setf (shapes-points board) (copy-2d-array (shapes-points from-board)))
|
|
|
|
(copy-slots (next-shape-id) board from-board))))
|
|
|
|
|
|
|
|
(defmacro shape-id (board coords)
|
|
|
|
`(get-2d-stone (shape-board ,board) ,coords))
|
2008-06-18 00:46:32 +00:00
|
|
|
|
2008-06-19 06:16:51 +00:00
|
|
|
(defun add-shape (board coords)
|
2008-06-18 00:46:32 +00:00
|
|
|
(set-2d-stone (shape-board board) coords (next-shape-id board))
|
|
|
|
(vector-push-extend 1 (shape-sizes board))
|
2008-06-19 06:16:51 +00:00
|
|
|
(vector-push-extend (make-array 1 :fill-pointer 0 :adjustable t) (shapes-points board))
|
|
|
|
(vector-push-extend coords (aref (shapes-points board) (next-shape-id board)))
|
2008-06-18 00:46:32 +00:00
|
|
|
(incf (next-shape-id board)))
|
|
|
|
|
2008-06-19 06:16:51 +00:00
|
|
|
(defun add-to-shape (board coords shape-id)
|
2008-06-18 00:46:32 +00:00
|
|
|
(set-2d-stone (shape-board board) coords shape-id)
|
2008-06-19 06:16:51 +00:00
|
|
|
(vector-push-extend coords (aref (shapes-points board) shape-id))
|
2008-06-18 00:46:32 +00:00
|
|
|
(incf (aref (shape-sizes board) shape-id)))
|
|
|
|
|
2008-06-24 17:45:26 +00:00
|
|
|
(defmacro shape-size (board shape-id)
|
2008-06-19 06:16:51 +00:00
|
|
|
`(aref (shape-sizes ,board) ,shape-id))
|
|
|
|
|
2008-06-21 06:03:06 +00:00
|
|
|
(defgeneric convert-shape (board shape-id to-id))
|
|
|
|
|
|
|
|
(defmethod convert-shape ((board shape-board) shape-id to-id)
|
2008-06-19 06:16:51 +00:00
|
|
|
; (format t "convert-shape ~a to ~a~%" shape-id to-id)
|
|
|
|
(loop for index from 0 to (1- (length (aref (shapes-points board) shape-id))) do
|
|
|
|
(add-to-shape board (aref (aref (shapes-points board) shape-id) index ) to-id))
|
|
|
|
(setf (aref (shapes-points board) shape-id) (make-array 1 :fill-pointer 0 :adjustable t))
|
|
|
|
(setf (aref (shape-sizes board) shape-id) 0))
|
|
|
|
|
2008-06-21 06:03:06 +00:00
|
|
|
(defgeneric join-shapes (board nexus shapes-list))
|
2008-06-18 00:46:32 +00:00
|
|
|
|
2008-06-21 06:03:06 +00:00
|
|
|
(defmethod join-shapes ((board shape-board) nexus shapes-list)
|
2008-06-19 06:16:51 +00:00
|
|
|
(let ((biggest-shape (first shapes-list)))
|
|
|
|
(loop for shape-id in shapes-list do
|
2008-06-24 17:45:26 +00:00
|
|
|
(if (> (shape-size board shape-id) (shape-size board biggest-shape))
|
2008-06-18 00:46:32 +00:00
|
|
|
(setf biggest-shape shape-id)))
|
|
|
|
|
2008-06-19 06:16:51 +00:00
|
|
|
(loop for shape-id in shapes-list do
|
2008-06-18 00:46:32 +00:00
|
|
|
(if (not (= shape-id biggest-shape))
|
|
|
|
(convert-shape board shape-id biggest-shape)))
|
|
|
|
(add-to-shape board nexus biggest-shape)))
|
|
|
|
|
|
|
|
|
|
|
|
(defmethod set-stone :after ((board shape-board) coords val)
|
|
|
|
(let ((alist nil))
|
|
|
|
(do-over-adjacent (coords-var board coords)
|
2008-06-19 06:16:51 +00:00
|
|
|
(if (eql val (get-stone board coords-var))
|
|
|
|
(push (get-2d-stone (shape-board board) coords-var) alist)))
|
|
|
|
(if (eql alist nil)
|
|
|
|
(add-shape board coords)
|
|
|
|
(if (eql (cdr alist) nil) ; one item
|
|
|
|
(add-to-shape board coords (car alist))
|
|
|
|
(join-shapes board coords alist)))))
|
2008-06-18 00:46:32 +00:00
|
|
|
|
2008-06-19 06:16:51 +00:00
|
|
|
;(defun shape-to-analyze ())
|
2008-06-18 00:46:32 +00:00
|
|
|
|
2008-06-19 06:16:51 +00:00
|
|
|
(defun shapes-to-analyze (board)
|
|
|
|
(concatenate 'string (board-to-analyze (shape-board board))
|
|
|
|
'(#\newline) " TEXT next-shape-id: " (write-to-string (next-shape-id board)) " length(shapes-points): " (write-to-string (length (shapes-points board)))))
|
|
|
|
|