I am currently making a text-based game. In this game, the character has to move around between rooms. Each room as a "roomAddress" or "roomId". Currently, I am trying to make the options for a level. Then, when an option is made, the character moves or does something like pick up an item.
Right now, I have the base for moving between rooms set up. I also have created 2 room IDs: "Introduction" and "StartingRoom_1_f_1". However, when trying to move from "Introduction" to "StartingRoom_1_f_1", I get an error message (
NOT a compileation error, a self-made error handling message). This message reads:
cout << "Next room not defined for the chosen option." << endl;
Note: I'm not entirely sure what all needs to be included, so I wil try to keep it short, but it may not work right away. Please tell me if this is so.
main.cpp:
#include "StarterIncludeFile.hpp" //Assume that all needed includes are here. So if there is an include you need, it's wrapped in here
#include "entities.hpp"
#include "levels.hpp"
#include "outputHandling.hpp"
#include "playLevels.hpp"
#include "inputHandling.hpp"
int main() {
createValidInput();
Player player = createPlayer();
vector<item> itemsInInventory = player.itemsInInventory;
initializeLevelInfo();
playGame();
return 0;
}
levels.hpp:
vector<string> createOptions(string roomId, string progression);
string createDescription(string roomId, string progression);
vector<item> itemsOnFloor;
struct LevelInfo {
std::string description;
std::vector<std::string> options;
};
std::unordered_map<std::string, std::unordered_map<std::string, LevelInfo>> levelInfo;
unordered_map<string, unordered_map<string, string>> transitions;
void initializeLevelInfo() {
levelInfo["Introduction"]["beginning"] = {
"In the vast expanse of the cosmos, humanity teeters on the edge of insignificance, our existence but a fleeting whisper in the echoing\nchambers of eternity.\n"
"We scurry about like ants beneath the gaze of titanic beings, ancient gods who regard us with indifferent eyes, if they regard us at\nall.\n"
"In this realm where mortal coils and consciousness unravel, we are but morsels of flesh adrift in a sea of shadows.\n\n"
"Our cities, our achievements, our very essence, pale against the backdrop of cosmic indifference.\n"
"Yet amidst the darkness, whispers linger of forgotten truths, of entities that loom beyond the veil of comprehension.\n"
"Their presence whispers through the rustling leaves, their footsteps echoing in the silence of the night.\n\n"
"There are those who speak of a shepherd, a figure shrouded in myths and legends, whose gaze transcends the boundaries of mortality.\n"
"They say he walks among us, his presence a beacon of hope amidst the encroaching darkness.\n"
"But beware, for even the shepherd's light may cast long shadows, and salvation may come at a price too terrible to comprehend.\n\n"
"As you venture forth into this realm of uncertainty, remember the fragility of human life, the insignificance of our existence in the eyes of our gods and beings beyond mortal ken.\n"
"For in this world of shadows and whispers, even the faintest glimmer of hope may be swallowed whole by the abyss.",
{"Continue"} };
levelInfo["StartingRoom_1_f_1"]["beginning"] = {
"You wake up in your bedroom. It's not exactly the cleanest... or the brightest, but it's home.\n"
"You rise out of bed and stand up.\n"
"It's time to get to work... the day is fresh.",
{"Walk Forward", "Check Inventory"}
};
transitions["Introduction"]["Option 1"] = "StartingRoom_1_f_1";
}
struct Level {
string displayName;
string levelAddress;
int floorNumber;
string description;
vector<string> options;
vector<item> itemsOnFloor;
bool doesGameContinue;
};
Level createLevel(string roomId, string displayName, string progression, int floorNumber, vector<item> itemsOnFloor) {
Level level;
level.displayName = displayName;
level.levelAddress = roomId;
level.floorNumber = floorNumber;
level.description = levelInfo[roomId][progression].description;
level.options = levelInfo[roomId][progression].options;
level.itemsOnFloor = itemsOnFloor;
level.doesGameContinue = true; return level;
}
vector<Level> beginningLevels {
createLevel("Introduction", "Introduction", "beginning", 0, {} ),
createLevel("StartingRoom_1_f_1", "Starting Room, Facing Forward", "beginning", 1, {{"Coins", 5, false, "Currency"}})
};
outputHandling.hpp:
void displayDescription(string roomId, string progression) {
string description;
if (levelInfo.find(roomId) != levelInfo.end() &&
levelInfo[roomId].find(progression) != levelInfo[roomId].end()) {
description = levelInfo[roomId][progression].description;
}
if (!description.empty()) {
cout << description << endl << endl;
} else {
cout << "Description not found for the given roomId and progression." << endl;
}
}
void displayOptions(string roomId, string progression) {
vector<string> options;
if (levelInfo.find(roomId) != levelInfo.end() &&
levelInfo[roomId].find(progression) != levelInfo[roomId].end()) {
options = levelInfo[roomId][progression].options;
} else {
cout << "Room ID or progression not found." << endl;
return;
}
cout << "Your options: " << endl;
if (!options.empty()) {
for (int i = 0; i < options.size(); ++i) { cout << i + 1 << ". " << options[i] << endl; }
} else { cout << "No options available for this level." << endl; }
cout << endl << endl;
}
entities.hpp:
struct item {
string name;
int quantityOnGround;
bool hasBeenUsed;
string className;
int quantityCollected;
int currentQuantity;
};
struct Player {
int numberItems;
int numberDeaths;
vector<item> itemsInInventory;
string roomAddress;
int floorNumber;
};
void initializePlayer(Player& player) {
player.numberItems = 0;
player.numberDeaths = 0;
player.roomAddress = "Starting Room";
player.floorNumber = 0;
handItem(player, "NONE", 0, false, "NAN", 0, 0);
}
Player createPlayer() {
Player player;
initializePlayer(player);
return player;
}
playLevels.hpp:
string actionChoice;
bool gameContinues = true;
string beginningProgressionRoomID = ""; string middleProgressionRoomID = "";
int playLevel(string roomId, string progression) {
displayDescription(roomId, progression);
displayOptions(roomId, progression);
cout << "What would you like to do? ";
int actionChoice;
cin >> actionChoice;
return actionChoice;
}
void playGame() {
string roomId = "Introduction"; string progression = "beginning";
while (true) {
int choice = playLevel(roomId, progression);
if (choice >= 1 && choice <= levelInfo[roomId][progression].options.size()) {
string chosenOption = levelInfo[roomId][progression].options[choice - 1];
if (transitions.find(roomId) != transitions.end() &&
transitions[roomId].find(chosenOption) != transitions[roomId].end()) {
roomId = transitions[roomId][chosenOption];
if (roomId == beginningProgressionRoomID) { progression = "middle"; } else
if (roomId == middleProgressionRoomID) { progression = "final"; } else
{ progression = "beginning"; }
} else {
cout << "Next room not defined for the chosen option." << endl;
return;
}
} else {
cout << "Invalid option. Please choose again." << endl;
}
}
endGame();
}
What I have tried:
I have tried changing some if statements, refactoring my levels, creating and deleting random things, almost anything. I can't actually figure out what is happening.
I have done some couts and checks. In "outputHandling.hpp", the line
if (levelInfo.find(roomId) != levelInfo.end() &&
levelInfo[roomId].find(progression) != levelInfo[roomId].end())
is determined as false. Causing the error message to run.
As a side note: One of the last versions of this code I had used a "nextRoomId" which (as implied) was the ID of the next room.If I did
cout << nextRoomID << ": next room id";
I would get a blank space,