我试图为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 .我意识到,如果我的代码编译,它将不是一个递归。有人能教我怎么做吗?
发布于 2016-11-07 22:42:49
您的实现中存在多个问题,这些问题比Haskell更多算法问题。
Minimax是一种递归算法,它通过评估从某个位置到一定深度(或游戏结束)的所有动作来建立一个分数。
在递归期间,Max player与Min player交替。
由此,minimax函数应该以板子、最大深度和播放器类型作为参数。
类似于:
minimax :: Board -> Int -> Bool -> Int
minimax board depth isMax = ...minimax也应该在由移动产生的所有可能的董事会中自居。然后应用maximum或minimum,这取决于isMax参数。
另一件事是,你试图在树上回溯。您在文献中经常看到的树只不过是minimax函数的递归调用。
换句话说,不需要树作为参数,树是通过连续的minimax调用隐式构建的。
作为一个边注,虽然抽象从一个特定的游戏,它可能是有用的添加作为一个参数,一个函数,以确定董事会是否代表一个完成的游戏。
https://stackoverflow.com/questions/40459769
复制相似问题