如何解决如何在 C++ 上的游戏中添加加载游戏功能
我正在开发一个 C++ 故事游戏,您是汽车经销商,想要添加保存游戏和加载游戏功能。我使用 fstream 库添加了保存游戏功能。有人可以向我解释一种更好的方法来添加加载游戏功能,而无需放置太多 goto
函数或 if else
语句。
我在下面发布了游戏代码,我知道目前它不是完美无缺的,但我只是想知道我的问题的解决方案。
这是主文件
//Vehicle Shop Sim
//All dialogues are normal text
//All scene setting text is italics text
//All choices are in bold
#include <iostream>
#include <string>
#include <fstream>
#include "classAndobjects.hpp"
#include <conio.h>
using namespace std;
ifstream playerDetailsBring("userDetails.txt");
ofstream playerDetailsSave("userDetails.txt");
ifstream emilyBring("emily.txt");
ofstream emilySave("emily.txt");
ifstream oscarBring("oscar.txt");
ofstream oscarSave("oscar.txt");
ifstream oliverBring("oliver.txt");
ofstream oliverSave("oliver.txt");
ifstream harperBring("harper.txt");
ofstream harperSave("harper.txt");
/*inline void defObjects(void)
{
oscar.name = "Oscar";
oscar.salary = 35000;
oliver.name = "Oliver";
oliver.salary = 31000;
harper.name = "Harper";
harper.salary = 36000;
emily.name = "Emily";
emily.salary = 32000;
}*/
int main()
{
// void defObjects(void);
oscar.name = "Oscar";
oscar.salary = 35000;
oscar.id = 1;
oliver.name = "Oliver";
oliver.salary = 31000;
oliver.id = 2;
harper.name = "Harper";
harper.salary = 36000;
harper.id = 3;
emily.name = "Emily";
emily.salary = 32000;
emily.id = 4;
nate.name = "Nate";
nate.nature = trustworthy;
emilySave << emily.name << endl
<< emily.salary << endl
<< emily.id << endl;
harperSave << harper.name << endl
<< harper.salary << endl
<< harper.id << endl;
oscarSave << oscar.name << endl
<< oscar.salary << endl
<< oscar.id << endl;
oliverSave << oliver.name << endl
<< oliver.salary << endl
<< oliver.id;
cls();
type("Hello! This is the Vehicle Dealer Simulator");
el();
type("Type yes or no when questioned");
el();
type("Enter dealer's (your) name: ");
getline(cin,player.name);
type("So,you entered your name as Nincompoop,right? ");
string zorx;
while (true)
{
getline(cin,zorx);
if (zorx[0] == 'n' || zorx[0] == 'N')
{
type("Okay then,please re enter your name: ");
while (true)
{
getline(cin,player.name);
if (player.name.size() > 40)
{
type("Name too long,try again: ");
}
if (player.name.size() < 4)
{
type("Name too short,try again: ");
}
}
playerDetailsSave << "player name: " << player.name << endl;
break;
}
if (zorx[0] == 'y' || zorx[0] == 'Y')
{
player.name = "Nincompoop";
playerDetailsSave << "player name: " << player.name << endl;
break;
}
if (zorx[0] != 'y' && zorx[0] != 'Y' && zorx[0] != 'n' && zorx[0] != 'N')
{
type("Invalid anwer,try again: ");
}
}
cls();
type("Enter your company's name: ");
el();
while (true)
{
getline(cin,userCompany.name);
if (4 < userCompany.name.size() < 40)
break;
if (userCompany.name.size() > 40)
{
type("Company name too long (max 40 characters),try again: ");
}
if (userCompany.name.size() < 4)
{
type("Company name too short (min 4 characters),try again: ");
}
}
playerDetailsSave << "company's name: " << userCompany.name << endl;
getch();
cls();
cout << userCompany.name;
type(" has been going bankcurpt due to unpopularity,heavy competetion and many other reasons");
getch();
cls();
type("Lets start with a little tour of your shop ");
el();
type("Your shop has 3 rooms - \nThe storage room where all parts are stored\nOne work room where all the work is done");
el();
type("And the final room is your personal office where you spend time thinking and meeting with dealers and interviewing new emplyees");
el();
type("But don't worry because you can buy or sell land to change your shop in future ;)");
getch();
cls();
type("As of Now,");
cout << userCompany.name;
type(" has 3 employees: ");
el();
cout << oscar.name;
type(" salary :");
cout << oscar.salary;
el();
cout << oliver.name;
type(" salary :");
cout << oliver.salary;
el();
cout << harper.name;
type(" salary :");
cout << harper.salary;
el();
cout << emily.name;
type(" salary :");
cout << emily.salary;
el();
getch();
cls();
type("So lets begin...");
getch();
cls();
cout << player.name;
typeI(" is sitting in his office thinking about a christmas schemes");
el();
typeI("A tall man with a groomed beard enters");
el();
type("The person: Hello! I am Nate and I own Auto Supplies limited.");
el();
cout << player.name;
typeI(" offers him a seat and orders a coffee");
el();
type("Nate: Auto Supplies limited is an enterprise which produces automobile accessories");
el();
type("Nate: I got to kNow about you from a customer who said that he got his car ");
type("serviced in your company and wanted a back camera for his brand new jeep");
el();
type("Nate: I have come with partnership interests with you ");
el();
choices3("accept Nate as a partner dealer","refuse him","delay him and ask for time to think");
cin>>zorx;
}
解决方法
不是真正的答案,但不能将其作为评论发布。
尝试稍微组织一下您的代码。这一切都在一个函数中。难以阅读,难以修改。您有三个主要例程:玩游戏、保存状态和加载游戏状态。加载发生在开始,保存在结束。因此,尝试将其组织如下:
void save_game()
{
/* do the saving */
}
void load_game()
{
/* do the loading */
}
void play_game()
{
/* do the playing */
}
然后,您的 main()
将如下所示:
int main()
{
load_game();
play_game();
save_game();
}
当然,您可以询问用户是加载还是保存游戏状态。如果这样做,请添加更多功能。下面是一个例子:
bool should_load_game()
{
char option;
std::cout << "Load game? (/Y/N): ";
std::cin >> option;
return option == 'y' || option == 'Y';
}
bool should_save_game()
{
char option;
std::cout << "Save game? (/Y/N): ";
std::cin >> option;
return option == 'y' || option == 'Y';
}
然后,您的主要内容将更改如下:
int main()
{
if(should_load_game())
load_game();
play_game();
if(should_save_game())
save_game();
}
看,这已经变得更简单了,人们可以阅读您的代码。
如何,您拥有全局变量中的所有内容。当你想学习东西时,这对于一个小程序来说很好。但总体来说不是很好。因此,下一步是在本地定义需要从一个函数传递到另一个函数的数据,并将其作为参数传递。或者您可以将这里的所有内容放在一个类中,并将全局定义为数据成员。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。