Skip to content

Instantly share code, notes, and snippets.

@CodingDino
Created May 2, 2024 16:16
Show Gist options
  • Save CodingDino/0e2c688f1eee885379f4b01c6ee2f4c6 to your computer and use it in GitHub Desktop.
Save CodingDino/0e2c688f1eee885379f4b01c6ee2f4c6 to your computer and use it in GitHub Desktop.
Example of the use of Access Modifiers, Getters, and Setters in an Adventure Game
// Libraries
#include <string>
#include <iostream>
#include <vector>
#include "Room.h"
// Needed to avoid having to type std:: a lot!
using namespace std;
void TempFuncRoomDestructorTest(Room* existingRoom)
{
Room tempRoom("TempName", "TempDesc");
tempRoom.AddExit(existingRoom);
existingRoom->AddExit(&tempRoom);
}
// Function to process the player's input
int ReadPlayerChoice(Room * &currentRoom, int& stamina)
{
// INITIALISE a local bool vairable called processInput, to true
bool processInput = true;
// WHILE processInput is true...
while (processInput)
{
// INITIALISE a local playerChoice variable
int playerChoice = 0;
// DISPLAY the player's current stamina
cout << "Stamina remaining: " << stamina << endl;
// DISPLAY the player's current stamina
cout << "Where would you like to go? (enter the number)" << endl;
// READ the player's response into the playerChoice variable
cin >> playerChoice;
// IF the player's choice between 1 and the roomNames vector size...
if (playerChoice >= 1 && playerChoice <= currentRoom->GetNumExits())
{
currentRoom = currentRoom->GetExit(playerChoice-1);
return playerChoice;
}
// ELSE IF the player's choice is equal to the size of the room vector...
else if (playerChoice == currentRoom->GetNumExits() + 1)
{
return -1;
}
// ELSE
else
{
// DISPLAY an error message due to unrecognised input
cout << "Sorry, the number you typed was not one of the options: " << playerChoice << endl;
// CLEAR the input stream
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} // END IF
} // ENDWHILE
}
// Main function where program code execution begins and ends.
int main()
{
// INITIALISE variables
string playerName = "";
int playerChoice = -1;
int moveCost = 5;
int playerStamina = 20;
bool exit = false;
// Create rooms
Room courtyard("Courtyard", "A barren, weed-filled courtyard of a crumbling ruined castle.");
Room entryHall("Entry Hall", "A once-grand hll with a rotting red carpet, lined with rusted suits of armour.");
Room gate("Gate", "The dilapidated gates hang crookedly from their hinges, set into the crumbling stone wall that surrounds the ruined castle.");
courtyard.AddExit(&entryHall);
courtyard.AddExit(&gate);
gate.AddExit(&courtyard);
entryHall.AddExit(&courtyard);
Room * currentRoom = &courtyard;
// TEST our room destructor. If successful, we should not have a connection to the temp room as it will delete itself
TempFuncRoomDestructorTest(&courtyard);
// DISPLAY the title of the program.
cout << "Welcome to the Example Adventure Game!" << endl;
// DISPLAY a message to the player asking their name.
cout << "What are you called, brave adventurer?" << endl;
// READ the player's response into the playerName variable
getline(cin, playerName);
cout << endl;
// DISPLAY a greeting to the player, using the playerName variable
cout << "Welcome, " << playerName << ", soon to be hero of the realm!" << endl << endl;
// WHILE exit is false...
while (!exit)
{
// CALL Print() for the current room
currentRoom->Print();
// SET platerChoice to the result of a CALL to ReadPlayerChoice(), passing the current room and stamina.
playerChoice = ReadPlayerChoice(currentRoom, playerStamina);
// IF the player's choice is -1...
if (playerChoice == -1)
{
// DISPLAY a goodbye message to the player
cout << "Thank you for playing!" << endl;
// SET exit to true
exit = true;
}
// ELSE
else
{
// SUBTRACT the stamina required to move from the player's stamina value
playerStamina = playerStamina - moveCost;
// DISPLAY a message that the player has moved and used moveCost stamina
cout << "You have changed rooms and used " << moveCost << " stamina." << endl;
}
// IF the player stamina reaches 0 or lower...
if (playerStamina <= 0)
{
// DISPLAY a message to the player telling them they have died
cout << "You have run out of stamina and died! Better luck next time. Please play again!" << endl;
// SET exit to true
exit = true;
}
// ENDIF
}
// END WHILE
}
#include "Room.h"
#include <iostream>
Room::Room()
: name("")
, desc("")
, exits()
{
}
Room::Room(string newName, string newDesc)
: name(newName)
, desc(newDesc)
, exits()
{
}
Room::~Room()
{
// Check each connected room
// and erase THIS room from it, since this room is being destroyed and cannot connect anymore.
for (int i = 0; i < exits.size(); ++i)
{
// Store the other room in a temporary variable to make our code more easy to understand
Room* otherRoom = exits[i];
// We will check the OTHER room's list of exits (exits[i]->exits) for THIS room (the "this" pointer)
// We use the find function to do this
auto it = find(otherRoom->exits.begin(), otherRoom->exits.end(), this);
// If the element was not found, it will be equal to the end() value.
// So if it is NOT equal to that, it was found, and we should erase it!
if (it != otherRoom->exits.end()) {
otherRoom->exits.erase(it);
}
}
}
void Room::Print()
{
// DISPLAY the the name of the room
cout << "You find yourself in the " << name << " - ";
// DISPLAY the room description
cout << desc << endl << endl;
// DISPLAY exits
cout << "Exits are:" << endl;
// FOR each room in the exits vector...
for (unsigned int i = 0; i < exits.size(); ++i)
{
// DISPLAY the loop index + 1 and the name of the room at that index
cout << (i + 1) << ". " << exits[i]->name << endl;
}
// END FOR
// DISPLAY a final option to quit, numbered equal to the size of roomNames+1
cout << (exits.size() + 1) << ". " << "QUIT" << endl;
}
string Room::GetName()
{
return name;
}
string Room::GetDescription()
{
return desc;
}
void Room::AddExit(Room* newExit)
{
exits.push_back(newExit);
}
int Room::GetNumExits()
{
return exits.size();
}
Room* Room::GetExit(int index)
{
return exits[index];
}
#pragma once
#include <string>
#include <vector>
using namespace std;
class Room
{
private:
string name;
string desc;
vector<Room*> exits;
public:
Room();
Room(string newName, string newDesc);
~Room();
void Print();
string GetName();
string GetDescription();
void AddExit(Room* newExit);
int GetNumExits();
Room* GetExit(int index);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment