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

C++ 在程序/基于文本的游戏/C+内重新启动程序+;,c++,recursion,C++,Recursion,我的问题是,当你在一个简单的基于文本的游戏中“死亡”时,你会被问到是否要重新启动,如果你说“是”或“是”,那么游戏会重新开始。多谢各位 我非常抱歉,如果我没有正确地复制代码,如果从这里复制代码不起作用,那么请写信给我,这样我可以安排一个谷歌驱动器链接,这样你就可以下载游戏了 #include <iostream> #include <windows.h> #include <cmath> #include <cstdlib> #include &l

我的问题是,当你在一个简单的基于文本的游戏中“死亡”时,你会被问到是否要重新启动,如果你说“是”或“是”,那么游戏会重新开始。多谢各位

我非常抱歉,如果我没有正确地复制代码,如果从这里复制代码不起作用,那么请写信给我,这样我可以安排一个谷歌驱动器链接,这样你就可以下载游戏了

#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 = true) {
    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";
        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" || "n") {
                cout << "Thankyou For Playing Hobo Life\n";
                gameloop = false;
                break;
            }
            if (sc == "yes" || "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") {
        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" || "n") {
            cout << "Thankyou For Playing Hobo Life\n";
            gameloop = false;
        }
        if (sc == "yes" || "y"){
            gameloop = true;
            break;
        }
        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使用break将强制最内部的环路退出

 if (sc == "yes" || "y"){
        gameloop = true;
        break; // <--- exits your while loop
 }
在这里:

你打破了循环-这与你的意图相反-你总是这样做,因为你也忽略了前一个条件的结果

如果只希望接受有效输入,则需要使用循环:

bool is_valid = true;
do {
    is_valid = false;
    cin >> sc;
    if (sc == "no" || sc == "n") {
        cout << "Thankyou For Playing Hobo Life\n";
        is_valid = true;
        gameloop = false;
    }
    /* The 'else' is important here. */
    else if (sc == "yes" || sc == "y"){
        is_valid = true;
    }
    else {
        cout << "Invalid Input\n";
        cout << "\n";
        cin >> sc;
    }
} while (!is_valid);
bool is_valid=true;
做{
is_valid=false;
cin>>sc;
如果(sc==“否”| sc==“n”){

请将
while(gameloop=true)
更改为
while(gameloop)
。请记住:
=
是赋值,
=
是相等的比较。但是,没有必要将
bool
true
进行比较。如果值为
true
则比较结果为
true
,如果值为
false
,则比较结果为
false
。因此,这是一个不必要的标识我把
while(gameloop=true)
改成
while(gameloop)
,我明白
=
=
之间的区别,但程序还是像以前一样关闭。如果我不懂英文“x是A或B”,我很抱歉翻译成逻辑的不是
x==A | | B
,而是
x==A | | x==B
。你可以从问题中删除游戏的所有细节–逻辑很重要,但你的“对话”并不重要。阅读关于。这回答了你的问题吗?我在
gameloop=true
之后添加了重置状态,这解决了问题,但是一个poi当我尝试输入时,游戏会说我已经死了,我被困在这个循环中。唯一的办法就是说“不”当我被问及是否要重新启动时,您需要重置正在使用的变量的值。这不仅仅是上述代码的副本。我认为您可能只是在重新声明它们。您需要将
int F=10,W=10;
更改为
F=10;W=10;
if (sc == "yes" || "y"){
    gameloop = true;
    break;
}
bool is_valid = true;
do {
    is_valid = false;
    cin >> sc;
    if (sc == "no" || sc == "n") {
        cout << "Thankyou For Playing Hobo Life\n";
        is_valid = true;
        gameloop = false;
    }
    /* The 'else' is important here. */
    else if (sc == "yes" || sc == "y"){
        is_valid = true;
    }
    else {
        cout << "Invalid Input\n";
        cout << "\n";
        cin >> sc;
    }
} while (!is_valid);