haskell中的极小极大

haskell中的极小极大,haskell,minimax,Haskell,Minimax,我正在尝试为connectfour游戏编写一个minimax函数,这是我未完成的代码 minimax:: RT Board ->Int minimax (Tree board subtrees) = case subtrees of [] >evaluate board (get_turn board) _

我正在尝试为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
player与
Min
player交替进行

因此,
minimax
功能应将棋盘、最大深度和玩家类型作为参数

比如:

minimax :: Board -> Int -> Bool -> Int
minimax board depth isMax = ...
minimax
也应该在移动生成的所有可能的板上调用自己。然后应用
最大值
最小值
,具体取决于
isMax
参数

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

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


作为补充说明,在从特定游戏中抽象时,添加一个函数作为参数可能会很有用,该函数用于确定棋盘是否代表一个已完成的游戏。

我会尝试将minimax作为一个递归函数。i、 e映射到极大极小值上,不求值。谢谢。还有一个问题,如何限制最大深度?@kkjjj在每次递归调用时减少
depth
参数,当达到
0