/
main.cpp
1514 lines (1229 loc) · 49.7 KB
/
main.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
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#pragma GCC optimize("-O3")
#pragma GCC optimize("inline")
#pragma GCC optimize("omit-frame-pointer")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <climits>
#include <chrono>
using namespace std;
using namespace std::chrono;
// #####################################################################################################################
// ######### GLOBALS ###################################################################################################
// #####################################################################################################################
high_resolution_clock::time_point roundStart;
bool no_attack = false;
// #####################################################################################################################
// ######### UTILS #####################################################################################################
// #####################################################################################################################
void trap() {
*(int *) 0 = 0;
}
template<typename Enumeration>
auto as_integer(Enumeration const value)
-> typename std::underlying_type<Enumeration>::type {
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}
double elapsed_time() {
duration<double> time_span = duration_cast<duration<double>>(high_resolution_clock::now() - roundStart);
return time_span.count() * 1000;
}
// #####################################################################################################################
// ######### CONSTANTS #################################################################################################
// #####################################################################################################################
constexpr int MAX_MONSTERS_BOARD = 6;
constexpr int MAX_MONSTERS_PER_LANE = 3;
constexpr int MAX_HAND = 8;
constexpr int TIME_LIMIT = 190;
constexpr int NUMBER_OF_CARDS = 160;
// #####################################################################################################################
// ######### CARD ######################################################################################################
// #####################################################################################################################
enum class CardType {
Monster = 0, GreenItem = 1, RedItem = 2, BlueItem = 3
};
enum class CardLocation {
PlayerHand = 0, PlayerBoard = 1, EnemyBoard = -1, Graveyard = -2, EnemyHand = -3
};
struct Card {
friend ostream &operator<<(ostream &os, const Card &card) {
os << "index: " << card.index << " attack: " << card.attack << " defense: " << card.defense
<< " location: " << as_integer(card.location) << " lane: " << card.lane;
return os;
}
int id;
int instanceId;
int index;
CardLocation location;
int lane;
CardType type;
int cost;
int attack;
int defense;
int playerHpChange;
int enemyHpChange;
int cardDraw;
bool breakthrough = false;
bool charge = false;
bool drain = false;
bool guard = false;
bool lethal = false;
bool ward = false;
bool attacked = false;
bool canAttack = true;
bool passed = false;
int monsterEval() {
int score = 0;
if (attack > 0) {
score += (defense + attack * 2 + ward * attack + lethal * 4) * 5;
score += 20;
}
score += guard * 9;
return score;
}
};
// #####################################################################################################################
// ######### PLAYER ####################################################################################################
// #####################################################################################################################
struct Player {
int hp;
int mana;
int handCount;
int cardsToDraw;
int cardsRemaining;
int rune;
CardLocation handLocation;
CardLocation boardLocation;
int guardsCount;
int guardsLaneCount[2];
int boardCount;
int boardLaneCount[2];
bool firstPlayer;
};
// #####################################################################################################################
// ######### ACTION ####################################################################################################
// #####################################################################################################################
enum class ActionType {
Pass,
Summon,
Attack,
Use,
Pick,
PassCard,
Null
};
struct Action {
Action();
ActionType type = ActionType::Null;
int index = -1;
int targetIndex = -1;
Action(ActionType type, int id, int targetId);
Action(ActionType type, int id);
void print(const vector<Card> &cards);
void printErr(const vector<Card> &cards);
friend ostream &operator<<(ostream &os, const Action &action);
};
void Action::print(const vector<Card> &cards) {
int instanceId = (index == -1 ? -1 : cards[index].instanceId);
int targetInstanceId = (targetIndex == -1 ? -1 : cards[targetIndex].instanceId);
switch (type) {
case ActionType::Pass:
cout << "PASS";
break;
case ActionType::Attack:
cout << "ATTACK " << instanceId << " " << targetInstanceId << ";";
break;
case ActionType::Summon:
cout << "SUMMON " << instanceId << " " << targetIndex << ";";
break;
case ActionType::Use:
cout << "USE " << instanceId << " " << targetInstanceId << ";";
break;
case ActionType::Pick:
cout << "PICK " << index;
break;
case ActionType::PassCard:
break;
default:
cerr << as_integer(type) << endl;
cerr << "Error: Action not is not defined" << endl;
trap();
}
}
void Action::printErr(const vector<Card> &cards) {
int instanceId = (index == -1 ? -1 : cards[index].instanceId);
int targetInstanceId = (targetIndex == -1 ? -1 : cards[targetIndex].instanceId);
switch (type) {
case ActionType::Pass:
cerr << "PASS";
break;
case ActionType::Attack:
cerr << "ATTACK " << instanceId << " " << targetInstanceId << ";";
break;
case ActionType::Summon:
cerr << "SUMMON " << instanceId << " " << targetIndex << ";";
break;
case ActionType::Use:
cerr << "USE " << instanceId << " " << targetInstanceId << ";";
break;
case ActionType::Pick:
cerr << "PICK " << index;
break;
case ActionType::PassCard:
break;
default:
cerr << as_integer(type) << endl;
cerr << "Error: Action not is not defined" << endl;
trap();
}
}
Action::Action(ActionType type, int id, int targetId) : type(type), index(id), targetIndex(targetId) {}
Action::Action(ActionType type, int id) : type(type), index(id) {}
ostream &operator<<(ostream &os, const Action &action) {
os << "type: " << as_integer(action.type) << " index: " << action.index << " targetIndex: " << action.targetIndex;
return os;
}
Action::Action() {
}
// #####################################################################################################################
// ######### STATE #####################################################################################################
// #####################################################################################################################
struct State {
Player player;
Player enemy;
int turn = 0;
vector<Card> cards;
bool isDraft;
vector<Action> legalActions;
Action lastAction;
void executeAction(const Action &action);
void executeActions(const vector<Action> &actions);
void attack(int index, int targetIndex);
void summon(int index, int lane);
void use(int index, int targetIndex);
int evaluate();
vector<Action> legalMoves();
void passCard(int index);
void swapPlayers();
bool isEnemyTurn = false;
};
// Assume the action is valid
void State::executeAction(const Action &action) {
lastAction = action;
switch (action.type) {
case ActionType::Pass:
break;
case ActionType::Attack:
this->attack(action.index, action.targetIndex);
break;
case ActionType::Summon:
this->summon(action.index, action.targetIndex);
break;
case ActionType::Use:
this->use(action.index, action.targetIndex);
break;
case ActionType::Pick:
break;
case ActionType::PassCard:
this->passCard(action.index);
break;
default:
cerr << as_integer(action.type) << endl;
cerr << "Error: Action not is not defined" << endl;
trap();
}
}
void State::executeActions(const vector<Action> &actions) {
for (auto &action : actions) {
executeAction(action);
}
}
void State::attack(int index, int targetIndex) {
Card &monster = cards[index];
monster.attacked = true;
if (targetIndex == -1) {
enemy.hp -= monster.attack;
return;
}
Card &target = cards[targetIndex];
if (!monster.ward) {
monster.defense -= target.attack;
if ((target.lethal && target.attack > 0) || monster.defense <= 0) {
monster.defense = 0;
monster.location = CardLocation::Graveyard;
if (monster.guard) {
--player.guardsCount;
--player.guardsLaneCount[monster.lane];
}
--player.boardCount;
--player.boardLaneCount[monster.lane];
}
} else {
monster.ward = target.attack == 0;
}
if (!target.ward) {
target.defense -= monster.attack;
if ((monster.lethal && monster.attack > 0) || target.defense <= 0) {
if (monster.breakthrough) {
enemy.hp -= min(target.defense, 0);
}
target.defense = 0;
target.location = CardLocation::Graveyard;
if (target.guard) {
--enemy.guardsCount;
--enemy.guardsLaneCount[target.lane];
}
--enemy.boardCount;
--enemy.boardLaneCount[target.lane];
}
if (monster.drain) {
player.hp += monster.attack;
}
} else {
target.ward = monster.attack == 0;
}
}
void State::summon(int index, int lane) {
auto &card = cards[index];
card.location = player.boardLocation;
player.mana -= card.cost;
player.cardsToDraw += card.cardDraw;
player.hp += card.playerHpChange;
enemy.hp += card.enemyHpChange;
--player.handCount;
++player.boardCount;
++player.boardLaneCount[lane];
card.canAttack = card.charge;
card.lane = lane;
if (card.guard) {
++player.guardsCount;
++player.guardsLaneCount[lane];
}
}
void State::use(int index, int targetIndex) {
auto &item = cards[index];
--player.handCount;
player.mana -= item.cost;
player.cardsToDraw += item.cardDraw;
item.location = CardLocation::Graveyard;
if (targetIndex != -1) {
auto &target = cards[targetIndex];
if (item.type == CardType::GreenItem) {
target.breakthrough = item.breakthrough || target.breakthrough;
target.charge = item.charge || target.charge;
if (target.charge) {
target.canAttack = true;
}
target.drain = item.drain || target.drain;
if (!target.guard && item.guard) {
target.guard = true;
++player.guardsCount;
++player.guardsLaneCount[target.lane];
}
target.lethal = item.lethal || target.lethal;
target.ward = item.ward || target.ward;
} else {
target.breakthrough = item.breakthrough && target.breakthrough;
target.charge = !item.charge && target.charge;
target.drain = !item.drain && target.drain;
if (target.guard && item.guard) {
target.guard = false;
--enemy.guardsCount;
--enemy.guardsLaneCount[target.lane];
}
target.lethal = !item.lethal && target.lethal;
target.ward = !item.ward && target.ward;
}
target.attack = max(0, target.attack + item.attack);
if (target.ward && item.defense < 0) {
target.ward = false;
} else {
target.defense += item.defense;
}
if (target.defense <= 0) {
target.defense = 0;
target.location = CardLocation::Graveyard;
--enemy.boardCount;
--enemy.boardLaneCount[target.lane];
if (target.guard) {
--enemy.guardsCount;
--enemy.guardsLaneCount[target.lane];
}
}
player.hp += item.playerHpChange;
enemy.hp += item.enemyHpChange;
} else if (item.type == CardType::BlueItem) {
player.hp += item.playerHpChange;
enemy.hp += item.enemyHpChange;
enemy.hp += item.defense;
}
}
void State::passCard(int index) {
auto &card = cards[index];
card.passed = true;
}
int State::evaluate() {
int score = 0;
int enemySumAttack = 0;
for (auto &card : cards) {
if (card.location == player.boardLocation) {
score += card.monsterEval();
} else if (card.location == enemy.boardLocation) {
score -= card.monsterEval();
enemySumAttack += card.attack;
}
if (card.location == player.boardLocation) {
// score += card.passed * 3;
} else if (card.location == player.handLocation) {
if (card.type != CardType::Monster) {
score += card.passed * 21;
}
}
}
if (player.handCount + player.cardsToDraw <= MAX_HAND) {
score += player.cardsToDraw * 5;
}
score += player.hp * 2;
score -= enemy.hp * 2;
if (player.hp < 5) {
score -= 100;
}
if (enemy.hp <= 0) {
score += 100000;
} else if (player.hp <= 0) {
score -= 100000;
}
return score;
}
vector<Action> State::legalMoves() {
// Remove actions to avoid regenerating each time
// TODO Fix the remove actions
// draftChoicesSeed=-876250167555862261
// shufflePlayer0Seed=-4422762594392573393
// seed=4882437402899864600
// shufflePlayer1Seed=2206163186945330453
if (false && !legalActions.empty() && !isEnemyTurn) {
if (lastAction.type == ActionType::Use) {
bool defenderDead =
lastAction.targetIndex != -1 && cards[lastAction.targetIndex].location == CardLocation::Graveyard;
bool defenderGuard = lastAction.targetIndex != -1 && cards[lastAction.targetIndex].guard;
// bool guardRemoved = lastAction.targetIndex != -1 && !cards[lastAction.targetIndex].guard && cards[lastAction.index].guard; // TODO
if (!(defenderGuard && defenderDead)) {
if (defenderDead) {
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index) ||
(action.targetIndex == lastAction.targetIndex) ||
((action.type == ActionType::Summon ||
action.type == ActionType::Use) &&
cards[action.index].cost > player.mana);
}), legalActions.end());
} else {
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index) ||
((action.type == ActionType::Summon ||
action.type == ActionType::Use) &&
cards[action.index].cost > player.mana);
}), legalActions.end());
}
return legalActions;
}
} else if (lastAction.type == ActionType::Attack) {
// TODO ward lose, ward item
// TODO dead can summon
bool defenderDead =
lastAction.targetIndex != -1 && cards[lastAction.targetIndex].location == CardLocation::Graveyard;
bool attackerDead = cards[lastAction.index].location == CardLocation::Graveyard;
bool defenderGuard = lastAction.targetIndex != -1 && cards[lastAction.targetIndex].guard;
if (!defenderGuard || !defenderDead) {
if (defenderDead && !attackerDead) {
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index) ||
(action.targetIndex == lastAction.targetIndex);
}), legalActions.end());
} else if (defenderDead && attackerDead) {
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index) ||
(action.targetIndex == lastAction.targetIndex) ||
(action.targetIndex == lastAction.index);
}), legalActions.end());
} else if (!defenderDead && attackerDead) {
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index) ||
(action.targetIndex == lastAction.index);
}), legalActions.end());
} else if (!defenderDead && !attackerDead) {
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index);
}), legalActions.end());
}
// cerr << "---------" << endl;
// cerr << lastAction << endl;
// cerr << "defenderDead: " << defenderDead << " attackerDead: " << attackerDead << " defenderGuard: "
// << defenderGuard << " " << endl;
return legalActions;
} else if (lastAction.type == ActionType::Summon && !cards[lastAction.index].charge) { // TODO item
legalActions.erase(remove_if(legalActions.begin(),
legalActions.end(),
[&](Action action) {
return (action.index == lastAction.index) ||
((action.type == ActionType::Summon ||
action.type == ActionType::Use) &&
cards[action.index].cost > player.mana);
}), legalActions.end());
return legalActions;
}
}
}
vector<Action> actions;
for (auto &card1 : cards) {
if (card1.passed) {
continue;
}
// Check for monster attacks
if (card1.location == player.boardLocation && card1.attack > 0 && card1.canAttack && !card1.attacked) {
for (auto &enemyMonster : cards) {
if (enemyMonster.location != enemy.boardLocation || enemyMonster.lane != card1.lane) {
continue;
}
if (enemy.guardsLaneCount[card1.lane] > 0) {
if (enemyMonster.guard) {
actions.emplace_back(ActionType::Attack, card1.index, enemyMonster.index);
}
} else {
actions.emplace_back(ActionType::Attack, card1.index, enemyMonster.index);
}
}
if (isEnemyTurn && enemy.guardsLaneCount[card1.lane] == 0) {
actions.emplace_back(ActionType::Attack, card1.index, -1);
}
actions.emplace_back(ActionType::PassCard, card1.index);
// Check for spells and summon
} else if (card1.location == player.handLocation && card1.cost <= player.mana) {
if (card1.type == CardType::Monster) {
if (player.boardLaneCount[0] < MAX_MONSTERS_PER_LANE) {
actions.emplace_back(ActionType::Summon, card1.index, 0);
}
if (player.boardLaneCount[1] < MAX_MONSTERS_PER_LANE) {
actions.emplace_back(ActionType::Summon, card1.index, 1);
}
} else if (card1.type == CardType::GreenItem) {
for (auto &allyMonster : cards) {
if (allyMonster.location != player.boardLocation) {
continue;
}
if (allyMonster.attacked) {
continue;
}
actions.emplace_back(ActionType::Use, card1.index, allyMonster.index);
}
} else if (card1.type == CardType::RedItem) {
for (auto &enemyMonster : cards) {
if (enemyMonster.location != enemy.boardLocation ||
(enemyMonster.attack == 0 && !enemyMonster.guard)) {
continue;
}
// Staff of Suppression or Rootkin Ritual
if (card1.id == 142 || card1.id == 149) {
if (!enemyMonster.breakthrough && !enemyMonster.drain && !enemyMonster.guard &&
!enemyMonster.lethal && !enemyMonster.ward) {
continue;
}
// Pierce Armour
} else if (card1.id == 143) {
if (!enemyMonster.guard) {
continue;
}
// Decimate
} else if (card1.id == 151) {
if (enemyMonster.attack < 4 && enemyMonster.defense < 4 && !enemyMonster.ward &&
!enemyMonster.lethal && !enemyMonster.guard) {
continue;
}
}
actions.emplace_back(ActionType::Use, card1.index, enemyMonster.index);
}
} else if (card1.type == CardType::BlueItem) {
for (auto &enemyMonster : cards) {
if (enemyMonster.location != enemy.boardLocation ||
(enemyMonster.attack == 0 && !enemyMonster.guard)) {
continue;
}
if (card1.defense == 0) {
continue;
}
actions.emplace_back(ActionType::Use, card1.index, enemyMonster.index);
}
actions.emplace_back(ActionType::Use, card1.index, -1);
}
actions.emplace_back(ActionType::PassCard, card1.index);
}
}
legalActions = actions;
return actions;
}
void State::swapPlayers() {
auto temp = player;
player = enemy;
enemy = temp;
lastAction.type = ActionType::Null;
isEnemyTurn = !isEnemyTurn;
}
// #####################################################################################################################
// ######### AGENT #####################################################################################################
// #####################################################################################################################
struct Agent {
State state;
vector<Action> actions;
int leaf = 0;
int manaCurve[13] = {0};
int monsterCardsCount = 0;
int greenCardsCount = 0;
int redCardsCount = 0;
int blueCardsCount = 0;
void read();
void readPlayersInfo();
void readCards();
void think();
void draft();
int hardcodedDraft();
int evalDraft();
int evalCardDraft(const Card &card);
void allAttackEnemyPlayer(vector<Action> &actions);
bool checkLethal(State &state);
void play();
int bruteForce(State root, int depth, Action &bestAction, int alpha);
int bruteForceLeaf(State &root, Action &bestAction, int alpha);
int runBruteForce(State root, int depth, vector<Action> &bestActions, int alpha);
int randomSearch(const State &root, vector<Action> &bestActions);
int enemyRandomSearch(const State &root);
void benchmark();
void print();
};
void Agent::read() {
if (!state.isDraft)
state.turn++;
state.lastAction.type = ActionType::Null;
this->readPlayersInfo();
this->readCards();
state.isDraft = state.player.mana == 0;
}
void Agent::readPlayersInfo() {
Player *players[2] = {&state.player, &state.enemy};
for (auto &player : players) {
int playerHealth;
int playerMana;
int playerDeck;
int playerRune;
int playerDraw;
cin >> playerHealth >> playerMana >> playerDeck >> playerRune >> playerDraw;
cin.ignore();
player->hp = playerHealth;
player->mana = playerMana;
player->cardsRemaining = playerDeck;
player->rune = playerRune / 5;
}
if (state.isDraft) {
state.player.firstPlayer = state.player.cardsRemaining == state.enemy.cardsRemaining;
state.enemy.firstPlayer = !state.player.firstPlayer;
}
state.player.handLocation = CardLocation::PlayerHand;
state.player.boardLocation = CardLocation::PlayerBoard;
state.enemy.handLocation = CardLocation::EnemyHand;
state.enemy.boardLocation = CardLocation::EnemyBoard;
int opponentHand;
int opponentActions;
cin >> opponentHand >> opponentActions;
cin.ignore();
for (int i = 0; i < opponentActions; i++) {
string cardNumberAndAction;
getline(cin, cardNumberAndAction);
}
state.enemy.handCount = opponentHand;
state.player.cardsToDraw = 0;
state.player.cardsToDraw = 0;
}
void Agent::readCards() {
state.cards.clear();
state.enemy.boardCount = 0;
state.enemy.boardLaneCount[0] = 0;
state.enemy.boardLaneCount[1] = 0;
state.enemy.guardsCount = 0;
state.enemy.guardsLaneCount[0] = 0;
state.enemy.guardsLaneCount[1] = 0;
state.enemy.handCount = 0;
state.player.boardCount = 0;
state.player.boardLaneCount[0] = 0;
state.player.boardLaneCount[1] = 0;
state.player.guardsCount = 0;
state.player.guardsLaneCount[0] = 0;
state.player.guardsLaneCount[1] = 0;
state.player.handCount = 0;
int cardCount;
cin >> cardCount;
cin.ignore();
for (int i = 0; i < cardCount; i++) {
state.cards.emplace_back();
Card &card = state.cards.back();
int cardNumber;
int instanceId;
int location;
int cardType;
int cost;
int attack;
int defense;
string abilities;
int myHealthChange;
int opponentHealthChange;
int cardDraw;
int lane;
cin >> cardNumber >> instanceId >> location >> cardType >> cost >> attack >> defense >> abilities
>> myHealthChange >> opponentHealthChange >> cardDraw >> lane;
cin.ignore();
card.id = cardNumber;
card.index = i;
card.instanceId = instanceId;
card.location = (CardLocation) location;
card.type = (CardType) cardType;
card.cost = cost;
card.attack = attack;
card.defense = defense;
card.lane = lane;
for (char &ability : abilities) {
if (ability == 'B') {
card.breakthrough = true;
} else if (ability == 'C') {
card.charge = true;
} else if (ability == 'D') {
card.drain = true;
} else if (ability == 'G') {
card.guard = true;
} else if (ability == 'L') {
card.lethal = true;
} else if (ability == 'W') {
card.ward = true;
}
}
if (card.type == CardType::Monster) {
if (card.location == state.player.boardLocation) {
++state.player.boardCount;
++state.player.boardLaneCount[card.lane];
} else if (card.location == state.enemy.boardLocation) {
++state.enemy.boardCount;
++state.enemy.boardLaneCount[card.lane];
}
}
card.playerHpChange = myHealthChange;
card.enemyHpChange = opponentHealthChange;
card.cardDraw = cardDraw;
if (card.type == CardType::Monster && card.guard) {
if (card.location == state.player.boardLocation) {
++state.player.guardsCount;
++state.player.guardsLaneCount[card.lane];
} else if (card.location == state.enemy.boardLocation) {
++state.enemy.guardsCount;
++state.enemy.guardsLaneCount[card.lane];
}
}
if (card.location == state.player.handLocation) {
++state.player.handCount;
}
}
}
void Agent::think() {
actions.clear();
if (state.isDraft) {
draft();
double sum = 0;
for (int i = 0; i < 13; ++i) {
sum += manaCurve[i] * i;
}
double average = sum / 30.0;
average = manaCurve[2];
if (average > 6 && state.player.firstPlayer) {
no_attack = false;
} else if (average > 6 && !state.player.firstPlayer) {
no_attack = true;
} else {
no_attack = false;
}
cerr << "average mana: " << average << endl;
} else {
play();
}
}
int Agent::hardcodedDraft() {
int sorted_cards1[] = {68, 7, 65, 49, 116, 69, 151, 48, 53, 51, 44, 67, 29, 139, 84, 18, 158, 28, 64, 80, 33, 85,
32, 147, 103, 37, 54, 52, 50, 99, 23, 87, 66, 81, 148, 88, 150, 121, 82, 95, 115, 133, 152,
19, 109, 157, 105, 3, 75, 96, 114, 9, 106, 144, 129, 17, 111, 128, 12, 11, 145, 15, 21, 8,
134, 155, 141, 70, 90, 135, 104, 41, 112, 61, 5, 97, 26, 34, 73, 6, 36, 86, 77, 83, 13, 89,
79, 93, 149, 59, 159, 74, 94, 38, 98, 126, 39, 30, 137, 100, 62, 122, 22, 72, 118, 1, 47, 71,
4, 91, 27, 56, 119, 101, 45, 16, 146, 58, 120, 142, 127, 25, 108, 132, 40, 14, 76, 125, 102,
131, 123, 2, 35, 130, 107, 43, 63, 31, 138, 124, 154, 78, 46, 24, 10, 136, 113, 60, 57, 92,
117, 42, 55, 153, 20, 156, 143, 110, 160, 140};
int sorted_cards2[] = {68, 7, 65, 49, 116, 69, 151, 48, 53, 51, 44, 67, 29, 139, 84, 18, 158, 28, 64, 80, 33, 85,
32, 147, 103, 37, 54, 52, 50, 99, 23, 87, 66, 81, 148, 88, 150, 121, 82, 95, 115, 133, 152,
19, 109, 157, 105, 3, 75, 96, 114, 9, 106, 144, 129, 17, 111, 128, 12, 15, 11, 145, 21, 8,
134, 155, 141, 70, 90, 135, 104, 112, 41, 61, 5, 97, 26, 34, 73, 6, 36, 86, 77, 83, 89, 13,
79, 93, 59, 149, 159, 74, 94, 38, 126, 98, 39, 30, 100, 62, 137, 122, 22, 72, 118, 1, 47, 71,
4, 91, 56, 27, 119, 101, 45, 146, 16, 120, 58, 142, 25, 127, 108, 132, 40, 14, 76, 125, 102,
123, 131, 2, 35, 130, 107, 43, 63, 31, 138, 124, 154, 78, 46, 24, 10, 136, 113, 60, 57, 92,
117, 55, 42, 153, 20, 156, 143, 110, 160, 140};
int sorted_cards1Monster[] = {68, 7, 65, 49, 116, 69, 151, 48, 53, 51, 44, 67, 29, 139, 84, 18, 158, 28, 64, 80, 33,
85, 32, 147, 103, 37, 54, 52, 50, 99, 23, 87, 66, 81, 148, 88, 150, 121, 82, 95, 115,
133, 152, 19, 109, 157, 105, 3, 75, 96, 114, 9, 106, 144, 129, 17, 111, 128, 12, 11,
145, 15, 21, 8, 134, 155, 141, 70, 90, 135, 104, 41, 112, 61, 5, 97, 26, 34, 73, 6,
36, 86, 77, 83, 13, 89, 79, 93, 149, 59, 159, 74, 94, 38, 98, 126, 39, 30, 137, 100,
62, 122, 22, 72, 118, 1, 47, 71, 4, 91, 27, 56, 119, 101, 45, 16, 146, 58, 120, 142,
127, 25, 108, 132, 40, 14, 76, 125, 102, 131, 123, 2, 35, 130, 107, 43, 63, 31,
138, 124, 154, 78, 46, 24, 10, 136, 113, 60, 57, 92, 117, 42, 55, 153, 20, 156, 143,
110, 160, 140};
int sorted_cards2Monster[] = {68, 7, 65, 49, 116, 69, 151, 48, 53, 51, 44, 67, 29, 139, 84, 18, 158, 28, 64, 80, 33,
85, 32, 147, 103, 37, 54, 52, 50, 99, 23, 87, 66, 81, 148, 88, 150, 121, 82, 95, 115,
133, 152, 19, 109, 157, 105, 3, 75, 96, 114, 9, 106, 144, 129, 17, 111, 128, 12, 15,
11, 145, 21, 8, 134, 155, 141, 70, 90, 135, 104, 112, 41, 61, 5, 97, 26, 34, 73, 6,
36, 86, 77, 83, 89, 13, 79, 93, 59, 149, 159, 74, 94, 38, 126, 98, 39, 30, 100, 62,
137, 122, 22, 72, 118, 1, 47, 71, 4, 91, 56, 27, 119, 101, 45, 146, 16, 120, 58, 142,
25, 127, 108, 132, 40, 14, 76, 125, 102, 123, 131, 2, 35, 130, 107, 43, 63, 31,
138, 124, 154, 78, 46, 24, 10, 136, 113, 60, 57, 92, 117, 55, 42, 153, 20, 156, 143,
110, 160, 140};
auto total = (float) (monsterCardsCount + greenCardsCount + blueCardsCount + redCardsCount);
if ((float) monsterCardsCount / total < 0.4) {
copy(begin(sorted_cards1Monster), end(sorted_cards1Monster), begin(sorted_cards1));
copy(begin(sorted_cards2Monster), end(sorted_cards2Monster), begin(sorted_cards2));
}
auto sorted_cards = sorted_cards1;
if (!state.player.firstPlayer) {
sorted_cards = sorted_cards2;
}
int index = -1;
int bestIndex = -1;
int bestRank = INT_MAX;
for (auto &card : state.cards) {
++index;
for (int cardRank = 0; cardRank < NUMBER_OF_CARDS; ++cardRank) {
auto card_id = sorted_cards[cardRank];
if (card_id == card.id && cardRank < bestRank) {
bestRank = cardRank;
bestIndex = index;
}
}