首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MiniMax搜索递归不正确

MiniMax搜索递归不正确
EN

Stack Overflow用户
提问于 2016-10-16 12:56:19
回答 1查看 70关注 0票数 0

我正在尝试写一个AI来玩ConnectK (一个连接4的游戏,需要连接k个棋子,重力可以开或关)。这是我的函数,使用Minimax算法获得最佳移动。

代码语言:javascript
复制
struct MoveNode //This struct is defined in header file
{
    MoveNode() {};
    MoveNode(int Score) : score(Score) {}
    Move move;
    int score;
};

MoveNode AIShell::getBestMove(int depth, int player) {//Find the best move using MiniMax
    if (depth <= 0) 
        return MoveNode(heuristic());
    else if (boardIsFull() && getWinner() == 0)//Tie
        return 0;
    else if (getWinner() == AI_PIECE)
        return 100000;
    else if (getWinner() == HUMAN_PIECE)
        return -100000;

    std::vector<MoveNode> mds;

    for (auto i : getMoveList()) {//For each available move
        MoveNode md;
        md.move = i; //i is Move(col,row)
        gameState[i.col][i.row] = player;
        if (player == AI_PIECE) {
            md.score = getBestMove(depth - 1, HUMAN_PIECE).score;
        }
        else {
            md.score = getBestMove(depth - 1, AI_PIECE).score;
        }
        mds.push_back(md);
    }

    //Get the best move after recursion
    int best_move_index = 0;
    if (player == AI_PIECE) {
        int best_score = -1000000;
        for (int i = 0; i < mds.size(); i++) {
            if (mds[i].score > best_score) {
                best_move_index = i;
                best_score = mds[i].score;
            }
        }
    } else if (player == HUMAN_PIECE) {
        int best_score = 1000000;
        for (int i = 0; i < mds.size(); i++) {
            if (mds[i].score < best_score) {
                best_move_index = i;
                best_score = mds[i].score;
            }
        }
    }
    return mds[best_move_index];
} 

getBestMove()函数似乎做了一些我不太期望的事情。该函数将尝试在递归之前获得最佳移动,并且AI转弯和人类转弯并未以递归方式均匀处理。我花了相当多的时间调试这个函数,但仍然不能弄清楚。很抱歉我的英语很差,但我真的很感激在这里能帮上忙。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-10-16 20:30:44

您返回的是最佳移动索引。极小极大树必须返回节点值(best_score),而不是最佳移动。您应该返回最佳移动的唯一时间是在根节点,这样搜索就会通过给出最佳移动而终止。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40067002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档