"Guess the number" 使用 C++ 的游戏

"Guess the number" game with C++

正如标题所示,我对“猜数字”游戏有疑问。

我需要编写一个程序,在执行开始时,该程序应要求客户输入最小和最大次数以及尝试次数(例如,用户将定义尝试次数自己猜), 在用户输入最小值、最大值和尝试次数后, 该程序应生成一个介于 user-defined 最小值和最大值之间的随机数,并为用户提供尝试猜测该数字的次数,该数字已由用户定义。

如果用户没有猜中数字,输出“Game over”,如果猜中数字“Congratulations”程序应该停止执行。

我不知道如何在用户输入的最小和最大数字之间生成 pseudo-random 数字,以及如何向用户提供 X 次尝试嗯

到目前为止,我在这里:


//REQUIREMENTS:
/*In the beginning of the execution, ask the customer to enter min and max number and tries count,
after the user enters min, max, tries count,
the program should generate random number between min and max and to give the user X number of tries to guess the number

If the user does not guess the number, output "Game over", if he guesses the number "Congratulations" and exit code 0!*/

/*------------------------------------------------------------------------------------------------------------------
/*Algorithm:
 * 1. Define the variables
 * 2. Output to the customer to enter the variable values
 * 3. Input with the variables
 * 4. for loop (since we know how many tries/iterations will be done as the user will define that)
 * 5. The user should guess the generated random number
 * 6. Output message depending on if he won or not - "Game over" or "Congratulations"
------------------------------------------------------------------------------------------------------------------*/

#include <iostream>
#include <cstdlib> //this header contains the srand() and rand() methods/functions for random number generation

int main() {

    //The minimum and maximum numbers which define the range and the tries counter variables
    int minNumber;
    int maxNumber;
    int triesCounter;
    int randomlyGeneratedNumber;

    //IO operations
    std::cout << "Please, enter minimum number: ";
    std::cin >> minNumber;

    std::cout << "Please, enter maximum number: ";
    std::cin >> maxNumber;

    std::cout << "Please, enter the number of tries you will guess the number with: ";
    std:: cin >> triesCounter;

    /* initialize random seed: */
    srand (time(NULL));

    /* generate secret number between "minNumber" and "maxNumber": */
    randomlyGeneratedNumber = rand() % minNumber + maxNumber;




   //Using ternary operator instead of if/else conditionals
   //($var==$var1)? std::cout << "Congratulations!" : std::cout << "Game over!";


    return 0;
}

/*I've read quite a lot over the web on the stuff, however, most of the information is related to guessing games where the range of numbers is pre-defined, which is not the case with me and I am not an expert, but a beginner. */

//Could you please help me? 

除了随机数生成中的修正,我只是填空:

/* generate secret number between "minNumber" and "maxNumber": */
randomlyGeneratedNumber = rand() % (maxNumber - minNumber + 1) + minNumber;

int guessedNumber;
do
{
  std::cout << "Please, enter your guessed number: ";
  std::cin >> guessedNumber;
}
while(randomlyGeneratedNumber != guessedNumber && (--triesCounter > 0))

//Using ternary operator instead of if/else conditionals
(randomlyGeneratedNumber == guessedNumber )
? std::cout << "Congratulations!"
: std::cout << "Game over!";

运算符 % 会将数字切分到一个范围内。在这种情况下,范围将从 0(maxNumber - minNumber) 然后我们添加 minNumber 以获得正确的范围。

循环将在第一个条件为假的正确猜测或当第二个条件为假时尝试次数变为零时中断。