正确使AI循环正确
Making AI Loop Properly?
我正在为猜测游戏制作AI,并且我遇到了一个我似乎无法自己解决的问题。目的是让用户输入一个数字,以便在合理的时间内猜测AI,我正在生成1-100之间的随机数,并通过循环运行它以调整较低或更高。
void AI::AIguess(int usernum)
{
srand(time(NULL));
AIchoice = rand() % High + Low;
// "too high" or "too low" accordingly
do {
if (AIchoice == usernum)
{
cout << AIchoice << " is this correct?" << endl;
}
else if (AIchoice <= usernum)
{
cout << AIchoice << " seems a little low.." << endl;
Low = AIchoice;
AIchoice = 0;
AIchoice = rand() % High + Low;
AIguesses++;
}
else if (AIchoice >= usernum)
{
cout << AIchoice << " might have overshot a bit :/" << endl;
High = AIchoice;
AIchoice = 0;
AIchoice = rand() % High + Low;
AIguesses++;
}
} while (AIchoice != usernum);
}
我正在使用先前生成的数字作为下一个生成数字的参数,希望能找到用户号码。它在if语句之间弹跳并分别调整了高和低和低的,但是我面临的问题是在循环Aichoice几次之后开始添加超过100。任何人都可以帮助我?
p.s:任何有用的AI创建信息都非常感谢:)
在间隔代码中您的随机数是错误的。要生成min
和max
之间的数字,请执行(rand() % (max - min)) + min
。
所以更改 AIchoice = rand() % High + Low;
到 AIChoice = (rand() % (High - Low)) + Low;
。
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 正确使AI循环正确