Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Haskell中创建函数minimax?_Haskell_Minimax - Fatal编程技术网

如何在Haskell中创建函数minimax?

如何在Haskell中创建函数minimax?,haskell,minimax,Haskell,Minimax,我一直在努力在Haskell中创建minimax函数,但找不到任何更好的替代方法,因此我问: 我想创建以下函数: minimax :: Player -> Rose Board -> Rose Int 我想得到一个由整数组成的玫瑰树,这些整数必须是1,0或-1(一个动作对轮到的玩家来说可以是好的,中性的,也可以是坏的) (root :> leaves) -- constructor of a Rose (board :> boards)

我一直在努力在Haskell中创建minimax函数,但找不到任何更好的替代方法,因此我问:

我想创建以下函数:

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 -> Rose Board -> Rose Int
minimax p (r :> [])   | hasWinner r == Just P1 = 1    :> []
                      | hasWinner r == Just P2 = (-1) :> []
                      | otherwise              = 0    :> []
minimax P1 (r :> rs) = maximum (map root xs) :> xs
    where xs = map (minimax (nextPlayer P1)) rs

minimax P2 (r :> rs) = minimum (map root xs) :> xs
    where xs = map (minimax (nextPlayer P2)) rs
目前我正处于这样的境地:

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

minimax播放器(board:>boards)| maximum'[(isWinner player b(minimax'player))|(b:>bs)bs)我发现另一篇帖子讨论了我在这里遇到的同样问题:

答案如下:

minimax player (board :> []) = (0 :> [])
minimax :: Player -> Rose Board -> Rose Int
minimax p (r :> [])   | hasWinner r == Just P1 = 1    :> []
                      | hasWinner r == Just P2 = (-1) :> []
                      | otherwise              = 0    :> []
minimax P1 (r :> rs) = maximum (map root xs) :> xs
    where xs = map (minimax (nextPlayer P1)) rs

minimax P2 (r :> rs) = minimum (map root xs) :> xs
    where xs = map (minimax (nextPlayer P2)) rs
我希望这至少能在将来帮助别人。 本回答中使用的功能包括:

root :: Rose a -> a
root (a :> bs) = a 

nextPlayer :: Player -> Player
nextPlayer P1 = P2
nextPlayer P2 = P1

hasWinner :: Board -> Maybe Player

minimum :: Ord a => [a] -> a
maximum :: Ord a => [a] -> a
干杯