-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDungeon.cpp
570 lines (472 loc) · 13.4 KB
/
Dungeon.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "Dungeon.h"
Dungeon::Dungeon(string name, int minimumLevelEnter, const vector<int> &minGold, const vector<int> &maxGold,const vector<vector<Monster>> &monsters, const vector<vector<vector<char>>> &stages, const vector<int> &goldByStages,
const vector<int> &expByStages, const vector<std::pair<Item, int>> &itemsByStage) {
this->name=name;
this->minimumLevelEnter = minimumLevelEnter;
this->monsters = monsters;
this->stages = stages;
this->itemsByStage = itemsByStage;
this->goldByStages = goldByStages;
this->expByStages = expByStages;
this->itemsByStage = itemsByStage;
this->minGold = minGold;
this->maxGold = maxGold;
this->level = stages.size();
//initalize the Fog_Of_Wall with vetcotr of 10x10 '?'
vector<char>temp;
vector<vector<char>> fog;
for (int i = 0; i < 10; i++)
temp.push_back('?');
for (int i = 0; i < 10; i++)
fog.push_back(temp);
//0,0 is where hero will be at first
fog[0][0] = 'H';
//push for each level
for (int i = 0; i < level; i++)
Fog_of_War.push_back(fog);
for(int i =0; i<level; i++)
this->levelClear.push_back(false);
}
Dungeon::Dungeon(string name, int minimumLevelEnter, const vector<int> &minGold, const vector<int> &maxGold, const vector<vector<Monster>> &monsters, string s1, const vector<int> &goldByStages,
const vector<int> &expByStages, const vector<std::pair<Item, int>> &itemsByStage) {
this->name=name;
this->minimumLevelEnter = minimumLevelEnter;
this->monsters = monsters;
this->itemsByStage = itemsByStage;
this->expByStages = expByStages;
this->goldByStages = goldByStages;
this->itemsByStage = itemsByStage;
this->minGold = minGold;
this->maxGold = maxGold;
this->level = s1.size() / 100;
int index = 0;
if (s1.size()%100 != 0) {
cerr << "size of the string not match" << endl;
}
for (int L = 0; L < level; L++) {
stages.push_back(vector<vector<char>>());
for (int i = 0; i < 10; i++) {
stages[L].push_back(vector<char>());
for (int j = 0; j < 10; j++) {
stages[L][i].push_back(s1[index]);
index++;
}
}
}
//initalize the Fog_Of_Wall with vetcotr of 10x10 '?'
vector<char>temp;
vector<vector<char>> fog;
for (int i = 0; i < 10; i++)
temp.push_back('?');
for (int i = 0; i < 10; i++)
fog.push_back(temp);
//0,0 is where hero will be at first
fog[0][0] = 'H';
//push for each level
for (int i = 0; i < level; i++)
Fog_of_War.push_back(fog);
for(int i =0; i<level; i++)
this->levelClear.push_back(false);
}
void Dungeon::DisplayFogAtLevel(int n) {
int size = monsters[n-1].size();
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 41; k++){
cout<<"-";
}
cout<<endl;
cout<<"| ";
for (int j = 0; j < 10; j++) {
std::cout << Fog_of_War[n-1][i][j] << " | ";
}
if(i/2<size){
if(i%2==0){
cout<<"Monster No."<<i/2<<" "<<monsters[n-1][i/2].To_String_Part_One();
}
else
cout<<" "<<monsters[n-1][i/2].To_String_Part_Two();
}
cout<<endl;
}
for (int i = 0; i < 41; i++){
cout<<"-";
}
cout << endl << endl;
}
Monster& Dungeon::GetMonster(int l, int x, int y){
int index = 10*x+y;
int size = monstersByPosition[l].size();
for(int i =0; i<size; i++){
if(monstersByPosition[l][i].first == index){
return monstersByPosition[l][i].second;
}
}
Monster defaultM;
return defaultM;;
}
int Dungeon::GetDirection()
{
int ret = 0;
do
{
char c = ' ';
c = cin.get();
switch (c)
{
case 'w':
ret = 2; // top
break;
case 'a':
ret = 1; // left
break;
case 'd':
ret = 3; // right
break;
case 's':
ret = 4; // down
break;
case 'q':
ret = 5;//return
break;
case 'e':
autoFight = false;
cout<<"Auto fight has been turnned off\n";
default:
break;
}
} while (ret == 0);
return ret;
}
void Dungeon::ClearAllNearBlock() {
int currentLevel = LevelComplete;
if (playerX > 0) {
Fog_of_War[currentLevel][playerX - 1][playerY] = stages[currentLevel][playerX - 1][playerY];
if (playerY > 0) {
Fog_of_War[currentLevel][playerX - 1][playerY - 1] = stages[currentLevel][playerX - 1][playerY - 1];
}
if (playerY < 9) {
Fog_of_War[currentLevel][playerX - 1][playerY + 1] = stages[currentLevel][playerX - 1][playerY + 1];
}
}
if(playerY>0)
Fog_of_War[currentLevel][playerX][playerY - 1] = stages[currentLevel][playerX][playerY - 1];
if(playerY<9)
Fog_of_War[currentLevel][playerX][playerY + 1] = stages[currentLevel][playerX][playerY + 1];
if (playerX < 9) {
Fog_of_War[currentLevel][playerX + 1][playerY] = stages[currentLevel][playerX + 1][playerY];
if (playerY > 0) {
Fog_of_War[currentLevel][playerX + 1][playerY - 1] = stages[currentLevel][playerX + 1][playerY - 1];
}
if (playerY < 9) {
Fog_of_War[currentLevel][playerX + 1][playerY + 1] = stages[currentLevel][playerX + 1][playerY + 1];
}
}
}
void Dungeon::Help()
{
cout << "How can I help you, Master";
}
void Dungeon::FightInstruction()
{
cout << "It's your turn. You can chose from: \n"
<< "1. Attack\n"
<< "2. Defense\n" // For now, it means do nothing
<< "3. Use Skill\n"
<< "4. Use Item\n"
<< "5. Turn on Auto fight mode (Enter e to turn off)\n"
<< "7. Enter Pro Mode\n"
<< "8. To Do, Call For Help\n"
<< "9. End Fight(Escape)\n"
<< "Pleas type a number and press Enter\n";
//<< "If you're Pro, try action number+P to "
}
void Dungeon::Attack(Hero &h, Monster &m, int attacker) // 0 for monster, 1 for hero
{
if (attacker == 1) // Hero attacking
{
int damage = h.GetAtt()-m.GetDef()/2<0?0:(h.GetAtt()-m.GetDef());
m.UpdateHP(-damage);
cout << "\nMonster received " << damage << " damage.\n";
cout << "Monster HP: " << m.GetHP() << std::endl<<endl;
}
else if(attacker == 0)
{
int damage = m.GetAtt()-h.GetDef()<0?0:(m.GetAtt()-h.GetDef());
h.UpdateHP(-damage);
cout << "You received " << damage << " damage.\n";
cout << "Hero HP: " << h.GetHP() << std::endl<<endl;;
}
else
{
cout << "Error: Invalid Attacker!\n";
}
}
void Dungeon::MonsterTurn(Hero &h, Monster &m)
{
//srand(time(NULL));
// Temperory solution for testing, monster only attack
this->Attack(h,m,0);
//cout << m.GetName() << "has gave you " << da;
}
void Dungeon::Fight(Hero &h, Monster &m)
{
Fight(h,m,0);
}
void Dungeon::Fight(Hero &h, Monster &m, int pro)
{
if (h.GetHP() <= 0)
{
cout << "You have been defeated by " << m.GetName() << " , Master. I will wait for your respawn in GG valley ...\n\n";
}
else if (m.GetHP() <= 0)
{
cout << "You have defeated " << m.GetName() << " .\n";
m.DefeatedBy(h);
return;
}
else
{
if (pro == 7)
cout << "Pro Mode, Your Turn: ";
else
FightInstruction();
char instruction=0;
if(autoFight)
instruction = '1';
else
cin>>instruction;
if(instruction == '5'){
autoFight = true;
instruction = '1';
}
while((instruction-'0')<1||(instruction-'0')>9){
cout<<"Wrong button pressed, only 0~9 is meaningful, try again\n";
cin >> instruction;
cout<<"Instrcution I get this time is "<<instruction<<endl;
}
switch (instruction)
{
case '1': // Attack
{
Attack(h,m,1);
break;
}
case '2': // Defense
{
break;
}
case '3': // Use Skill
{
cout << "Which skill do you want to use?";
if (pro != 7)
h.SkillsToString();
int skillNum = -1;
cin >> skillNum;
h.UseSkill(skillNum);
break;
}
case '4': // Use Item
{
cout << "Which item do you want to use?";
if (pro != 7)
h.GetBag().ToString();
int itemNum = -1;
cin >> itemNum;
h.UseItem(itemNum);
break;
}
case '7': // Pro mode
{
Fight(h,m,7);
return;
break;
}
case '8': // Call for Help
{
this->Help();
break;
}
case '9':
{
return;
}
default:
{
return;
}
}
MonsterTurn(h,m);
Fight(h,m,pro);
}
}
void Dungeon::PickUpGoldAtLevel(Hero& h, int l){
srand(time(NULL));
int temp = rand()%(maxGold[l-1]-minGold[l-1]);
temp = temp+minGold[l-1];
cout<<"You just pick up "<<temp<<" coins!\n";
h.GainCoins(temp);
return;
}
//increment levelComplete if complete
void Dungeon::StartDungeon(Hero & h) {
//space represents nothing, H represents hero, number 0-9 represents monsters, T represents trap,
//I represents Item, C reprents Coins, E reprents exit
int directiopn = 0;
char command = ' ';
//L is one less than the current level
for (int L = 0; L < level; L++) {
//Reset hero position at start of each level
playerX = playerY =0;
cout<<"Enter w,a,s,d to move and q to quit\n";
while (true) {
//Display the 8 blocks near the hero
ClearAllNearBlock();
//Replace the current Hero location with the origin Item/Monster/Space
cout<<"HP: "<<h.GetHP()<<"/"<<h.GetTotalHP();
cout<<" EXP: "<<h.GetEXP()<<"/"<<h.GetMaxEXP();
cout<<" ATT: "<<h.GetAtt()<<" Def: "<<h.GetDef();
cout<<" STAM: "<<h.GetStam()<<" INTL: "<<h.GetIntl()<<endl;
DisplayFogAtLevel(L + 1);
Fog_of_War[L][playerX][playerY] = stages[L][playerX][playerY];
directiopn = GetDirection();
if (directiopn == 1 && playerY > 0)
playerY -= 1;
else if (directiopn == 2 && playerX > 0)
playerX -= 1;
else if (directiopn == 3 && playerY < 9)
playerY += 1;
else if (directiopn == 4 && playerX < 9)
playerX += 1;
else if (directiopn == 5){
LeaveDungeon(h);
return;
}
Fog_of_War[L][playerX][playerY] = 'H';
//Check if current position is a Entrance to Next Level
if(stages[L][playerX][playerY]=='E'){
cout<<"Do you want to Enter Next Level?\nEnter Y to confirm\n";
cin>>command;
if(command == 'Y'||command == 'y'){
cout<<"You will be tranport to next Level. Goold luck, Hero\n";
this->LevelComplete++;
break;
}
}
//Check if current position is a Monster
else if(isdigit(stages[L][playerX][playerY])){
Monster& currentMonster = GetMonster(L,playerX,playerY);
currentMonster.ShowInformation();
cout<<endl;
Fight(h,currentMonster);
if(h.GetHP()==0){
LeaveDungeon(h);
return;
}
if (currentMonster.GetHP()<=0){
stages[L][playerX][playerY] = ' ';
}
}
//check if current position is coins that can be pick Up
else if(stages[L][playerX][playerY]=='C'){
PickUpGoldAtLevel(h,L+1);
stages[L][playerX][playerY] = ' ';
}
}
}
cout<<"\nWow, you have complete all level of " <<this->name<<"\n";
LeaveDungeon(h);
}
void Dungeon::LeaveDungeon(Hero& h) {
if(LevelComplete==0){
cout<<"It is too sad that you did not complete even first level\nGo home, kid\n";
return;
}
//check if hero clear all of the monster at certain level
bool clearThisLevel = true;
for(int i =0; i<LevelComplete; i++){
for(int j=0; j<10; j++){
for(int k=0; k<10; k++){
if(isdigit(stages[i][j][k])){
clearThisLevel = false;
break;
}
}
if(!clearThisLevel){
break;
}
}
if(clearThisLevel){
levelClear[i]=true;
}
clearThisLevel = true;
}
srand(time(NULL));
int coinGet = 0;
int expGet = 0;
for(int i=0; i<LevelComplete; i++){
if(levelClear[i]){
coinGet+=goldByStages[i];
expGet+=expByStages[i];
}
}
h.GainCoins(coinGet);
h.GainEXP(expGet);
cout<<"You have complete "<<LevelComplete<<" levels\n";
cout<<"The level that hero clear is: ";
for(int i =0; i<LevelComplete; i++)
if(levelClear[i]==true)
cout<<"level "<<i+1<<" ";
cout<<endl;
cout<<"Bonus Gold: "<<coinGet<<endl;
cout<<"Bonus EXP: "<<expGet<<endl;
//bonus Item
for (int i = 0; i < LevelComplete; i++) {
if(levelClear[i]){
if (rand() % 1000 < itemsByStage[i].second){
cout<<"Bonus Item: "<<itemsByStage[i].first.GetName()<<endl;
h.GainItem(itemsByStage[i].first);
}
}
}
}
void Dungeon::Reset(){
srand(time(NULL));
vector<char>temp;
int toSwitch = 0;
char tp = ' ';
for(int l=0; l<level; l++){
for(int i =0; i<10; i++){
toSwitch = rand()%10;
temp = stages[l][toSwitch];
stages[l][toSwitch] = stages[l][i];
stages[l][i] = temp;
}
for(int i =0; i<10; i++){
toSwitch = rand()%10;
for(int j=0; j<10; j++){
tp = stages[l][toSwitch][j];
stages[l][toSwitch][j] = stages[l][i][j];
stages[l][i][j] = tp;
}
}
}
//initalize monstersByPosition
char tmp = ' ';
int mIndex = 0;
for(int l=0; l<level; l++){
monstersByPosition.push_back(vector<pair<int,Monster>>());
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
tmp = stages[l][i][j];
if(isdigit(tmp)){
mIndex = 10*i+j;
Monster tempMonster = monsters[l][(int)tmp-'0'];
if(rand()%1 == 0)
tempMonster.SetSpecial();
monstersByPosition[l].push_back(pair<int,Monster>(mIndex,tempMonster));
}
}
}
}
return;
}