【发布时间】:2016-11-30 02:59:25
【问题描述】:
我正在尝试创建一个可以在 LISP 中解决游戏 Mastermind 的玩家。我尝试在辅助函数中使用以下嵌套循环
(defparameter *counter* 0)
;analyze the score to determine whether points need to be added to counter
(defun analyze (last-response)
(if (> (first last-response) 0)
(setf *counter* (+ (first last-response) *counter*))))
;helper function for finding correct color
(defun guessColor (length colors last-response)
(loop while (< *counter* 4) do
(loop for i from 1 to length
collect (first colors)
(setf colors (rest colors)))
(analyze (last-reponse))))
;baseline Holyguacamole player will guess all colors before trying all combinations of correct color
(defun HolyGuacamole (board colors SCSA last-response)
(declare (ignore SCSA))
;(print last-response)
(guessColor board colors last-response)
(print *counter*)
;(sortColor)
)
while 循环应该在全局变量*counter* 小于 4 时运行。内部循环应该根据所需的钉子长度(可变长度)猜测颜色。我一直遇到编译错误
在 (LOOP WHILE (COUNTER 4) ...) 的宏展开期间。使用 ;
BREAK-ON-SIGNALS 拦截。
我不熟悉 LISP,所以我不确定该错误代码的含义以及如何修复它。我觉得我用正确的括号正确地嵌套了它,但我实际上不确定它有什么问题。
Link 到 Mastermind 环境。
【问题讨论】:
-
整个程序有多大?您可以粘贴它还是将其缩减为一个独立的示例?我没有看到
score或analyze的定义或*counter*的声明 -
@GregoryNisbet 游戏本身的代码非常大。我编辑了我的辅助函数以及 counter 的声明。 score 更改为 last-response
-
我认为
(analyze (last-reponse))))应该是(analyze last-reponse)))...似乎还有其他一些错误...我无法使用clisp或@ 重现那个确切的编译器错误987654330@. -
我认为即使括号正确,我最大的问题还是循环本身。
-
错误可能是内层循环中的
(SETF...)引起的。COLLECT子句中只允许使用一种形式。你可能应该把它放在DO-clause 中。或者您可以使用POPinsted ofFIRST并删除SETF。我不确定您期望内部循环实现什么。它从COLORS中收集第一个LENGTH元素,但它的返回值只是被外循环丢弃。
标签: common-lisp