C++ 在程序内部重新启动程序&;基于文本的游戏&;C++;

C++ 在程序内部重新启动程序&;基于文本的游戏&;C++;,c++,C++,我已经制作这个游戏有一段时间了,但仍然无法解决这个大问题。我很年轻,需要指导。谢谢。我的问题是,当你在游戏中“死亡”时,你会被问到是否要重新启动,如果你说“是”或“是”,那么游戏会按你的要求重新启动,但当我尝试输入任何其他内容时,程序会说我们死了,我被迫进入一个循环。结束循环的唯一方法是在被问及是否要重新启动时键入“否”,从而关闭程序 #include <iostream> #include <windows.h> #include <cmath> #incl

我已经制作这个游戏有一段时间了,但仍然无法解决这个大问题。我很年轻,需要指导。谢谢。我的问题是,当你在游戏中“死亡”时,你会被问到是否要重新启动,如果你说“是”或“是”,那么游戏会按你的要求重新启动,但当我尝试输入任何其他内容时,程序会说我们死了,我被迫进入一个循环。结束循环的唯一方法是在被问及是否要重新启动时键入“否”,从而关闭程序

#include <iostream>
#include <windows.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>

using namespace std;

//int rand_0toN1(int F);

int main()
{
bool gameloop = true;

int F = 10, W = 10; //F = Food Level -- W = Water Level
int BegS, PG; //BegS = Begging Skills -- PG = People Giving choices
double thieveN, thieveI; //Thieve = Thievery -- thieveI = items you steal
int D = 0; // Number of days survived
int MC = 0; // Money checker, if you earn money then additional commands will be added so you can use money
int M = 0; // Amount of money
string c, sc, thieve; //C = Choice -- SC = Seacond Choice -- thieves = thievery strings

srand(time(NULL)); //set seed for random numberss
BegS = (rand() % 20) + 1;
thieveN = (rand() % 20) + 1;
thieveI = (rand() % 4) + 1;
PG = (rand() % 2) + 1;

cout << "Hello There, Lets get straight to the point. You are homeless in the streets of New York\n";
cout << "Yeah it sucks I know\n";
cout << "You will have to survive the streets\n";
cout << "If you ever need help with the commands of this game please type help\n";
cout << "\n";
cin >> c;

while (gameloop) {
    if (c == "help") {
        cout << "\n";
        cout << "Here are the basic commands of the Game\n";
        Sleep(50);
        cout << "beg--To Beg what did you expect\n";
        Sleep(50);
        cout << "levels--To Check your hunger and water levels (if any of your levels reaches 0 you DIE!!!)\n";
        Sleep(50);
        cout << "steal--To Steal (will open a window for more commands)\n";
        Sleep(50);
        cout << "die--this command will kill your character \n";
        cout << "\n";
        cin >> c;
    }

    if (c == "die") {
        F = 1;
    }

    if (c == "levels") {
        cout << "\n";
        cout << "Your Water level is " << W << endl;
        cout << "Your Hunger level is " << F << endl;
        cout << "You Have currently survived for " << D << " days\n";
        cout << "\n";
        cin >> c;
    }
    if (c == "levels" && MC == 1) {
        cout << "\n";
        cout << "Your Water level is " << W << endl;
        cout << "Your Hunger level is " << F << endl;
        cout << "You Have currently survived for " << D << " days\n";
        cout << "Your money totals up to " << M << endl;
        cout << "\n";
        cin >> c;
    }
    if (c == "beg" && BegS >= 18) {  //10% chance
        cout << "Your Beggging Payed of and A Kind Person Gave You Water and Food\n";
        cout << "\n";
        BegS = (rand() % 10) + 1;
        D++;
        cin >> c;
    }
    if (c == "beg" && BegS >= 13 && BegS <= 17 && PG == 1) {  // 20% chance
        cout << "Your Beggging Payed of and A Kind Person Gave You Food\n";
        cout << "\n";
        BegS = (rand() % 20) + 1;
        PG = (rand() % 2) + 1;
        D++;
        W--;
        cin >> c;
    }
    if (c == "beg" && BegS >= 13 && BegS <= 17 && PG == 2) {  // 20% chance
        cout << "Your Beggging Payed of and A Kind Person Gave You Water\n";
        cout << "\n";
        BegS = (rand() % 20) + 1;
        PG = (rand() % 2) + 1;
        D++;
        F--;
        cin >> c;
    }
    if (c == "beg" && BegS >= 14 && BegS <= 17) {  // 15% chance
        cout << "A Kind Person came by you and game you some money\n";
        cout << "(you can use money to buy either food or water by using the \"buy water/food\" command\n";
        cout << "\n";
        BegS = (rand() % 20) + 1;
        PG = (rand() % 2) + 1;
        D++;
        F--;
        W--;
        MC = 1;
        M++;
        cin >> c;
    }
    if (c == "beg" && BegS >= 3 && BegS <= 13) {  // 50% chance
        cout << "Sadly your begging did not get you anything\n";
        cout << "\n";
        BegS = (rand() % 20) + 1;
        PG = (rand() % 2) + 1;
        D++;
        F--;
        W--;
        cin >> c;
    }

    //-------------Specialty Items----------------
    if (c == "beg" && BegS == 2) {  // 5% chance
        cout << "You get noticed by some person who gives you a soda\n";
        cout << "(Items like Soda gives you +2 Water)\n";
        cout << "\n";
        BegS = (rand() % 20) + 1;
        PG = (rand() % 2) + 1;
        D++;
        F--;
        W += 2;
        cin >> c;
    }
    if (c == "beg" && BegS == 1) {  // 5% chance
        cout << "You get noticed by some person who gives you a Pizza\n";
        cout << "(Items like Pizza gives you +2 Food)\n";
        cout << "\n";
        BegS = (rand() % 20) + 1;
        PG = (rand() % 2) + 1;
        D++;
        F += 2;
        W--;
        cin >> c;
    }
    //-----------------Thievery--------------------

    if (c == "steal") {
        cout << "What would you like to steal from? (gas station-1)\n";
        cin >> thieve;
        if (thieve == "1" && thieveN >= 12 && thieveN <= 16) { // 30% chance
            cout << "You have chosen the gas station\n";
            cout << "\n";
            cout << "You manage to sneak a waterbottle into your pocket\n";
            thieveN = (rand() % 20) + 1;
            cout << "\n";
            cout << "Would you Like to test your chances and steal something else?\n";
            cin >> thieve;
        }
        if (thieve == "1" && thieveN >= 17 && thieveN <= 20) {   // 30% chance
            cout << "You manage to sneak a sandwich into your pocket\n";
            thieveN = (rand() % 20) + 1;
            cout << "\n";
            cout << "Would you Like to test your chances and steal something else?\n";
            cin >> thieve;
        }
        if (thieve == "1" && thieveN >= 1 && thieveN <= 11) {   // 40% chance
            cout << "You get noticed trying to sneak food into your pocket and get arrested\n";
            cout << "\n";
            cout << "GAME OVER\n";
            cout << "You Survived " << D << " Days\n";
            Sleep(1250);
            cout << "Would you like to start over? (yes/ no)\n";
            cin >> sc;
            if (sc == "no" || sc == "n") {
                cout << "Thankyou For Playing Hobo Life\n";
                gameloop = false;
                break;
            }
            if (sc == "yes" || sc == "n") {
                cout << "You find yourself on a park bench, its early morning. Better start begging\n";
                cin >> c;
            }
            else {
                cout << "Invalid Input\n";
                cin >> sc;
            }
        }
    }
    //-------------Invalid Input-------------------
    if (c != "beg" && c != "levels" && c != "steal" && thieve != "1" && c != "die" && c != "help") {
        cout << "Invalid Input\n";
        cout << "\n";
        cin >> c;
    }

    //-------------------Death---------------------
    if (F == 1 || W == 1) {
        cout << "You died\n";
        cout << "You Survived " << D << " Days\n";
        Sleep(1250);
        cout << "Would you like to start over? (yes/ no)\n";
        cin >> sc;
        if (sc == "no" || sc == "n") {
            cout << "Thankyou For Playing Hobo Life\n";
            gameloop = false;
            break;
        }
        if (sc == "yes" || sc == "y"){
            gameloop = true;
            int F = 10, W = 10; //F = Food Level -- W = Water Level
            int BegS, PG; //BegS = Begging Skills -- PG = People Giving choices
            double thieveN, thieveI; //Thieve = Thievery -- thieveI = items you steal
            int D = 0; // Number of days survived
            int MC = 0; // Money checker, if you earn money then additional commands will be added so you can use money
            int M = 0; // Amount of money
            string c, sc, thieve; //C = Choice -- SC = Seacond Choice -- thieves = thievery strings
            cout << "\n";
            cout << "Hello There, Lets get straight to the point. You are homeless in the streets of New York\n";
            cout << "Yeah it sucks I know\n";
            cout << "You will have to survive the streets\n";
            cout << "If you ever need help with the commands of this game please type help\n";
            cout << "\n";
            cin >> c;
        }
        else {
            cout << "Invalid Input\n";
            cout << "\n";
            cin >> sc;
        }
    }
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//内部随机变量1(内部F);
int main()
{
bool gameloop=true;
int F=10,W=10;//F=食物水位——W=水位
int BegS,PG;//BegS=乞讨技能--PG=给予选择的人
双重偷窃,偷窃;//Thieve=thiever--thieveI=你偷的物品
int D=0;//存活天数
int MC=0;//金钱检查器,如果您赚了钱,那么将添加其他命令,以便您可以使用金钱
int M=0;//金额
字符串c,sc,thieve;//c=Choice--sc=Seacond Choice--thiever=thiever字符串
srand(time(NULL));//为随机数设置种子
BegS=(rand()%20)+1;
thi偶=(rand()%20)+1;
蒂维维=(兰德()%4)+1;
PG=(rand()%2)+1;

cout让我们看一些示例代码

int main() {
    int F = 10;
    std::cout << F; // will print 10

    if (F == 10) {
        int F = 5; // This will declare a new variable F that shadows the previous F
        std::cout << F; // will print 5
    }
    // Leaving the scope of if, our second F goes out of scope and is destroyed
    // Our first F is still there though, and it's still 10

    std::cout << F; // Will print 10
}
改变旧变量,而不是创建新变量

if (sc == "yes" || sc == "y"){
    gameloop = true;
    F = 10;
    W = 10;
    ...
}

您在不同的作用域中使用相同的名称声明不同的变量。
(给两个事物起同一个名字并不意味着它们是同一事物。)

使用两个嵌套循环-一个循环玩一轮游戏,另一个循环重复这些循环直到玩家想要退出

像这样的结构:

int main()
{
    bool quit = false;
    while (!quit)
    {
        // Declare variables for a round here so you don't need to reset them.
        int food = 100;
        int water = 100;
        // ... more game state...
        // Now play a round.
        std::cout << "Let's play." << std::endl;
        bool still_alive = true;
        while (still_alive)
        {
            std::string command;
            std::cin >> command;
            // ... interpret commands ...

            if (food <= 0 || water <= 0)
            {
                still_alive = false;
            }
        }
        std::cout << "Game Over" << std::endl;
        // Ask if the player wants to go again.
        std::string answer;
        // ...
        if (answer == "no")
        {
            quit = true;
        }
        // ...
    }
}
intmain()
{
bool-quit=false;
而(!退出)
{
//在这里声明一轮的变量,这样就不需要重置它们。
int食品=100;
int水=100;
//…更多游戏状态。。。
//现在打一轮。
std::cout命令;
//…解释命令。。。

如果(食物
if(sc==“yes”| | sc==“y”){
之前是否应该有一个
else
如果
?既然你检查输入是否是否定的,那么它是否是肯定的,或者是无效的输入。请将你的代码转换为。这有两个目的:(1)它增加了可读性,因此使您更有可能得到答案这是一种优秀的调试技术:通常,你会在最小化问题代码的同时自己解决问题。发布MWE也证明你已经尝试自己解决问题。你已经重新定义了
F
W
c
sc
,等等。请调试你的程序,找出你遇到的问题(很多问题)。并认为如果用户类型
die
,您可以将
F
设置为1,当您要求重新开始时,如果用户选择了任何无效的内容,您不会更改
c
值,因此在下一个循环中,此变量再次将
die
设置为值。如果用户类型
yes
,则重新定义
F
W
设置10作为值,但在此
if
块之外,您正在将其他
F
W
变量与其他值一起使用。您应该了解一下变量范围的cocnet。每当您编写
int var\u name
时,此变量仅存在于这个特定范围内,即两个花括号之间的区域(只有全局变量和静态变量例外,但您的项目中没有)。当访问在多个位置定义的变量时,只使用最内部的作用域,其他作用域不受影响!祝您在未来的游戏开发中好运:)请将您的代码简化为您问题的一部分。您当前的代码包含许多与您的问题无关的内容-最小样本通常看起来类似于良好的单元测试:仅执行一项任务,输入值指定用于再现性。感谢您的帮助,我一定会记住这一点
int main()
{
    bool quit = false;
    while (!quit)
    {
        // Declare variables for a round here so you don't need to reset them.
        int food = 100;
        int water = 100;
        // ... more game state...
        // Now play a round.
        std::cout << "Let's play." << std::endl;
        bool still_alive = true;
        while (still_alive)
        {
            std::string command;
            std::cin >> command;
            // ... interpret commands ...

            if (food <= 0 || water <= 0)
            {
                still_alive = false;
            }
        }
        std::cout << "Game Over" << std::endl;
        // Ask if the player wants to go again.
        std::string answer;
        // ...
        if (answer == "no")
        {
            quit = true;
        }
        // ...
    }
}