【问题标题】:C++ Tic Tac Toe AIC++ 井字游戏 AI
【发布时间】:2015-03-11 14:43:29
【问题描述】:

我的井字游戏差不多完成了。目前它被设置为两人对人,但我知道我必须实现一个简单的人工智能才能获得批准。现在我需要你的帮助。我知道我必须分小步考虑它,例如三个“采取行动”方法,例如

  • 如果 AI 在第 1 列中移动 && 右侧的两个框打开,则在任一框中移动并返回 true
  • 如果 AI 在中间有一个移动 && 左右的盒子是打开的,在任何一个盒子里移动并返回 true
  • 如果 AI 在第 3 列中移动 && 左侧的两个框打开,则在任一框中移动并返回 true

我无法确切理解如何在下面的代码中实现它:

#include <iostream>
using namespace std;
char matrix[3][3] = { '7', '8', '9', '4', '5', '6', '1', '2', '3' };
char player = 'X';
int n;
void Draw()
{
    system("cls");
    cout << "Tic Tac Toe !\n" << endl;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}
void Input()
{
    int a;
    cout << "\nIt's " << player << " turn. " << "Press the number of the field: ";
    cin >> a;

    if (a == 7)
    {
        if (matrix[0][0] == '7')
            matrix[0][0] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 8)
    {
        if (matrix[0][1] == '8')
            matrix[0][1] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 9)
    {
        if (matrix[0][2] == '9')
            matrix[0][2] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 4)
    {
        if (matrix[1][0] == '4')
            matrix[1][0] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 5)
    {
        if (matrix[1][1] == '5')
            matrix[1][1] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 6)
    {
        if (matrix[1][2] == '6')
            matrix[1][2] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 1)
    {
        if (matrix[2][0] == '1')
            matrix[2][0] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 2)
    {
        if (matrix[2][1] == '2')
            matrix[2][1] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }
    else if (a == 3)
    {
        if (matrix[2][2] == '3')
            matrix[2][2] = player;
        else
        {
            cout << "Field is already in use try again!" << endl;
            Input();
        }
    }

}
void TogglePlayer()
{
    if (player == 'X')
        player = 'O';
    else
        player = 'X';
}
char Win()
{
    //first player
    if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
        return 'X';
    if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
        return 'X';

    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
        return 'X';
    if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
        return 'X';
    if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
        return 'X';

    if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
        return 'X';

    //second player
    if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
        return 'O';
    if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
        return 'O';

    if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
        return 'O';
    if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
        return 'O';
    if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
        return 'O';

    if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
        return 'O';

    return '/';
}

int main()
{
    n = 0;
    Draw();
    while (1)
    {
        n++;
        Input();
        Draw();
        if (Win() == 'X')
        {
            cout << "X wins!" << endl;
            break;
        }
        else if (Win() == 'O')
        {
            cout << "O wins!" << endl;
            break;
        }
        else if (Win() == '/' && n == 9)
        {
            cout << "It's a draw!" << endl;
            break;
        }
        TogglePlayer();
    }
    system("pause");
    return 0;
}

【问题讨论】:

  • 你真的需要学习 for 循环。
  • else if (a == 9){ if (matrix[0][2] == '9') matrix[0][2] = player; 有 9 个职位,而不是 100 个职位,您不是很幸运吗?您实际上已经在 9 个不同的地方复制了代码,唯一的区别是数字和几个矩阵索引。另外,你为什么要递归调用Input()??
  • 提示:如果您在每个方格中使用相同的占位符值,事情会变得更简单。

标签: c++ artificial-intelligence


【解决方案1】:

可以使用Minimax 算法来实现像井字游戏这样的简单棋盘游戏的计算机播放器,该算法可以使用α-β-pruning 进行改进。虽然最终的实现会非常小,但可能需要一些时间来理解。

【讨论】:

  • 虽然我认为 Minimax 和 a-b 修剪对于 OP 来说可能有点高级主题,但维基百科对事情的解释很糟糕。以下是对井字游戏应用 Minimax 的更好解释:neverstopbuilding.com/minimax
【解决方案2】:
    #include <iostream>
    #include <string>
    using namespace std;



    int main()
    {
/*
the chart is :
X 2 X
4 5 6
X 8 X
*/
        char Matrix[3][3] = { 'X','2','X','4','5','6','X','8','X' };
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cout << Matrix[i][j] << "     ";
            }
            cout << endl;
        }
/*
after this for :
X O X
4 5 6
X O X
*/
        int l = 0, m = 0, p[3] = { 0,0,0 };
        for (l; l <= 2; l++)
        {
            for (m; m <= 2; m++)
            {
                if ((Matrix[l][m]) == 'X')
                {
                    p[l]++;
                    if ((p[l]) == 2)
                    {
                        for (m; m >= 0; m--)
                        {
                            if ((Matrix[l][m]) != 'X')
                            {
                                Matrix[l][m] = 'O';
                            }
                        }
                    }
                }
            }
        }


        return 0;
    }
why this code dosent work?

【讨论】:

  • 代码解释有助于其他人理解您的代码。因此,如果对您要传达的内容进行一些解释,那将是一个更好的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多