-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypersonic.cpp
More file actions
1233 lines (1056 loc) · 26.1 KB
/
Copy pathHypersonic.cpp
File metadata and controls
1233 lines (1056 loc) · 26.1 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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <chrono>
#include <unordered_map>
#include <tuple>
#include <queue>
using namespace std;
enum ITEM { None, Range, BombCount };
enum ACTION { UP, DOWN, LEFT, RIGHT, BOMB, STAY };
class World;
typedef tuple<World, ACTION, int, int> Path;
//CONFIGS
const int DEPTH = 5;
const bool USE_BEAM = true;
const bool USE_DFS = false;
const bool USE_BFS = false;
const int BEAM_WIDHT = 100;
const int MAP_WIDTH = 13;
const int MAP_HEIGHT = 11;
const bool timer_on = false;
string toString(ACTION action)
{
map<ACTION, string> dict = { { UP, "UP" },{ DOWN, "DOWN" },{ LEFT, "LEFT" },{ RIGHT, "RIGHT" }, { BOMB, "BOMB" }, {STAY, "STAY" } };
return dict.at(action);
}
class Timer
{
public:
Timer(string name) {
start = std::chrono::high_resolution_clock::now();
this->name = name;
}
~Timer()
{
if (timer_on) {
Stop("DST");
}
}
static void GlobalStart()
{
global_start = std::chrono::high_resolution_clock::now();
}
static long long GlobalElapsed()
{
auto elapsed = std::chrono::high_resolution_clock::now() - global_start;
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
return microseconds / 1000;
}
static void GlobalPrint()
{
cerr << "Globaltime: " << GlobalElapsed() << endl;
}
void Stop(string reason = "")
{
auto elapsed = std::chrono::high_resolution_clock::now() - start;
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
if (stats.find(name) == stats.end()) {
stats[name] = make_tuple(0, 0);
}
stats[name] = make_tuple(get<0>(stats[name]) + microseconds, get<1>(stats[name]) + 1);
}
static void PrintStats()
{
if (timer_on) {
cerr << "====ROUND TIME STATS==== T" << GlobalElapsed() << "ms" << endl;
for (auto it = stats.begin(); it != stats.end(); it++)
{
int dur = get<0>(it->second);
int calls = get<1>(it->second);
cerr << it->first << " took:" << dur << "us, calls:" << calls << " avg:" << (float)dur / calls << "us" << endl;
}
}
}
static void ResetStats()
{
stats.clear();
GlobalStart();
}
private:
string name;
std::chrono::system_clock::time_point start;
static std::chrono::system_clock::time_point global_start;
static map<string, tuple<int, int>> stats;
};
class Point
{
public:
Point()
: m_x(0), m_y(0)
{}
Point(int x, int y)
: m_x(x), m_y(y)
{}
void Set(int x, int y)
{
m_x = x;
m_y = y;
}
int x() const
{
return m_x;
}
int y() const
{
return m_y;
}
const string toString()
{
return "X:" + to_string(m_x) + " Y:" + to_string(m_y);
}
private:
int m_x;
int m_y;
};
//For point hashmap
struct PointHash {
size_t operator()(const Point &data) const {
return (size_t)(data.x() * 100 + data.y());
}
};
bool operator ==(const Point &p1, const Point &p2) {
return p1.x() == p2.x() && p1.y() == p2.y();
}
class Unit
{
public:
Point GetLoc() const
{
return loc;
}
void SetLoc(int x, int y)
{
loc.Set(x, y);
}
private:
Point loc;
};
class Player : public Unit
{
public:
void SetId(int id)
{
m_id = id;
}
int GetId()
{
return m_id;
}
void SetBombs(int bombs)
{
this->bombs = bombs;
}
int GetBombs()
{
return this->bombs;
}
void SetRange(int range)
{
this->range = range;
}
int GetRange()
{
return this->range;
}
void SetRemainingBombs(int num)
{
this->remaining_bombs = num;
}
int GetRemainingBombs()
{
return this->remaining_bombs;
}
private:
int m_id = 0;
int range = 3;
int bombs = 1;
int remaining_bombs = 1;
};
class Bomb : public Unit
{
public:
Bomb() {};
Bomb(int expl_round, int owner, int radius, int x, int y)
{
SetRadius(radius);
SetExplRound(expl_round);
SetLoc(x, y);
SetOwner(owner);
}
void SetExplRound(int round)
{
this->expl_round = round;
}
void SetRadius(int radius)
{
this->radius = radius;
}
int GetRadius() const
{
return this->radius;
}
void SetOwner(int owner)
{
//cerr << "Set Bomb owner" << owner << "at " << GetLoc().toString() << endl;
this->owner = owner;
}
int GetOwner()
{
//cerr << "Get Bomb owner" << owner << "at " << GetLoc().toString() << endl;
return this->owner;
}
int GetExplRound()
{
return expl_round;
}
bool GetCounted()
{
return counted;
}
void SetCounted()
{
counted = true;
}
private:
int expl_round;
int radius;
int owner = 999;
bool counted = false; //Did i count this in score
};
class Tile : public Unit
{
public:
int GetExplRound()
{
return expl_round;
}
void SetBomb(Bomb bomb)
{
this->bomb = bomb;
//cerr << "Setting bomb at " << GetLoc().toString() << "owned by" << this->bomb.GetOwner() << endl;
this->has_bomb = true;
}
Bomb* GetBomb()
{
//cerr << "Returning bomb at " << GetLoc().toString() << "owned by" << this->bomb.GetOwner() << endl;
return &this->bomb;
}
void SetExplRound(int round)
{
//cerr << GetLoc().toString() << " will explode at " << round << endl;
if (this->expl_round == 0 || this->expl_round > round)
{
this->expl_round = round;
}
}
void SetBox(bool has_box)
{
//cerr << GetLoc().toString() << "Setting box" << has_box << endl;
this->has_box = has_box;
}
void SetItem(ITEM id)
{
this->item = id;
}
ITEM GetItem()
{
return this->item;
}
void SetWall()
{
this->is_wall = true;
}
bool HasBomb()
{
return this->has_bomb;
}
bool HasBox()
{
//cerr << GetLoc().toString() << " has box:" << has_box << endl;
return has_box;
}
bool IsTraversible()
{
return !has_box && !has_bomb && !is_wall;
}
bool IsWall() {
return is_wall;
}
bool ExplosionCanPass()
{
return !IsWall() && GetItem() == ITEM::None && !HasBox() && !HasBomb();
}
void Explode()
{
this->has_bomb = false;
if (!this->has_box) {
this->item = ITEM::None;
}
this->has_box = false;
this->explosion_by_me = false;
this->expl_round = 0;
}
void SetExplosionByMe()
{
this->explosion_by_me = true;
}
bool ExplosionByMe()
{
return this->explosion_by_me;
}
private:
int expl_round = 0;
bool explosion_by_me = false;
Bomb bomb;
bool has_bomb = false;
bool has_box = false;
ITEM item = ITEM::None;
bool is_wall = false;
};
class World
{
public:
World()
{
//nothing
};
void InitializeGraph()
{
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
StoreNeighbourTiles(Point(x, y));
}
}
}
void DoNextTurn()
{
ResolveBombs();
SetSimStartRound(GetCurrentRound());
if (USE_DFS) {
cerr << "Starting DFS at" << GetCurrentLoc().toString() << endl;
DFS(DEPTH);
} else
if (USE_BEAM) {
cerr << "Starting Beam at" << GetCurrentLoc().toString() << endl;
Beam(DEPTH);
} else
if (USE_BFS) {
cerr << "Starting BFS at" << GetCurrentLoc().toString() << endl;
BFS(DEPTH);
}
this->currentActions = this->bestActions;
cerr << "Round " << GetCurrentRound() << " Besactions " << GetActionList() << " score " << this->bestScore << " bombsleft" << players[GetPlayerId()].GetRemainingBombs() << endl;
bool do_bomb = false;
int action_id = 0;
if (this->bestActions[action_id] == ACTION::BOMB) {
DoAction(this->bestActions[action_id]);
do_bomb = true;
action_id++;
}
ACTION direction = this->bestActions[action_id];
Point new_loc = GetCurrentLoc();
if (direction == UP) {
new_loc.Set(new_loc.x(), new_loc.y() - 1);
} else
if (direction == DOWN) {
new_loc.Set(new_loc.x(), new_loc.y() + 1);
} else
if (direction == LEFT) {
new_loc.Set(new_loc.x() - 1, new_loc.y());
} else
if (direction == RIGHT) {
new_loc.Set(new_loc.x() + 1, new_loc.y());
}
if (direction == STAY) {
//dont change it
}
DoAction(this->bestActions[action_id]);
if (do_bomb) {
DoBomb(new_loc.x(), new_loc.y());
} else {
DoMove(new_loc.x(), new_loc.y());
}
this->currentActions.clear();
this->bestActions.clear();
this->bestScore = 0;
this->currentScore = 0;
}
void SetPlayerId(int id)
{
this->player_id = id;
}
Tile* GetTile(int x, int y)
{
if (x < MAP_WIDTH && x >= 0 && y < MAP_HEIGHT && y >= 0) {
return &grid[x][y];
} else {
return nullptr;
}
}
void SetPlayerLoc(int player_id, int x, int y)
{
players[player_id].SetLoc(x, y);
ResolvePickups(player_id);
}
void SetPlayerRange(int player_id, int range)
{
players[player_id].SetRange(range);
}
void SetPlayerBombs(int player_id, int bombs)
{
players[player_id].SetBombs(bombs);
}
bool HasBombAt(int x, int y)
{
Tile* tile = GetTile(x, y);
return tile->HasBomb();
}
void SetBomb(int x, int y, int owner, int expl_round, int range)
{
Tile* tile = GetTile(x, y);
Bomb bomb(expl_round, owner, range, x, y);
tile->SetBomb(bomb);
UpdateExplosion(tile->GetLoc());
active_bombs.push_back(bomb.GetLoc());
}
private:
int GetSimStartRound()
{
return this->sim_start_round;
}
void SetSimStartRound(int round)
{
this->sim_start_round = round;
}
int GetCurrentRound()
{
return this->current_round;
}
void SetCurrentRound(int round)
{
this->current_round = round;
}
int GetPlayerId()
{
return this->player_id;
}
void SetMyLoc(int x, int y)
{
SetPlayerLoc(GetPlayerId(), x, y);
}
Point GetCurrentLoc()
{
return players[GetPlayerId()].GetLoc();
}
Point GetPlayerLoc(int player_id)
{
players[player_id].GetLoc();
}
string GetActionList()
{
string action_list = "";
for (ACTION act : this->currentActions) {
action_list += ">" + toString(act);
}
return action_list;
}
//Tile helpers
Tile* GetTileAt(Point point)
{
return GetTile(point.x(), point.y());
}
Tile* GetNonWallTile(int x, int y)
{
Tile* tile = GetTile(x, y);
if (tile != nullptr && !tile->IsWall()) {
return tile;
}
return nullptr;
}
Tile* LeftTile(Tile* current_tile)
{
return GetDirTile(current_tile, "left");
}
Tile* RightTile(Tile* current_tile)
{
return GetDirTile(current_tile, "right");
}
Tile* UpTile(Tile* current_tile)
{
return GetDirTile(current_tile, "up");
}
Tile* DownTile(Tile* current_tile)
{
return GetDirTile(current_tile, "down");
}
Tile* GetDirTile(Tile* current_tile, const string& dir)
{
Timer tim("World::GetDirTile");
if (graph[current_tile->GetLoc()].find(dir) != graph[current_tile->GetLoc()].end()) {
return GetTileAt(graph[current_tile->GetLoc()][dir]);
} else {
return nullptr;
}
}
vector<Tile*> GetNeighbours(const Point& loc)
{
Tile* tile = GetTileAt(loc);
vector<Tile*> neighbours;
neighbours.push_back(UpTile(tile));
neighbours.push_back(DownTile(tile));
neighbours.push_back(RightTile(tile));
neighbours.push_back(LeftTile(tile));
return neighbours;
}
void StoreNeighbourTiles(Point loc)
{
Tile* up = GetTile(loc.x(), loc.y() - 1);
Tile* down = GetTile(loc.x(), loc.y() + 1);
Tile* left = GetTile(loc.x() - 1, loc.y());
Tile* right = GetTile(loc.x() + 1, loc.y());
map<string, Point> neighbours;
if (up != nullptr) {
neighbours["up"] = up->GetLoc();
}
if (down != nullptr) {
neighbours["down"] = down->GetLoc();
}
if (left != nullptr) {
neighbours["left"] = left->GetLoc();
}
if (right != nullptr) {
neighbours["right"] = right->GetLoc();
}
graph[loc] = neighbours;
}
//Bombing related
void SetMyBomb(int x, int y) {
SetBomb(x, y, GetPlayerId(), GetCurrentRound() + 9, players[GetPlayerId()].GetRange());
players[GetPlayerId()].SetRemainingBombs(players[GetPlayerId()].GetRemainingBombs() - 1);
}
vector<Tile*> GetAffectedTiles(const Bomb& bomb)
{
vector<Tile*> result;
Tile* curr_tile = GetTileAt(bomb.GetLoc());
Tile* left = curr_tile;
Tile* right = curr_tile;
Tile* up = curr_tile;
Tile* down = curr_tile;
result.push_back(curr_tile);
//cerr << "UpdateExplosion " << loc.toString() << endl;
for (int i = 0; i < bomb.GetRadius() - 1; i++) {
left = (left != nullptr ? LeftTile(left) : nullptr);
right = (right != nullptr ? RightTile(right) : nullptr);
up = (up != nullptr ? UpTile(up) : nullptr);
down = (down != nullptr ? DownTile(down) : nullptr);
if (left != nullptr) {
result.push_back(left);
left = (left->ExplosionCanPass() ? left : nullptr);
}
if (right != nullptr) {
result.push_back(right);
right = (right->ExplosionCanPass() ? right : nullptr);
}
if (up != nullptr) {
result.push_back(up);
up = (up->ExplosionCanPass() ? up : nullptr);
}
if (down != nullptr) {
result.push_back(down);
down = (down->ExplosionCanPass() ? down : nullptr);
}
}
return result;
}
void UpdateBombExplosions()
{
for (const Point& loc : this->active_bombs) {
UpdateExplosion(loc);
}
}
void ResolveBombs()
{
Timer tim("Resolving bomb");
bool bombs_left = active_bombs.size() > 0;
//COUNT SCORES FOR NEW BO)MBS
for (const Point& loc : this->active_bombs) {
Bomb* current_bomb = GetTileAt(loc)->GetBomb();
if (current_bomb->GetOwner() == GetPlayerId()) {
if (!current_bomb->GetCounted()) {
vector<Tile*> aff_tiles = GetAffectedTiles(*current_bomb);
for (Tile* tile : aff_tiles) {
if (tile->HasBox() && tile->GetExplRound() >= current_bomb->GetExplRound()) {
this->currentScore += (8 - (GetCurrentRound() - GetSimStartRound())) * 100;
}
}
current_bomb->SetCounted();
}
}
}
players[GetPlayerId()].SetRemainingBombs(players[GetPlayerId()].GetRemainingBombs() + increment_bombs);
increment_bombs = 0;
if (IDieNextRound()) {
this->currentScore -= 99999999;
}
bool smth_exploded = false;
//===RESOLVE EXPLOSIONS===
for (auto loc_it = active_bombs.begin(); loc_it != active_bombs.end(); loc_it++) {
Bomb* current_bomb = GetTileAt(*loc_it)->GetBomb();
if (current_bomb->GetExplRound() == GetCurrentRound()) {
bool my_bomb = current_bomb->GetOwner() == GetPlayerId();
if (my_bomb) {
increment_bombs++;
}
loc_it = active_bombs.erase(loc_it);
loc_it--;
smth_exploded = true;
}
}
if (smth_exploded) {
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
Tile* tile = GetTile(x, y);
if (tile->GetExplRound() == GetCurrentRound()) {
if (tile->GetLoc() == GetPlayerLoc(GetPlayerId())) {
this->currentScore -= 99999999;
}
//cerr << tile->GetLoc().toString() << " exploding at " << GetCurrentRound() << endl;
tile->Explode();
}
}
}
}
if (smth_exploded) {
UpdateBombExplosions();
}
}
void UpdateExplosion(const Point& loc)
{
Timer tim("World::UpdateExplosion");
Tile* curr_tile = GetTileAt(loc);
Bomb* bomb = curr_tile->GetBomb();
int tile_expl_round = curr_tile->GetExplRound();
int bomb_expl_round = bomb->GetExplRound();
if (tile_expl_round != 0 && tile_expl_round < bomb_expl_round) {
bomb->SetExplRound(tile_expl_round);
bomb_expl_round = tile_expl_round;
}
bool my_bomb = (bomb->GetOwner() == GetPlayerId());
vector<Tile*> aff_tiles = GetAffectedTiles(*bomb);
for (Tile* tile : aff_tiles) {
if (tile->GetExplRound() > bomb_expl_round || tile->GetExplRound() == 0) {
tile->SetExplRound(bomb_expl_round);
if (my_bomb) {
tile->SetExplosionByMe();
}
if (tile->HasBomb() && tile->GetBomb()->GetExplRound() > bomb_expl_round) {
UpdateExplosion(tile->GetLoc());
}
}
}
}
void ResolvePickups(int player_id)
{
bool is_me = player_id == GetPlayerId();
bool smth_changed = false;
Tile* tile = GetTileAt(GetPlayerLoc(player_id));
if (tile->GetItem() == ITEM::BombCount) {
players[player_id].SetBombs(players[player_id].GetBombs() + 1);
players[player_id].SetRemainingBombs(players[player_id].GetRemainingBombs() + 1);
this->currentScore += (is_me ? 300 : 0);
smth_changed = true;
} else
if (tile->GetItem() == ITEM::Range) {
players[player_id].SetRange(players[player_id].GetRange() + 1);
this->currentScore += (is_me ? 300 : 0);
smth_changed = true;
}
tile->SetItem(ITEM::None);
if (smth_changed) {
if (tile->GetExplRound() != 0) {
UpdateBombExplosions();
}
}
}
//Scoring
bool AmIStuck()
{
Tile* curr_tile = GetTileAt(GetPlayerLoc(GetPlayerId()));
vector<Tile*> neighbours = GetNeighbours(curr_tile->GetLoc());
vector<Tile*> traversed;
vector<Tile*> to_process;
to_process.push_back(curr_tile);
to_process.insert(to_process.end(), neighbours.begin(), neighbours.end());
//when standing on bomb im immediately stuck
for (int i = 0; i < 3; i++) {
vector<Tile*> current_proc;
current_proc.insert(current_proc.end(), to_process.begin(), to_process.end());
to_process.clear();
for (Tile* tile : current_proc) {
if (tile != nullptr
&& find(traversed.begin(), traversed.end(), tile) == traversed.end()
&& tile->IsTraversible())
{
if (tile->GetExplRound() == 0) {
return false;
}
vector<Tile*> new_tiles = GetNeighbours(tile->GetLoc());
to_process.insert(to_process.end(), new_tiles.begin(), new_tiles.end());
}
traversed.push_back(tile);
}
}
return true;
}
bool IDieNextRound()
{
Tile* player_loc = GetTileAt(GetPlayerLoc(GetPlayerId()));
if ((player_loc->GetExplRound() - GetCurrentRound()) == 1) {
return true;
}
return false;
}
int GetScore()
{
int score = this->currentScore;
if (AmIStuck()) {
score -= 99999999;
}
return score;
}
//Walking
bool TimeIsUp()
{
int timeout_at = (GetSimStartRound() == 0 ? 970 : 95);
if (Timer::GlobalElapsed() >= timeout_at && !this->bestActions.empty()) {
timeout_detected = true;
return true;
}
return false;
}
vector<ACTION> GetPossibleActions()
{
Timer tim("World::GetPossibleActions");
vector<ACTION> actions;
Tile* cur_tile = GetTileAt(GetCurrentLoc());
if (cur_tile == nullptr) {
cerr << "No possible actions from this, im at insane location:" << GetCurrentLoc().toString() << endl;
}
Tile* up = UpTile(cur_tile);
Tile* down = DownTile(cur_tile);
Tile* right = RightTile(cur_tile);
Tile* left = LeftTile(cur_tile);
if (!cur_tile->HasBomb() && players[GetPlayerId()].GetRemainingBombs() > 0) {
actions.push_back(ACTION::BOMB);
}
if (up != nullptr && up->IsTraversible()) {
actions.push_back(ACTION::UP);
}
if (right != nullptr && right->IsTraversible()) {
actions.push_back(ACTION::RIGHT);
}
if (down != nullptr && down->IsTraversible()) {
actions.push_back(ACTION::DOWN);
}
if (left != nullptr && left->IsTraversible()) {
actions.push_back(ACTION::LEFT);
}
actions.push_back(ACTION::STAY);
//Simple heuristic to keep towards middle, as my depth currently cant cover
//entire map (so i dont want to get stuck in a corner with no boxes in sight)
map<ACTION, double> heur_map;
heur_map[UP] = (double)GetCurrentLoc().y() / MAP_HEIGHT;
heur_map[DOWN] = (double)1 - heur_map.at(UP);
heur_map[LEFT] = (double)GetCurrentLoc().x() / MAP_WIDTH;
heur_map[RIGHT] = (double)1 - heur_map.at(LEFT);
heur_map[STAY] = -1;
heur_map[BOMB] = 2;
sort(actions.begin(), actions.end(), [&](const auto& a, const auto& b)
{
return heur_map.at(a) > heur_map.at(b);
});
return actions;
}
//Beam Search
void Walk(int path_idx)
{
World world = get<0>(paths[path_idx]);
ACTION action = get<1>(paths[path_idx]);
int depth = get<2>(paths[path_idx]);
world.ResolveBombs();
world.DoAction(action);
world.GetPaths(depth);
paths.erase(paths.begin() + path_idx);
}
void GetPaths(int depth)
{
Timer tim("World::GetPaths");
int score = GetScore();
if (depth == 0) {
if (score > this->bestScore || this->bestActions.empty()) {
//cerr << ">>Optional best Round" << GetCurrentRound() << " Besactions " << GetActionList() << " score " << score << endl;
this->bestScore = score;
this->bestActions = this->currentActions;
}
return;
}
if (TimeIsUp()) {
return;
}
for (ACTION action : GetPossibleActions()) {
Path path = make_tuple(*this, action, depth - 1, score);
paths.push_back(path);
}
}
void Beam(int depth)
{
paths.clear();
this->GetPaths(depth);
while (!paths.empty()) {
if (TimeIsUp()) {
return;
}
Walk(0);
if (paths.size() > BEAM_WIDHT * 4) {
std::sort(paths.begin(), paths.end(), [](const Path& a, const Path& b)
{
return get<3>(a) > get<3>(b);
}
);
cerr << "Clear up paths: " << paths.size() << "First score:" << get<0>(paths[0]).GetScore() << " Last score:" << get<0>(paths[paths.size() - 1]).GetScore() << endl;
paths.erase(paths.begin() + BEAM_WIDHT, paths.end());
}
}
}
//Best first search
struct ComparePaths
{
public:
bool operator() (const Path& a, const Path& b)
{
return get<3>(a) < get<3>(b);
}
};
void GetBFSPaths(int depth)
{
Timer tim("World::GetBFSPaths");
if (TimeIsUp()) {
return;
}
int score = GetScore();
if (depth == 0) {
if (score > this->bestScore || this->bestActions.empty()) {
//cerr << ">>Optional best Round" << GetCurrentRound() << " Besactions " << GetActionList() << " score " << score << endl;
this->bestScore = score;
this->bestActions = this->currentActions;
}