Tic Tac Toe Minimax算法

时间:2014-07-11 19:48:31

标签: c algorithm

我正在尝试实施 minimax 算法,以便为计算机找到最佳移动以响应人为移动。我正在关注UCB的this视频。我需要一些关于算法的帮助。

//Data Structures used :

#define NO_OF_MAX_MOVES 9
char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
int bestPosition;

int getNextBestMove(int player)
{
int i, currentMoveScore;

// humanPlayer = 0;
// computerPlayer = 1;

if (isGameWon() == 1)
{
    return 1;   // computer has won
}
else if (isGameWon() == -1)
{
    return -1;  // human has won
}
else if (isGameDrawn())
{
    return 0;
}

int bestScore;
if(player == 0)
{
    bestScore = 2;  // for human
}
else
{
    bestScore = -2;  // for computer
}

for (i = 1; i <= 9; i++)
{
    if(board[i] == ' ')
    {
        if(player == 1)   //  computer is playing so maximize
        {
            board[i] = 'O';
            currentMoveScore = getNextBestMove(0);  // res stores the various tree leaf values

            board[i] = ' ';
            if(currentMoveScore > bestScore)
            {
                bestScore = currentMoveScore;
                bestPosition = i;
            }
        }

        else if (player == 0)  // player is playing so minimise
        {
            board[i] = 'X';
            currentMoveScore = getNextBestMove(1);
            board[i] = ' ';
            if (currentMoveScore > bestScore)
            {
                bestScore = currentMoveScore;
                bestPosition = i;
            }
        }
    }
}

return bestScore;
}

有一次,我将此方法称为 getNextBestMove(1); ,变量** bestPosition **应该为我提供下一个最佳移动的索引。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

问题是: -

对于最小玩家,你应该有if (currentMoveScore < bestScore)但是代码正在评估max而不是min。