游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2197|回复: 1

在极大极小算法上增加剪枝功能

[复制链接]

1

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2012-2-23 22:30:00 | 显示全部楼层 |阅读模式
朋友初入游戏开发,给了我一段代码,要求增加剪枝功能,我做普通应用开发,这种算法接触的不多,最近也挺忙的,希望朋友们帮下忙,谢谢
代码是java的,不过影响不大呢。
部分源码:


// 极小值极大值搜索函数
    static int MinMaxSearch(int depth) {
        if (currentPlayer == BLACK)
            return Min(depth);
        else
            return Max(depth);
    }

    public static int Max(int depth) {
        int[] allMoves = new int[MAX_GEN_MOVES];
        int eatCount, value, bestValue = 0;
        int[] eatTable = new int[2];
        bestValue = -INFINITY_VALUE;
        if (depth <= 0) {
            return evaluatePosition();
        }
        int genCount = generateAllMoves(allMoves);
        for (int i = 0; i < genCount; i++) {
            eatCount = makeOneMove(allMoves, eatTable); // 走棋并吃子
            if (eatCount != 0) { // 如果吃子成功
                theDepth++;
                value = Min(depth - 1); // 递归
                undoOneMove(allMoves, eatCount - 1, eatTable); // 还原
                theDepth--;
                // 如果当前走棋方是白方,找一个评分最大的局面(对白方最有利)
                if (value > bestValue) {
                    bestValue = value;
                    if (depth == search_deepth) { // 如果是根节点 保存最佳走法
                        bestMove = allMoves;
                    }
                }
            }
        }
        
        // 如果是杀棋,就根据距杀棋的步数给出评价
        if (bestValue == -INFINITY_VALUE) {
            return theDepth - INFINITY_VALUE;
        }
        return bestValue;
    }

    public static int Min(int depth) {
        int[] allMoves = new int[MAX_GEN_MOVES];
        int eatCount, value, bestValue = 0;
        int[] eatTable = new int[2];
        bestValue = INFINITY_VALUE;
        if (depth <= 0) {
            return evaluatePosition();
        }
        int genCount = generateAllMoves(allMoves);
        for (int i = 0; i < genCount; i++) {
            eatCount = makeOneMove(allMoves, eatTable); // 走棋并吃子
            if (eatCount != 0) { // 如果吃子成功
                theDepth++;
                value = Max(depth - 1); // 递归
                undoOneMove(allMoves, eatCount - 1, eatTable); // 还原
                theDepth--;
                // 如果当前走棋方是黑,找一个评分最小的局面(对黑方最有利)
                if (value < bestValue) {
                    bestValue = value;
                    if (depth == search_deepth) { // 如果是根节点 保存最佳走法
                        bestMove = allMoves;
                    }
                }
            }
        }
        
        // 如果是杀棋,就根据距杀棋的步数给出评价
        if (bestValue == INFINITY_VALUE) {
            return INFINITY_VALUE - theDepth;
        }
        return bestValue;
    }

1

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2012-2-28 12:30:00 | 显示全部楼层

Re:在极大极小算法上增加剪枝功能

帮下忙吧
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-1 01:14

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表