使用srand(time(0))猜测一个范围内的数字
C++ Guessing a number within a range using srand(time(0))
我的问题是…我几乎完成了我的小游戏,但我想测试一下,看看它是否与"调试作弊"一起工作,看看程序是否实际上显示了正确的数字。
我设置了srand(time(0)),这样当你玩游戏时,程序显然会使每次的数字选择不同。但是,它显然一直在生成一个不同的数字,因为系统时间在变化。
正如您在代码中看到的,我有一个名为"ANSWER"的变量,又名Range(low,high)。检查名为"guessN"的用户输入变量,看它是否与"ANSWER"匹配。如果是这样,那么程序就会说用户赢了游戏,我相信这是可行的。
问题是,当我想让程序首先显示答案时,正好是47,当我输入47时,它实际上是错误的,因为时间生成器仍然在运行,它已经变成了37。
我需要的帮助:是否有一种方法可以当前暂停生成,直到用户输入他的答案?…
提前感谢你对我的帮助!:)屏幕截图:https://i.stack.imgur.com/CWAhw.png
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib> // For srand and rand
using namespace std;
/*Guessing Game: Program generates random #'s,
user has 3 chances to guess one number at a time,
otherwise the Computer wins.
*/
static unsigned int rangevalue;
void NewRange(int nLow, int nHigh) // Range for generated #'s.
{
rangevalue = (rand() % (nHigh - nLow + 1)) + nLow;
}
unsigned int getRange()
{
return rangevalue;
}
//DON'T GET LOST!
string DescRange; // Descriptive Range
string SelectionInput;
int low = 1;
int high;
//DON'T GET LOST!
void NumberGen()
{
do
{
cout << "Guess a number in between...n " << endl;
cout << "1.) 1-5tt[EASY]" << endl;
cout << "2.) 1-10t[MEDIUM]" << endl;
cout << "3.) 1-50t[HARD]" << endl;
cout << "4.) 1-100t[IMPOSSIBLE]" << endl;
cout << "n5.) Choose a custom range? *" << endl;
cout << "6.) EXIT n" << endl;
cout << "=> ";
getline(cin, SelectionInput);
} while (
SelectionInput != "1" && SelectionInput != "2" && SelectionInput != "3" && SelectionInput != "4" && SelectionInput != "5" && SelectionInput != "6"
&& SelectionInput != "EASY" && SelectionInput != "Easy" && SelectionInput != "easy"
&& SelectionInput != "MEDIUM" && SelectionInput != "Medium" && SelectionInput != "medium"
&& SelectionInput != "HARD" && SelectionInput != "Hard" && SelectionInput != "hard"
&& SelectionInput != "IMPOSSIBLE" && SelectionInput != "Impossible" && SelectionInput != "impossible"
&& SelectionInput != "CUSTOM" && SelectionInput != "Custom" && SelectionInput != "custom"
&& SelectionInput != "EXIT" && SelectionInput != "Exit" && SelectionInput != "exit"
);
if (SelectionInput == "1" || SelectionInput == "EASY" || SelectionInput == "Easy" || SelectionInput == "easy")
{
high = 5;
DescRange = "1-5";
cout<<"t[Input was "" << SelectionInput << ""]nn" << endl;
}
else if (SelectionInput == "2" || SelectionInput == "MEDIUM" || SelectionInput == "Medium" || SelectionInput == "medium")
{
high = 10;
DescRange = "1-10";
cout<<"t[Input was "" << SelectionInput << ""]nn" << endl;
}
else if (SelectionInput == "3" || SelectionInput == "HARD" || SelectionInput == "Hard" || SelectionInput == "hard")
{
high = 50;
DescRange = "1-50";
cout<<"t[Input was "" << SelectionInput << ""]nn" << endl;
}
else if (SelectionInput == "4" || SelectionInput == "IMPOSSIBLE" || SelectionInput == "Impossible" || SelectionInput == "impossible")
{
high = 100;
DescRange = "1-100";
cout<<"t[Input was "" << SelectionInput << ""]nn" << endl;
}
else if (SelectionInput == "5" || SelectionInput == "CUSTOM" || SelectionInput == "Custom" || SelectionInput == "custom")
{
cout<<"t[Input was "" << SelectionInput << ""]nn" << endl;
cout << "Input a digit range for you to guess a number in between. " << endl;
cout << "Low: ";
cin >> low;
cout << "High: ";
cin >> high;
}
else if (SelectionInput == "6" || SelectionInput == "EXIT" || SelectionInput == "Exit" || SelectionInput == "exit")
{
high = 10;
cout << "nYou've chosen to terminate the program!" << "t[Input was "" << SelectionInput << ""]nn" << endl;
for (int iii = 0; iii < 5; iii++)
{
cout << "The program will now collapse..." << endl;
}
cout << "n" << endl;
}
NewRange;
int ANSWER = getRange();
cout << "For debugging/test purposes, the range is: "" << ANSWER << "". n" << endl; // DEBUG for knowing what the 'ANSWER' is.
}
void Guessing()
{
NumberGen();
int ANSWER = getRange(); // Making things SIMPLE
int guessN;
if (SelectionInput == "5" || SelectionInput == "CUSTOM" || SelectionInput == "Custom" || SelectionInput == "custom")
{
cout << "Guess the number I picked between " << low << "-" << high << ": ";
cin >> guessN;
if (guessN == ANSWER)
cout << "Oh no, you won! The answer "" << guessN << "", is... correct! nn" << endl;
else
cout << "nSorry you lose, try again! Answer was " << ANSWER << ". nn" << endl;
}
else
{
cout << "Guess the number I picked between " << DescRange << ": ";
cin >> guessN;
if (guessN == ANSWER)
cout << "Oh no, you won! The answer "" << guessN << "", is... correct! nn" << endl;
else
cout << "nSorry you lose, try again! Answer was "<<ANSWER<<". nn" << endl;
}
}
int main()
{
srand(time(0));
Guessing();
//system("PAUSE"); // * Temporarily Disabled *
return 0;
}
您应该在程序中只执行一次srand(time(0))
—在调用其他代码之前将其移到main()程序中。
srand(time(0))
通常被认为是一个坏主意,因为安全是一个问题,但这是一个不同的主题,在这里讨论太长了。
编辑
对代码中最简单的修复是创建两个函数——一个用于生成新值,另一个用于检索该值;static unsigned int rangevalue;
void NewRange(int nLow, int nHigh) // Range for generated #'s.
{
rangevalue = (rand() % (nHigh - nLow + 1)) + nLow;
}
unsigned int getRange() {
return rangevalue;
}
然后在代码中使用这两个函数。
如果你想让你的代码更像c++,可以考虑使用类的概念来包装你的逻辑…类似的;
class RangeValues {
unsigned int rvalue;
public:
RangeValues() {
static bool initialized = false;
if (!initialized) {
initialized = true;
srand(time(0));
}
rvlaue = rand();
}
unsigned getRange(int nLow, int nHigh) { // Range for generated #'s.
return (rvalue % (nHigh - nLow + 1)) + nLow;
}
}
当你每次创建一个新的RangeValues实例时,它将生成一个新的值,然后你可以通过引用传递它。
相关文章:
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何获取一个数字的前3位
- 以C++输出一个数字三角形
- 反转一个数字程序不起作用,为什么?
- 如何让用户在 c++ 中选择一个数字
- 输出一个数字,该数字可能是三种类型之一
- C++在不使用pow或循环的情况下计算一个数字的幂
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 找到所有与自己求和的数字X的快速方法,去掉一个数字得到N
- C++[递归]将一个数字写成2的升序之和
- 试图找到一个数字的平方根,但代码不起作用。C++
- 我没有得到一个数字作为输出,而是一个表情符号
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- 将一个数字拆分为多个数字,每个数字只有一个有效位
- 你怎么编码,如果x不等于一个数字,程序就会退出
- 如何将整数数组相乘得到一个数字?
- 为无符号字符* 分配一个数字
- 如何以更有效的方式检查一个数字是否是素数?
- C++显示两个区间之间的数字的程序检查一个数字是否可以表示为两个素数的总和
- 字符串和双精度的麻烦,等值后再得到一个数字