Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Finish the object header & class ( update function in object.cpp ) , base code and specification provided below Specification: public: Must overload in the

Finish the object header & class (update function in object.cpp), base code and specification provided below
Specification:
public:
Must overload in the Guard and Player classes
update
For randomness
static std::random_device seed;
static std::default_random_engine engine;
add getters/setters AS NEEDED!
protected:
Room::Name currentRoom{ Room::Name::cell };
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"
#include
Player::Player(){}
void Player::update(Room::Name currentRoom){
this->currentRoom = currentRoom;
}
void Player::changeGameState(Room::Direction action, std::map& dungeonMap){
// Pick up the key if it is in this room
if (dungeonMap[currentRoom].hasKey){
hasKey = true;
dungeonMap[currentRoom].hasKey = false;
std::cout << "You picked up the key.
";
}
// If we are trying to go to the exit but don't have the key, display illegal move
if (currentRoom == Room::Name::gate && !hasKey && action == Room::Direction::E){
displayIllegalMove(*this, action);
return;
}
// Check for door
auto& exits = dungeonMap[currentRoom].doorways;
auto doorway = exits.find(action);
if (doorway != exits.end()){
currentRoom = doorway->second;
--health;
return;
}
// If there is not a connected room in that direction, it is illegal
displayIllegalMove(*this, action);
}
void Player::displayIllegalMove(const Player& player, Room::Direction action){
if (player.currentRoom == Room::Name::gate && !player.hasKey && action == Room::Direction::E){
std::cout << "The door is locked. Perhaps if you had a key???
";
} else {
std::cout << "You cannot go that way.
";
}
}
Room::Direction Player::getAction(){
char action;
std::cout << std::endl;
std::cout << "Select action: ";
std::cin >> action;
switch (action){
case 'w': return Room::Direction::N;
case 'a': return Room::Direction::W;
case 'd': return Room::Direction::E;
case 's': return Room::Direction::S;
default: return Room::Direction::none;
}
}
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"
#include
Guard::Guard(){
// Initialize current room randomly within the circular hallway
std::uniform_int_distribution distribution(static_cast(Room::Name::NW), static_cast(Room::Name::W));
currentRoom = static_cast(distribution(Object::engine));
// Randomly determine clockwise movement
std::uniform_int_distribution boolDistribution(0,1);
clockwise = boolDistribution(Object::engine);
}
//updates guards position
void Guard::update(){
// Calculate next room based on current room and direction
int currentRoomIndex = static_cast(currentRoom);
int numRooms = static_cast(Room::Name::numHallwayRooms);
// Determine next room index based on movement direction
int nextRoomIndex;
if (clockwise)
nextRoomIndex =(currentRoomIndex +1)% numRooms;
else
nextRoomIndex =(currentRoomIndex -1+ numRooms)% numRooms;
}

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

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions

Question

Calculate div(F) and curl(F). X y F= [x2 + y2' x2 + y2' 12.0)

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago