j13159044063 于 2016.05.12 09:26 提问

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;
}

3个回答

qq423399099      2016.05.12 12:43

beifengche   2016.05.12 15:04

Lov_NinGx   2016.05.12 17:56

j13159044063 不太懂你说的分形几何，我的AI对这种简单的棋型是完全能堵的，它可是能在十几秒内搜索一亿个局面的AI啊