使用srand(time(0))猜测一个范围内的数字

C++ Guessing a number within a range using srand(time(0))

本文关键字:一个 数字 范围内 srand time 使用      更新时间:2023-10-16

我的问题是…我几乎完成了我的小游戏,但我想测试一下,看看它是否与"调试作弊"一起工作,看看程序是否实际上显示了正确的数字。

我设置了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实例时,它将生成一个新的值,然后你可以通过引用传递它。