【问题标题】:AI is ignoring set parameters in Connect4AI 忽略 Connect4 中的设置参数
【发布时间】:2019-12-06 04:51:11
【问题描述】:

我目前完成了 90% 的 C++ Connect4 游戏,但我遇到了一个相当烦人的错误。游戏玩得很好,除了一件事外一切正常;玩家与计算机对战,但计算机忽略了我为它设置的参数,并将其棋子放在棋盘上的任何位置。这可能是我的逻辑错误吗?我知道这不是人工智能的例子,但它是总结它的最佳方式。 这是函数:

void columnChoiceComputer(int computer)
{
int number = 0;
srand(time(0));
cout << (rand() % 6 + 1) << endl;
if (cin.fail())
{
    cout << "Error!";
    srand(time(0));
    cout << (rand() % 6 + 1) << endl;
}
while ((rand() % 6 + 1) > WIDTH || (rand() % 6 + 1) <= 0)
{
    cout << "Please select a different column.";
    srand(time(0));
    cout << (rand() % 6 + 1) << endl;
}
while (boardMatrix[(HEIGHT - 1) - number][((rand() % 6 + 1) - 1)] != 0)
{
    number++;
    if (number > (HEIGHT - 1))
    {
        cout << "Please select a different column.";
        srand(time(0));
        cout << (rand() % 6 + 1) << endl;
        number = 0;
    }
};


boardMatrix[(HEIGHT - 1) - number][(rand() % 6 + 1) - 1] = computer;
lastMoveY = (HEIGHT - 1) - number;
lastMoveX = ((rand() % 6 + 1)) - 1;
(system("cls"));

}

【问题讨论】:

  • 您的问题的核心是rand() 每次通话都会获得一个新号码,因此使用(rand() % 6 + 1) 更新板的步骤与您之前的所有其他(rand() % 6 + 1)s 完全不同检查,可能会引起各种混乱。您对srand() 的使用也可能存在问题。你应该包括你的人工智能应该做什么,以便有人可以提供更明确的答案。
  • 感谢kmdreko的回复。 AI 的目标是简单地播放 connect4,它的播放方式与用户的播放方式相同,因为它正在选择要放置一块的列;但是,我让它对数字 1-6 使用随机数生成器,而不是像用户函数那样从键盘输入。

标签: c++ artificial-intelligence


【解决方案1】:

首先非常感谢帮助过我的人。解决方案非常简单,我不必每次调用函数时都调用 (rand() % 6 + 1),而只需将随机数的第一次迭代设置为一个名为“computerChoice”的变量,然后简单地调用它多变的。这是更新的代码:

int number = 0;
srand(time(0));
int computerChoice = (rand() % 6 + 1);

while (computerChoice > WIDTH || computerChoice <= 0)
{
    cout << "Please select a different column.";
    srand(time(0));
    cout << (rand() % 6 + 1) << endl;
}
while (boardMatrix[(HEIGHT - 1) - number][(computerChoice - 1)] != 0)
{
    number++;
    if (number > (HEIGHT - 1))
    {
        cout << "Please select a different column.";
        srand(time(0));
        cout << (rand() % 6 + 1) << endl;
        number = 0;
    }
};

boardMatrix[(HEIGHT - 1) - number][computerChoice - 1] = computer;
lastMoveY = (HEIGHT - 1) - number;
lastMoveX = (computerChoice) - 1;
(system("cls"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多