井字游戏的这个最小最大值实现有什么问题?

What's wrong with this Minimax implementation for Tic-Tac-Toe?

本文关键字:实现 最大值 什么 问题 游戏      更新时间:2023-10-16

我想扩展我制作的较旧的tic tac toe游戏,两个玩家可以互相玩。我想为用户提供对抗困难AI的选择。问题是AI不会一直选择最佳的举动。例如,如果先行,它将始终选择点1。如果用户选择斑点2,它将选择4。在此之后,无论用户选择什么(除斑点7(此时游戏(,所以这不是问题。

任何帮助将不胜感激。谢谢!

我很肯定,问题是我的minimax或BestMove功能。可能只是我没有通过minimax函数正确实现,但是我找不到问题。

#include <iostream>
#include <iomanip>
#include <string>
#include <array>
// This is a program to play a single game of tic-tac-toe
// between either two human (non-AI) players or an AI.
using namespace std;
void PrintBoard(array <char, 9>);
int programprogress();
int checkwin(array <char, 9>);
int minimax(array <char, 9>, int, int, bool);
int bestMove(array <char, 9>, int);
int Opposite(int);
char PlayerSymbol(int);
const int SIZE = 9;
array <char, SIZE> Pos = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int player_number = 1;
int k = -11, result;
bool AI = false, first;
// Global variables used by 2 or more functions.
// Array had to be initialized with numbers instead of blank spaces
// because the check win function wouldn't work properly.
int main()
{
    string userinp;
    cout << "This is tic tac toe! Here's your board!" << endl;
    PrintBoard(Pos);
    cout << "Would you like to play versus an AI? (Y/N)" << endl;
    cin >> userinp;
    if (userinp[0] == 'Y')
    {
        cout << "Excellent! Would you like to start first, or second? (F/S)" << endl;
        cin >> userinp;
        if (userinp[0] == 'F')
        {
            cout << "You will start first!" << endl;
            first = false;
            player_number = 2;
        }
        else
        {
            cout << "The AI will start first!" << endl;
            first = true;
        }
        AI = true;
    }
    else
    {
        cout << "Excellent! Your game will start soon." << endl;
    }
    result = programprogress();
    player_number--;
    PrintBoard(Pos);
    if (result == 1)
        cout << endl << "Player " << player_number << " has won!!!n";
    else if (result == 10)
        cout << endl << "The AI has won! Better luck next time!n";
    else if (result = -10)
        cout << endl << "You beat the world's best AI! Congratulations!n";
    else
        cout << endl << "The game has been drawn!" << endl;
    return 0;
}
void PrintBoard(array <char, 9> Pos)
{
    system("cls");
    cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[0] << setw(3) << "|" << setw(3) << Pos[1] << setw(3) << "|" << setw(3) << Pos[2] << "     TIC           TOE" << endl;
    cout << "_____|_____|_____" << endl;
    cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[3] << setw(3) << "|" << setw(3) << Pos[4] << setw(3) << "|" << setw(3) << Pos[5] << "            TAC   " << endl;
    cout << "_____|_____|_____" << endl;
    cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[6] << setw(3) << "|" << setw(3) << Pos[7] << setw(3) << "|" << setw(3) << Pos[8] << "     TIC            TOE  " << endl;
    cout << "     |     |" << endl;
}

int programprogress()
{
    while (k == -11 && AI)
    {
        bool InvalidChoice = false;
        char letter;
        //player_number = (player_number % 2) ? 1 : 2;
        int PlayerChoice;
        if (player_number == 2)
        {
            cout << endl << "What is your move?" << endl;
            cin >> PlayerChoice;
            while ((PlayerChoice < 1) || (PlayerChoice > 9))
            {
                cout << "That's an invalid choice! Please choose a number that is 1-9!" << endl;
                cin >> PlayerChoice;
            }
            PlayerChoice--;
            letter = (!first) ? 'X' : 'O';
            if (Pos[PlayerChoice] == '1' || Pos[PlayerChoice] == '2' || Pos[PlayerChoice] == '3' || Pos[PlayerChoice] == '4' || Pos[PlayerChoice] == '5' || Pos[PlayerChoice] == '6' || Pos[PlayerChoice] == '7' || Pos[PlayerChoice] == '8' || Pos[PlayerChoice] == '9')
            {
                Pos[PlayerChoice] = letter;
                PrintBoard(Pos);
            }
            /*else
            {
                cout << "That space is already taken!" << endl;
                player_number--;
            }*/
            k = checkwin(Pos);
            if (k != -11)
                k = k * -10;
            player_number = 1;
        }
        else
        {
            cout << endl << "The computer has made its move!" << endl;
            letter = (first) ? 'X' : 'O';
            if (first)
                PlayerChoice = bestMove(Pos, 1);
            else
                PlayerChoice = bestMove(Pos, 2);
            Pos[PlayerChoice] = letter;
            PrintBoard(Pos);
            k = checkwin(Pos);
            if (k != -11)
                k = k * 10;
            player_number = 2;
        }
    }
    while (k == -11 && !AI)
    {
        bool InvalidChoice = false;
        char letter;
        player_number = (player_number % 2) ? 1 : 2;
        int PlayerChoice;
        cout << endl << "What's player " << player_number << "'s move?" << endl;
        cin >> PlayerChoice;
        while ((PlayerChoice < 1) || (PlayerChoice > 9))
        {
            cout << "That's an invalid choice! Please choose a number that is 1-9!" << endl;
            cin >> PlayerChoice;
        }
        PlayerChoice--;
        letter = (player_number == 1) ? 'X' : 'O';
        if (Pos[PlayerChoice] == '1' || Pos[PlayerChoice] == '2' || Pos[PlayerChoice] == '3' || Pos[PlayerChoice] == '4' || Pos[PlayerChoice] == '5' || Pos[PlayerChoice] == '6' || Pos[PlayerChoice] == '7' || Pos[PlayerChoice] == '8' || Pos[PlayerChoice] == '9')
        {
            Pos[PlayerChoice] = letter;
            PrintBoard(Pos);
        }
        else
        {
            cout << "That space is already taken!" << endl;
            player_number--;
        }
        k = checkwin(Pos);
        player_number++;
    }
    return k;
}
int checkwin(array <char, SIZE> Pos)
{
    if (Pos[0] == Pos[1] && Pos[1] == Pos[2])
        return 1;
    else if (Pos[3] == Pos[4] && Pos[4] == Pos[5])
        return 1;
    else if (Pos[6] == Pos[7] && Pos[7] == Pos[8])
        return 1;
    else if (Pos[0] == Pos[3] && Pos[3] == Pos[6])
        return 1;
    else if (Pos[1] == Pos[4] && Pos[4] == Pos[7])
        return 1;
    else if (Pos[2] == Pos[5] && Pos[5] == Pos[8])
        return 1;
    else if (Pos[0] == Pos[4] && Pos[4] == Pos[8])
        return 1;
    else if (Pos[2] == Pos[4] && Pos[4] == Pos[6])
        return 1;
    else if (Pos[0] != '1' && Pos[1] != '2' && Pos[2] != '3'
        && Pos[3] != '4' && Pos[4] != '5' && Pos[5] != '6'
        && Pos[6] != '7' && Pos[7] != '8' && Pos[8] != '9')
        return 0;
    else
        return -11;
}
int minimax(array <char, SIZE> newpos, int depth, int player, bool opp)
{
    int scale = 0;
    if ((player == 1 && first) || (player == 2 && !first))
        scale = 10;
    else
        scale = -10;
    //cout << scale;
    int score = scale*checkwin(newpos);
    if (score < 0)
        score += depth;
    else if (score > 0)
        score -= depth;
    if (score == -10 || score == 10 || score == 0)
        return score;
    if (opp)
    {
        int best = -1000;
        for (int i = 0; i < SIZE; i++)
        {
            if (newpos[i] != 'X' && newpos[i] != 'O')
            {
                char temp = newpos[i];
                newpos[i] = PlayerSymbol(player);
                best = max(best, minimax(newpos, depth + 1, Opposite(player), !opp));
                newpos[i] = temp;
            }
        }
        return best;
    }
    else
    {
        int best = 1000;
        for (int i = 0; i < SIZE; i++)
        {
            if (newpos[i] != 'X' && newpos[i] != 'O')
            {
                char temp = newpos[i];
                newpos[i] = PlayerSymbol(player);
                best = min(best, minimax(newpos, depth + 1, Opposite(player), !opp));
                newpos[i] = temp;
            }
        }
        return best;
    }
}
int bestMove(array <char, SIZE> newpos, int player)
{
    int best = -1000;
    int bestpos = -1;
    for (int i = 0; i < SIZE; i++)
    {
        if (newpos[i] != 'X' && newpos[i] != 'O')
        {
            char temp = newpos[i];
            newpos[i] = PlayerSymbol(player);
            int move = minimax(newpos, 0, player, !first);
            newpos[i] = temp;
            if (move > best)
            {
                //cout << "I like pineapple on pizza" << endl;
                bestpos = i;
                best = move;
            }
            /*if (move == best)
            {
                cout << "I like pineapple on pizza" << endl;
            }*/
        }
    }
    cout << bestpos;
    return bestpos;
}
int Opposite(int x)
{
    if (x == 1)
        return 2;
    else
        return 1;
}
char PlayerSymbol(int x)
{
    if (x == 1)
        return 'X';
    else
        return 'O';
}

由于bestPOS的-1值,由于界限出现了界限。不过,我不确定如何更改。

我可以找到4个问题。解决它们似乎导致了预期的行为。

首先,当您从bestMove函数内部调用minimax(newpos, 0, player, !first);时,您通过player而不是Opposite(player),这表明第一个minimax步骤将由与bestMove步骤相同的播放器执行。换句话说:AI自己做出了两个连续的举动。因此,player需要更改为Opposite(player)

其次,Minimax具有名为OPP的Bool变量,它似乎表明是AI还是其对手进行此举。对于第一个minimax步骤,OPP设置为!first,表明只有在AI首先进行时,对手才会在AI之后采取行动。那是不正确的。它总是在AI之后采取行动。因此,bestMove应使用true致电minimax,而不是!first。顺便说一句,opp是多余的,因为您可以使用(player == 1 && first) || (player == 2 && !first)检查是AI还是其对手进行移动。

第三,scale设置了错误的方式。使用(player == 1 && first) || (player == 2 && !first),您可以检查其AI是否进行移动。但是,在可能获胜的举动之后,您可以在下一个minimax步骤中这样做。因此,如果AI正在采取行动并且比赛已经赢得胜利,那么对手就取得了胜利,而不是AI。ergo,比例应为

if ((player == 1 && first) || (player == 2 && !first))
    scale = -10;
else
    scale = 10;

而不是。

最后,您在添加depth后检查score是10还是-10。如果depth不是0,则此检查将始终失败。因此,除了深度0之外,AI只能看到平局,从来没有赢。你可以写

if (score == -10 || score == 10 || score == 0)
{
    if (score < 0)
        score += depth;
    else if (score > 0)
        score -= depth;
    return score;
}

希望这充分回答您的问题。