我目前正在使用MINIMAX算法构建国际象棋AI。它最初在没有线程的情况下非常好,但最大深度只有5层,所以为了增加深度,我使用了多线程,但它陷入了一个无限循环,有人能帮助它工作吗?
代码:
void* best_move(void *_args){
struct forThread *args = (struct forThread *) _args;
for(int i=0; i<8; ++i){
for(int j=0; j<8; ++j){
args->res += args->state_score[i][j];
printf("%c", state[i][j]);
}
printf("n");
}
if(args->depth == 5){
//printf("Return value in the last node = %lldn", current_score);
pthread_exit((void*)(args->res));
}
// TURN OF THE PLAYER THAT MAXIMIZES THE SCORE :
int mid[] = {0, 7, 1, 6, 2, 5, 3, 4};
if(args->depth % 2 == 0){
//printf("Reached the maximizing playern");
int ans = -1e8;
for(int i=0; i<8; ++i){
for(int j=0; j<8; ++j){
if(isUpper(args->state[i][mid[j]]) && args->state_score[i][mid[j]]>0){
vector<pair<int, int>> possible_moves = get_moves(i, mid[j], args->state);
if(possible_moves.size() == 0) continue;
for(auto u : possible_moves){
int x = u.first, y = u.second;
int removed_points = args->state_score[x][y];
char state_copy[8][9];
int state_score_copy[8][8];
for(int k=0; k<8; ++k){
for(int io=0; io<9; ++io){
if(io!=8) state_score_copy[k][io] = args->state_score[k][io];
state_copy[k][io] = args->state[k][io];
}
}
int new_score = args->res - args->state_score[x][y];
if(args->depth >= 3 && new_score == args->start_score) continue;
state_copy[x][y] = state_copy[i][mid[j]];
state_copy[i][mid[j]] = '_';
state_score_copy[x][y] = state_score_copy[i][mid[j]];
state_score_copy[i][mid[j]] = 0;
struct forThread *arg = (forThread*) malloc (sizeof (struct forThread));
arg->depth = args->depth + 1;
arg->depth = args->start_score;
for(int k=0; k<8; ++k){
for(int io=0; io<9; ++io){
if(io!=8) arg->state_score[k][io] = args->state_score[k][io];
arg->state[k][io] = args->state[k][io];
}
}
arg->alpha = args->alpha;
arg->beta = args->beta;
arg->res = -1;
pthread_t id;
pthread_create(&id, NULL, best_move, arg);
void *result = NULL;
//long long res = best_move(depth+1, state_copy, state_score_copy, alpha, beta);
pthread_join(id, &result);
ans = min(ans, (int)result);
args->res = ans;
//long long res = best_move(depth+1, state_copy, state_score_copy, alpha, beta);
//printf("%lldn", res);
args->alpha = max(args->alpha, ans);
if(args->beta <= args->alpha) break;
if(args->depth == 0 && ans == args->res){
//printf("got the answer = %dn", ans);
moveX = i;
moveY = mid[j];
toX = x;
toY = y;
}
}
}
}
}
pthread_exit((void*)(args->res));
}
else{
int ans = +1e8;
//printf("Reached the minimizing playern");
for(int i=0; i<8; ++i){
for(int j=0; j<8; ++j){
if(isLower(args->state[i][mid[j]]) && args->state_score[i][mid[j]]<0){
vector<pair<int, int>> possible_moves = get_moves(i, mid[j], args->state);
if(possible_moves.size() == 0) continue;
for(auto u : possible_moves){
int x = u.first, y = u.second;
int removed_points = args->state_score[x][y];
char state_copy[8][9];
int state_score_copy[8][8];
for(int k=0; k<8; ++k){
for(int io=0; io<9; ++io){
if(io!=9) state_score_copy[k][io] = args->state_score[k][io];
state_copy[k][io] = args->state[k][io];
}
}
int new_score = args->res - args->state_score[x][y];
if(args->depth >= 3 && new_score == args->start_score) continue;
state_copy[x][y] = state_copy[i][mid[j]];
state_copy[i][mid[j]] = '_';
state_score_copy[x][y] = state_score_copy[i][mid[j]];
state_score_copy[i][mid[j]] = 0;
struct forThread *arg = (forThread*) malloc (sizeof (struct forThread));
arg->depth = args->depth + 1;
arg->depth = args->start_score;
for(int k=0; k<8; ++k){
for(int io=0; io<9; ++io){
if(io!=8) arg->state_score[k][io] = args->state_score[k][io];
arg->state[k][io] = args->state[k][io];
}
}
arg->alpha = args->alpha;
arg->beta = args->beta;
arg->res = -1;
pthread_t id;
pthread_create(&id, NULL, best_move, arg);
void *result = NULL;
//long long res = best_move(depth+1, state_copy, state_score_copy, alpha, beta);
pthread_join(id, &result);
ans = min(ans, (int)result);
args->res = ans;
args->beta = min(ans, args->beta);
if(args->beta <= args->alpha) break;
}
}
}
}
pthread_exit((void*)(args->res));
}
}
这可能是罪魁祸首:
arg->depth = args->depth + 1;
arg->depth = args->start_score;
您希望在代码的第二行中将depth
更改为start_score
两次。
您还必须free()
分配的参数,或者使用更具C++风格的内存管理,因为您的程序现在存在内存泄漏。