确定tic tac toe的最佳动作

时间:2015-01-20 22:45:21

标签: c recursion minimax

我想编写一个递归函数来确定给定抽动游戏中的最佳动作

int nextMove(struct Game g, enum Symbol player) {
if (game_over(g) != 0) {
return -1;
}
int i, j;
int score, end;
for(i = 0; i < 3; i += 1) {
    for (j = 0; j < 3; j += 1) {
        if(g.board.fields[i][j] == 0) {
            g.board.fields[i][j] = player;
            score = nextMove(g, -player);
        }
    }
}
end = game_over(g)
}

我的game_over功能:

enum Symbol game_over(struct Game g)
{
int x, y = x = 0;
int full = 0;

for (y = 0; y < SIZE_Y_AXIS; y += 1)
{
    for (x = 0; x < SIZE_X_AXIS; x += 1)
    {
        if (g.board.fields[x][y] == NONE)
            full++;
        else if (x < SIZE_X_AXIS - 2 &&
                 g.board.fields[x][y] == g.board.fields[x+1][y] &&
                 g.board.fields[x][y] == g.board.fields[x+2][y])
            return g.board.fields[x][y];
        else if (y < SIZE_Y_AXIS - 2 &&
                 g.board.fields[x][y] == g.board.fields[x][y+1] &&
                 g.board.fields[x][y] == g.board.fields[x][y+2])
            return g.board.fields[x][y];
        else if (x < SIZE_X_AXIS - 2 && y < SIZE_Y_AXIS - 2 &&
                 g.board.fields[x][y] == g.board.fields[x+1][y+1] &&
                 g.board.fields[x][y] == g.board.fields[x+2][y+2])
            return g.board.fields[x][y];
        else if (x >= 2 && y < SIZE_Y_AXIS - 2 &&
                 g.board.fields[x][y] == g.board.fields[x-1][y+1] &&
                 g.board.fields[x][y] == g.board.fields[x-2][y+2])
            return g.board.fields[x][y];
    }
}
if (full == 0)
    return FULL;
return NONE;
}

我认为叶子生产工作正常,但我不知道如何确定哪个叶子(或哪个移动)是最好的?现在有什么建议吗?

由于

1 个答案:

答案 0 :(得分:5)

在像tic-tac-toe这样的游戏中,搜索树非常小,以至于可以评估所有叶子节点(不像在象棋或Go这样的搜索被缩短的游戏中)。由于检查了所有节点,因此可以通过检查叶节点是赢,输还还是绘制来评估叶节点。您可以分别使用1,-1和0来表示这些值。完成后,将节点的值返回到其父节点。     对于非叶节点,它必须选择其子节点的最高值,或者最低值,具体取决于它是最大节点(计算机)还是最小节点(其对手)。这将一直备份树到根,为它所有可能的动作赋予它一个值。具有最高值的树根处的移动是该位置的最佳移动 这是minimax算法。此外,在您提供的代码示例中,您无法在所有字段都已满之前检查游戏是否已结束。相反,检查玩家是否已经连续三次,因为游戏已经结束。    注意:您的nextMove函数声称返回int但在大多数情况下不返回。这必须修复。 这是我将添加到您的代码(psuedo-code中添加的部分)。我不确定game_over函数到底是做什么的,所以我无法确定具体代码应该是什么。因此,我将把它拿出来并添加伪代码取代它。

int nextMove(struct Game g, enum Symbol player) {
    if (computerWon) {
    return 1;
    }
    if (OpponnentWon) {
    return -1;
    }
    if (allSquaresAreFull) {
    return 0;
    }
    int i, j;
    int bestScore;//use this to calculate which move to return
    int score, end;
    //now check whose turn it is
    if(player == 1){//I'm assuming this is the computer player
        bestScore = -100;//start with an arbitrary low value 
        for(i = 0; i < 3; i += 1) {
            for (j = 0; j < 3; j += 1) {
                if(g.board.fields[i][j] == 0) {
                    g.board.fields[i][j] = player;
                    score = nextMove(g, -player);
                    g.board.fields[i][j] = 0;//make sure to undo every move made in the search
                    if(score > bestScore){//a better move for this player
                         bestScore = score;//update bestScore
                    }
               }
           }
        }
        return bestScore;//return best move to parent;

    }
    if(player == -1){//I'm assuming this is the opponent
        bestScore = 100;//start with an arbitrary high value 
        for(i = 0; i < 3; i += 1) {
            for (j = 0; j < 3; j += 1) {
                if(g.board.fields[i][j] == 0) {
                    g.board.fields[i][j] = player;
                    score = nextMove(g, -player);
                    g.board.fields[i][j] = 0;//make sure to undo every move made in the search
                    if(score < bestScore){//a better move for the opponent is a lower move
                         bestScore = score;//update bestScore
                    }
               }
           }
        }
        return bestScore;//return best move to parent;

    }
end = game_over(g)//this variable is never used. Not sure what it does. I would remove it, it seems useless
}

这应返回位置的值(如果赢了,丢失​​或绘制)。要确定要采取的移动,必须稍加修改。添加测试以查看我们是否在第一次调用nextMove(即根)时,如果我们是,而不是跟踪ony bestMove,这是移动的值,还跟踪实际的最佳移动是什么(也许在一个移动结构中)。如果最佳移动,请返回。现在nextMove将返回采取的行动。