【问题标题】:Minimax in haskellHaskell中的极小极大
【发布时间】:2016-11-07 07:21:38
【问题描述】:

我正在尝试为 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...而且我意识到如果我的代码编译,它不会是递归。谁能教我怎么做?

【问题讨论】:

  • 我会尝试使 minimax 成为递归函数。即在 minimax 上映射而不评估。

标签: haskell minimax


【解决方案1】:

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

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

在递归过程中,Max player 与 Min player 交替出现。

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

类似:

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

minimax 也应该在所有可能的棋盘上调用自己。然后根据isMax 参数应用maximumminimum

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

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

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

【讨论】:

  • 谢谢。还有一个问题,如何限制最大深度?
  • @kkkjjj 你在每次递归调用时递减depth 参数,当你到达0时不再调用你的函数
猜你喜欢
  • 2017-02-09
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多