-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.cpp
More file actions
102 lines (86 loc) · 3.02 KB
/
Room.cpp
File metadata and controls
102 lines (86 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Room.h"
#include <stdexcept>
#include "Dragon.h"
#include "Goblin.h"
using namespace std;
int Room::roomsCapacity = 9;
Room::Room(const string &roomID, const string& monsterFirstChar,int campfire, int monsterCurrAmountOfLife, int monsterAttackValue): room_id(roomID) ,m_campfire(campfire), roomMonster(nullptr), roomsCounter(0) {
if (monsterCurrAmountOfLife < 0 || monsterAttackValue < 0) {
throw invalid_argument("Invalid input for room creation");
}
if (monsterCurrAmountOfLife != 0 && monsterAttackValue != 0) {
//create the room's monster
if (monsterFirstChar == "D") {
roomMonster = unique_ptr<Dragon>(new Dragon("Dragon", monsterCurrAmountOfLife, monsterAttackValue));
}
else if (monsterFirstChar == "G") {
roomMonster = unique_ptr<Goblin>(new Goblin("Goblin", monsterCurrAmountOfLife, monsterAttackValue));
}
}
}
//copy constructor
Room::Room(const Room &other) {
//copy simple fields
this->room_id = other.room_id;
this->m_campfire = other.m_campfire;
this->roomsCounter = other.roomsCounter;
this->roomsAccess = other.roomsAccess;
if (other.roomMonster)
this->roomMonster = unique_ptr<Monster>(other.roomMonster->clone());
else
this->roomMonster = nullptr;
}
Room::~Room() {
roomsAccess.clear();//handle the rooms memory at the Game destructor
}
// Assignment Operator
Room &Room::operator=(const Room *other) {
if (this != other) {
room_id = other->room_id;
m_campfire = other->m_campfire;
roomsAccess = other->roomsAccess;
roomsCounter = other->roomsCounter;
if (other->roomMonster) {
roomMonster = unique_ptr<Monster>(other->roomMonster->clone());
} else {
roomMonster = nullptr;
}
}
return *this;
}
Room *Room::operator[](int index) {
if (roomsCounter == 0 || index >= static_cast<int>(roomsAccess.size())) {
throw out_of_range("Index out of range");
} else {
return this->roomsAccess[index];
}
}
const Room *Room::operator[](int index) const {
if (roomsCounter == 0 || index >= static_cast<int>(roomsAccess.size())) {
throw out_of_range("Index out of range");
} else {
return this->roomsAccess[index];
}
}
int Room::getRoomCounter() const {
return roomsCounter;
}
// Set access to a specific room
bool Room::setRoomAccess(Room* room, int index) {
if (index < 0 || index >= roomsCapacity) {
throw out_of_range("Index out of range");
}
if (!room) {
throw invalid_argument("Invalid input: room is null");
}
// Ensure the vector can accommodate the specified index
if (index >= static_cast<int>(roomsAccess.size())) {
roomsAccess.resize(index + 1, nullptr);
}
roomsAccess[index] = room;
roomsCounter = index + 1;
return true; // Assignment successful
}
string Room::getID() const {
return room_id;
}