-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofApp.cpp
2046 lines (1938 loc) · 64.4 KB
/
ofApp.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ofApp.h"
// 20192138 조명재
// 컴퓨터공학설계및실험1 기말 프로젝트
const int OF_KEY_SPACE = 32; // ASCII Code Value of Space Key
//--------------------------------------------------------------
void ofApp::setup(){ // Set the game
ofSetFrameRate(15);
ofBackground(255);
ofSetLineWidth(4);
load_flag = 0, draw_flag = 0;
play_Time = 0, game_Over = 0;
Map_Width = 0, Map_Height = 0;
// Determine the Location with a dx and dy index
// 0 => Right, 1 => Left, 2 => Down, 3 => Up
dx[0] = 1, dx[1] = -1, dx[2] = 0, dx[3] = 0;
dy[0] = 0, dy[1] = 0, dy[2] = 1, dy[3] = -1;
srand(time(NULL));
}
void ofApp::visited_Init() {
for (int i = 0; i < Map_Width; i++) { // Initialize the visited to false.
for (int j = 0; j < Map_Height; j++) {
visited[i][j] = false;
}
}
}
void ofApp::player_Life_Decrease() { // If the player touches the balloon or encounters the enemy, Player life is decrease.
if(play_Time) p.life--;
if (p.life == 0) { // If player life is 0, Game over.
game_Over = 1;
cout << "Game Over!!! => Player's life : " << p.life <<" Player's final score : " << p.score << endl;
mySound.load("Crazy Arcade Lose.mp3");
mySound.play();
play_Time = -1;
}
else {
if(p.life) cout << "Player life's : " << p.life << endl;
}
}
void ofApp::enemy_Life_Decrease(int index) { // If the Enemy touches the balloon, Enemy life is decrease.
if(play_Time) e[index].life--;
if (e[index].life == 0) { // If Enemy life is 0, Enemy disappears on the map.
if (e[index].balloonX >= 0 && e[index].balloonY >= 0) {
Map[e[index].balloonX][e[index].balloonY] = 'G';
e[index].balloonX = -1, e[index].balloonY = -1;
}
e[index].x = -1, e[index].y = -1;
e[index].op = 0;
p.score += 100;
cout << "Enemy" << index+1 << " is slayed! => Enemy" << index+1 << "'s life : " << e[index].life << endl;
}
else {
cout << "Enemy" << index+1 << "'s life : " << e[index].life << endl;
}
int cnt = 0;
for (int i = 0; i < 4; i++) {
if (e[i].life == 0) cnt++;
}
if (cnt == 4) { // If all enemys have been slayed, Player Win.
cout << "Player Win! => Player's life : " << p.life << " Player's final score : " << p.score << endl;
play_Time = -1;
mySound.load("Crazy Arcade Win.mp3");
mySound.play();
}
}
bool ofApp::find_Player(int startX, int startY, int endX, int endY) {
// Check that the Enemy can find the Player by using Breadth First Search.
visited_Init(); // Initialize 2D Array visited to false.
queue<pair<int, int>> q;
q.push({ startX,startY });
visited[startX][startY] = true;
while (!q.empty()) { // Through process of Queue's push and pop, Enemy searchs the location of player.
int curX = q.front().first;
int curY = q.front().second;
q.pop();
if (curX == endX && curY == endY) return true; // If Enemy can search the location of player, return true.
for (int i = 0; i < 4; i++) { // save the next path to the queue
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if (nextX >= 0 && nextX < Map_Width && nextY >= 0 && nextY < Map_Height) {
if (!visited[nextX][nextY] && Map[nextX][nextY]=='G') {
q.push({ nextX,nextY });
visited[nextX][nextY] = true;
}
}
}
}
return false; // If Enemy can't search the location of player, return false.
}
void ofApp::enemy_Kill_Player(int startX,int startY,int endX,int endY, int index) {
// Enemy searches the location of player by using BFS.
e[index].op = 0;
visited_Init(); // Initialize 2D Array visited to false.
queue<pair<pair<int, int>, vector<pair<int,int>>>> q;
vector<pair<int,int>> path_V; // Save the paths that pass in vector of path_V.
path_V.push_back({ startX, startY });
q.push({ { startX, startY }, path_V });
visited[startX][startY] = true;
// Find the case that the Enemy move the ground.
while (!q.empty()) {
int curX = q.front().first.first;
int curY = q.front().first.second;
vector<pair<int,int>> path = q.front().second;
q.pop();
if (curX == endX && curY == endY) {
// Move closer to the player.
int nextX = path[1].first;
int nextY = path[1].second;
if (nextX - e[index].x == 1) { // Go to Right
e[index].direction = 0;
}
else if (e[index].x - nextX == 1) { // Go to Left
e[index].direction = 1;
}
else if (nextY - e[index].y == 1) { // Go to Down
e[index].direction = 2;
}
else if (e[index].y - nextY == 1) { // Go to Up
e[index].direction = 3;
}
e[index].x = nextX, e[index].y = nextY;
if (e[index].x == p.x && e[index].y == p.y) {
player_Life_Decrease();
}
return;
}
for (int i = 0; i < 4; i++) { // save the next path to the queue
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if (nextX >= 0 && nextX < Map_Width && nextY >= 0 && nextY < Map_Height) {
if (Map[nextX][nextY] == 'G' && !visited[nextX][nextY]) {
path.push_back({ nextX,nextY });
q.push({ {nextX,nextY},path });
path.pop_back();
visited[nextX][nextY] = true;
}
}
}
}
}
bool ofApp::enemy_In_Balloon_Range(int x,int y, int index) { // Check that enemy is within the range of the balloon.
if (x == e[index].balloonX && y == e[index].balloonY) return true;
// Right case
int nextX = e[index].balloonX + e[index].balloon_Power;
if (x > e[index].balloonX && x <= nextX && y == e[index].balloonY) return true;
// Left case
nextX = e[index].balloonX - e[index].balloon_Power;
if (x < e[index].balloonX && x >= nextX && y == e[index].balloonY) return true;
// Down case
int nextY = e[index].balloonY + e[index].balloon_Power;
if (y > e[index].balloonY && y <= nextY && x == e[index].balloonX) return true;
// Up case
nextY = e[index].balloonY - e[index].balloon_Power;
if (y < e[index].balloonY && y >= nextY && x == e[index].balloonX) return true;
// All case are not satisfied? => Get out the balloon Range.
return false;
}
void ofApp::enemy_Move_Ground(int x,int y, int index) { // Find if enemy can move to the ground.
queue<pair<pair<int, int>,vector<pair<int,int>>>> q;
vector<pair<int,int>> path_V; // Save the paths that pass in vector of path_V.
q.push({ { x, y }, path_V });
visited[x][y] = true;
// Find a case that enemy can move to the ground.
while (!q.empty()) {
int curX = q.front().first.first;
int curY = q.front().first.second;
vector<pair<int,int>> path = q.front().second;
q.pop();
if (!enemy_In_Balloon_Range(curX, curY, index)) { // enemy find the ground? => change the enemy location.
int nextX = path[0].first;
int nextY = path[0].second;
if (nextX - e[index].x == 1) { // Go to Right.
e[index].direction = 0;
}
else if (e[index].x - nextX == 1) { // Go to Left.
e[index].direction = 1;
}
else if (nextY - e[index].y == 1) { // Go to Down.
e[index].direction = 2;
}
else if (e[index].y - nextY == 1) { // Go to Up.
e[index].direction = 3;
}
e[index].x = nextX, e[index].y = nextY;
return;
}
for (int i = 0; i < 4; i++) {
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if (nextX >= 0 && nextX < Map_Width && nextY >= 0 && nextY < Map_Height) {
if (Map[nextX][nextY] == 'G' && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
path.push_back({ nextX,nextY });
q.push({ {nextX,nextY}, path });
path.pop_back();
}
}
}
}
}
void ofApp::enemy_Pop_Balloon(int index) {
visited_Init(); // Initialize 2D Array visited to false.
// Set that the enemy pops the balloon.
int player_Life_Op = 0; // Check that the player's life is decreasing.
int enemy_Life_Op[4] = { 0,0,0,0 }; // Check that the enemy's life is decreasing.
int currentX = e[index].balloonX, currentY = e[index].balloonY, power = e[index].balloon_Power;
int e_loc = currentY * Map_Width + currentX; // convert two-dimensional coordinates to index.
enemy_Map[e_loc] = 0; // Remove the enemy's balloon
e[index].op = 0, e[index].balloonX = -1, e[index].balloonY = -1; // Get away the balloon.
Map[currentX][currentY] = 'G';
for (int i = 0; i < 4; i++) { // Pop the balloon in left, right, side to side.
// Let the balloon be as effective as the enemy's power.
int nextX = currentX + dx[i] * power;
int nextY = currentY + dy[i] * power;
// Is there a block that needs to be removed by approaching one space in left, right, side to side?
if (i == 0) { // Find the block in right.
for (int x = currentX; x <= nextX; x++) {
if (x >= Map_Width || visited[x][currentY]) break; // Don't invade the spot where the balloon pop.
if (x == p.x && currentY == p.y) { // If the current player's location is within the range of the balloon range, decrease the player life.
if (!player_Life_Op) {
player_Life_Decrease();
player_Life_Op = 1;
}
}
for (int k = 0; k < 4; k++) { // If the current enemy's location is within the range of the balloon range, decrease the enemy life.
if (k == index) continue;
if (x == e[k].x && currentY == e[k].y) {
if (!enemy_Life_Op[k]) {
enemy_Life_Decrease(k);
enemy_Life_Op[k] = 1;
}
}
}
if (Map[x][currentY] == 'B' || Map[x][currentY] == 'X' || Map[x][currentY] == 'F') {
// Block, Box, Flow_Box removed by balloon => Each different score.
if (Map[x][currentY] == 'B') {
e[index].score += 10;
Map[x][currentY] = 'G'; // Block removed by balloon => change the ground
visited[x][currentY] = true; // The location is pop, so visit processing is essential.
// (To keep the other balloon from crossing its position)
}
else if (Map[x][currentY] == 'X') {
e[index].score += 15;
Map[x][currentY] = 'G'; // Box removed by balloon => change the ground
visited[x][currentY] = true; // The location is pop, so visit processing is essential.
// (To keep the other balloon from crossing its position)
}
else if (Map[x][currentY] == 'F') {
int loc_index = currentY * Map_Width + x; // convert the location to index.
if (++flow_Block[loc_index] == 3) { // If balloon pop by weight(3)?
e[index].score += 20;
Map[x][currentY] = 'G'; // Flow_Box removed by balloon => change the ground
visited[x][currentY] = true; // The location is pop, so visit processing is essential.
// (To keep the other balloon from crossing its position)
}
else {
visited[x][currentY] = true; // The location is pop, so visit processing is essential.
// (To keep the other balloon from crossing its position)
}
}
break;
}
else if (Map[x][currentY] == 'H' || Map[x][currentY] == 'T' || Map[x][currentY] == 'W')
{
break; // Case of House, Tree, Water Balloon touched => Finish the balloon effect in direction.
}
}
}
else if (i == 1) { // If you want information, Check i==0.
for (int x = currentX; x >= nextX; x--) {
if (x < 0 || visited[x][currentY]) break;
if (x == p.x && currentY == p.y) {
if (!player_Life_Op) {
player_Life_Decrease();
player_Life_Op = 1;
}
}
for (int k = 0; k < 4; k++) {
if (k == index) continue;
if (x == e[k].x && currentY == e[k].y) {
if (!enemy_Life_Op[k]) {
enemy_Life_Decrease(k);
enemy_Life_Op[k] = 1;
}
}
}
if (Map[x][currentY] == 'B' || Map[x][currentY] == 'X' || Map[x][currentY] == 'F') {
if (Map[x][currentY] == 'B') {
e[index].score += 10;
Map[x][currentY] = 'G';
visited[x][currentY] = true;
}
else if (Map[x][currentY] == 'X') {
e[index].score += 15;
Map[x][currentY] = 'G';
visited[x][currentY] = true;
}
else if (Map[x][currentY] == 'F') {
int loc_index = currentY * Map_Width + x;
if (++flow_Block[loc_index] == 3) {
e[index].score += 20;
Map[x][currentY] = 'G';
visited[x][currentY] = true;
}
else {
visited[x][currentY] = true;
}
}
break;
}
else if (Map[x][currentY] == 'H' || Map[x][currentY] == 'T' || Map[x][currentY] == 'W')
{
break;
}
}
}
else if (i == 2) { // If you want information, Check i==0.
for (int y = currentY; y <= nextY; y++) {
if (y >= Map_Height || visited[currentX][y]) break;
if (currentX == p.x && y == p.y) {
if (!player_Life_Op) {
player_Life_Decrease();
player_Life_Op = 1;
}
}
for (int k = 0; k < 4; k++) {
if (k == index) continue;
if (currentX == e[k].x && y == e[k].y) {
if (!enemy_Life_Op[k]) {
enemy_Life_Decrease(k);
enemy_Life_Op[k] = 1;
}
}
}
if (Map[currentX][y] == 'B' || Map[currentX][y] == 'X' || Map[currentX][y] == 'F') {
if (Map[currentX][y] == 'B') {
e[index].score += 10;
Map[currentX][y] = 'G';
visited[currentX][y] = true;
}
else if (Map[currentX][y] == 'X') {
e[index].score += 15;
Map[currentX][y] = 'G';
visited[currentX][y] = true;
}
else if (Map[currentX][y] == 'F') {
int loc_index = y * Map_Width + currentX;
if (++flow_Block[loc_index] == 3) {
e[index].score += 20;
Map[currentX][y] = 'G';
visited[currentX][y] = true;
}
else {
visited[currentX][y] = true;
}
}
break;
}
else if (Map[currentX][y] == 'H' || Map[currentX][y] == 'T' || Map[currentX][y] == 'W')
{
break;
}
}
}
else if (i == 3) { // If you want information, Check i==0.
for (int y = currentY; y >= nextY; y--) {
if (y < 0 || visited[currentX][y]) break;
if (currentX == p.x && y == p.y) {
if (!player_Life_Op) {
player_Life_Decrease();
player_Life_Op = 1;
}
}
for (int k = 0; k < 4; k++) {
if (k == index) continue;
if (currentX == e[k].x && y == e[k].y) {
if (!enemy_Life_Op[k]) {
enemy_Life_Decrease(k);
enemy_Life_Op[k] = 1;
}
}
}
if (Map[currentX][y] == 'B' || Map[currentX][y] == 'X' || Map[currentX][y] == 'F') {
if (Map[currentX][y] == 'B') {
e[index].score += 10;
Map[currentX][y] = 'G';
visited[currentX][y] = true;
}
else if (Map[currentX][y] == 'X') {
e[index].score += 15;
Map[currentX][y] = 'G';
visited[currentX][y] = true;
}
else if (Map[currentX][y] == 'F') {
int loc_index = y * Map_Width + currentX;
if (++flow_Block[loc_index] == 3) {
e[index].score += 20;
Map[currentX][y] = 'G';
visited[currentX][y] = true;
}
else {
visited[currentX][y] = true;
}
}
break;
}
else if (Map[currentX][y] == 'H' || Map[currentX][y] == 'T' || Map[currentX][y] == 'W')
{
break;
}
}
}
}
}
void ofApp::enemy_Avoid_Balloon(int index) {
if (enemy_In_Balloon_Range(e[index].x, e[index].y, index)) { // Current location is in a balloon range
visited_Init(); // Initialize 2D Array visited to false.
// find the closet distance to the ground and move there
enemy_Move_Ground(e[index].x, e[index].y, index);
}
else { // Current location is not in a balloon range
enemy_Pop_Balloon(index);
}
}
int ofApp::enemy_Estimated_Score(int x,int y, int index) {
int currentX = x, currentY = y;
int power = e[index].balloon_Power;
int score = 0;
// Estimate how many scores will be in the (x, y) coordinates of the map.
for (int i = 0; i < 4; i++) {
int nextX = currentX + dx[i] * power;
int nextY = currentY + dy[i] * power;
// Is there a block that needs to be removed by approaching one space from top to bottom ?
if (i == 0) { // Find the block in right.
for (int x = currentX; x <= nextX; x++) {
if (x >= Map_Width) break;
if (Map[x][currentY] == 'B' || Map[x][currentY] == 'X' || Map[x][currentY] == 'F') {
// Block, Box, Flow_Box removed by balloon => Each different score.
if (Map[x][currentY] == 'B') {
score += 10;
}
else if (Map[x][currentY] == 'X') {
score += 15;
}
else if (Map[x][currentY] == 'F') {
score += 20;
}
break;
}
else if (Map[x][currentY] == 'H' || Map[x][currentY] == 'T' || Map[x][currentY] == 'W')
{
break; // Case of House, Tree, Water Balloon touched => Finish the balloon effect in direction.
}
}
}
else if (i == 1) { // If you want information, Check i==0.
for (int x = currentX; x >= nextX; x--) {
if (x < 0) break;
if (Map[x][currentY] == 'B' || Map[x][currentY] == 'X' || Map[x][currentY] == 'F') {
if (Map[x][currentY] == 'B') {
score += 10;
}
else if (Map[x][currentY] == 'X') {
score += 15;
}
else if (Map[x][currentY] == 'F') {
score += 20;
}
break;
}
else if (Map[x][currentY] == 'H' || Map[x][currentY] == 'T' || Map[x][currentY] == 'W')
{
break;
}
}
}
else if (i == 2) { // If you want information, Check i==0.
for (int y = currentY; y <= nextY; y++) {
if (y >= Map_Height) break;
if (Map[currentX][y] == 'B' || Map[currentX][y] == 'X' || Map[currentX][y] == 'F') {
if (Map[currentX][y] == 'B') {
score += 10;
}
else if (Map[currentX][y] == 'X') {
score += 15;
}
else if (Map[currentX][y] == 'F') {
score += 20;
}
break;
}
else if (Map[currentX][y] == 'H' || Map[currentX][y] == 'T' || Map[currentX][y] == 'W')
{
break;
}
}
}
else if (i == 3) { // If you want information, Check i==0.
for (int y = currentY; y >= nextY; y--) {
if (y < 0) break;
if (Map[currentX][y] == 'B' || Map[currentX][y] == 'X' || Map[currentX][y] == 'F') {
if (Map[currentX][y] == 'B') {
score += 10;
}
else if (Map[currentX][y] == 'X') {
score += 15;
}
else if (Map[currentX][y] == 'F') {
score += 20;
}
break;
}
else if (Map[currentX][y] == 'H' || Map[currentX][y] == 'T' || Map[currentX][y] == 'W')
{
break;
}
}
}
}
return score;
}
bool ofApp::enemy_Is_Alive(int x, int y, int index) {
visited_Init(); // Initialize 2D Array visited to false.
queue<pair<int, int>> q;
q.push({ x, y });
visited[x][y] = true;
// We find that Enemy is moving to the Ground.
while (!q.empty()) {
int curX = q.front().first;
int curY = q.front().second;
q.pop();
if (!enemy_In_Balloon_Range(curX, curY, index)) { // Find the Ground
return true;
}
for (int i = 0; i < 4; i++) {
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if (nextX >= 0 && nextX < Map_Width && nextY >= 0 && nextY < Map_Height) {
if (Map[nextX][nextY] == 'G' && !visited[nextX][nextY]) {
visited[nextX][nextY] = true;
q.push({ nextX,nextY });
}
}
}
}
return false;
}
void ofApp::enemy_Find_Ground(int x,int y, int index) {
// When you use the BFS to go to a point from the current point and place a water balloon,
// make sure to store the path to the highest score.
// The saved paths are assigned to em_V.
visited_Init(); // Initialize 2D Array visited to false.
queue<pair<pair<int, int>, vector<Enemy_HighScore_Path>>> q;
vector<Enemy_HighScore_Path> path_V; // Save the paths that pass in vector of path_V.
path_V.push_back({ x,y,enemy_Estimated_Score(x,y,index) });
em_V[index].push_back(path_V);
q.push({ { x, y }, path_V });
visited[x][y] = true;
// Find that Enemy can move to the ground.
while (!q.empty()) {
int curX = q.front().first.first;
int curY = q.front().first.second;
vector<Enemy_HighScore_Path> path = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if (nextX >= 0 && nextX < Map_Width && nextY >= 0 && nextY < Map_Height) {
if (Map[nextX][nextY]=='G' && !visited[nextX][nextY]) {
path.push_back({ nextX,nextY,enemy_Estimated_Score(nextX,nextY,index) });
em_V[index].push_back(path);
q.push({ {nextX,nextY},path });
path.pop_back();
visited[nextX][nextY] = true;
}
}
}
}
// Find index for the maximum score at a point when you move from the current point to a point.
int max_Score=0, max_index=0, e_loc=0, e_before_loc=-1;
for (int i = 0; i < em_V[index].size(); i++) {
int score = em_V[index][i][em_V[index][i].size() - 1].score;
if (score == 0) continue;
if (score >= max_Score) {
int move_X = em_V[index][i][em_V[index][i].size() - 1].x;
int move_Y = em_V[index][i][em_V[index][i].size() - 1].y;
e_loc = move_Y * Map_Width + move_X;
e[index].balloonX = move_X, e[index].balloonY = move_Y;
if (enemy_Is_Alive(move_X,move_Y, index) && !enemy_Map[e_loc]) { // Extract scores from moved points.
max_Score = score, max_index = i;
enemy_Map[e_before_loc] = 0;
enemy_Map[e_loc] = 1;
e_before_loc = e_loc;
}
e[index].balloonX = -1, e[index].balloonY = -1;
}
}
// It contains all the information in index corresponding to the maximum score in the em_V vector in e_V.
for (int i = 0; i < em_V[index][max_index].size(); i++) {
e_V[index].push_back({ em_V[index][max_index][i].x,em_V[index][max_index][i].y,em_V[index][max_index][i].score });
}
em_V[index].clear(); // Since the role of the em_V vector has ended, free.
e[index].op = 1;
enemy_Put_Balloon(index); // Directly to path.
}
void ofApp::enemy_Put_Balloon(int index) {
if (e_V[index].size() == 1) { // What if the only path is the current Enemy location?
Map[e[index].x][e[index].y] = 'W';
e[index].balloonX = e[index].x, e[index].balloonY = e[index].y;
e[index].op = 2; // Change Balloon to avoid.
}
else {
if (e_Index[index] == e_V[index].size()) { // You have moved to the last point where you can get the maximum score.
Map[e[index].x][e[index].y] = 'W';
e[index].balloonX = e[index].x, e[index].balloonY = e[index].y;
e[index].op = 2; // Change Balloon to avoid.
}
else {
int nextX = e_V[index][e_Index[index]].x;
int nextY = e_V[index][e_Index[index]].y;
if (nextX - e[index].x == 1) { // Go to Right.
e[index].direction = 0;
}
else if (e[index].x - nextX == 1) { // Go to Left.
e[index].direction = 1;
}
else if (nextY - e[index].y == 1) { // Go to Down.
e[index].direction = 2;
}
else if (e[index].y - nextY == 1) { // Go to Up.
e[index].direction = 3;
}
e[index].x = nextX, e[index].y = nextY;
e_Index[index]++;
}
}
}
void ofApp::enemy_Random_Set(int index) {
e_Index[index] = 1;
e_V[index].clear();
em_V[index].clear();
}
void ofApp::enemy_Random_Play(int index) {
if (e[index].op == 0) { // Process that Enemy finds the ground of high score
enemy_Random_Set(index);
enemy_Find_Ground(e[index].x, e[index].y, index);
}
else if (e[index].op == 1) { // Process that Enemy puts the Water Balloon
enemy_Put_Balloon(index);
}
else if (e[index].op == 2) { // Process that Enemy avoids the Water Balloon
enemy_Avoid_Balloon(index);
}
}
void ofApp::score_Update(){ // Show that player and enemy balloon power and count.
if (p.score) { // Show the player information.
if (p.score >= 450) {
if (p.balloon_Count != 10) {
p.balloon_Count = 10;
p.balloon_Power = 10;
cout << "Finish Upgrade! Max Player's Balloon Power : " << p.balloon_Power
<< " Max Player's Balloon Count : " << p.balloon_Count << endl;
}
}
else {
int op = p.balloon_Power;
switch (p.score / 50) {
case 8:
p.balloon_Count = p.balloon_Power = 9;
break;
case 7:
p.balloon_Count = p.balloon_Power = 8;
break;
case 6:
p.balloon_Count = p.balloon_Power = 7;
break;
case 5:
p.balloon_Count = p.balloon_Power = 6;
break;
case 4:
p.balloon_Count = p.balloon_Power = 5;
break;
case 3:
p.balloon_Count = p.balloon_Power = 4;
break;
case 2:
p.balloon_Count = p.balloon_Power = 3;
break;
case 1:
p.balloon_Count = p.balloon_Power = 2;
break;
}
if (op != p.balloon_Power) {
cout << "Upgrade! Player's Balloon Power : " << p.balloon_Power
<< " Player's Balloon Count : " << p.balloon_Count << endl;
}
}
}
for (int i = 0; i < 4; i++) { // show the enemys information.
if (e[i].score) {
if (e[i].score >= 250) {
if (e[i].balloon_Power != 6) {
e[i].balloon_Power = 6;
cout << "Finish Upgrade! Max Enemy" << i+1 <<"'s Balloon Power : " << e[i].balloon_Power << endl;
}
}
else {
int op = e[i].balloon_Power;
switch (e[i].score / 50) {
case 4:
e[i].balloon_Power = 5;
break;
case 3:
e[i].balloon_Power = 4;
break;
case 2:
e[i].balloon_Power = 3;
break;
case 1:
e[i].balloon_Power = 2;
break;
}
if (op != e[i].balloon_Power) {
cout << "Upgrade! Enemy" << i+1 <<"'s Balloon Power : " << e[i].balloon_Power << endl;
}
}
}
}
}
//--------------------------------------------------------------
void ofApp::update(){
if (draw_flag && game_Over==0) {
score_Update();
if (play_Time > 0) { // If player pressed direction key or space Key, increase the play_Time
if (play_Time == 1) cout << "Game Start!" << endl;
play_Time = play_Time++ == 100000 ? 2 : play_Time; // 1 ~ 100000
// Implement the enemy to move.
for (int i = 0; i < 4; i++) {
if (!(e[i].x == p.x && e[i].y == p.y) && e[i].life) { // If Enemy and Player did not meet?
int rand_Time = play_Random[i] / 2;
if (play_Time % rand_Time == 1 && find_Player(e[i].x, e[i].y, p.x, p.y)) { // If Enemy found the Player?
if (e[i].op == 2) { // If Enemy finds Player while placing the water balloon?
enemy_Avoid_Balloon(i);
}
else {
enemy_Kill_Player(e[i].x, e[i].y, p.x, p.y, i);
enemy_Random_Set(i);
}
}
else if (play_Time % play_Random[i] == 1) { // If Enemy does not find the Player?
enemy_Random_Play(i);
}
}
}
}
}
}
void ofApp::createMap() { // Compose the written Map
for (int h = 0; h < Map_Height; h++) {
for (int w = 0; w < Map_Width; w++) {
// (x1,y1) to (x2,y2) positioned from top left to bottom right
int x1 = 40 + 65 * w;
int y1 = 40 + 60 * h;
int x2 = 40 + 65 * (w + 1);
int y2 = 40 + 60 * (h + 1);
if (Map[w][h] == 'G') { // Ground, w is six to eight, Gray, and green.
if (w == 6 || w == 8) {
if (h == 2 || h == 10) {
ofSetColor(255); // White draws a rectangle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(189, 189, 189); // Gray |] [] [| Draw.
ofDrawRectangle(x1, y1 + 5, 10, y2 - y1 - 10);
ofDrawRectangle(x1 + 25, y1 + 5, 15, y2 - y1 - 10);
ofDrawRectangle(x1 + 55, y1 + 5, 10, y2 - y1 - 10);
}
else {
ofSetColor(189, 189, 189); // Draw a gray rectangle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
}
}
else if (w == 7) {
if (h == 2 || h == 10) {
ofSetColor(255); // White draws a rectangle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(189, 189, 189); // Gray |] [] [| Draw.
ofDrawRectangle(x1, y1 + 5, 10, y2 - y1 - 10);
ofDrawRectangle(x1 + 25, y1 + 5, 15, y2 - y1 - 10);
ofDrawRectangle(x1 + 55, y1 + 5, 10, y2 - y1 - 10);
}
else {
ofSetColor(189, 189, 189); // Gray draws a rectangle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(255); // White [] Draw.
ofDrawRectangle(x1 + 30, y1 + 5, 5, y2 - y1 - 10);
}
}
else {
ofSetColor(125, 254, 116); // Green draws a rectangle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(206, 251, 201); // Draw four shallow green circles.
ofDrawCircle(x1 + 18, y1 + 16, 11);
ofDrawCircle(x1 + 18, y1 + 44, 11);
ofDrawCircle(x1 + 47, y1 + 16, 11);
ofDrawCircle(x1 + 47, y1 + 44, 11);
}
}
else if (Map[w][h] == 'B') // Block, (w+h) Red if even, Orange if odd.
{
if ((w + h) % 2 == 0) {
ofSetColor(244, 95, 95); // Red rectangle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(255, 167, 167); // shallow red circle 4.
ofDrawCircle(x1 + 18, y1 + 14, 10);
ofDrawCircle(x1 + 18, y1 + 38, 10);
ofDrawCircle(x1 + 47, y1 + 14, 10);
ofDrawCircle(x1 + 47, y1 + 38, 10);
ofSetColor(152, 0, 0); // dark red bottom.
ofDrawRectangle(x1, y1 + 52, x2 - x1, y2 - (y1 + 52));
}
else {
ofSetColor(242, 150, 97); // Orange Rectnagle.
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(255, 193, 158); // 4 shallow orange circles.
ofDrawCircle(x1 + 18, y1 + 14, 10);
ofDrawCircle(x1 + 18, y1 + 38, 10);
ofDrawCircle(x1 + 47, y1 + 14, 10);
ofDrawCircle(x1 + 47, y1 + 38, 10);
ofSetColor(153, 56, 0); // dark orange bottom.
ofDrawRectangle(x1, y1 + 52, x2 - x1, y2 - (y1 + 52));
}
}
else if (Map[w][h] == 'H') // House,
{
if (h < 6) { // Up
if (w < 6) { // Left
ofSetColor(204, 114, 61); // Orange Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(0); // Black in the center
ofDrawRectangle(x1 + 10, y1 + 20, 45, 40);
ofSetColor(255); // Door Handle
ofDrawCircle(x1 + 45, y1 + 40, 3.5);
ofSetColor(102, 37, 0); // Roof
ofDrawRectangle(x1, y1, 65, 15);
}
else { // Right
ofSetColor(196, 183, 59); // Dark yelloow Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(0); // Black in the center
ofDrawRectangle(x1 + 10, y1 + 20, 45, 40);
ofSetColor(255); // Door Handle
ofDrawCircle(x1 + 45, y1 + 40, 3.5);
ofSetColor(102, 92, 0); // Roof
ofDrawRectangle(x1, y1, 65, 15);
}
}
else { // Down
if (w < 6) { // Left
ofSetColor(70, 65, 217); // Blue Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(0); // Black in the center
ofDrawRectangle(x1 + 10, y1 + 20, 45, 40);
ofSetColor(255); // Door Handle
ofDrawCircle(x1 + 45, y1 + 40, 3.5);
ofSetColor(3, 0, 102); // Roof
ofDrawRectangle(x1, y1, 65, 15);
}
else { // Right
ofSetColor(204, 114, 61); // Orange Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(0); // Black in the center
ofDrawRectangle(x1 + 10, y1 + 20, 45, 40);
ofSetColor(255); // Door handle
ofDrawCircle(x1 + 45, y1 + 40, 3.5);
ofSetColor(102, 37, 0); // Roof
ofDrawRectangle(x1, y1, 65, 15);
}
}
}
else if (Map[w][h] == 'X') // Box
{
ofSetColor(250, 237, 125);
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(153, 138, 0);
ofDrawRectangle(x1 + 10, y1 + 5, 45, 30);
ofSetColor(250, 237, 125);
ofSetLineWidth(5);
ofDrawLine(x1 + 10, y1 + 5, x1 + 55, y1 + 35);
ofDrawLine(x1 + 10, y1 + 35, x1 + 55, y1 + 10);
ofSetColor(229, 216, 92); // both sides of the bottom
ofDrawRectangle(x1, y1 + 40, x2 - x1, 20);
ofSetColor(153, 138, 0); // bottom
ofDrawRectangle(x1 + 10, y1 + 40, 45, 20);
}
else if (Map[w][h] == 'T') // Tree
{
ofSetColor(34, 116, 28); // shadow
ofDrawEllipse(x1 + 35, y1 + 35, 55, 40);
ofSetColor(153, 56, 0); // Bottom column
ofDrawRectangle(x1 + 30, y1 + 30, 5, 10);
ofSetColor(47, 157, 39); // Triangle
ofDrawTriangle(x1, y1 + 30, x1 + 65, y1 + 30, x1 + 33.5, y1 - 20);
}
else if (Map[w][h] == 'F') // Flow Block => Composed as a three-step block
{
int index = h * Map_Width + w;
if (flow_Block[index] == 0) { // Initialization
ofSetColor(102, 92, 0); // Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(196, 183, 59); // Rectangle
ofDrawRectangle(x1 + 10, y1 + 10, x2 - x1 - 20, y2 - y1 - 20);
ofSetColor(255, 255, 72); // Rectangle
ofDrawRectangle(x1 + 20, y1 + 20, x2 - x1 - 40, y2 - y1 - 40);
}
else if (flow_Block[index] == 1) { // step 1
ofSetColor(102, 92, 0); // Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
ofSetColor(196, 183, 59); // Rectangle
ofDrawRectangle(x1 + 10, y1 + 10, x2 - x1 - 20, y2 - y1 - 20);
}
else if (flow_Block[index] == 2) { // step 2
ofSetColor(102, 92, 0); // Rectangle
ofDrawRectangle(x1, y1, x2 - x1, y2 - y1);
}
// step 3 is finalized. (convert Flow Block to Ground!)
}
}
}
}
void ofApp::create_Player_Balloon() { // Show the Player balloon
visited_Init(); // Initialize 2D Array visited to false.
for (int i = 0; i < pb_V.size(); i++) { // Check the current player's Balloon Location + expression.
int curX = pb_V[i].x, curY = pb_V[i].y;
visited[curX][curY] = true;
// Expression of the balloon
ofSetColor(126, 255, 255);
ofDrawRectangle(40 + 65 * curX, 40 + 60 * curY, 65, 60);
ofSetColor(0, 216, 255);
ofDrawCircle(40 + 65 * curX + 32.5, 40 + 60 * curY + 30, 30);
}
for (int i = 0; i < pb_V.size(); i++) { // The range that can be seen in Balloon's Map using Power in the current Player.
int curX = pb_V[i].x, curY = pb_V[i].y;
for (int j = 0; j < 4; j++) {
int nextX = curX + dx[j] * p.balloon_Power;
int nextY = curY + dy[j] * p.balloon_Power;
if (j == 0) // If you find something to pop with balloon in the right direction, you'll be able to process visits to the area and break.
{
for (int x = curX + 1; x <= nextX; x++) {
if (x >= Map_Width || visited[x][curY]) break;
if (Map[x][curY] == 'H' || Map[x][curY] == 'T') break;
if (Map[x][curY] == 'B' || Map[x][curY] == 'X' || Map[x][curY] == 'F') // When we encounter a block, box, flow box, No express.
{
break;
}
// Expression of balloon range
ofSetColor(126, 255, 255);
ofDrawRectangle(40 + 65 * x, 40 + 60 * curY, 65, 60);
for (int k = 0; k < 4; k++) {
if (e[k].x == x && e[k].y == curY) {