haskell 中的极小极大值

Minimax in haskell

我正在尝试为 connectfour 游戏编写一个 minimax 函数,这是我未完成的代码

minimax:: RT Board ->Int
minimax (Tree board subtrees) = case subtrees of 
    []                                                                  >evaluate board (get_turn board)  
    _                                                                  ->case get_turn board of 
            Player_X                                                   ->maximum (next_socres)  
            Player_O                                                   ->minimum  (next_socres) 
 where next_socres = map evaluate (map get_board subtrees) 

--get the node from sub trees
get_board:: RT Board -> Board
get_board (Tree board subtrees) = board

--evaluate moves(not finished)
evaluate :: Board -> Player -> Int
evaluate board me'   
    |[me,me,me,Blank] `isInfixOf_e` all_stone_lines                         = 10
    |[opp,opp,opp,Blank] `isInfixOf_e` all_stone_lines                      = -10
    |otherwise                                                              = 0 
    where
        me = get_stone_of me'
        opp = opponent' me
        all_stone_lines = combine_list stones_from_rows (combine_list     stones_from_cols (combine_list stones_from_left_dias                  stones_from_right_dias))   
        stones_from_rows = map (get_from_row board) [1..board_size] 
        stones_from_cols = map (get_from_column board) [1..board_size] 
        stones_from_left_dias = map (get_from_left_diagonal board) [-(board_size-1)..(board_size-1)]
        stones_from_right_dias = map (get_from_right_diagonal board) [2..(2*board_size)]  

我想在计算整棵树之前使用 map 评估每个子树,但我不知道如何在这里使用 map...而且我意识到如果我的代码被编译,它就不会是递归.谁能教我怎么做?

您的实现中有多个算法问题比 Haskell 多。

Minimax 是一种递归算法,通过评估从一个位置到特定深度(或游戏结束)的所有可能移动来建立分数。

在递归过程中,Max 玩家与 Min 玩家交替。

据此,minimax 函数应该将棋盘、最大深度和玩家类型作为参数。

类似于:

minimax :: Board -> Int -> Bool -> Int
minimax board depth isMax = ...

minimax 也应该在移动生成的所有可能板上调用自己。然后根据 isMax 参数应用 maximumminimum

另一件事是你正试图在树上递归。 文献中经常看到的树,无非是minimax函数的递归调用

换句话说,您不需要树作为参数,树是通过连续的 minimax 调用隐式构建的。

作为旁注,在从特定游戏中抽象出来时,添加一个函数作为参数可能很有用,它可以确定棋盘是否代表已完成的游戏。