Minimax 算法未按预期工作
Minimax algorithm not working as expected
我正在为一个棋盘游戏构建 AI,使用 Realm of Racket book as a basis. So far, everything has gone well. However, when I try to run my minimax
函数中给出的树根上的存根,它 returns 一个最低的列表您可以通过 运行 它获得的可能值(以任一玩家为谓词)。
这是函数的代码转储:
(define (minimax tree player depth)
(define (generate-score tree player depth)
(define won (winner (ttt-tree-board tree)))
(if (<= depth 0) 0
(+ (cond
[(equal? won player) 1]
[(false? won) 0]
[else -1])
(apply +
(for/list ([t (ttt-tree-moves tree)])
(generate-score (second t) player (sub1 depth)))))))
(for/list ([t (ttt-tree-moves tree)])
(generate-score (second t) player depth)))
这只是我的minimax
功能,因为其他的none(winner
除外)需要显示。这里是 winner
:
(define (winner board)
(or
(ormap (lambda (row) (if (all-equal? row) (first row) #f)) board) ; Horizontal
(ormap (lambda (i) (if ; Vertical
(all-equal? (map (lambda (j) (list-ref (list-ref board j) i)) (stream->list (in-range (length board)))))
(list-ref (first board) i) #f))
(stream->list (in-range (length board))))
(if ; Diagonal
(all-equal? (map (lambda (i) (list-ref (list-ref board i) i)) (stream->list (in-range (length board)))))
(first (first board)) #f)
(if ; Diagonal cont.
(all-equal? (map (lambda (i) (list-ref (reverse (list-ref board i)) i)) (stream->list (in-range (length board)))))
(last (first board)) #f)))
(有点草率,所以如果有人有想法可以缩短它,请告诉我你的建议)
ttt-tree
结构遵循这种模式:
(ttt-tree board moves)
其中board
是一个二维数组,对应于棋盘(形式为'((X - O) (O X -) (- X O))
),moves
是一个可能的走法列表由该节点组成,每个节点都是一个包含两个元素的列表:更改的位置和构成下一个节点的 ttt-tree
。所以(second t)
指的是下一个节点,如果t
是(list-ref (ttt-tree-moves #<ttt-tree>) x)
我最初的想法是 cond
中可能存在一些错误,但我使用的是 equal?
而不是 eq?
,所以我不知道那会怎样一个问题。另一种可能是我定义 winner
错误,但我已经测试了很多次,所以我不知道哪里会出错。请帮忙!
事实证明,问题是在我的 winner
函数中,我从来没有检查获胜者是否是 "-"
,空方块。因此,在大多数情况下会触发,激活 else 子句并导致得分为 -1。
我正在为一个棋盘游戏构建 AI,使用 Realm of Racket book as a basis. So far, everything has gone well. However, when I try to run my minimax
函数中给出的树根上的存根,它 returns 一个最低的列表您可以通过 运行 它获得的可能值(以任一玩家为谓词)。
这是函数的代码转储:
(define (minimax tree player depth)
(define (generate-score tree player depth)
(define won (winner (ttt-tree-board tree)))
(if (<= depth 0) 0
(+ (cond
[(equal? won player) 1]
[(false? won) 0]
[else -1])
(apply +
(for/list ([t (ttt-tree-moves tree)])
(generate-score (second t) player (sub1 depth)))))))
(for/list ([t (ttt-tree-moves tree)])
(generate-score (second t) player depth)))
这只是我的minimax
功能,因为其他的none(winner
除外)需要显示。这里是 winner
:
(define (winner board)
(or
(ormap (lambda (row) (if (all-equal? row) (first row) #f)) board) ; Horizontal
(ormap (lambda (i) (if ; Vertical
(all-equal? (map (lambda (j) (list-ref (list-ref board j) i)) (stream->list (in-range (length board)))))
(list-ref (first board) i) #f))
(stream->list (in-range (length board))))
(if ; Diagonal
(all-equal? (map (lambda (i) (list-ref (list-ref board i) i)) (stream->list (in-range (length board)))))
(first (first board)) #f)
(if ; Diagonal cont.
(all-equal? (map (lambda (i) (list-ref (reverse (list-ref board i)) i)) (stream->list (in-range (length board)))))
(last (first board)) #f)))
(有点草率,所以如果有人有想法可以缩短它,请告诉我你的建议)
ttt-tree
结构遵循这种模式:
(ttt-tree board moves)
其中board
是一个二维数组,对应于棋盘(形式为'((X - O) (O X -) (- X O))
),moves
是一个可能的走法列表由该节点组成,每个节点都是一个包含两个元素的列表:更改的位置和构成下一个节点的 ttt-tree
。所以(second t)
指的是下一个节点,如果t
是(list-ref (ttt-tree-moves #<ttt-tree>) x)
我最初的想法是 cond
中可能存在一些错误,但我使用的是 equal?
而不是 eq?
,所以我不知道那会怎样一个问题。另一种可能是我定义 winner
错误,但我已经测试了很多次,所以我不知道哪里会出错。请帮忙!
事实证明,问题是在我的 winner
函数中,我从来没有检查获胜者是否是 "-"
,空方块。因此,在大多数情况下会触发,激活 else 子句并导致得分为 -1。