我试图用Haskell编写一个Tic Tac Toe程序,使用minimax算法。我构建了自己的"Rose a“数据类型如下:
data Rose a = a :> [Rose a]这是我想要“存储”我的minimax树的数据类型。我理解minimax算法是如何工作的,但似乎无法在递归函数中实现它。
minimax :: Player -> Rose Board -> Rose Int
minimax p (r :> []) | hasWinner r == Just p = 1 :> []
| hasWinner r == Just (nextPlayer p) = (-1) :> []
| otherwise = 0 :> []
minimax p (r :> rs) = maximum(map root xs) :> xs
where xs = map (minimax' (nextPlayer p)) rs
minimax' :: Player -> Rose Board -> Rose Int
minimax' p b@(r :> []) = minimax p b
minimax' p (r :> rs) = minimum(map root xs) :> xs
where xs = map (minimax p) rs"Player“也是一种自构造的数据类型,可以是值P1或P2。"hasWinner“函数检查给定的”板“(数据类型可以容纳Tic脚趾板)是否有胜利者,并返回可能是P1或P2,或者什么都没有。
我写的这个"minimax“函数没有给出错误,但是结果不正确。我的minimax实现中的缺陷在哪里?
发布于 2016-09-30 19:30:25
你不能正确地在两位玩家之间切换。minimax' p b@(r :> []) = minimax p b错了。map (minimax p) rs in minimax'不会切换到另一个玩家以获得最大化的一半。
我建议为P1 (试图最大化)和P2 (试图最小化)显式地写出这篇文章。
最终游戏可以指定胜利者,而不关心哪个玩家正在玩。
minimax :: Player -> Rose Board -> Rose Int
minimax p (r :> []) | hasWinner r == Just P1 = 1 :> []
| hasWinner r == Just P2 = (-1) :> []
| otherwise = 0 :> []玩家P1正试图最大化
minimax P1 (r :> rs) = maximum (map root xs) :> xs
where xs = map (minimax (nextPlayer p)) rs播放器P2正在尝试最小化
minimax P2 (r :> rs) = minimum (map root xs) :> xs
where xs = map (minimax (nextPlayer p)) rs发布于 2016-10-01 14:45:08
经过大量的测试和困惑,我发现创建游戏树的功能有一些缺陷。经过调试,Cirdec提出的算法工作正常!
https://stackoverflow.com/questions/39798342
复制相似问题