Question: Finish the functions in player.cpp . Update function is a copy paste from last assignments displayGameState function, however it also calls the changeGameState function. Add

Finish the functions in player.cpp. Update function is a copy paste from last assignments displayGameState function, however it also calls the changeGameState function. Add getters and setters as needed. The rest of the functions are an exact copy paste from previous assignment but with modification for being in a class. The only addition is in changeGameState outputting the proper message when a key other than w, a, s, d is pressed, displayIllegalMove is called rather than returning true or false.
Room.h
#ifndef ROOM_H
#define ROOM_H
#include
#include
#include
class Room
{
public:
enum class Name { exit =-4, cell, closet, pantry, NW, gate, NE, E, SE, S, SW, W, numHallwayRooms };
enum class Direction { N, S, E, W, none };
Room();
~Room();
bool exitRoom(Direction direction, Name& nextRoom);
void addDoor(Direction direction, Name room);
bool pickupKey();
void dropKey();
private:
Name name{ Name::NW };
std::map doorways;
bool hasKey{ false };
};
#endif //!ROOM_H
Room.cpp
#include "Room.h"
Room::Room(){}
Room::~Room(){}
bool Room::exitRoom(Direction direction, Name& nextRoom){
auto it = doorways.find(direction);
if (it != doorways.end()){
nextRoom = it->second;
return true;
}
return false;
}
void Room::addDoor(Direction direction, Name room){
doorways[direction]= room;
}
bool Room::pickupKey(){
if (hasKey){
hasKey = false;
return true;
}
return false;
}
void Room::dropKey(){
hasKey = true;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "Object.h"
#include "Room.h"
class Player:public Object
{
public:
Player();
void update(Room::Name currentRoom);
private:
void changeGameState();
void displayIllegalMove();
void getAction();
int health{10};
bool hasKey{ false };
};
#endif //!PLAYER_H
Player.cpp
#include "Player.h"
Player::Player(){}
//update player position
void Player::update(Room::Name currentRoom){
}
//changes game state
void Player::changeGameState(){
}
//illegal move message
void Player::displayIllegalMove(){
}
//gets player action
void Player::getAction(){
}
Object.h
#ifndef OBJECT_H
#define OBJECT_H
#include
#include "Room.h"
class Object
{
public:
virtual void update()=0;
static std::random_device seed;
static std::default_random_engine engine;
protected:
Room::Name currentRoom{ Room::Name::cell };
};
#endif //!OBJECT_H
Object.cpp
#include "Object.h"
std::random_device Object::seed;
std::default_random_engine Object::engine;
Object::Object(){
currentRoom = Room::Name::cell;
}
void Object::update(){
}
Guard.h
#ifndef GUARD_H
#define GUARD_H
#include "Object.h"
class Guard: public Object
{
public:
Guard();
void update();
private:
bool clockwise{ true };
};
#endif //!GUARD_H
Guard.cpp
#include "Guard.h"
Guard::Guard(){}
//updates guards position
void Guard::update(){
}
Source.cpp
#include
#include
#include
#include
#include
#include
#include "Room.h"
#include "Player.h"
#include "Guard.h"
//Bigger map
//guard chasing.
//find key and exit.
//bonus:guard moves towards player.
/* GUI Functions */
void splashScreen();
/* Engine Functions*/
std::map buildMap();
void randomizeKey(std::map& dungeonMap);
bool gameIsNotDone(const Player& player, const Guard& guard);
void displayGameDone(const Player& player, const Guard& guard, const std::map& dungeonMap);
int main(){
/*Splash Screen*/
system("CLR");
splashScreen();
/*set up game*/
std::map dungeonMap{ buildMap()};
Player player;
Guard guard;
/*Until Game Termination Condition Do: */
while (gameIsNotDone(player, guard))
{
/*Display Game State*/
system("CLS");
std::cout <<"P:"<<(int)player.getCurrentRoom()<< std::endl;
std::cout <<"G:"<<(int)guard.getCurrentRoom()<< std::endl;
player.update(dungeonMap);
guard.update(dungeonMap);
}
/*Display Termination Game State*/
displayGameDone(player, guard, dungeonMap);
return 0;
}
std::map buildMap()
{
//enum class Name { cell, gate, armory, jailers, exit };
std::map dungeonMap;
std::ifstream fin;
fin.open("dungeonLayout.txt");
if (fin.is_open())
{
int name{0};
int numDoors{0};
while (name <(int)Room::Name::numHallwayRooms-1)
{
fin >> name >> numDoors;
for (int i{0}; i < numDoors; i++)
{
int direction;
int room;
fin >> direction >> room;
dungeonMap[(Room::Name)name].addDoor((Room::Direction)direction,(Room::Name)room);
}
}
randomizeKey(dungeonMap);
}
return dungeonMap;
}
void randomizeKey(std::map& dungeonMap)
{
std::bernoulli_distribution closetVsPantry(.5);
if (closetVsPantry(Object::engine))
{
dungeonMap[Room::Name::closet].dropKey();
}
else
{
dungeonMap[Room::Name::pantry].dropKey();
}
}
void clearConsole(){
system("cls");
}
void pauseConsole(){
system("PAUSE");
}
void displayGameDone(const Player& player, const Guard& guard, const std::map& dungeonMap)
{
clearConsole();
if (player.getHealth()>0)
{
if (player.getCurrentRoom()== Room::Name::exit)
{
std::cout << "You found the key and escaped" << std::endl;
}
else if( player.getCurrentRoom()== guard.getCurrentRoom())
{
std::cout << "The guard caught you and you are thrown in cell!" << std::endl;
}
}
else
{
std::cout<< "You Died... RIP." << std::endl;
}
std::cout << std::endl;
pauseConsole();
//check end of game conditions if the player out of health or reached exit then game is over
bool gameIsNotDone(const Player& player, const Guard& guard){
return !(player.getHealth()<=0
|| player.getCurrentRoom()== Room::Name::exit
|| player.getCurrentRoom()== guard.getCurrentRoom()
);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!