gpt4 book ai didi

list - 检查对角线时出现逻辑错误 - nQueens

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

在用 values-list 解决了我的错误并能够运行我的程序直到结束后,我发现我的对角线检查似乎有逻辑错误。我的输入如下:

(威胁?'(1 3)'((1 0)(2 4)(3 0)(4 0)(5 0)(6 0)(7 0)(8 0)))

第一个参数是我们正在测试的棋盘空间是否可以放置女王,第二个参数是棋盘的状态,y 值 1-8 决定棋子的列位置,0 值将表示 x 值行将不包含任何部分。我的代码如下:

(defun diagonal(point1 point2)
(= (abs (- ( car point1 ) ( car point2 )))
(abs (- ( cadr point1 ) ( cadr point2 ))))
)

(defun THREAT?(x y)
; Checks threat on the vertical
(when (not (eq (values-list (cdr (nth (- (car x) 1 ) y )) ) '0 ) )
(return-from THREAT? t)
)
(loop for i from 0 to (list-length y)

; Checks threat on the horizontal
when (eq (values-list ( cdr x )) (values-list (cdr (nth i y))) )
do (return-from THREAT? t)
; With the help of the diagonal function checks along the diagonal
when (diagonal x (nth i y) )
do (return-from THREAT? t)
)
)

如果我的理解是正确的,我的程序应该循环遍历 y 的每个元素。它会将 x 和当前 y 对传递给对角线函数。对角线函数将减去两个和它们的绝对值并检查它们是否相等(如果它们是对角线那么它们应该是例如(1 2)和(2 3)是对角线因此|1 - 2| = 1和| 2 - 3| = 1).如果这些数字相等,则对角线函数应返回 true。相应的 when 语句应该只在它从对角线函数接收到 true 时激活,但它似乎总是返回 true,即使我给程序一个完全空白的板。如何修复对角线以正确确定棋盘上的威胁?非常感谢任何帮助!

最佳答案

我已将您的代码重写为更好的 Lisp 风格。

  • 更好的命名。
  • 具有有用名称的过程使注释变得多余
  • 单个程序更易于测试
  • 摆脱了 VALUES-LIST 废话
  • 去除所有 CAR、CDR、CADR。使用 FIRST 和 SECOND。
  • 为点的 x 和 y 分量引入了访问器
  • 用 RETURN-FROM 摆脱了奇怪的控制流,用简单的 OR 代替了它
  • 实际上直接遍历一个列表,而不是一直使用 NTH
  • EQ不是用来比较数字相等的,用=代替
  • 不要将括号单独放在一行中。
  • 正确缩进和格式化代码
  • 不要在括号之间放置空格
  • 在原子和左括号之间放置一个空格

代码:

(defun get-x (point)
(first point))

(defun get-y (point)
(second point))

(defun diagonal? (point1 point2)
(= (abs (- (get-x point1) (get-x point2)))
(abs (- (get-y point1) (get-y point2)))))

(defun vertical? (point)
(not (zerop (get-y point))))

(defun horizontal? (point1 point2)
(= (get-y point1)
(get-y point2)))

(defun threat? (point list-of-columns)
(or (vertical? (nth (1- (get-x point)) list-of-columns))
(loop for point2 in list-of-columns
when (or (horizontal? point point2)
(diagonal? point point2))
return t)))

示例

现在我们可以追踪三个威胁谓词:

? (trace vertical? diagonal? horizontal?)
NIL

现在你可以调用你的例子了:

? (threat? '(1 3) '((1 0) (2 4) (3 0) (4 0) (5 0) (6 0) (7 0) (8 0)))
0> Calling (VERTICAL? (1 0))
<0 VERTICAL? returned NIL
0> Calling (HORIZONTAL? (1 3) (1 0))
<0 HORIZONTAL? returned NIL
0> Calling (DIAGONAL? (1 3) (1 0))
<0 DIAGONAL? returned NIL
0> Calling (HORIZONTAL? (1 3) (2 4))
<0 HORIZONTAL? returned NIL
0> Calling (DIAGONAL? (1 3) (2 4))
<0 DIAGONAL? returned T
T

这应该有所帮助,以便您可以更好地调试代码...查看跟踪输出。

不使用空列描述的版本

(defun get-x (point)
(first point))

(defun get-y (point)
(second point))

(defun diagonal? (point1 point2)
(= (abs (- (get-x point1) (get-x point2)))
(abs (- (get-y point1) (get-y point2)))))

(defun vertical? (point list-of-columns)
(let ((point2 (find (get-x point) list-of-columns :key #'get-x)))
(and point2 (not (zerop (get-y point2))))))

(defun horizontal? (point1 point2)
(= (get-y point1)
(get-y point2)))

(defun threat? (point list-of-columns)
(or (vertical? point list-of-columns)
(loop for point2 in list-of-columns
when (or (horizontal? point point2)
(diagonal? point point2))
return t)))

(defun print-board (board)
(format t "~%+-+-+-+-+-+-+-+-+")
(dotimes (y 8)
(terpri)
(dotimes (x 8)
(format t "|~a" (if (member (list x y) board :test #'equal) "x" " ")))
(format t "|~%+-+-+-+-+-+-+-+-+")))

例子:

CL-USER 138 > (threat? '(1 2) '((2 4)))
NIL

CL-USER 139 > (print-board '((1 2) (2 4)))

+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| |x| | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | |x| | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
NIL

另一个例子:

CL-USER 140 > (threat? '(1 2) '((2 4) (4 5)))
T

CL-USER 141 > (print-board '((1 2) (2 4) (4 5)))

+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| |x| | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | |x| | | | | |
+-+-+-+-+-+-+-+-+
| | | | |x| | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
NIL

关于list - 检查对角线时出现逻辑错误 - nQueens,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39883917/

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