-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
1308 lines (1114 loc) · 38.8 KB
/
game.cpp
File metadata and controls
1308 lines (1114 loc) · 38.8 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
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 "game.h"
void split(const std::string &s, char delim, std::vector<std::string> &out) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
out.push_back(item);
}
}
/* COORD CLASS */
Coord::Coord() :
x(0), y(0), dir(NODIR)
{}
Coord::Coord(int x, int y) :
x(x), y(y), dir(NODIR)
{}
Coord::Coord(int x, int y, int dir) :
x(x), y(y), dir(dir)
{}
Coord::Coord(const Coord& other) :
x(other.x), y(other.y), dir(other.dir)
{}
std::string Coord::toString() {
std::string out = "";
out += std::to_string(this->x);
out += std::to_string(this->y);
out += std::to_string(this->dir);
return out;
}
/* PORTAL CLASS */
Portal::Portal() :
owner(nullptr), color(-1), location(Coord(-1, -1, NODIR))
{}
Portal::Portal(Player* owner, int color, Coord& location) :
owner(owner), color(color), location(location)
{}
Portal::Portal(Portal* other, Player* newOwner) :
owner(newOwner), color(other->color), location(Coord(other->location))
{}
/* TRAIL CLASS */
Trail::Trail(int owner, int tick, int type) {
if (type == APOCALYPSE) {
this->p1Exist = false;
this->p1Tick = -1;
this->p1Type = NODIR;
this->p2Exist = false;
this->p2Tick = -1;
this->p2Type = NODIR;
this->apocalypse = true;
} else if (owner == PLAYER1) {
this->p1Exist = true;
this->p1Tick = tick;
this->p1Type = type;
this->p2Exist = false;
this->p2Tick = -1;
this->p2Type = NODIR;
this->apocalypse = false;
} else {
this->p2Exist = true;
this->p2Tick = tick;
this->p2Type = type;
this->p1Exist = false;
this->p1Tick = -1;
this->p1Type = NODIR;
this->apocalypse = false;
}
}
Trail::Trail(Trail* other) :
p1Exist(other->p1Exist), p1Tick(other->p1Tick), p1Type(other->p1Type),
p2Exist(other->p2Exist), p2Tick(other->p2Tick), p2Type(other->p2Type),
apocalypse(other->apocalypse)
{}
void Trail::set(int owner, int tick, int type) {
if (owner == PLAYER1) {
this->p1Exist = true;
this->p1Tick = tick;
this->p1Type = type;
} else {
this->p2Exist = true;
this->p2Tick = tick;
this->p2Type = type;
}
}
void Trail::apocalify() {
this->p1Exist = false;
this->p1Tick = -1;
this->p1Type = NODIR;
this->p2Exist = false;
this->p2Tick = -1;
this->p2Type = NODIR;
this->apocalypse = true;
}
/* BOMB CLASS */
Bomb::Bomb(Player* owner, Coord& location) :
owner(owner), location(location), tick(5)
{}
Bomb::Bomb(Bomb* other, Player* newOwner) :
owner(newOwner), location(Coord(other->location)), tick(other->tick)
{}
/* PLAYER CLASS */
Player::Player(int playerNum) :
alive(true), playerNum(playerNum),
coins(0), bombCount(1), bombPierce(0), bombRange(3),
lastMove("")
{
this->orangePortal = nullptr;
this->bluePortal = nullptr;
int x = 1;
int y = 1;
int dir = WEST;
if (playerNum == PLAYER2) {
x = 9;
y = 9;
}
this->location = Coord(x, y, dir);
}
Player::Player(Player* other) :
alive(other->alive), playerNum(other->playerNum),
coins(other->coins), bombCount(other->bombCount),
bombPierce(other->bombPierce), bombRange(other->bombRange),
location(Coord(other->location)),
lastMove(other->lastMove)
{
this->orangePortal = nullptr;
this->bluePortal = nullptr;
}
void Player::kill() {
this->location.x = -1;
this->location.y = -1;
this->alive = false;
}
/* GAME CLASS */
// A lot of this can be improved to better utilize the coord class and the fact
// that I have access to pointers, but for now I will put in minimal effort on
// the simulation in order to focus on the rest of the ai. Once I have a working
// version, I will make improvements here so that I can have a faster bot.
std::vector<std::string> makeAllMoves() {
std::vector<std::string> allMoves;
allMoves.push_back("b");
allMoves.push_back("mu");
allMoves.push_back("mr");
allMoves.push_back("ml");
allMoves.push_back("md");
//allMoves.push_back("op");
//allMoves.push_back("bp");
//allMoves.push_back("buy_block");
allMoves.push_back("buy_range");
allMoves.push_back("buy_pierce");
allMoves.push_back("buy_count");
allMoves.push_back("");
//allMoves.push_back("tu");
//allMoves.push_back("tl");
//allMoves.push_back("td");
//allMoves.push_back("tr");
return allMoves;
}
std::vector<std::string> Game::allMoves = makeAllMoves();
bool Game::isAMoveMove(std::string move) {
if (move == "mu" || move == "md" ||
move == "ml" || move == "mr")
{
return true;
}
return false;
}
std::string Game::getCounterMove(std::string move) {
std::string countermove = "";
if (move == "md") {
countermove = "mu";
} if (move == "mu") {
countermove = "md";
} else if (move == "ml") {
countermove = "mr";
} else if (move == "mr") {
countermove = "ml";
}
return countermove;
}
int Game::getDirectionOf(std::string clm) {
int dir = NODIR;
if (clm == "mu") {
dir = NORTH;
} else if (clm == "mr") {
dir = EAST;
} else if (clm == "ml") {
dir = WEST;
} else if (clm == "md") {
dir = SOUTH;
}
return dir;
}
Game::Game() :
boardSize(11), winner(-1), currentTurn(0),
running(true), p1First(true), mePlayer(0),
lastMove("NaM"), lastPlayer(0), moveNumber(0),
apocalypseIterator1(0, 10, NORTH), apocalypseIterator2(10, 0, SOUTH)
{
this->player1 = new Player(0);
this->player2 = new Player(1);
this->currentPlayer = this->player1;
// Initialize the hardBlockBoard and softBlockBoard
for (int y = 0; y < this->boardSize; y++) {
for (int x = 0; x < this->boardSize; x++) {
// local iterators, because c++ is not grunt
if (x == 0 || y == 0 || x == this->boardSize-1 || y == this->boardSize-1 || x%2 == 0 && y%2 == 0) {
this->hardBlockBoard.push_back(1);
this->softBlockBoard.push_back(0);
continue;
} else {
this->hardBlockBoard.push_back(0);
}
if ((x == 1 || y == 1) && (x <= 2 && y <= 2)) {
this->softBlockBoard.push_back(0);
continue;
}
if ((x == this->boardSize - 2 || y == this->boardSize - 2)
&& (x >= this->boardSize - 3 && y >= this->boardSize - 3))
{
this->softBlockBoard.push_back(0);
continue;
}
if (rand() % 100 > 70){
this->softBlockBoard.push_back(0); // might fiddle with %
} else {
this->softBlockBoard.push_back(1);
}
}
}
// guaranteed soft block spaces
this->softBlockBoard[3*this->boardSize + 1] = 1;
this->softBlockBoard[1*this->boardSize + 3] = 1;
this->softBlockBoard[(this->boardSize - 2) * this->boardSize + this->boardSize - 4] = 1;
this->softBlockBoard[(this->boardSize - 4) * this->boardSize + this->boardSize - 2] = 1;
}
Game::Game(Game* other) :
boardSize(other->boardSize), winner(other->winner), currentTurn(other->currentTurn),
running(other->running), p1First(other->p1First), mePlayer(other->mePlayer),
lastMove(other->lastMove), lastPlayer(other->lastPlayer), moveNumber(other->moveNumber)
{
this->player1 = new Player(other->player1);
this->player2 = new Player(other->player2);
std::map<Player*, Player*> old2new;
old2new[other->player1] = this->player1;
old2new[other->player2] = this->player2;
this->currentPlayer = old2new[other->currentPlayer];
for (size_t i = 0; i < other->boardSize * (other->boardSize); i++) {
this->hardBlockBoard.push_back(other->hardBlockBoard[i]);
this->softBlockBoard.push_back(other->softBlockBoard[i]);
}
for (auto it = other->bombMap.begin(); it != other->bombMap.end(); ++it) {
int i = it->first;
Bomb* oldBomb = it->second;
this->bombMap[i] = new Bomb(oldBomb, old2new[oldBomb->owner]);
}
for (auto it = other->trailMap.begin(); it != other->trailMap.end(); ++it) {
int i = it->first;
Trail* oldTrail = it->second;
this->trailMap[i] = new Trail(oldTrail);
}
for (auto it1 = other->portalMap.begin(); it1 != other->portalMap.end(); ++it1) {
int i = it1->first;
std::map<int, Portal*>* oldPortalsOnTile = &(it1->second);
for (auto it2 = oldPortalsOnTile->begin(); it2 != oldPortalsOnTile->end(); ++it2) {
int direction = it2->first;
//Portal* oldPortal = it2->second;
Portal* newPortal = new Portal(it2->second, old2new[it2->second->owner]); // WHY IS THIS BREAKING
this->portalMap[i][direction] = newPortal;
if (it2->second->color == PC_ORANGE) {
newPortal->owner->orangePortal = newPortal;
} else {
newPortal->owner->bluePortal = newPortal;
}
}
}
}
Game::~Game() {
// Free all that mammary
delete this->player1;
this->player1 = nullptr;
delete this->player2;
this->player2 = nullptr;
for (auto it = this->bombMap.begin(); it != this->bombMap.end(); ++it) {
delete it->second;
it->second = nullptr;
}
this->bombMap.clear();
for (auto it = this->trailMap.begin(); it != this->trailMap.end(); ++it) {
delete it->second;
it->second = nullptr;
}
this->trailMap.clear();
for (auto it1 = this->portalMap.begin(); it1 != this->portalMap.end(); ++it1) {
for (auto it2 = it1->second.begin(); it2 != it1->second.end(); ++it2) {
if (it2->second != nullptr) {
delete it2->second;
it2->second = nullptr;
}
}
it1->second.clear();
}
this->portalMap.clear();
}
void Game::advanceApocalypseIterator() {
Coord nextSquare1 = this->getNextSquare(this->apocalypseIterator1.x, this->apocalypseIterator1.y, this->apocalypseIterator1.dir);
int i = (this->boardSize)*nextSquare1.x + nextSquare1.y;
if (nextSquare1.x < 0 || nextSquare1.x >= this->boardSize
|| nextSquare1.y < 0 || nextSquare1.y >= this->boardSize
|| ((this->trailMap.find(i) != this->trailMap.end()) && this->trailMap[i]->apocalypse))
{
// both sides always turn at the same time
this->apocalypseIterator1.dir = (this->apocalypseIterator1.dir + 1) % 4;
this->apocalypseIterator2.dir = (this->apocalypseIterator2.dir + 1) % 4;
nextSquare1 = getNextSquare(this->apocalypseIterator1.x, this->apocalypseIterator1.y, this->apocalypseIterator1.dir);
}
Coord nextSquare2 = getNextSquare(this->apocalypseIterator2.x, this->apocalypseIterator2.y, this->apocalypseIterator2.dir);
this->apocalypseIterator1.x = nextSquare1.x;
this->apocalypseIterator1.y = nextSquare1.y;
this->apocalypseIterator2.x = nextSquare2.x;
this->apocalypseIterator2.y = nextSquare2.y;
}
void Game::loadFromJSON(const json& j) {
delete this->player1;
this->player1 = nullptr;
delete this->player2;
this->player2 = nullptr;
this->mePlayer = j["playerIndex"];
this->currentTurn = j["moveIterator"];
this->boardSize = j["boardSize"];
this->moveNumber = j["moveNumber"];
// Advance the apocalypse iterators
for (int i = 400; i < this->moveNumber; i++) {
this->advanceApocalypseIterator();
}
int first = j["moveOrder"].at(0);
this->p1First = (first == 0); // Look how smart I am
this->softBlockBoard.clear();
for (size_t i = 0; i < (this->boardSize)*(this->boardSize); i++) {
this->hardBlockBoard.push_back(j["hardBlockBoard"][i]);
this->softBlockBoard.push_back(j["softBlockBoard"][i]);
}
Player *player, *opponent;
if (this->mePlayer == PLAYER1) {
player = new Player(0);
opponent = new Player(1);
this->player1 = player;
this->player2 = opponent;
this->currentPlayer = this->player1;
} else {
player = new Player(1);
opponent = new Player(0);
this->player2 = player;
this->player1 = opponent;
this->currentPlayer = this->player2;
}
// Player 1 info
player->location = Coord(j["player"]["x"], j["player"]["y"], j["player"]["orientation"]);
player->coins = j["player"]["coins"];
player->bombCount = j["player"]["bombCount"];
player->bombPierce = j["player"]["bombPierce"];
player->bombRange = j["player"]["bombRange"];
player->alive = j["player"]["alive"];
// Player 2 info
opponent->location = Coord(j["opponent"]["x"], j["opponent"]["y"], j["opponent"]["orientation"]);
opponent->coins = j["opponent"]["coins"];
opponent->bombCount = j["opponent"]["bombCount"];
opponent->bombPierce = j["opponent"]["bombPierce"];
opponent->bombRange = j["opponent"]["bombRange"];
opponent->alive = j["opponent"]["alive"];
// Populate bomb map
for (json::const_iterator bombInfo = j["bombMap"].begin(); bombInfo != j["bombMap"].end(); ++bombInfo) {
std::string position = bombInfo.key();
int x = position.at(0) - '0';
int y = position.at(2) - '0';
int i = x*(this->boardSize) + y;
int tick = bombInfo.value()["tick"];
Coord location = Coord(x, y);
Player* owner = this->player1;
if (bombInfo.value()["owner"] == PLAYER2) {
owner = this->player2;
}
this->bombMap[i] = new Bomb(owner, location);
this->bombMap[i]->tick = tick;
}
// Populate trail map
for (json::const_iterator trailInfo = j["trailMap"].begin(); trailInfo != j["trailMap"].end(); ++trailInfo) {
std::string position = trailInfo.key();
std::vector<std::string> parts;
split(position, ',', parts);
int x = std::stoi(parts[0]);
int y = std::stoi(parts[1]);
this->placeTrail(this->player1, x, y, 'h');
}
// Populate portal map
for (json::const_iterator portalMapInfo = j["portalMap"].begin(); portalMapInfo != j["portalMap"].end(); ++portalMapInfo) {
std::string position = portalMapInfo.key();
std::vector<std::string> parts;
split(position, ',', parts);
int x = std::stoi(parts[0]);
int y = std::stoi(parts[1]);
for (json::const_iterator portalInfo = portalMapInfo.value().begin(); portalInfo != portalMapInfo.value().end(); ++portalInfo) {
std::string pik = portalInfo.key();
int direction = pik.at(0) - '0';
Player* owner = this->player1;
if (portalInfo.value()["owner"] == PLAYER2) {
owner = this->player2;
}
int color = PC_ORANGE;
if (portalInfo.value()["portalColor"] == "blue") {
color = PC_BLUE;
}
Coord location = Coord(x, y, direction);
Portal* portal = new Portal(owner, color, location);
if (color = PC_ORANGE) {
owner->orangePortal = portal;
} else {
owner->bluePortal = portal;
}
this->portalMap[x*(this->boardSize)+y][direction] = portal;
}
}
}
std::string Game::hash() {
/*
Player *player1, *player2; // Player class also has person id
*/
std::string out = "";
out += std::to_string(this->p1First);
out += std::to_string(this->winner);
out += std::to_string(this->currentTurn);
if (this->lastMove == "") {
out += "n";
} else {
out += this->lastMove;
}
out += std::to_string(this->player1->coins);
out += ".";
out += std::to_string(this->player1->bombCount);
out += std::to_string(this->player1->bombRange);
out += std::to_string(this->player1->bombPierce);
out += this->player1->location.toString();
out += std::to_string(this->player2->coins);
out += ".";
out += std::to_string(this->player2->bombCount);
out += std::to_string(this->player2->bombRange);
out += std::to_string(this->player2->bombPierce);
out += this->player2->location.toString();
for (size_t i=0; i < this->softBlockBoard.size(); i++) {
out += std::to_string(this->softBlockBoard[i]);
}
for (auto it = this->bombMap.begin(); it != this->bombMap.end(); ++it) {
out += std::to_string(it->first);
out += ".";
out += std::to_string(it->second->tick);
out += it->second->location.toString();
}
for (auto it = this->trailMap.begin(); it != this->trailMap.end(); ++it) {
out += std::to_string(it->first);
out += ".";
out += std::to_string(it->second->p1Type);
out += std::to_string(it->second->p1Type);
out += std::to_string(it->second->p1Exist);
out += std::to_string(it->second->p2Type);
out += std::to_string(it->second->p2Type);
out += std::to_string(it->second->p2Exist);
}
for (auto it = this->portalMap.begin(); it != this->portalMap.end(); ++it) {
out += std::to_string(it->first);
out += ".";
for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
out += std::to_string(it2->first);
out += std::to_string(it2->second->color);
out += std::to_string(it2->second->owner->playerNum);
out += it2->second->location.toString();
}
}
return out;
}
int Game::whoWon() {
//check if any player is on a trail
if (!(this->running)) {
return this->winner;
} else {
bool p1Alive = true;
bool p2Alive = true;
int p1Loc = this->player1->location.x * (this->boardSize) + this->player1->location.y;
if (this->trailMap.find(p1Loc) != this->trailMap.end()) {
p1Alive = false;
}
int p2Loc = this->player2->location.x * (this->boardSize) + this->player2->location.y;
if (this->trailMap.find(p2Loc) != this->trailMap.end()) {
p2Alive = false;
}
if (p1Alive && p2Alive) {
return NO_PLAYER;
} else if (!p1Alive && !p2Alive) {
return BOTH_PLAYER;
} else if (p1Alive) {
return PLAYER1;
} else if (p2Alive) {
return PLAYER2;
}
}
}
bool Game::isOutOfBounds(const Coord& loc) {
if (loc.x < 0 || loc.x >= this->boardSize || loc.y < 0 || loc.y >= this->boardSize) {
return true;
}
return false;
}
Coord Game::getNextSquare(int x, int y, int direction) {
switch (direction) {
case WEST:
return Coord(x-1, y, direction);
case NORTH:
return Coord(x, y-1, direction);
case EAST:
return Coord(x+1, y, direction);
case SOUTH:
return Coord(x, y+1, direction);
}
}
int Game::getBlockValue(int x, int y) {
int rawScore = std::abs((this->boardSize-1 - x)*x * (this->boardSize-1 - y)*y);
int scaledScore = std::floor(10*rawScore / ((this->boardSize-1) * (this->boardSize-1) * (this->boardSize-1) * (this->boardSize-1) / 16));
if (scaledScore == 0) {
return 1;
} else {
return scaledScore;
}
}
int Game::querySpace(int x, int y) {
// This function figures out what is on the specified square
// If a player and bomb are on the same square, it returns bomb
// This could be made easier with polymorphism, but OOP is bad
int i = x*(this->boardSize) + y;
if (x < 0 || x >= this->boardSize || y < 0 || y >= this->boardSize) {
return SP_OUT;
} else if (this->hardBlockBoard[i] == 1) {
return SP_HARDBLOCK;
} else if (this->softBlockBoard[i] == 1) {
return SP_SOFTBLOCK;
} else if (this->bombMap.find(i) != this->bombMap.end()) {
return SP_BOMB;
} else if (this->player1->location.x == x && this->player1->location.y == y) {
return SP_PLAYER1;
} else if (this->player2->location.x == x && this->player2->location.y == y) {
return SP_PLAYER2;
} else {
return SP_AIR;
}
}
Coord Game::simulateMovement(int x, int y, int direction) {
Coord nextSquare = getNextSquare(x, y, direction);
if (this->isOutOfBounds(nextSquare)) {
return Coord(x, y, direction);
}
int nextSquareContents = this->querySpace(nextSquare.x, nextSquare.y);
int sz = this->boardSize;
if (nextSquareContents == SP_AIR) {
return nextSquare;
} else {
int i = nextSquare.x*sz + nextSquare.y;
if (this->portalMap.find(i) != this->portalMap.end()) {
std::map<int, Portal*> portalsOnTile = this->portalMap[i];
int side = (direction+2) % 4; // Can't go into portals from the wrong side
if (portalsOnTile.find(side) != portalsOnTile.end()) {
Portal* portal = portalsOnTile[side];
Player* player = portal->owner;
if (player->orangePortal != nullptr && player->bluePortal != nullptr) {
Coord otherPortalBlock, throughPortalSquare;
// Get the location of the other portal
if (portal->color == PC_ORANGE) {
otherPortalBlock = player->bluePortal->location;
} else if (portal->color == PC_BLUE) {
otherPortalBlock = player->orangePortal->location;
}
// This is where you will land
throughPortalSquare = this->simulateMovement(otherPortalBlock.x,
otherPortalBlock.y,
otherPortalBlock.dir);
// Make sure you don't portal into a block
if (throughPortalSquare.x != otherPortalBlock.x ||
throughPortalSquare.y != otherPortalBlock.y) {
return throughPortalSquare;
}
}
}
}
return Coord(x, y, direction); // No movement
}
}
// This function needs no better name
void Game::trailResolveSquare(int x, int y) {
int i = x*(this->boardSize) + y;
if (this->trailMap.find(i) == this->trailMap.end()) {
return;
}
int space = this->querySpace(x, y);
if (space == SP_SOFTBLOCK) {
this->softBlockBoard[i] = 0;
this->deletePortal(x, y, NODIR); // NODIR means delete all portals
// Give coins to p1
if (this->trailMap[i]->p1Exist) {
this->player1->coins += getBlockValue(x, y);
}
// Also give coins to p2
if (this->trailMap[i]->p2Exist) {
this->player2->coins += getBlockValue(x, y);
}
} else if (space == SP_PLAYER1 || space == SP_PLAYER2) {
Player* player;
if (space == SP_PLAYER1) {
this->player1->kill();
} else if (space == SP_PLAYER2) {
this->player2->kill();
}
}
}
//type is orientation (horizontal vs vertical)
void Game::placeTrail(Player* owner, int x, int y, int type) {
int ownerNum = 0;
if (owner == this->player2) {
ownerNum = 1;
}
int i = x*(this->boardSize) + y;
if (this->trailMap.find(i) == this->trailMap.end()) {
Trail* trail;
if (owner == nullptr || type == APOCALYPSE) {
trail = new Trail(-1, 0, APOCALYPSE);
} else {
trail = new Trail(ownerNum, 2, type);
}
this->trailMap[i] = trail;
} else {
if (owner == nullptr || type == APOCALYPSE) {
this->trailMap[i]->apocalify();
} else {
this->trailMap[i]->set(ownerNum, 2, type);
}
}
}
void Game::recursiveDetonate(Player* owner, int x, int y, int direction, int range, int pierce, bool pierceMode) {
if (range == 0 || (pierceMode && pierce < 0)) {
return;
}
// This isn't the actual output, but I'm keeping old naming conventions
Coord output = getNextSquare(x, y, direction);
int outputContents = this->querySpace(output.x, output.y);
if (outputContents == SP_HARDBLOCK || outputContents == SP_SOFTBLOCK) {
int i = output.x*(this->boardSize) + output.y;
if (this->portalMap.find(i) != this->portalMap.end()) {
int side = (direction+2) % 4;
std::map<int, Portal*> portalsOnTile = this->portalMap[i];
if (portalsOnTile.find(side) != portalsOnTile.end()) {
Portal* portal = portalsOnTile[side];
Player* player = portal->owner;
if (player->orangePortal != nullptr && player->bluePortal != nullptr) { // then we're traveling through poooortals
Coord otherPortalBlock, throughPortalSquare;
Portal* otherPortal = nullptr;
if (portal->color == PC_ORANGE) {
otherPortal = player->bluePortal;
} else {
otherPortal = player->orangePortal;
}
this->recursiveDetonate(owner, otherPortal->location.x, otherPortal->location.y, otherPortal->location.dir, range, pierce, pierceMode);
return;
}
}
}
}
int type = VERTICAL;
if (direction == EAST || direction == WEST) {
type = HORIZONTAL;
}
// Player* owner = this->player1;
// if (this->currentPlayer->playerNum == PLAYER2) {
// owner = this->player2;
// }
// try this
this->detonate(output.x, output.y);
if (outputContents != SP_OUT) {
this->placeTrail(owner, output.x, output.y, type);
} else {
return;
}
if (outputContents == SP_BOMB) {
this->detonate(output.x, output.y);
}
if (outputContents != SP_AIR) {
pierceMode = true;
}
if (pierceMode) {
pierce -= 1;
}
this->recursiveDetonate(owner, output.x, output.y, direction, range-1, pierce, pierceMode);
}
void Game::detonate(int x, int y) {
int i = x*(this->boardSize) + y;
if (this->bombMap.find(i) == this->bombMap.end()) {
return; // no bomb here.
}
Bomb* bomb = this->bombMap[i];
Player* owner = bomb->owner;
owner->bombCount += 1; // operator++ is for posers
// delete the bomb, make sure there aren't any mammary leaks
delete this->bombMap[i];
this->bombMap[i] = nullptr;
this->bombMap.erase(i);
this->placeTrail(owner, x, y, ORIGIN);
for (int direction = WEST; direction < SOUTH+1; direction++) {
this->recursiveDetonate(owner, x, y, direction, owner->bombRange, owner->bombPierce, false);
}
}
void Game::deletePortal(int x, int y, int direction) {
int i = x*(this->boardSize) + y;
if (this->portalMap.find(i) == this->portalMap.end()) {
return; // can't delete a portal that doesnt exist
}
if (direction == NODIR) { // delete all portals on the square
// first time using crazy new c++ 11 things, maybe not so clean
for (auto it = this->portalMap[i].begin(); it != this->portalMap[i].end(); ++it) {
Portal* portal = it->second;
if (portal->color == PC_ORANGE) {
portal->owner->orangePortal = nullptr;
} else {
portal->owner->bluePortal = nullptr;
}
delete portal;
portal = nullptr;
it->second = nullptr;
this->portalMap[i].erase(it->first);
}
} else if (this->portalMap[i].find(direction) != this->portalMap[i].end()) {
Portal* portal = this->portalMap[i][direction];
if (portal->color == PC_ORANGE) {
portal->owner->orangePortal = nullptr;
} else {
portal->owner->bluePortal = nullptr;
}
delete portal;
portal = nullptr;
this->portalMap[i][direction] = nullptr;
this->portalMap[i].erase(direction);
}
// collect the garbage
if (this->portalMap[i].empty()) {
this->portalMap.erase(i);
}
}
void Game::shootPortal(Player* owner, int direction, int color) {
Coord nextSquare = getNextSquare(owner->location.x, owner->location.y, owner->location.dir);
// if (this->isOutOfBounds(nextSquare)) {
// return;
// }
int nextSquareContents = this->querySpace(nextSquare.x, nextSquare.y);
while (nextSquareContents != SP_HARDBLOCK && nextSquareContents != SP_SOFTBLOCK) {
nextSquare = getNextSquare(nextSquare.x, nextSquare.y, nextSquare.dir);
// if (this->isOutOfBounds(nextSquare)) {
// return;
// }
nextSquareContents = this->querySpace(nextSquare.x, nextSquare.y);
}
// now nextsquare is guaranteed to be a block or something
// this means it can maybe have a portal
int newPortalDirection = (direction + 2) % 4;
Coord newPortalLocation = Coord(nextSquare.x, nextSquare.y, newPortalDirection);
Portal* newPortal = new Portal(owner, color, newPortalLocation);
Portal* oldPortal = nullptr;
if (color == PC_ORANGE) {
oldPortal = owner->orangePortal;
} else {
oldPortal = owner->bluePortal;
}
if (oldPortal != nullptr) {
Coord oldPortalLocation = oldPortal->location;
this->deletePortal(oldPortalLocation.x, oldPortalLocation.y, oldPortalLocation.dir);
oldPortal = nullptr;
}
// need reduplication of code here since deleteportal relies on the player still owning that portal
if (color == PC_ORANGE) {
owner->orangePortal = newPortal;
} else {
owner->bluePortal = newPortal;
}
// portals overwrite older ones in the same spot
int i = nextSquare.x*(this->boardSize) + nextSquare.y;
if (this->portalMap.find(i) != this->portalMap.end()) {
std::map<int, Portal*>* portalsOnTile = &(this->portalMap[i]);
if (portalsOnTile->find(newPortalDirection) != portalsOnTile->end()) {
oldPortal = (*portalsOnTile)[newPortalDirection];
Coord oldPortalLocation = oldPortal->location;
this->deletePortal(oldPortalLocation.x, oldPortalLocation.y, oldPortalLocation.dir);
}
}
// finally, place the new portal on the map
this->portalMap[i][newPortalDirection] = newPortal;
}
std::string Game::submit(Player* currentPlayer, const std::string& move) {
std::string outputNote = "";
if (currentPlayer != this->currentPlayer) {
return "Not your turn.";
}
Coord output;
this->lastMove = move;
currentPlayer->lastMove = move;
this->lastPlayer = this->currentPlayer->playerNum;
this->moveNumber += 1;
if (move == "ml") {
output = this->simulateMovement(currentPlayer->location.x, currentPlayer->location.y, WEST);
currentPlayer->location.x = output.x;
currentPlayer->location.y = output.y;
currentPlayer->location.dir = output.dir;
} else if (move == "mu") {
output = this->simulateMovement(currentPlayer->location.x, currentPlayer->location.y, NORTH);
currentPlayer->location.x = output.x;
currentPlayer->location.y = output.y;
currentPlayer->location.dir = output.dir;
} else if (move == "mr") {
output = this->simulateMovement(currentPlayer->location.x, currentPlayer->location.y, EAST);
currentPlayer->location.x = output.x;
currentPlayer->location.y = output.y;
currentPlayer->location.dir = output.dir;
} else if (move == "md") {
output = this->simulateMovement(currentPlayer->location.x, currentPlayer->location.y, SOUTH);
currentPlayer->location.x = output.x;
currentPlayer->location.y = output.y;
currentPlayer->location.dir = output.dir;
} else if (move == "tl") {
currentPlayer->location.dir = WEST;
} else if (move == "tu") {
currentPlayer->location.dir = NORTH;
} else if (move == "tr") {
currentPlayer->location.dir = EAST;
} else if (move == "td") {
currentPlayer->location.dir = SOUTH;
} else if (move == "") {
// nothing to see here
} else if (move == "b") {
int i = currentPlayer->location.x * (this->boardSize) + currentPlayer->location.y;