C++中的可视化随机猜测游戏

visual Random Guessing Game in C++

本文关键字:游戏 随机 可视化 C++      更新时间:2023-10-16

我的任务是创建一个猜谜游戏,计算机在其中猜测我的号码,我在其中猜测计算机的号码。我已经写好了代码,但它不完整。我想创建一个菜单,这样用户就可以选择他想玩的游戏,而且我很难将两个程序合并为一个,这样它们都可以运行。有人能帮我吗?第一场比赛打得很好,但从那以后我真的不知道自己在做什么。

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
    const int min = 1;
    const int max = 100;
    int num = 2 && 3 && 4;
    int prevMin = 0;
    int prevMax = 0;
    int tries = 0;
    int guess = 0;
    int userNum = 0;
    int userGuess = 0;
    int systemNum = 0;
    int systemGuess = 0;

    cout << "Welcome to the Guessing Game!" << endl;
    cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
    cin >> num;
    if (num == 2)
    {
        cout << "Welcome to Game 1!" << endl;
        cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
    }

    srand(time(0));
    systemGuess = rand() % 100 + 1;
    do
    {
        cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
        cin.get();
        ++tries;
        if (systemGuess > max || systemGuess<min)
        {
            cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
        }
        if (systemGuess > userNum)
        {
            cout << "Too high. Guess lower." << endl;
            prevMax = systemGuess;
            systemGuess = rand() % (prevMax - prevMin) + prevMin;
            if (systemGuess == prevMin)
                systemGuess++;
        }
        else if (systemGuess < userNum)
        {
            cout << "Too low. Guess higher." << endl;
            prevMin = systemGuess;
            systemGuess = rand() % (prevMax - prevMin) + prevMin;
            if (systemGuess == prevMin)
                systemGuess++;
        }


    } while (systemGuess != userNum);
    cout << systemGuess << endl;
    cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;


    srand(time(0));
    systemNum = rand()% 100 + 1;


    //Beginning of second game
    do
    {
        if (num == 3)
    {
        cout << "Welcome to Game 2!" << endl;
        cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
    }

        cout << "Enter your guess." << endl;
        cin >> userGuess;
        tries++;
        if (userGuess > systemNum)
        {
            cout << "Too high. Guess lower." << endl;
            cin >> userGuess;
        }
        else if (userGuess < systemNum)
        {
            cout << "Too low. Guess higher." << endl;
            cin >> userGuess;
        }
        else
        {
            cout << "Correct! It took you long enough! Lol... " << endl;
            cin >> userGuess;
        }
        while (userGuess != systemNum);
    }



    system("pause");
    return 0;

}

使两个游戏成为两个不同的函数(每个函数都有自己的局部变量),创建第三个函数,即提示游戏的循环(选择"退出"时中断),并调用相应的函数,然后从main调用第三个。

您可以使用if...else:

    cout << "Welcome to the Guessing Game!" << endl;
    cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
    cin >> num;
if (num == 2)
{//game1
}
else if(num==3)
{//game2
}
else if(num==4)
{return 0;
}else
{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;

您的全部代码:

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
    const int min = 1;
    const int max = 100;
    int num = 2 && 3 && 4;
    int prevMin = 0;
    int prevMax = 0;
    int tries = 0;
    int guess = 0;
    int userNum = 0;
    int userGuess = 0;
    int systemNum = 0;
    int systemGuess = 0;
        srand(time(0));

    cout << "Welcome to the Guessing Game!" << endl;
    cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
    cin >> num;
    if (num == 2)
    {
        cout << "Welcome to Game 1!" << endl;
        cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;

    systemGuess = rand() % 100 + 1;
    do
    {
        cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
        cin.get();
        ++tries;
        if (systemGuess > max || systemGuess<min)
        {
            cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
        }
        if (systemGuess > userNum)
        {
            cout << "Too high. Guess lower." << endl;
            prevMax = systemGuess;
            systemGuess = rand() % (prevMax - prevMin) + prevMin;
            if (systemGuess == prevMin)
                systemGuess++;
        }
        else if (systemGuess < userNum)
        {
            cout << "Too low. Guess higher." << endl;
            prevMin = systemGuess;
            systemGuess = rand() % (prevMax - prevMin) + prevMin;
            if (systemGuess == prevMin)
                systemGuess++;
        }


    } while (systemGuess != userNum);
    cout << systemGuess << endl;
    cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
    }

    systemNum = rand()% 100 + 1;

//Beginning of second game
        if (num == 3)
    {
    do
    {
        cout << "Welcome to Game 2!" << endl;
        cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;

        cout << "Enter your guess." << endl;
        cin >> userGuess;
        tries++;
        if (userGuess > systemNum)
        {
            cout << "Too high. Guess lower." << endl;
            cin >> userGuess;
        }
        else if (userGuess < systemNum)
        {
            cout << "Too low. Guess higher." << endl;
            cin >> userGuess;
        }
        else
        {
            cout << "Correct! It took you long enough! Lol... " << endl;
            cin >> userGuess;
        }}
        while (userGuess != systemNum);

    }else if(num==4)return 0;    
else{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores

    system("pause");
    return 0;
}