【发布时间】:2018-09-28 17:24:12
【问题描述】:
我一直在努力在 Haskell 中创建一个极小极大函数,但在 SO 上找不到任何好的替代方案,因此我问:
我想创建以下函数:
minimax :: Player -> Rose Board -> Rose Int
我想要一棵整数玫瑰树,它们必须是 1、0 或 -1(对于轮到的玩家来说,移动可以是好的、中性的或坏的。
(root :> leaves) -- constructor of a Rose
(board :> boards) -- constructor of a Rose Board
(Int :> Ints) -- constructor of a Rose Int
我写了函数 hasWinner, minimum' 和 maximum' 让事情变得更容易:
hasWinner :: Board -> Maybe Player
minimum' :: [Int] -> Int
minimum' (x:xs) | x == -1 = -1
| otherwise = minimum' xs
maximum' :: [Int] -> Int
maximum' (x:xs) | x == 1 = 1
| otherwise = maximum' xs
此外,我认为我的基本情况如下:
minimax player (board :> []) = (0 :> [])
目前这是我所在的位置:
minimax player (board :> boards)| maximum' [(isWinner player b (minimax' player)) | (b :> bs) <- boards] == 1 = _
| minimum' [(isWinner player b (minimax' player)) | (b :> bs) <- boards] == -1 = _
| otherwise = _
where minimax' player | player == P1 = P2
| otherwise = P1
isWinner p1 board p2 | hasWinner board == Just p1 = 1
| hasWinner board == Just p2 = -1
| otherwise = 0
我似乎无法弄清楚当我找到一个赢了、输了或没有任何后果的棋盘时会发生什么。那么如何使用正确的 Rose Board 再次调用 minimax 函数呢?
非常感谢任何帮助!
拉蒙
【问题讨论】: