int AI::AlphaBeta(int depth, int alpha, int beta) {
int val;
total++;
// 对方最后一子连五
if (CheckWin())
return -10000;
// 叶节点
if (depth == 0)
return evaluate();
int count = GetMove(depth);
// 遍历所有走法
for (int i = 1; i <= count; i++) {
MakeMove(s[depth][i].p);
val = -AlphaBeta(depth - 1, -beta, -alpha);
DelMove();
if (val >= beta) {
ABcut++;
return val;
}
if (val > alpha) {
alpha = val;
}
return alpha;
}
}
struct xy AI::gobang() {
int i;
clock_t start, finish;
start = clock();
cout << "电脑思考中......\n";
total = 0;
ABcut = 0;
srand(time(NULL));
// 第一步下中心点
if (step == 0) {
BestMove.x = size / 2;
BestMove.y = size / 2;
return BestMove;
}
// 第二,三步随机
if (step == 2 || step == 1) {
int rx, ry;
do {
rx = rand() % 5 + chessxy[1].x - 2;
ry = rand() % 5 + chessxy[1].y - 2;
} while (chessboard[rx][ry] != 0);
BestMove.x = rx;
BestMove.y = ry;
return BestMove;
}
// 迭代加深搜索
for (int i= 2; i <= SearchDepth; i += 2) {
int depth=i;
int val;
int alpha=-10001;
int beta=10000;
total++;
int count = GetMove(depth);
// 遍历所有走法
for (int j = 1; j<= count; j++) {
MakeMove(s[depth][j].p);
val = -AlphaBeta(depth - 1, -beta, -alpha);
DelMove();
if (val >= beta) {
BestVal = val;
BestMove = s[depth][j].p;
ABcut++;
break;
}
if (val > alpha) {
BestVal=alpha = val;
BestMove = s[depth][j].p;
}
}
if (BestVal == 10000)
break;
}
finish = clock();
ThinkTime = (double)(finish - start) / CLOCKS_PER_SEC;
return BestMove;
}

求助大神五子棋alphabeta的迭代加深算法(出现异常)
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-