首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >C++随机数猜测游戏

C++随机数猜测游戏
EN

Stack Overflow用户
提问于 2013-10-29 02:14:27
回答 5查看 18.2K关注 0票数 0

我必须编写一个程序来运行一个随机的猜测游戏。游戏是从1-100的数字,猜测者得到20次尝试,最后应该被问他们是否愿意再玩一次。如果猜测者是高还是低,打印输出也必须有多个选项。我已经完成了我的部分程序,我知道我仍然需要为打印输出添加其他选项,但现在我的问题是,当我尝试运行我说成功的内容时,会出现一个错误,即变量"number“被使用而不需要初始化。显然,我不知道该怎么做才能初始化它。(我是C++的新手)我更新了我的程序,现在有一个不同的问题。我的程序运行,但如果猜测低于它打印的数字太高,再试一次和太低再试,但当数字太高,它只是打印过高,再试一次。我还注意到,当用户选择再次播放时,尝试计数器不会与游戏重置。最后一件事,我必须增加更多的信息,当他们赢,输,并被要求玩另一个游戏,我必须使用随机数中选择。因此,任何关于最佳路线的建议都是很棒的

代码语言:javascript
代码运行次数:0
运行
复制
#include <iostream>
#include <iomanip> 
#include <ctime>
using namespace std;
char chr;

int main()
{
srand(time(NULL));                                           //the function that generates random numbers
int number=rand()%100+1;                                     //the range of the random numbers
int guess;                                                   //The guess is stored here
int tries=0;                                                 //The number of tries stored here
   char answer;                                                 //The answer to the question is stored here
answer='y';                  

while(answer=='y'||answer=='Y') 
{
    while (tries<=20 && answer=='y'|| answer=='Y')
    {
    cout<<"Enter a number between 1 and 100 "<<endl;          //The user is asked for a guess
    cin>>guess;                                               //The guess is stored here
    tries++;                                                 //Adding a number for every try
    if(guess==0||guess>100)                                  //If statement that produces an error message if user enters a number out of the peramiters
    {
     cout<<"This is not an option try again"<<endl;          //Error message
    }

    if(tries<20)                                            
    cout<<"Tries left: "<<(20-tries)<<endl;                   //Output to let the user know how many guess they have left

    if(number<guess);                                         //if the guess is to high
    cout<<"Too high try again"<<endl;                         //This message prints if guess it to high

    if(number>guess)                                          //if the guess is to low
    cout<<"Too low try again"<<endl;                          //This message prints if the guess is to low

    if(number==guess)                                          //If the user guesses the number
    {
     cout<<"Congratualtions!! "<<endl;                          //Message printed out if the user guesses correctly
     cout<<"You got the right number in "<<tries<<" tries"<<endl;  //Lets the user know how many guess they used
     answer = 'n';
    }
    if(tries >= 20)                                               //If the user uses all their guesses
    {
    cout << "You've run out of tries!"<<endl;                      //The message that prints when a user is out of guesses
    answer='n';
    }
    if(answer=='n')
    {
     cout<<"Would you like to play again?  Enter Y/N"<<endl;       //asking if play would like to play again
     cin>>answer;                                                  //Store users answer
     if (answer=='N'|| answer=='n')                                //if the user says no
     cout<<"Thanks for playing!"<<endl;                            //This message prints out if they choose not to play again

    else
        number=rand()%100+1;                                        //This starts the game over if they choose to play again
    }

    }
    }

cin>>chr;
    return 0;

}
EN

回答 5

Stack Overflow用户

发布于 2013-10-29 02:22:33

编辑:添加强制转换以消除编译器警告.

正如吉姆·罗兹在评论中所说的那样,问题在于这句话。

代码语言:javascript
代码运行次数:0
运行
复制
srand(number>0);

srand()用于初始化随机数生成器,因此使用number>0甚至number调用它是没有意义的。它需要一个“种子”值,这应该是不同的,每次你运行程序。获得这种种子的一个常见方法是使用系统时间:

代码语言:javascript
代码运行次数:0
运行
复制
srand(static_cast<unsigned int>(time(NULL)));

您可能需要#include另一个头来访问time()

票数 8
EN

Stack Overflow用户

发布于 2013-10-29 03:03:37

编译器警告您的问题是使用以下两行:

代码语言:javascript
代码运行次数:0
运行
复制
int number;
//...
srand(number>0);

在这里,您还没有给变量number一个初始值,所以它是未初始化的--您完全不可能知道此时的值可能是什么。事实上,每次你运行你的程序时,它都可能发生变化。接下来你要问的是,神秘值是否大于零--它可能是,也许不是,但你只是不知道。这是一个神秘的行为,这是编译器警告你的事情。

当然,你正在尝试初始化随机种子,所以拥有这种神秘的行为可能就是你想要的!但不幸的是,即使这样也无法维持现状。

在C++中,表达式number>0是布尔表达式,即表达式的结果要么是true,要么是false。但是srand()函数将unsigned int作为参数,因此编译器必须将bool转换为unsigned,并通过将false更改为0,将true更改为1 (可能:我认为这在技术上取决于实现)。因此,无论number的初始值是什么,随机种子只会有两个可能的值中的一个--一点也不随机!

更好的办法是在一些(相对)不可预知的事情上使用随机种子。非加密需要根据当前时间初始化种子是很常见的。类似于:

代码语言:javascript
代码运行次数:0
运行
复制
#include <ctime>

std::time_t now = std::time(0);
srand(static_cast<unsigned>(now));

会很好的。

票数 3
EN

Stack Overflow用户

发布于 2013-10-29 03:10:45

我看到的一个问题是,您正在为每个用户猜测选择一个新的随机数。这是错误的。

要解决这个问题,您应该将

代码语言:javascript
代码运行次数:0
运行
复制
number=rand()%100+1;

循环询问猜测的do while循环之前的行。

第二个问题是该循环上的条件不正确。您应该循环直到number ==猜测,而不是number >猜测,并将这两个couts放在循环中,告诉用户猜测是高的还是低的,并在循环中增加尝试。

另外,您可能希望对要求您再次播放的问题有一个外层do while()循环,而不是在等待用户获得正确数字的循环之后执行这个循环。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19648471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档