gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:03 25 4
gpt4 key购买 nike

我正在尝试表示 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代表其中的一个位置。例如,如果我将两 block (一 block 黑色和另一 block 白色)放在第一个位置,它看起来像这样:

(
(
((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 值的列表?

最佳答案

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

然后,您只需通过该接口(interface)操作您的数据,这样您的代码就具有可读性和易于维护性。我正在使用一个 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... .... .... .... >

关于list - 如何操作由列表构成的复杂数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38641323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com