Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Finish the getAction function and the gameIsNotDone function. Please write the code based on the comments and current code and I will make tweaks as

Finish the getAction function and the gameIsNotDone function. Please write the code based on the comments and current code and I will make tweaks as needed.
#include
#include
#include
struct Room
{
enum class Name { cell, gate, armory, jailers, exit };
enum class Direction { N, S, E, W, none };
std::string message; // the message displayed when in the room.
std::vector doors;// the directions in which there are doors.
std::vector connectedRoom;// the name of the room in which the corresponding door leads.
//NOTE:: these two vectors will eventually become a std::map after the STL level.
bool hasKey{ false }; //whether or not the player has picked up the key.
};
//struct keeps track of the room the player is in, their health, and rather or not they have picked up the key.
struct Player
{
Room::Name currentRoom{ Room::Name::cell };
int health{10};
bool hasKey{ false }
std::vector buildMap();
void randomizeKey(std::vector& map);
bool changeGameState(Room::Direction action, Player& player, const std::vector& map);
bool gameIsNotDone(const Player& player);
int main(){
/*Splash Screen*/
clearConsole();
splashScreen();
/*set up game*/
std::vector map{ buildMap()};
Player player;
/*Until Game Termination Condition*/
while (gameIsNotDone(player))
{
/*Display Game State*/
clearConsole();
displayGameState(player, map);
/*Collect Player Action*/
Room::Direction action{ getAction()};
/*Update Game State*/
if (!changeGameState(action, player, map))
{
displayIllegalMove(player, action);
}
}
/*Display Termination Game State*/
displayGameDone(player, map);
return 0;
}
std::vector buildMap()
{
std::vector map;
map.push_back(
{
"A small, dark prison cell with doors South and East.",
{Room::Direction::S, Room::Direction::E},
{Room::Name::armory, Room::Name::gate},
false
});
map.push_back(
{
"A large, torchlit room with doors West, South, and East.
There is daylight entering under the door to the East.",
{Room::Direction::W, Room::Direction::S, Room::Direction::E},
{Room::Name::cell, Room::Name::jailers, Room::Name::exit},
false
});
map.push_back(
{
"A store room with doors North and East.",
{Room::Direction::N, Room::Direction::E},
{Room::Name::cell, Room::Name::jailers},
false
});
map.push_back(
{
"A jailer's barracks with doors West and North.",
{Room::Direction::W, Room::Direction::N},
{Room::Name::armory, Room::Name::gate},
true
});
map.push_back(
{
"YOU FOUND THE KEY AND ESCAPED!",
{},
{},
false
});
return map;
}
void randomizeKey(std::vector& map){
//randomly places key in any room
int keyRoomIndex = rand()% map.size();
//hasKey = true for selected room
map[keyRoomIndex].hasKey = true;
}
void displayGameState(const Player& player, const std::vector& map){
//player info
std::cout << "Player Health: "<< player.health << std::endl;
std::cout << "Current Room: "<< static_cast(player.currentRoom)<< std::endl;
std::cout << "HasKey: "<< std::boolalpha << player.hasKey << std::endl;
//room info
std::cout << "Current Room Message: "<< map[static_cast(player.currentRoom)].message << std::endl;
std::cout << "Doors: ";
for (const auto& direction : map[static_cast(player.currentRoom)].doors){
std::cout << static_cast(direction)<<"";
}
std::cout << std::endl;
}
//output messages based on if the player has won or lost
void displayGameDone(const Player& player, const std::vector& map)
{
if (player.currentRoom == Room::Name::exit){
std::cout << "Congratulations! You have won!";
}
else {
std::cout << "Game Over!";
}
}
//output illegal move messages depending if player tries to exit without the key
//or the player tries to exit the wrong way out of a room.
void displayIllegalMove(const Player& player, Room::Direction action)
{
std::cout << "Illegal Move!";
if (action == Room::Direction::none){
std::cout << "Invalid direction";
}
else if (player.hasKey){
std::cout << "The only exit is the exit room.";
}
else {
std::cout << "You must find the key first before exiting.";
}
}
//ask for user input, convert char to Room::Direction
getAction(){
}
bool changeGameState(Room::Direction action, Player& player, const std::vector& map)
{
//checks if door is valid
auto currentRoomIndex = static_cast(player.currentRoom);
auto& currentRoom = map[currentRoomIndex];
auto doorRoom = std::find(currentRoom.doors.begin(), currentRoom.doors.end(), action);
if (doorRoom != currentRoom.doors.end()){
//condition if door is valid
auto connectedRoomIndex = static_cast(currentRoom.connectedRoom[doorRoom - currentRoom.doors.begin()]);
player.currentRoom = map[connectedRoomIndex].hasKey ? Room::Name::exit : map[connectedRoomIndex].connectedRoom[0];
return true;
}
//invalid will return false
return false;
}
//Check the end-of-game conditions. i.e If the player is out
//of health or the player has reached the exit
bool gameIsNotDone(const Player& player)
{
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

6. Identify characteristics of whiteness.

Answered: 1 week ago

Question

9. Explain the relationship between identity and communication.

Answered: 1 week ago