【发布时间】: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