C++ 井字游戏 AI

标签 c++ artificial-intelligence

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

  • 如果 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;
}

最佳答案

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

关于C++ 井字游戏 AI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27925608/

相关文章:

c++ - 试图将复杂数组存储到类 C++ 中

c++ - 格式错误的 C 或 C++ 程序是否保证在 Visual Studio 中的 DEBUG 配置下崩溃

c++ - 为什么基准测试 mod 运算符 (%) 经常显示 0 时间,即使是 5,000 轮?

machine-learning - 人脸检测和裁剪

php - 如何根据 'similar' 和 'title' 列在 MySQL 表中查找 'description' 记录?

c++ - `enable_if` 使编译器难以生成可读的错误消息呢?

c++ - 量化图像

algorithm - 第 2 部分弹性反向传播神经网络

machine-learning - WEKA 中潜在语义分析的可扩展性

java - Java 中的马尔可夫模型决策过程