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

Restarting program while inside a program / text based game / C++

本文关键字:程序 游戏 C++ 重新启动 于文本 文本      更新时间:2023-10-16

我的问题是,当你在游戏(一个简单的基于文本的游戏(中"死亡"时,如果你说"是"或"y",你会被问到是否要重新启动,然后游戏会重新启动到开头。谢谢

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

#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 Yorkn";
cout << "Yeah it sucks I known";
cout << "You will have to survive the streetsn";
cout << "If you ever need help with the commands of this game please type helpn";
cout << "n";
cin >> c;
while (gameloop = true) {
if (c == "help") {
cout << "n";
cout << "Here are the basic commands of the Gamen";
Sleep(50);
cout << "beg--To Beg what did you expectn";
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 << " daysn";
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 << " daysn";
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 Foodn";
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 Foodn";
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 Watern";
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 moneyn";
cout << "(you can use money to buy either food or water by using the "buy water/food" commandn";
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 anythingn";
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 sodan";
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 Pizzan";
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 stationn";
cout << "n";
cout << "You manage to sneak a waterbottle into your pocketn";
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 pocketn";
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 arrestedn";
cout << "n";
cout << "GAME OVERn";
cout << "You Survived " << D << " Daysn";
Sleep(1250);
cout << "Would you like to start over? (yes/ no)n";
cin >> sc;
if (sc == "no" || "n") {
cout << "Thankyou For Playing Hobo Lifen";
gameloop = false;
break;
}
if (sc == "yes" || "n") {
cout << "You find yourself on a park bench, its early morning. Better start beggingn";
cin >> c;
}
else {
cout << "Invalid Inputn";
cin >> sc;
}
}
}
//-------------Invalid Input-------------------
if (c != "beg" && c != "levels" && c != "steal" && thieve != "1" && c != "die") {
cout << "Invalid Inputn";
cout << "n";
cin >> c;
}
//-------------------Death---------------------
if (F == 1 || W == 1) {
cout << "You diedn";
cout << "You Survived " << D << " Daysn";
Sleep(1250);
cout << "Would you like to start over? (yes/ no)n";
cin >> sc;
if (sc == "no" || "n") {
cout << "Thankyou For Playing Hobo Lifen";
gameloop = false;
}
if (sc == "yes" || "y"){
gameloop = true;
break;
}
else {
cout << "Invalid Inputn";
cout << "n";
cin >> sc;
}
}
}
return 0;
}

使用 break 将强制最内层循环退出。

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

删除它,它应该继续。如果要真正重新启动,则需要重置游戏状态,您可以在此处执行此操作:

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

在这里:

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 Lifen";
is_valid = true;
gameloop = false;
}
/* The 'else' is important here. */
else if (sc == "yes" || sc == "y"){
is_valid = true;
}
else {
cout << "Invalid Inputn";
cout << "n";
cin >> sc;
}
} while (!is_valid);