AI 忽略 Connect4 中的设置参数
AI is ignoring set parameters in Connect4
我目前完成了 90% 的 Connect4 游戏C++,但我遇到了一个相当烦人的错误。游戏玩得很好,除了一件事之外,一切都很好;玩家与计算机对战,但计算机忽略我为其设置的参数,并将其棋子放在棋盘上的任何位置。这可能是我的逻辑错误吗?我知道这不是人工智能的例子,但它是总结它的最佳方式。 这是函数:
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(( % 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"));
相关文章:
- C++ 命名参数习惯用语 - 未设置字符串属性
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- 在 c++11 中为 pthread 设置调度参数
- 将矢量设置为常规参数
- 如何设置默认参数以防用户不输入另一个参数
- 如何在Visual Studio 2017上将类方法设置为参数并将它们与lambda一起使用?
- AI 忽略 Connect4 中的设置参数
- 了解sigwait()及其如何设置参数
- 如何使用 QProcess 设置参数以运行类文件?
- 具有构造函数的新对象数组,需要在C++中设置参数
- 使用OPENCV的GSTREAMER如何为ICECAST2设置参数
- 为 QtWebEngineProcess 设置参数
- 在功能中设置参数
- SimpleBlobDetection opencv C++设置参数出现符号问题
- 谷歌模拟:在EXPECT_CALL中设置参数
- 编译时配置文件解析和设置参数
- 使用c++源代码在gnuplot中设置参数
- 为通过引用设置参数的函数命名的样式
- Gmock设置参数
- -设置参数X大小的默认initializer_list参数Y,其中X和Y是函数参数