You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell自定义Tree结构操作求助:四子棋Minimax算法实现

操作自定义Haskell树结构并实现Minimax算法示例

一、基础节点访问与树操作

你的Tree a = Node a [Tree a]是典型的多叉树结构,Haskell中操作这类代数数据类型核心是模式匹配,下面是常用操作的示例:

1. 访问节点值与子节点

通过模式匹配直接提取节点的存储值和子树列表:

-- 获取根节点的值
rootValue :: Tree a -> a
rootValue (Node val _) = val

-- 获取所有子节点
getChildren :: Tree a -> [Tree a]
getChildren (Node _ kids) = kids

用你定义的树t测试:

t :: Tree Int 
t = Node 0 [Node 1 [], Node 2 []]

-- 调用结果:rootValue t → 0
-- getChildren t → [Node 1 [], Node 2 []]

2. 遍历树结构

以前序遍历为例(先访问根节点,再递归遍历所有子树):

preorder :: Tree a -> [a]
preorder (Node val kids) = val : concatMap preorder kids

测试preorder t会得到[0,1,2],这能帮你遍历所有节点的存储值。

3. 修改树(纯函数式方式)

Haskell是纯函数式语言,修改树意味着生成新树而非原地修改,比如替换根节点值:

replaceRoot :: a -> Tree a -> Tree a
replaceRoot newVal (Node _ kids) = Node newVal kids

调用replaceRoot 5 t会得到Node 5 [Node 1 [], Node 2 []]。

二、针对四子棋的Minimax算法实现

结合你的需求(AI回合取极大值,玩家回合取极小值),我们可以基于这个树结构实现Minimax:

1. 核心Minimax函数

假设树的每个节点值是当前棋盘状态的评估得分(比如AI获胜得100,玩家获胜得-100,平局得0,中间局势按优势打分),我们通过递归判断当前层级是极大层(AI回合)还是极小层(玩家回合):

minimax :: Bool -> Tree Int -> Int
-- 叶子节点:已到游戏终止状态,直接返回评估分
minimax _ (Node val []) = val
-- 极大层(AI回合):取所有子节点得分的最大值
minimax True (Node _ kids) = maximum (map (minimax False) kids)
-- 极小层(玩家回合):取所有子节点得分的最小值
minimax False (Node _ kids) = minimum (map (minimax True) kids)

用你的测试树t验证:

  • AI回合调用minimax True t,会返回max(1,2)=2
  • 玩家回合调用minimax False t,会返回min(1,2)=1

2. 游戏树生成逻辑

要让Minimax真正工作,你需要根据四子棋规则生成游戏树:

-- 定义棋盘状态类型(可根据实际需求扩展)
type BoardState = [[Char]]  -- 用二维列表表示棋盘

-- 评估当前棋盘状态的得分
evaluate :: BoardState -> Int
evaluate board = 
    if isAIWin board then 100
    else if isHumanWin board then -100
    else if isDraw board then 0
    else heuristicScore board  -- 中间局势的启发式打分

-- 生成当前状态的所有可能下一步状态
generateNextStates :: BoardState -> Player -> [BoardState]
generateNextStates board player = ...  -- 实现四子棋落子逻辑,返回所有合法落子后的棋盘

-- 定义玩家类型
data Player = AI | Human
-- 递归生成游戏树
generateTree :: BoardState -> Player -> Tree Int
generateTree board player = 
    let score = evaluate board
        nextStates = generateNextStates board player
        -- 切换下一层的玩家
        nextPlayer = case player of AI -> Human; Human -> AI
        -- 递归生成子树
        subTrees = map (\s -> generateTree s nextPlayer) nextStates
    in Node score subTrees

3. 完整调用流程

当需要AI决策时,先生成当前棋盘的游戏树,再调用Minimax:

-- AI决策:传入当前棋盘,返回最优得分
aiDecide :: BoardState -> Int
aiDecide board = minimax True (generateTree board AI)

内容的提问来源于stack exchange,提问作者P47HF1ND3R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 22:57:33