微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

递归的C ++线程无法正常工作

如何解决递归的C ++线程无法正常工作

我目前正在使用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 = %lld\n",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 player\n");
        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,beta);
                        //printf("%lld\n",res);
                        args->alpha = max(args->alpha,ans);
                        if(args->beta <= args->alpha) break;
                        if(args->depth == 0 && ans == args->res){
                            //printf("got the answer = %d\n",ans);
                            moveX = i;
                            moveY = mid[j];
                            toX = x;
                            toY = y;
                        }
                    }
                }
            }
        }
        pthread_exit((void*)(args->res));
    }


    else{

        int ans = +1e8;
        //printf("Reached the minimizing player\n");
        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,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,arg);
                        void *result = NULL;
                        //long long res = best_move(depth+1,(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()分配的args或使用更具C ++风格的内存管理,因为您的程序现在存在内存泄漏。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。