-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.cpp
More file actions
354 lines (315 loc) · 8.98 KB
/
Character.cpp
File metadata and controls
354 lines (315 loc) · 8.98 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Implementation of Character Class
//Brian Terry
//28 November 2019
//Includes
#include<iomanip>
#include <string>
#include <cstdlib>
#include<ctime>
#include "Character.h"
using namespace std;
int Character::NUM_STATS = 6;
Character::Character() {
mm = new MessageManager();
loadMessages();
id = -1;
name = "NO NAME";
raceType = "NO RACE";
classType = "NO CLASS";
level = 0;
armourClass = 0;
hitPoints = 0;
stats = new int[NUM_STATS];
rollStats();
inventory.clear();
}
Character::Character(const Character &obj){
mm = new MessageManager();
for (int i = 0; i < obj.mm->getMessage(); i++) {
string msg = obj.mm->getMessage(i);
mm->addMessage(i,msg);
}
id = obj.id;
name =obj.name;
raceType = obj.classType;
level = obj.level;
armourClass = obj.armourClass;
hitPoints = obj.hitPoints;
stats = new int[NUM_STATS];
for (int i = 0; i < NUM_STATS; i++){
stats[i] = obj.inventory;
}
inventory.clear();
for (Item item : obj.inventory) {
Item temp;
inventory.push_back(temp);
}
}
Character::~Character() {
inventory.clear();
delete mm;
mm = nullptr;
delete [] stats;
stats = nullptr;
}
void Character::loadMessages() {
mm->addMessage(LINE1, "==================================");
mm->addMessage(LINE2, "----------------------------------");
string str = "";
str += mm->getMessage(LINE1);
str += "\n UPDATE INVENTORY MENU\n";
str += mm->getMessage(LINE1);
str += "\n1)Add new item\n";
str += "2) Modifiy existing item \n";
str += "3) Delete exisiting item\n";
str += "0) Return to modify Character menu\n\n";
mm->addMessage(MENU_INV_MOD, str);
str.clear();
str += mm->getMessage(LINE1);
str += "\n UPDATE ITEM MENU\n";
str += mm->getMessage(LINE1);
str += "\n1) Modify name\n";
str += "2) Modify quantity\n";
str += "0) Return to modify Character menu\n\n";
mm->addMessage(MENU_ITEM_MOD, str);
mm->addMessage(PROMPT_CHOICE, "Please select an otption from the menu: ");
mm->addMessage(PROMPT_ITEM_MOD, "Please enter ID of Item to modify: ");
mm->addMessage(PROMPT_ITEM_DEL, "Please enter ID of Item to delete: ");
str.clear();
str += mm->getMessage(LINE1);
str += "\n DETAILED CHARACTER INFORMATION\n";
str += mm->getMessage(LINE1);
mm->addMessage(HEADER_FULL, str);
str.clear();
str += mm->getMessage(LINE2);
str += "\n HP AC STR DEX CON INT WIS CHA\n";
str += mm->getMessage(LINE2);
mm->addMessage(HEADER_STATS, str);
str.clear();
str += mm->getMessage(LINE2);
str += "\n INVENTORY\n";
str += mm->getMessage(LINE2);
mm->addMessage(HEADER_INV, str);
mm->addMessage(ERR_INVALID_CHOICE, "Sorry, that is not a valid selection! Please try again.\n\n");
mm->addMessage(ERR_INVALID_CHOICE, "Sorry, that ID was not found!\n\n");
}
string Character::summary() {
string s ="[" + to_string(id) + "] ";
if (id < 10) {
s += " ";
}
s += name + ", Level ";
s += to_string(level) + " ";
s += raceType + " ";
s += classType + "\n";
return s;
}
void Character::rollStats() {
static unsigned seed = 0;
if (!seed) { seed = time(0; srand(seed); }
int sides = 6;
int d1, d2, d3, d4, min;
for (int i = 0; i < NUM_STATS; i++) {
//roll4 random dice
d1 =(rand() % sides) + 1;
d2 =(rand() % sides) + 1;
d3 =(rand() % sides) + 1;
d4 =(rand() % sides) + 1;
//find min
min = d1;
min = d2 < min ? d2 : min;
min = d3 < min ? d3 : min;
min = d4 < min ? d4 : min;
//toss out smallest and set stat
stats[i] = d1 + d2 + d3 + d4 - min;
}
}
void Character::updateInventory() {
string str;
int index;
bool flag = true;
while (flag) {
system("cls");
cout << *this << endl << endl;
cout << mm->getMessage(MENU_INV_MOD);
cout << mm->getMessage(PROMPT_CHOICE);
getline(cin, str);
cout << endl;
switch (stoi(str)) {
case 1:
addItem();
break
case 2:
modifyItem();
break;
case 3:
deleteItem();
break;
case 0:
flag = false;
break;
default:
cout << mm->getMessage(ERR_INVALID_CHOICE);
system("pause");
break;
}
}
}
void Character::addItem() {
Item item;
cin >> item;
item.setId(inventory.size() + 1);
inventory.push_back(item);
}
void Character::modifyItem() {
string str;
int index;
cout << mm->getMessage(PROMPT_ITEM_MOD);
getline(cin, str);
if (isValidId(stoi(str))) {
bool flag = true;
index = stoi(str) - 1;
while (flag) {
system("cls");
cout << inventory[index] << endl << endl;
cout << mm->getMessage(MENU_ITEM_MOD);
cout << mm->getMessage(PROMPT_CHOICE);
getline(cin, str);
cout << endl;
switch (stoi(str)) {
case 1:
cout << "New name: ";
getline(cin, str);
inventory[index].setName(str);
break;
case 2:
cout << "New quantity: ";
getline(cin, str);
inventory[index].setQuantity(stoi(str));
break;
case 0:
flag = false;
break;
default:
cout << mm->getMessage(ERR_INVALID_CHOICE);
system("pause");
break;
}
}
}
else {
cout << mm->getMessage(ERR_ID_NOT_FOUND) << endl;
system("pause");
}
}
void Character::deleteItem() {
string str;
int index, newId = 1;
cout << mm->getMessage(PROMPT_ITEM_DEL);
getlint(cin, str);
if(isValidId(stoi(str))) {
index = stoi(str) - 1;
inventory.erase(inventory.begin() + index);
for (int i = 0; i < inventory.size(); i++) {
inventory[i].setId(newId);
newId++;
}
}
else {
cout << mm->getMessage(ERR_ID_NOT_FOUND) << endl;
}
}
void Character::saveCharacter(DataManager* dm) {
dm->writeInt(id);
dm->writeString(name);
dm->writeString(raceType);
dm->writeString(classType);
dm->writeInt(level);
dm->writeInt(armourClass);
dm->writeInt(hitPoints);
for (int i = 0; i < NUM_STATS; i++) {
inventory[i].saveItem(dm);
}
}
void Character::loadCharacter(DataManager* dm) {
id = dm->readInt();
name = dm->readString();
raceType = dm->readString();
classType = dm->readString();
level = dm->readInt();
armourClass = dm->readInt();
hitPoints = dm->readInt();
for (int = 0; i < NUM_STATS; i++) {
stats[i] = dm.readInt();
}
inventory.clear();
int numItems = dm->readInt();
for (int i = 0; i <numItems; i++) {
Item temp;
temp.loadItem(dm);
inventory.push_back(temp);
}
}
const Character Character::operator =(const Character &rhs)
{
if (this != &rhs){
mm = new MessageManager();
for (int i = 0; i < rhs.mm->count(); i++) {
string msg = rhs.mm->getMessage(i);
mm->addMessage(i, msg);
}
id = rhs.id;
name = rhs.name;
raceType = rhs.raceType;
classType = rhs.classType;
level = rhs.level;
armorClass = rhs.armorClass;
hitPoints = rhs.hitPoints;
stats = new int[NUM_STATS];
for (int i = 0; i < NUM_STATS; i++) {
stats[i] = rhs.stats[i];
}
inventory.clear();
for (Item item : rhs.inventory) {
Item temp = item;
inventory.push_back(temp);
}
}
return *this;
}
ostream& operator <<(ostream &strm, const Character &obj) {
strm << "[" << obj.id << "} ";
if (obj.id < 10) {
strm << " ";
}
strm << obj.name;
strm << ", Level " << obj.classType << endl;
strm << obj.mm->getMessage(Character::HEADER_STATS) << endl;
strm << setw(4) << obj.hitPoints << setw(5) << obj.armourClass;
for (int i = 0; i < obj.inventory.size(); i++) {
strm << obj.inventory[i] << endl;
}
return strm;
}
istream& operator >>(istream &strm, Character &obj) {
string str;
cout << "Character name: ";
getline(strm, str);
obj.name = str;
cout << "Character race: ";
getline(strm, str);
obj.raceType = str;
cout << "Character class: ";
getline(strm, str);
obj.classType = str;
cout << "Character level: ";
getline(strm, str);
obj.level = stoi(str);
cout << "Character armour class (AC): ";
getline(strm, str);
obj.armourClass = stoi(str);
cout << "Character health (HP): ";
getline(strm, str);
obj.hitPoints = stoi(str);
return strm;
}