首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

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

2013-01-08 
在极大极小算法上增加剪枝功能朋友初入游戏开发,给了我一段代码,要求增加剪枝功能,我做普通应用开发,这种

在极大极小算法上增加剪枝功能
朋友初入游戏开发,给了我一段代码,要求增加剪枝功能,我做普通应用开发,这种算法接触的不多,最近也挺忙的,看的心慌头大。。希望朋友们帮下忙,谢谢
部分源码:


// 极小值极大值搜索函数
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[i], eatTable); // 走棋并吃子
if (eatCount != 0) { // 如果吃子成功
theDepth++;
value = Min(depth - 1); // 递归
undoOneMove(allMoves[i], eatCount - 1, eatTable); // 还原
theDepth--;
// 如果当前走棋方是白方,找一个评分最大的局面(对白方最有利)
if (value > bestValue) {
bestValue = value;
if (depth == search_deepth) { // 如果是根节点 保存最佳走法
bestMove = allMoves[i];
}
}
}
}

// 如果是杀棋,就根据距杀棋的步数给出评价
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[i], eatTable); // 走棋并吃子
if (eatCount != 0) { // 如果吃子成功
theDepth++;
value = Max(depth - 1); // 递归
undoOneMove(allMoves[i], eatCount - 1, eatTable); // 还原
theDepth--;
// 如果当前走棋方是黑,找一个评分最小的局面(对黑方最有利)
if (value < bestValue) {
bestValue = value;
if (depth == search_deepth) { // 如果是根节点 保存最佳走法
bestMove = allMoves[i];
}
}
}
}

// 如果是杀棋,就根据距杀棋的步数给出评价
if (bestValue == INFINITY_VALUE) {
return INFINITY_VALUE - theDepth;
}
return bestValue;
}


[解决办法]
极大极小值上面加个,alpha-beta 咯,很简单的,自己搜索一下,先理解原理再写就可以了。

热点排行