我正在尝试创建一个 Mancala Lisp 中的游戏。它将有一个人工智能来与人类玩家对战,但我被困住了。我找不到将董事会表示为列表的方法;我心中的主要问题是如何移动代币。以下是 how to play mancala 的引用资料
我正在考虑一个循环列表,但我找不到任何关于如何在 Lisp 中做到这一点的明确文档。
抱歉我的语法问题;英语不是我的母语。
最佳答案
现在我还没有阅读规则(抱歉!),所以这只是为了解决使用循环数据结构的想法。
数据结构不一定是循环的。只要你假装是这样,它就会起作用! 阅读 mod function .
;; a1 a6 b1 b6
(defparameter *board* '(nil nil nil nil nil nil nil nil nil nil nil nil))
(defun wrap-position (pos)
(mod pos (length *board*)))
(defun push-token (position)
(push t (nth (wrap-position position) *board*)))
(defun pull-token (position)
(let ((contents (nth (wrap-position position) *board*)))
(setf (nth (wrap-position position) *board*) (rest contents))))
(defun print-board ()
(format t "| ~{~10<~a~>~} |~%| ~{~10<~a~>~} |" (reverse (subseq *board* 6))
(subseq *board* 0 6))
*board*)
现在,上面的技术是破坏性的。如果您还不知道 lisp 中的内容,请使用 google 或在 stackoveflow 上搜索,这里有一些很好的描述。这是值得研究的,因为您可能会发现您的人工智能想要“尝试”许多潜在的 Action ,同时“破坏”实际的游戏棋盘,非破坏性方法可以帮助实现这一点。这本非凡的书land of lisp有一些关于这方面的重要信息。
这是一个简单的使用示例
CL-USER> *board*
(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
CL-USER> (push-token 5)
(T)
CL-USER> *board*
(NIL NIL NIL NIL NIL (T) NIL NIL NIL NIL NIL NIL)
CL-USER> (push-token 5)
(T T)
CL-USER> *board*
(NIL NIL NIL NIL NIL (T T) NIL NIL NIL NIL NIL NIL)
CL-USER> (PULL-token 5)
(T)
CL-USER> *board*
(NIL NIL NIL NIL NIL (T) NIL NIL NIL NIL NIL NIL)
...I change the board before doing the next bit...
CL-USER> (print-board)
| NIL NIL NIL NIL NIL NIL |
| NIL NIL NIL NIL NIL (T T T T) |
现在看看 Sylwester 的答案,您会发现您可以仅用一些石头替换子列表。显然,您需要更改打印板,但这为您提供了一个非常简单的模型,您可以轻松操作(几乎可以是实现非破坏性所需的一大步)。尝试一下!
关于common-lisp - 如何在 Lisp 中抽象 Mancala 板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19558417/