如何操作由列表组成的复杂数据结构?

时间:2016-07-28 15:56:31

标签: list lisp common-lisp connect

我正在尝试代表3D Connect 4棋盘游戏:

BoardGameConnect4-3D

例如,我有以下列表结构列表:

(
(
(NIL NIL NIL NIL)
(NIL NIL NIL NIL)
(NIL NIL NIL NIL)
(NIL NIL NIL NIL)
)
(1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10)
)

每个NIL代表其中的一个位置。例如,如果我将两个部分(一个黑色和另一个白色)放在第一个位置,它将如下所示:

(
(
((B W) NIL NIL NIL)
(NIL NIL NIL NIL)
(NIL NIL NIL NIL)
(NIL NIL NIL NIL)
)
(1 1 1 1 1 1 1 1 1 1 1 1 10 10 10 10 10 10 10 10 10 10 10 10)
)

这意味着W将是底部的那个。我还需要将它们相互比较,以便程序说明何时找到胜利者。

如何在每个位置添加片段?我如何比较它们,因为它们是具有NIL值的列表?

2 个答案:

答案 0 :(得分:1)

原则上,如果您有列表的列表,则可以使用嵌套的nth调用来到达要检查的位置。在这个特定情况下,nth的线性时间性质可能并不可怕,但我可能会使用4x4列表或4x4x4阵列,尽管在这种情况下你最终需要自己跟踪“下一个位置”,即使这会简化“检查获胜条件”逻辑。

以及如何使用pushnth变异列表列表的示例(我已经编辑了 s 的方式,使其“数组”更容易看到):

* *s*

((NIL NIL NIL NIL) 
 (NIL NIL NIL NIL) 
 (NIL NIL NIL NIL) 
 (NIL NIL NIL NIL))
* (push 'w (nth 0 (nth 0 *s*)))

(W)
* *s*

(((W) NIL NIL NIL) 
 (NIL NIL NIL NIL) 
 (NIL NIL NIL NIL) 
 (NIL NIL NIL NIL))

答案 1 :(得分:1)

无论您选择使用哪种实现,最好定义一个操作对象的界面。在下面,我定义了make-boardpush-tokenpop-token函数。您可以定义其他访问者函数,例如在坐标(x y z)处获取值。

然后,您只能通过此界面操作数据,以便您的代码可读且易于维护。我正在使用2D矢量矩阵,其中内部矢量用作堆栈,这要归功于它们的填充指针(详见MAKE-ARRAY)。

董事会类和令牌类型

(defclass board ()
  ((matrix :reader board-matrix :initarg :matrix)
   (size :reader board-size :initarg :size)))

(deftype token-type () '(member white black))

构造

(defun make-board (size)
  (let ((board
         (make-array (list size size))))
    (dotimes (i (array-total-size board))
      (setf (row-major-aref board i)
            (make-array size
                        :element-type 'symbol
                        :fill-pointer 0)))
    (make-instance 'board :matrix board :size size)))

自定义打印机

(defmethod print-object ((b board) stream)
  (print-unreadable-object (b stream :type t)
    (let ((matrix (board-matrix b))
          (size (board-size b)))
      (dotimes (row size)
        (fresh-line)
        (dotimes (col size)
          (let* ((stack (aref matrix row col)))
            (dotimes (z size)
              (princ (case (aref stack z)
                       (white #\w)
                       (black #\b)
                       (t #\.))
                     stream)))
          (princ #\space stream))))))

按下(x,y)

(defun push-token (board x y token)
  (check-type token token-type)
  (vector-push token (aref (board-matrix board) y x)))

从(x,y)

弹出
(defun pop-token (board x y)
  (ignore-errors
    (let ((stack (aref (board-matrix board) y x)))
      (prog1 (vector-pop stack)
        ;; we reset the previous top-most place to NIL because we
        ;; want to allow the access of any cell in the 3D
        ;; board. The fill-pointer is just here to track the
        ;; position of the highest token.
        (setf (aref stack (fill-pointer stack)) nil)))))

测试

(let ((board (make-board 4)))
  (flet ((@ (&rest args) (print board)))
    (print board)
    (@ (push-token board 1 2 'white))
    (@ (push-token board 1 2 'black))
    (@ (push-token board 1 2 'white))
    (@ (push-token board 1 2 'black))
    (@ (push-token board 1 2 'black))
    (@ (push-token board 0 3 'white))
    (@ (pop-token board 1 2))
    (@ (pop-token board 1 2))))

输出

#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... .... .... .... 
.... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... w... .... .... 
.... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wb.. .... .... 
.... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wbw. .... .... 
.... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wbwb .... .... 
.... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wbwb .... .... 
.... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wbwb .... .... 
w... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wbw. .... .... 
w... .... .... .... > 
#<BOARD 
.... .... .... .... 
.... .... .... .... 
.... wb.. .... .... 
w... .... .... .... >