forked from ceciivanov/OOP_RPG_GameProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
484 lines (366 loc) · 11.5 KB
/
Game.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "Game.h"
Game::Game() {
std::cout << "Welcome to our game!" << std::endl;
map = new Grid(MAP_SIZE);
marketPlace = createMarket();
}
Game::~Game() {
std::cout << "Quit Game... Bye" << std::endl;
delete map;
delete marketPlace;
delete squad;
}
Market* Game::createMarket() {
marketPlace = new Market();
//random items for the market
Item* armor = new Armor("Emblem", 100, 1, 70);
marketPlace->addItem(armor);
armor = new Armor("Genji Shield", 150, 5, 110);
marketPlace->addItem(armor);
armor = new Armor("Rescue Shield", 200, 10, 200);
marketPlace->addItem(armor);
armor = new Armor("Gold Shield", 500, 15, 350);
marketPlace->addItem(armor);
Item* weapon = new Weapon("HeartBreaker", 100, 1, 100, true);
marketPlace->addItem(weapon);
weapon = new Weapon("Crystal Sword", 200, 4, 150, false);
marketPlace->addItem(weapon);
weapon = new Weapon("Full Metal Rod", 350, 8, 200, false);
marketPlace->addItem(weapon);
weapon = new Weapon("Darkness Staff", 450, 14, 350, true);
marketPlace->addItem(weapon);
Item* potion = new Potion("Antidote", 100, 1, 200, Health);
marketPlace->addItem(potion);
potion = new Potion("Mana Tonic", 180, 3, 250, Mana);
marketPlace->addItem(potion);
potion = new Potion("Power Distiller", 250, 7, 100, Strength);
marketPlace->addItem(potion);
potion = new Potion("Mega-Potion", 340, 12, 70, Dexterity);
marketPlace->addItem(potion);
potion = new Potion("Speed Distiller", 400, 15, 55, Agility);
marketPlace->addItem(potion);
Spell* spell = new FireSpell("Flame", 100, 1, 150, Range(100, 300), 50);
marketPlace->addSpell(spell);
spell = new IceSpell("Freeze", 150, 4, 300, Range(200, 350), 80);
marketPlace->addSpell(spell);
spell = new LightingSpell("Angel’s Mercy", 250, 8, 400, Range(250, 450), 5);
marketPlace->addSpell(spell);
spell = new FireSpell("Fireball", 350, 14, 500, Range(350, 550), 5);
marketPlace->addSpell(spell);
return marketPlace;
}
void Game::createHeroes() {
int n;
while (true)
{
std::cin >> n;
if ( n > MAX_TEAMMATES )
{
std::cout << "Please choose valid number of heroes" << std::endl;
continue;
}
break;
}
squad = new HeroSquad(n);
//random names for heroes
std::string names[10] = {"Karontor","Memnor","Luthic","Merlin","Sixin",
"Ilneval", "Kratos", "Eadro", "Skerrit", "Jubilex"};
std::vector<std::string> usedNames;
for(int i = 0; i < n; i++)
{
int HeroType = (int) random() % 3 + 1; //[1, 3]
Hero *hero = nullptr;
//check for duplicate names
std::string name = getUnusedName(usedNames, names);
switch (HeroType)
{
case warrior:
hero = new Warrior(name);
break;
case paladin:
hero = new Paladin(name);
break;
case sorcerer:
hero = new Sorcerer(name);
break;
default:
break;
}
squad->setHero(hero);
}
}
MonsterSquad *Game::createEnemies() {
//some random names for the monsters creation
std::string names[10] = {"Abbathor","Arvoreen","Ehlonna","Heironeous","Pelor",
"Urdlen", "Bahamut", "Orcus", "Vaprak", "Bahgtru"};
int size = (int)random() % 3 + 1; //create random from 1 to 3 monsters
MonsterSquad* enemies = new MonsterSquad(size);
int averageLevel = 0;
for(int i = 0; i < squad->getSize(); i++)
{
averageLevel += squad->getHero(i)->getLevel();
}
averageLevel = averageLevel/squad->getSize();
std::vector<std::string> usedNames;
for(int i = 0; i < size; i++)
{
int MonsterType = (int) random() % 3 + 1; //[1, 3]
Monster *monster = nullptr;
std::string name = getUnusedName(usedNames, names);
switch (MonsterType)
{
case dragon:
monster = new Dragon(name, averageLevel);
break;
case exoskeleton:
monster = new ExoSkeleton(name, averageLevel);
break;
case spirit:
monster = new Spirit(name, averageLevel);
break;
default:
break;
}
enemies->setMonster(monster);
}
return enemies;
}
void Game::play() {
std::cout << "Please create your Hero Squad of (1-3) heroes" << std::endl;
createHeroes();
std::cout << "Your Hero Squad is ready: " << std::endl;
squad->print();
std::cout << "Move your heroes by using keys (w, a, s, d) for up, left, down and right" << std::endl;
std::cout << "You can see map by pressing (m)" << std::endl;
std::cout << "You can quit game by pressing (q)" << std::endl;
std::cout << "You can check inventory of your heroes by pressing (i)" << std::endl;
std::cout << "You can check your heroes stats by pressing (c)\n" << std::endl;
//heroes start at coordinates 0,0 of map which contains market
Square* currentPos = map->getSquare(0, 0);
squad->move(currentPos);
if (!enterMarket())
return;
unsigned int x = -1;
unsigned int y = -1;
while(true)
{
unsigned int tempX = x;
unsigned int tempY = y;
if (!playerMove(currentPos, x, y))
return;
//check if player is at same coordinates
if ( (x == -1 && y == -1) || (x == tempX && y == tempY) )
continue;
if (map->outOfBounds(x, y))
{
std::cout << "You are going out of bounds please try again" << std::endl;
}
else
{
Square* next = map->getSquare(x, y);
if (next->getType() == nonAccessible)
{
std::cout << "Non-Accessible square, please go to another direction" << std::endl;
continue;
}
else if (next->getType() == market)
{
currentPos->setSquad(nullptr);
squad->move(next);
if (!enterMarket())
return;
}
else
{
currentPos->setSquad(nullptr);
squad->move(next);
if (!enterCommon())
return;
}
currentPos = map->getSquare(x, y);
}
}
}
bool Game::playerMove(Square *currentPos, unsigned int &x1, unsigned int &y1) {
unsigned int x = currentPos->getX();
unsigned int y = currentPos->getY();
std::cout << "Move your heroes" << std::endl;
char button;
std::cin >> button;
switch (button)
{
case MOVE_UP:
x1 = x + 1;
y1 = y;
break;
case MOVE_DOWN:
x1 = x - 1;
y1 = y;
break;
case MOVE_LEFT:
x1 = x;
y1 = y - 1;
break;
case MOVE_RIGHT:
x1 = x;
y1 = y + 1;
break;
case SHOW_MAP:
map->displayMap();
break;
case SHOW_STATS:
std::cout << "Your Heroes: " << std::endl;
squad->print();
break;
case OPEN_INVENTORY:
if(!squad->checkInventory()){
return false; //if player quits game while checking inventory
}
break;
case QUIT:
case QUIT_:
return false;
default:
std::cout << "Please enter a valid button" << std::endl;
break;
}
return true;
}
bool Game::enterMarket() {
std::cout << "You have entered a marketPlace do you want to open market? Y/N" << std::endl;
char answer;
std::cin >> answer;
while ( answer != YES && answer != YES_ && answer != NO && answer != NO_ && answer != QUIT && answer != QUIT_)
{
std::cout << "Invalid input please try again" << std::endl;
std::cin >> answer;
}
if (answer == YES || answer == YES_)
{
int i = 0;
while ( i < squad->getSize() )
{
std::cout << "Do you want to open market for " << squad->getHero(i)->getName() << "? Y/N" << std::endl;
std::cin >> answer;
if (answer == YES || answer == YES_)
{
marketPlace->open(squad->getHero(i));
}
else if (answer == NO || answer == NO_)
{
i++;
continue;
}
else if (answer == QUIT || answer == QUIT_)
{
return false;
}
else
{
std::cout << "Invalid input please try again" << std::endl;
continue;
}
i++;
}
}
else if (answer == QUIT || answer == QUIT_)
{
return false;
}
return true;
}
bool Game::enterCommon() {
std::cout << "You have entered common square" << std::endl;
if (!Fight::begin())
{
std::cout << "No monsters around..." << std::endl;
return true;
}
//each time heroes get evolved in fight generate random monsters in a form of team to fight them
MonsterSquad* enemies = createEnemies();
Fight fight(squad, enemies);
int round = 1;
while (fight.isNotOver())
{
std::cout << "Round: " << round << std::endl;
std::cout << "Your Turn" << std::endl;
//if player want to quit game during the fight
if (!fight.playerTurn())
{
delete enemies;
return false;
}
if (enemies->defeated())
break;
std::cout << "Enemies Turn" << std::endl;
fight.enemiesTurn();
//reduce active spells on each monster
enemies->unchargeActiveSpells();
//regenerate stats of both heroes and enemies
squad->regeneration();
enemies->regeneration();
round++;
}
if (squad->defeated())
{
std::cout << "YOU WERE DEFEATED!" << std::endl;
defeat();
}
else
{
std::cout << "VICTORY!" << std::endl;
victory(enemies->getSize());
}
delete enemies;
return true;
}
void Game::victory(int monstersDefeated) {
for (int i = 0; i < squad->getSize(); i++)
{
Hero* hero = squad->getHero(i);
if (hero->dead())
{
hero->revive();
}
}
for (int i = 0; i < squad->getSize(); i++)
{
Hero* hero = squad->getHero(i);
int level = hero->getLevel();
int xp = (level * 2 + 3) * monstersDefeated;
int money = (level*50) * monstersDefeated;
std::cout << hero->getName() << " + " << money << " money" << " + " << xp << " xp " << " ";
hero->addExperience(xp);
hero->earnMoney(money);
}
}
void Game::defeat() {
squad->revive();
for (int i = 0; i < squad->getSize(); i++)
{
Hero* hero = squad->getHero(i);
hero->looseMoney();
}
}
std::string Game::getUnusedName(std::vector<std::string> &usedNames, std::string *names) {
while (true)
{
std::string name;
int r = (int)random() % 10;
bool found = false;
for (int i = 0; i < usedNames.size(); i++)
{
if (names[r] == usedNames[i])
{
found = true;
break;
}
}
if (!found)
{
usedNames.push_back(names[r]);
name = names[r];
return name;
}
}
}