出于某种原因,当我去保存游戏时,我的 saveGame 函数没有向 PlayerData.txt 文件写入任何内容。它早些时候工作正常,但后来停止工作了。 这是我的 Game.cpp 文件:
#include "Game.h"
#include "EnemyEncounter.h"
#include "LootEncounter.h"
#include <fstream>
Game::Game()
{
IsRunning = false;
CurrentTurn = 0;
ThePlayer = Player();
for (int i = 0; i < 10; i++) {
int range = 100 - 1 + 1;
int roll = rand() % range + 1;
if (roll >= 80) {
int gold = rand() % 100 + 1;
int exp = rand() % (100 - 20 + 1) + 20;
Map.push_back(new LootEncounter(gold, exp));
}
else {
Map.push_back(new EnemyEncounter("Goblin", 10, 5, 20, 20));
}
}
CurrentEncounter = Map[0];
}
void Game::MainMenu()
{
using namespace std;
cout << "-----------Text Adventure Game-----------\n";
cout << "----------------Main Menu----------------\n\n";
cout << "1) New Game\n";
cout << "2) Load Game\n";
cout << "0) Exit Game\n";
int option;
cin >> option;
switch (option) {
case 1:
system("cls");
cin.clear();
cin.ignore();
cout << "Starting Game...\n";
cout << "The player is level: " << ThePlayer.GetLevel() << "\n";
cout << "Press ENTER key to begin.\n";
cin.ignore();
StartGame();
break;
case 2:
break;
case 0:
system("cls");
exit(0);
break;
}
}
void Game::StartGame()
{
using namespace std;
IsRunning = true;
while (IsRunning) {
system("cls");
TakeTurn();
}
}
void Game::TakeTurn()
{
using namespace std;
CurrentTurn++;
ofstream saveFile("PlayerData.txt");
string PlayerData = "PlayerData.txt";
cout << CurrentTurn << " Taking a turn...\n";
if (CurrentEncounter->GetType() == EncounterType::Monster) {
cout << CurrentEncounter->GetState();
EnemyEncounter* enc = dynamic_cast<EnemyEncounter*>(CurrentEncounter);
while (!enc->IsCompleted) {
// do stuff to the enemy
cout << ThePlayer.GetStatus();
cout << enc->GetEnemy().GetStatus();
cout << "What would you like to do?\n";
cout << "A) Attack\n";
cout << "R) Run\n";
cout << "S) Save Game\n";
cout << "X) Exit\n";
char option;
cin >> option;
// TODO: Fix multi character inputs from choosing multiple options.
option = tolower(option);
switch (option) {
case 'a':
// attack the enemy
enc->GetEnemy().TakeDamage(ThePlayer.GetDamage());
break;
case 'r':
// run away
cout << "You attempt to run away!\n";
cout << "No luck! You are stuck!\n";
enc->SetComplete();
break;
case 's':
// TODO savefile not writing for some reason
saveGame(ThePlayer, PlayerData);
if (saveGame(ThePlayer, PlayerData)) {
cout << "Game saved successfully.\n";
}
else {
cout << "Failed to save game.\n";
}
cin.ignore();
cin.get();
return;
break;
case 'x':
system("cls");
exit(0);
break;
default:
cout << "Invalid option, please choose again.\n";
break;
}
cin.clear();
cin.ignore();
if (!enc->GetEnemy().IsAlive()) {
Reward reward = enc->GetReward();
ThePlayer.GainExperience(reward.Experience);
ThePlayer.IncreaseGold(reward.Gold);
cout << "You gain: " << reward.Experience << " experience and " << reward.Gold << " gold.\n";
enc->SetComplete();
cout << "Press ENTER key to continue.\n";
cin.get();
}
else {
int damage = enc->GetEnemy().GetDamage();
cout << "The " << enc->GetEnemy().GetName() << " hits you for " << damage << "!\n";
ThePlayer.TakeDamage(damage);
cout << ThePlayer.GetStatus();
cout << "Press ENTER key to continue.\n";
cin.get();
}
}
if (CurrentTurn < 10) {
CurrentEncounter = Map[CurrentTurn];
}
}
else if (CurrentEncounter->GetType() == EncounterType::Default) {
cout << CurrentEncounter->GetState();
if (CurrentTurn < 10) {
CurrentEncounter = Map[CurrentTurn];
}
}
else if (CurrentEncounter->GetType() == EncounterType::Loot) {
cout << CurrentEncounter->GetState();
LootEncounter* enc = dynamic_cast<LootEncounter*>(CurrentEncounter);
Reward reward = enc->GetReward();
ThePlayer.GainExperience(reward.Experience);
ThePlayer.IncreaseGold(reward.Gold);
cout << ThePlayer.GetStatus();
// advance the turn
if (CurrentTurn < 10) {
CurrentEncounter = Map[CurrentTurn];
}
cout << "\nPress ENTER to continue\n";
cin.get();
}
else if (CurrentEncounter->GetType() == EncounterType::Puzzle) {
}
else {
cout << "The encounter here is invalid.\n";
}
if (!ThePlayer.IsAlive()) {
cout << "The player has died\n";
IsRunning = false;
}
if (CurrentTurn >= 10) {
IsRunning = false;
}
}
bool saveGame(Player& player, const std::string& PlayerData)
{
std::ofstream saveFile(PlayerData);
if (saveFile.is_open())
{
saveFile << "Level: " << player.GetLevel() << std::endl;
saveFile << "Gold: " << player.GetGold() << std::endl;
saveFile << "Experience: " << player.GetExperience() << std::endl;
saveFile.close();
return true;
}
else
{
return false;
}
}
我尝试了几种不同的方法来写入文件,但我一定是在这个过程中搞砸了一些我没有理解的东西。