📜  使用 rand()函数在 C++ 中猜数游戏

📅  最后修改于: 2022-05-13 01:55:01.087000             🧑  作者: Mango

使用 rand()函数在 C++ 中猜数游戏

在本文中,我们将开发一个 C++ 游戏,用于猜三个难度级别的秘密数字。

  • 在这个游戏中,计算机生成一个 1 到 100 范围内的秘密数字,玩家必须猜测它。
  • 游戏分为三个难度级别。玩家猜测的机会受到他们选择的级别的限制。简单级别给予玩家 10 次猜测秘密数字的机会,中等级别 7 次机会,而困难级别仅提供 5 次机会。
  • 在游戏过程中,玩家告诉计算机他对一个数字的假设,计算机会告诉玩家是否正确。如果他的号码小于或大于密码,计算机会通知玩家,玩家再试一次。
  • 玩家也可以随时结束游戏。

方法

第 1 步:生成1 到 100 之间的随机密码

C++ 中的任何函数都不会在给定范围内生成随机函数。因此,我们将使用 rand()函数。我们将需要 cstdlib 库来使用 rand()。在一个范围内生成随机函数的公式是

randomNumber = (rand() % (upper-lower) + 1)

假设我们希望生成一个介于 1 和 100 之间的数字,因此上等于 100,下等于 1。所以,公式变为

randomNumber = (rand() % (100-1) + 1)

现在,每次我们运行程序,计算机都会生成与密码相同的随机数,使得它非常重复和无聊,这种一次性游戏失去了本质。

为了在程序每次运行时生成不同的随机数,我们将使用 srand(time(0))函数。每次程序运行时,此函数都会更改种子。 time(0) 返回系统时钟显示的秒数。由于我们将使用 time函数,因此我们必须包含 ctime 库。

第 2 步:让用户选择难度级别

While 循环让我们实现了一个菜单驱动的程序,玩家可以在其中选择难度。用户可以按 1 选择简单级别,按 2 选择中等,按 3 选择难度级别。

第 3 步:根据难度级别确定玩家的机会数量

当玩家选择简单关卡时,他有 10 次机会猜出秘密数字。中等,他有 7 次机会,而硬,他有 5 次机会。

第四步:检查输入的号码是否与密码相同

使用 if-else 构造,我们将检查输入的数字是否与秘密数字匹配。

  • 由于玩家在简单级别有 10 次机会,我们将从 1 迭代到 10 以确定输入的数字是否与实际的秘密数字匹配。由于玩家在中等级别只有七个选择,我们从 1 到 7 迭代以检查数字是否与秘密数字匹配。如果玩家选择困难级别,我们将从 1 迭代到 5,因为只有 5 个选项。
  • 如果输入的数字小于密码,我们将显示密码小于选择的数字。
  • 每当输入的数字超过秘密数字时,我们会显示秘密数字更大,作为对玩家的提示。
  • 我们还将显示剩下的选择数量。
  • 一旦输入的数字与密码匹配,玩家将被通知他中奖了。
  • 如果玩家无法猜出数字并且他或她没有更多选择,则游戏结束。这将终止游戏。

执行:

C++
#include 
#include 
#include 
using namespace std;
  
int main()
{
  
    cout << "\n\t\t\tWelcome to GuessTheNumber game!"
         << endl;
    cout << "You have to guess a number between 1 and 100. "
            "You'll have limited choices based on the "
            "level you choose. Good Luck!"
         << endl;
  
    while (true) {
        cout << "\nEnter the difficulty level: \n";
        cout << "1 for easy!\t";
        cout << "2 for medium!\t";
        cout << "3 for difficult!\t";
        cout << "0 for ending the game!\n" << endl;
  
        // select the level of difficulty
        int difficultyChoice;
        cout << "Enter the number: ";
        cin >> difficultyChoice;
  
        // generating the secret number
        srand(time(0));
        int secretNumber = 1 + (rand() % 100);
        int playerChoice;
  
        // Difficulty Level:Easy
        if (difficultyChoice == 1) {
            cout << "\nYou have 10 choices for finding the "
                    "secret number between 1 and 100.";
            int choicesLeft = 10;
            for (int i = 1; i <= 10; i++) {
  
                // prompting the player to guess the secret
                // number
                cout << "\n\nEnter the number: ";
                cin >> playerChoice;
  
                // determining if the playerChoice matches
                // the secret number
                if (playerChoice == secretNumber) {
                    cout << "Well played! You won, "
                         << playerChoice
                         << " is the secret number" << endl;
                    cout << "\t\t\t Thanks for playing...."
                         << endl;
                    cout << "Play the game again with "
                            "us!!\n\n"
                         << endl;
                    break;
                }
                else {
                    cout << "Nope, " << playerChoice
                         << " is not the right number\n";
                    if (playerChoice > secretNumber) {
                        cout << "The secret number is "
                                "smaller than the number "
                                "you have chosen"
                             << endl;
                    }
                    else {
                        cout << "The secret number is "
                                "greater than the number "
                                "you have chosen"
                             << endl;
                    }
                    choicesLeft--;
                    cout << choicesLeft << " choices left. "
                         << endl;
                    if (choicesLeft == 0) {
                        cout << "You couldn't find the "
                                "secret number, it was "
                             << secretNumber
                             << ", You lose!!\n\n";
                        cout << "Play the game again to "
                                "win!!!\n\n";
                    }
                }
            }
        }
  
        // Difficulty level : Medium
        else if (difficultyChoice == 2) {
            cout << "\nYou have 7 choices for finding the "
                    "secret number between 1 and 100.";
            int choicesLeft = 7;
            for (int i = 1; i <= 7; i++) {
  
                // prompting the player to guess the secret
                // number
                cout << "\n\nEnter the number: ";
                cin >> playerChoice;
  
                // determining if the playerChoice matches
                // the secret number
                if (playerChoice == secretNumber) {
                    cout << "Well played! You won, "
                         << playerChoice
                         << " is the secret number" << endl;
                    cout << "\t\t\t Thanks for playing...."
                         << endl;
                    cout << "Play the game again with "
                            "us!!\n\n"
                         << endl;
                    break;
                }
                else {
                    cout << "Nope, " << playerChoice
                         << " is not the right number\n";
                    if (playerChoice > secretNumber) {
                        cout << "The secret number is "
                                "smaller than the number "
                                "you have chosen"
                             << endl;
                    }
                    else {
                        cout << "The secret number is "
                                "greater than the number "
                                "you have chosen"
                             << endl;
                    }
                    choicesLeft--;
                    cout << choicesLeft << " choices left. "
                         << endl;
                    if (choicesLeft == 0) {
                        cout << "You couldn't find the "
                                "secret number, it was "
                             << secretNumber
                             << ", You lose!!\n\n";
                        cout << "Play the game again to "
                                "win!!!\n\n";
                    }
                }
            }
        }
        // Difficulty level : Medium
        else if (difficultyChoice == 3) {
            cout << "\nYou have 5 choices for finding the "
                    "secret number between 1 and 100.";
            int choicesLeft = 5;
            for (int i = 1; i <= 5; i++) {
  
                // prompting the player to guess the secret
                // number
                cout << "\n\nEnter the number: ";
                cin >> playerChoice;
  
                // determining if the playerChoice matches
                // the secret number
                if (playerChoice == secretNumber) {
                    cout << "Well played! You won, "
                         << playerChoice
                         << " is the secret number" << endl;
                    cout << "\t\t\t Thanks for playing...."
                         << endl;
                    cout << "Play the game again with "
                            "us!!\n\n"
                         << endl;
                    break;
                }
                else {
                    cout << "Nope, " << playerChoice
                         << " is not the right number\n";
                    if (playerChoice > secretNumber) {
                        cout << "The secret number is "
                                "smaller than the number "
                                "you have chosen"
                             << endl;
                    }
                    else {
                        cout << "The secret number is "
                                "greater than the number "
                                "you have chosen"
                             << endl;
                    }
                    choicesLeft--;
                    cout << choicesLeft << " choices left. "
                         << endl;
                    if (choicesLeft == 0) {
                        cout << "You couldn't find the "
                                "secret number, it was "
                             << secretNumber
                             << ", You lose!!\n\n";
                        cout << "Play the game again to "
                                "win!!!\n\n";
                    }
                }
            }
        }
        // To end the game
        else if (difficultyChoice == 0) {
            exit(0);
        }
        else {
            cout << "Wrong choice, Enter valid choice to "
                    "play the game! (0,1,2,3)"
                 << endl;
        }
    }
    return 0;
}


输出:让我们试试这个中等难度的游戏,看看结果如何。

输出:猜猜秘密数字

输出