This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
2241 lines (2066 loc) · 69.2 KB
/
main.cc
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 <cmath>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
#include <stack>
#include <ctime>
#include <vector>
#include <bits/stdc++.h>
#include <csignal>
using namespace std;
#define MIN_ROUNDS 1
#define MAX_ROUNDS 13 //change this for different speeds
#define MIN_PLAYERS 2
#define MAX_PLAYERS 4 //change this for different speeds
#define MIN_GAMES 1
#define WIN_FACT 1 //factor to multiply with
#define LOSE_FACT 1
#define WIN_CONST 10
#define MAX_CARDS MAX_ROUNDS*MAX_PLAYERS
#define CARD_DIM 2
#define MCTS_CONSTANT sqrt(2)
int numberOfPlayers;
int numberOfRounds;
int numberOfGames;
int numberOfCards;
int numberOfSuits;
int orderSize;
int mcBudget;
int currentTrump = -1; //suit that is trump
bool suitPlayer[MAX_PLAYERS][MAX_PLAYERS]; //reducing the flexibility of number of suits here!!!!
//if suitPlayer[i][j] == true, suit i no longer in possesion of player j
int agentSelection[MAX_PLAYERS];
int algSelection[MAX_PLAYERS][2];
clock_t start;
double duration;
int totalPoints = 0;
int totalPPoints[MAX_PLAYERS] = {};
int totalPwins[MAX_PLAYERS] = {};
int absoluteVictorCounter[MAX_PLAYERS] = {};
int tempGames = 0;
int leafQuit = 0;
int simulationsCounter = 0;
string suitNames[MAX_PLAYERS];
string orderNames[MAX_ROUNDS];
string agentNames[5];
int agentSettings[5][2];
struct mctsNode {
//int player; //which player is at to move at root?
mctsNode * parent;
//The arrays below define WHICH MOVE has just been played, and by WHOM
int curTrick[MAX_PLAYERS][CARD_DIM]; //Which cards are currently ``on the table''?
int trickOrder[MAX_PLAYERS]; //what is the current trick order?
int player;
//More global game state info
bool blockedSuits[MAX_PLAYERS][MAX_PLAYERS]; //which suits are blocked for other players due to previous moves?
int cardInfo[MAX_PLAYERS][MAX_ROUNDS]; //which card belongs to the player/which card has been played already?
int tricksMade[MAX_PLAYERS]; //who has won how many tricks?
//node info
double evaluations; //evaluation so far
int simulations; //number of simulation ran
//node expansion
int numberOfChildren;
int unexploredMoves[MAX_CARDS][CARD_DIM]; //possible moves that haven't been checked yet;
mctsNode * children[MAX_CARDS];
mctsNode(mctsNode * p, int nodePlayer){
player = nodePlayer;
parent = p;
evaluations = 0;
simulations = 0;
numberOfChildren = 0;
if(parent != NULL){
parent->children[parent->numberOfChildren] = this;
parent->numberOfChildren++;
}
for(int i = 0; i < MAX_CARDS; i++){
children[i] = NULL;
unexploredMoves[i][0] = -1;
unexploredMoves[i][1] = -1;
}
}
};
//Call back handler
void signalHandler(int signum) {
cout << "Caught signal " << signum << endl;
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
cout << "Total scoreboard points(" << tempGames << " games): "<< totalPoints << endl;;
for(int i = 0; i < numberOfPlayers; i++){
cout << "Player " << i << "(" << agentNames[agentSelection[i]] << "): " << endl;
cout << "Total points: " << totalPPoints[i] << endl;
cout << "Player share: " << (double)totalPPoints[i]/totalPoints *100.0 << "%" << endl;
cout << "Player wins: " << (double)totalPwins[i]/tempGames * 100.0 << "%" << endl;
cout << "Number of absolute victories: " << absoluteVictorCounter[i] << endl;
}
cout << "Duration of calculation: " << (int)(duration/3600) << "h " << ((int)duration%3600) << "m " << ((int)duration%60) << "s" << endl;
//cout << "Leaf quits: " << leafQuit << "/" << simulationsCounter << endl;
// Terminate program
exit(signum);
}
/********************************************
* PRINT Functions
* Functions to call to display information in the terminal
*
*/
void printAllCards (int cards[][MAX_ROUNDS]){ //prints all cards their ownership
for(int i = 0; i < numberOfSuits; i++){
for (int j = 0; j < numberOfRounds; j++){
cout << cards[i][j] << " ";
}
cout << endl;
}
}
void printOrder(int order[]){
cout << endl;
for(int i = 0; i < numberOfPlayers; i++){
cout << i << ": Player #" << order[i] << endl;
}
cout << endl;
}
void printCards(int cards[][CARD_DIM], int length){ //prints some 2d array containing cards
for (int i = 0; i < length; i++){
cout << "Suit: " << cards[i][0] << ", Order: " << cards[i][1] << endl;
}
}
void printRoundScore(int curRound, int bids[], int wins[]){
cout << endl << endl << "--- Scores of round " << curRound << " ---" << endl << endl;
for (int i = 0; i < numberOfPlayers; i++){
cout << "Player #" << i << " bid " << bids[i] << " and won " << wins[i] << " tricks." << endl;
}
cout << endl;
}
void printScoreBoard(int score[]){
cout << endl << endl << "--- Score Board ---" << endl << endl;
for (int i = 0; i < numberOfPlayers; i++){
cout << "Player #" << i << " has " << score[i] << " points." << endl;
}
cout << endl;
}
/******************************************************************************
* EVALUATION Functions:
* These functions assist the agents in bidding based on their hand
* Or evaluate other statistics about the game at the current point
*/
int evaluateRandomBid(int maxBid, int iBid){
int bid = rand() % maxBid+1;
while(bid == iBid){
bid = rand() % (maxBid+1);
}
return bid;
}
double evalCardCurrent(int cards[][MAX_ROUNDS], int currentTrick[][CARD_DIM], int player, int cSuit, int cOrder){
double eval = 0.0;
int nPlayedCards = 0;
for(int i = 0; i < numberOfPlayers; i++){
if(currentTrick[i][0] == -1){
break;
}
nPlayedCards++;
}
int trumpOrder = 0;
int oPlayed = 0;
for (int i = cOrder; i < orderSize; i++){
if(cards[cSuit][i] == -2 || cards[cSuit][i] == player){ //a suit card was played at some point or is owned by the player
oPlayed++;
}
}
if(currentTrump != -1 && cSuit != currentTrump){ //card isnt a trump card, add factor
trumpOrder = orderSize;
for(int i = 0; i < orderSize; i++){
if(cards[currentTrump][i] == -2 ||
cards[currentTrump][i] == player){ //trump card has been played or is owned by the player
trumpOrder--;
}
}
}
eval = 1.0 - (double)((abs(cOrder - orderSize) - oPlayed) + trumpOrder) /((orderSize - oPlayed) + trumpOrder);
if(nPlayedCards > 0){ //A card has already been played in this trick, check if this card is even still usefull
if(currentTrick[0][0] != cSuit){
if(cSuit == currentTrump){ //checking a trump card
for(int i = 1; i < nPlayedCards; i++){
if(currentTrick[i][0] == currentTrump){ //checking for more trumps
if(currentTrick[i][1] > cOrder){
eval = eval * -1.0;
break;
}
}
}
}
else { //card has no chance of winning
eval = eval * -1.0;
}
}
else if(currentTrick[0][0] == cSuit){ //matched the suit
for(int i = 0; i < nPlayedCards; i++){ //check if other matchers are better or if there are trumps
if(currentTrick[i][0] == currentTrump ||
(currentTrick[i][0] == currentTrick[0][0] && currentTrick[i][1] > cOrder)){
eval = eval * -1.0;
break;
}
}
}
}
return eval;
}
int evalBidding(int cards[][MAX_ROUNDS], int player, int illegalBid){
double score = 0.0;
double cValue = 0.0;
for (int i = 0; i < numberOfSuits; i++){
for(int j = 0; j < numberOfRounds; j++){
if(cards[i][j] == player){ //card is owned by player
if(i == currentTrump){ //card is trump
cValue = (1.0 - (double)(abs(j - orderSize)) / (numberOfCards));
}
else { //card isnt trump
cValue = (1.0 - (double)((pow((double)(j - orderSize), 2.0)+ orderSize) / (numberOfCards)));
}
if (cValue > 0){
score += cValue;
}
}
}
}
if (round(score) == illegalBid){
if(round(score+0.5) == illegalBid){
score = (round(score)-1.0);
}
else {
score = round(score+0.5);
}
}
else {
score = round(score);
}
return score;
}
int evalWinPoints(int nRounds){ //calculates how many points ONE player can win from the current round onwards
return (nRounds*(nRounds +1)/2)*WIN_FACT + WIN_CONST*nRounds;
}
int evalLosePoints(int nRounds){ //calculates how many points ONE player can lose from the current round onwards
return (nRounds*(nRounds +1)/2)*LOSE_FACT;
}
//evaluates the quality of the scoreboard for the player passed as parameter
float evalScoreBoard(int player, int curRound, int scoreboard[]){
int normalize = scoreboard[player];
float adjustedScore[numberOfPlayers];
float SBvalue = 0.0;
for(int i = 0; i < numberOfPlayers; i++){ //normalize the array on the player
adjustedScore[i] = scoreboard[i] - normalize;
if(adjustedScore[i] == 0.0 && i != player){
adjustedScore[i] = 1.0;
}
}
sort(adjustedScore, adjustedScore+numberOfPlayers, greater<int>()); //sort the array
adjustedScore[0] = adjustedScore[0] * pow(adjustedScore[0], adjustedScore[0]/(curRound-1+WIN_CONST+curRound));
for(int i = 0; i < numberOfPlayers; i++){
SBvalue = SBvalue - (adjustedScore[i]/(i+1));
}
return SBvalue;
}
/******************************************************************************
* CHECKER Functions:
* These functions are used to get data for any player from the game
* - Determine who has won the trick -> trickWinner()
* - Check if a move is legit -> validMove()
* - Determine the game winner -> gameWinner()
* - absolute victor -> Check if someone has won the game before it is over
* - Possible moves -> returns legal moves for a certain player
* - playMove -> Plays a move and checks if that is a legal move
*/
//Checks who won the trick
int trickWinner(int playedMoves[][CARD_DIM]){ //returns the winning hand
int winningHand = 0;
for(int i = 1; i < numberOfPlayers; i++){
if(playedMoves[i][0] == -1){ //trick isnt complete yet!
//cout << "oy vey"
return -i; //cant decide a winner then
}
if(playedMoves[i][0] == playedMoves[0][0]){ //hand followed suit
if(playedMoves[i][0] == playedMoves[winningHand][0]){ //checking if suits match against current winner
if(playedMoves[i][1] > playedMoves[winningHand][1]){ //check if current is higher
winningHand = i; //current hand is the best
}
}
}
else if (currentTrump == playedMoves[i][0]){ //hand is trump suit
if(playedMoves[i][0] == playedMoves[winningHand][0]){ //current winning hand also played trump suit
if(playedMoves[i][1] > playedMoves[winningHand][1]){ //check if current is higher
winningHand = i; //current hand is the best
}
}
else { //current winning hand didn't play trump
winningHand = i; //current hand is the best
}
} //player did not follow suit or play trump therefore not eligible to win trick
}
return winningHand; //return the player NUMBER that won (so the actual player, not the hand)
}
//Checks if a move is legal
bool validMove(int cards[][MAX_ROUNDS], int playedMoves[][CARD_DIM], int player, int pSuit, int pCardn){
if(cards[pSuit][pCardn] == player){ //card belongs to the player
if (playedMoves[0][0] == -1 || pSuit == playedMoves[0][0]){ //first card to be played or card suit matches
return true;
}
else { //The player does not follow suit
for(int i = 0; i < numberOfRounds; i++){
if(cards[playedMoves[0][0]][i] == player){ //the player could follow suit
return false;
}
} //the player could not follow suit
return true;
}
}
return false; //the card does not belong to the player
}
//Checks who has won, tie means nobody won
int gameWinner(int score[]){
bool tie = false;
int winner = 0;
for (int i = 1; i < numberOfPlayers; i++){
if(score[i] > score[winner]){
winner = i;
tie = false;
}
else if (score[i] == score[winner]){
tie = true;
}
}
if (tie){
return -1;
}
return winner;
}
int absoluteVictor(int score[], int roundsToPlay){ //current scoreboard and how many rounds are yet to be played
if(roundsToPlay < 2){ //??????? Usefull / useless??
return -1;
}
int p1 = 0;
int p2 = 1;
if(score[p1] < score[p2]){
p1 = 1;
p2 = 0;
}
for(int i = 2; i < numberOfPlayers; i++){
if(score[i] > score[p1]){
p2 = p1;
p1 = i;
}
else if (score[i] > score[p2]){
p2 = i;
}
}
if(score[p1] == score[p2]){
return -1;
}
//calculate the distance
int minP1 = score[p1] - evalLosePoints(roundsToPlay);
int maxP2 = score[p2] + evalWinPoints(roundsToPlay);
if(minP1 > maxP2){
//cout << minP1 << ", " << maxP2 << endl;
return p1;
}
return -1;
}
//Places in the array playable cards which cards are possible to be played
void possibleMoves(int playableCards[][CARD_DIM], int cards[][MAX_ROUNDS], int &numMoves, int playedMoves[][CARD_DIM], int player, int curRound){
numMoves = 0;
for(int i = 0; i < numberOfSuits; i++){
for(int j = 0; j < numberOfRounds; j++){
if(validMove(cards, playedMoves, player, i, j)){
playableCards[numMoves][0] = i;
playableCards[numMoves][1] = j;
numMoves++;
}
}
}
}
//tries to play a move, returns false if it fails
bool playMove (int player, int suit, int order, int cards[][MAX_ROUNDS], int playedMoves[][CARD_DIM]){
if(!validMove(cards, playedMoves, player, suit, order)){ //card is not owned, therefore unplayable
return false;
}
for(int i = 0; i < numberOfPlayers; i++){
if(playedMoves[i][0] == -1){ //was able to play the move
playedMoves[i][0] = suit;
playedMoves[i][1] = order;
cards[suit][order] = -2; // -2 indicates that this card was played
if(playedMoves[0][0] != playedMoves[i][0]){ //The player did not follow suit
suitPlayer[playedMoves[0][0]][player] = true; //updating public information
}
return true;
}
}
return false; //no place available
}
/******************************************************************************
* SET-UP and GAME CONTROLLER Functions
* These functions are used to generate/control rounds/games, so tricks can be played
* as well as initialize a game and adjust the player order
*/
//shifts an array left
void shiftArray(int array[], int length){
int temp = array[0];
for(int i = 1; i < numberOfPlayers; i++){
array[i-1] = array[i];
}
array[length-1] = temp;
}
//Assigns a player number to a card, the indexes determine which card it is, the value stored to whom it belongs (-1 is nan)
void assignCards(int cards[][MAX_ROUNDS], int curRound){
for (int i = 0; i < numberOfPlayers; i++){ //for each player
int assigned = 0;
while(assigned < curRound){ //assign round number of cards
int suit = rand() % numberOfSuits;
int number = rand() % (numberOfCards / numberOfSuits);
if (cards[suit][number] == -1){ //card is free!
cards[suit][number] = i; //assign it to me!
assigned++;
}
}
}
if(curRound < numberOfRounds){ //assign Trump suit if possible
currentTrump = -1;
//random starting position
int suit = rand() % numberOfSuits;
int number = rand() % (numberOfCards / numberOfSuits);
for (int i = suit; i < numberOfSuits*2; i++){
for (int j = 0; j < (numberOfCards / numberOfSuits); j++){
if (i == suit && j == 0){
j = number;
}
if (cards[i%numberOfSuits][j] == -1){
currentTrump = i%numberOfSuits;
cards[i%numberOfSuits][j] = -2; //card has been selected as trump and is made known to the players
break;
}
}
if (currentTrump > -1){
break;
}
}
}
}
//Controller function of rounds setup
void initializeRound(int trickScore[], int roundNumber, int cards[][MAX_ROUNDS], int played[][CARD_DIM], int order[], int trickOrder[], bool pSuits[][MAX_PLAYERS]){
for(int i = 0; i < numberOfSuits; i++){ //initialize card ownership
for (int j = 0; j < numberOfRounds; j++){
cards[i][j] = -1;
}
}
for(int i = 0; i < numberOfPlayers; i++){ //initialize arrays
played[i][0] = -1;
played[i][1] = -1;
trickScore[i] = 0;
for (int j = 0; j < numberOfSuits; j++){
pSuits[j][i] = false;
}
}
if(roundNumber == numberOfRounds){ //first round
for(int i = 0; i < numberOfPlayers; i++){ //assign the order
while(true){ //not very neato but does its job
int place = rand() % 4;
if(order[place] == -1){
order[place] = i;
trickOrder[place] = i;
break;
}
}
}
}
else { //not first round, shift the order
int temp = order[0];
for(int i = 1; i < numberOfPlayers; i++){
order[i-1] = order[i];
trickOrder[i-1] = order[i];
}
order[numberOfPlayers-1] = temp;
trickOrder[numberOfPlayers-1] = temp;
}
assignCards(cards, roundNumber);
}
void initializeGame(int scores[], int tricks[], int order[]){
for (int i = 0; i < numberOfPlayers; i++){
scores[i] = 0;
tricks[i] = 0;
order[i] = -1;
}
}
/******************************************************************************
* PLAYOUT Functions
* These are assisting functions for the MC agents, playouts return a value
* of the quality of the playout for a certain player between [0, 1]
*/
void calculateRequiredCardsSim(int player, int requiredCards[], int tricksMade[],
int trickCards[][CARD_DIM], int tOrder[],
int curRound, int &tNum, int &nPlayer){
int trickNum = 0;
for(int i = 0; i < numberOfPlayers; i++){
trickNum += tricksMade[i];
}
int nextPlayer = numberOfPlayers; //assume the trick is already complete
for(int i = 0; i < numberOfPlayers; i++){
if(trickCards[i][0] == -1){ //this player hasnt played yet
nextPlayer = i;
break;
}
requiredCards[tOrder[i]] = 0;
}
for(int i = nextPlayer; i < numberOfPlayers; i++){
requiredCards[tOrder[i]] = 1;
}
for(int i = 0; i < numberOfPlayers; i++){
requiredCards[i] += (curRound-trickNum-1);
}
requiredCards[player] = 0;
tNum = trickNum;
nPlayer = nextPlayer;
}
bool checkValidCards(int player, int availableCards[], int assignCards[],
int cards[][MAX_ROUNDS], bool blockedSuits[][MAX_PLAYERS]){
int reqTest[numberOfPlayers] = {};
int totalAssign = 0;
for(int i = 0; i < numberOfPlayers; i++){
if(i == player){
continue;
}
reqTest[i] = assignCards[i];
totalAssign += assignCards[i];
if(availableCards[i] < assignCards[i]){
return false;
}
}
bool notDone = true;
float values[numberOfSuits][numberOfRounds] = {};
while(notDone){
notDone = false;
bool assignedOne = false;
int oneP = -1;
int oneSuit = -1;
for(int i = 0; i < numberOfSuits; i++){
int count = 0;
int lastP = -1;
for(int p = 0; p < numberOfPlayers; p++){
if(p == player){
continue;
}
if(!blockedSuits[i][p] && reqTest[p] > 0){
lastP = p;
count++;
}
}
float value = 0;
if(count > 0){
value = (float)1 / (float)count;
}
for(int j = 0; j < numberOfRounds; j++){
if(values[i][j] > -0.01 && cards[i][j] == -1){
values[i][j] = value;
if(!assignedOne && value == 1.0){
assignedOne = true;
oneP = lastP;
oneSuit = i;
}
}
else{
values[i][j] = 0;
}
}
}
if(assignedOne){
int temppp = 0;
for(int j = 0; j < numberOfRounds; j++){
if(values[oneSuit][j] == 1.0){
temppp++;
values[oneSuit][j] = -1.0;
reqTest[oneP]--;
if(!notDone){
notDone = true;
}
}
}
}
}
float cardPart[numberOfPlayers] = {};
for(int i = 0; i < numberOfSuits; i++){
for(int p = 0; p < numberOfPlayers; p++){
if(p == player){
continue;
}
if(!blockedSuits[i][p]){
for(int j = 0; j < numberOfRounds; j++){
cardPart[p] += values[i][j];
}
}
}
}
float donation[numberOfPlayers][numberOfSuits] = {};
int bums = 0;
for(int p = 0; p < numberOfPlayers; p++){
if(p == player){
continue;
}
if(cardPart[p] > (float)reqTest[p] && reqTest[p] > 0){ //have some to spare && actually still part taking in the divide, so the donation is valid
for(int s = 0; s < numberOfSuits; s++){
// if(blockedSuits[s][p]){ //cant donate from a blocked suit
// continue;
// }
int suitAvailable = 0;
for(int k = 0; k < numberOfRounds; k++){
if(cards[s][k] == -1){
suitAvailable++;
}
}
if(suitAvailable > 0){
donation[p][s] = (cardPart[p] - (float)reqTest[p]);
if(donation[p][s] > (float)suitAvailable){ //donating more in this suit than is possible
donation[p][s] = (float)suitAvailable; //donate the maximum possible;
}
}
}
}
else if(cardPart[p] + 0.01f < (float)reqTest[p]){ //need some of that spare
bums++;
}
}
bool impossible = false;
if(bums > 0){ //there are agents that might not make it
for(int p = 0; p < numberOfPlayers; p++){
if(p == player){
continue;
}
if(cardPart[p] + 0.01f < (float)reqTest[p]){ //this player is a bum
float usedDonation = 0.0f;
for(int i = 0; i < numberOfPlayers; i++){ //look for a donator
bool success = false;
if(i == player || i == p || (cardPart[i] + 0.01f < (float)reqTest[i])){ //cant loan from the actual player or yourself or another bum
continue;
}
for(int j = 0; j < numberOfSuits; j++){
// if(blockedSuits[j][p]){ //cant loan from a blocked suit
// continue;
// }
//Donator may also be able to donate through another donator..
if(donation[i][j] > 0.0f){ //the donator has some room in this suit
float localValue = donation[i][j];
usedDonation += localValue;
//donation[i][j] = 0.0f;
if((cardPart[p] + usedDonation + 0.01f) >= (float)reqTest[p]){ // now have enough
success = true;
if(cardPart[p] + usedDonation > (float)reqTest[p] + 0.01f){ //used more than needed
localValue -= (cardPart[p] + usedDonation - (float)reqTest[p]);
usedDonation -= (cardPart[p] + usedDonation - (float)reqTest[p]);
}
}
for(int k = 0; k < numberOfSuits; k++){
donation[i][k] -= localValue;
if(donation[i][k] < 0.0f){
donation[i][k] = 0.0f;
}
}
}
if(success){
break;
}
}
}
if((cardPart[p] + usedDonation + 0.01f) < (float)reqTest[p]){ //see if it now fits after borrowing
impossible = true;
break;
}
}
}
}
if(impossible){
return false;
}
return true;
}
void playSimulatedMove(int simPlayer, int suit, int order, int cards[][MAX_ROUNDS],
int playedMoves[][CARD_DIM], bool suits[][MAX_PLAYERS]){
for(int i = 0; i < numberOfPlayers; i++){
if(playedMoves[i][0] == -1){ //was able to play the move
playedMoves[i][0] = suit;
playedMoves[i][1] = order;
cards[suit][order] = -2; // -2 indicates that this card was played
if(playedMoves[0][0] != playedMoves[i][0]){ //did not follow suit
suits[playedMoves[0][0]][simPlayer] = true;
}
return;
}
}
}
//Made for agents that work within the incomplete information
void simulatePossibleMoves(int player, int simP, int currentTrick[][CARD_DIM],
int assignCards[], int posmovsim[][CARD_DIM],
int cards[][MAX_ROUNDS], bool blockedSuits[][MAX_PLAYERS],
int &numMoves){
int availableCards[numberOfPlayers] = {};
int trickPos = 0;
for(int i = 0; i < numberOfSuits; i++){
if(i < numberOfPlayers && currentTrick[i][0] != -1){
trickPos = i;
}
for(int j = 0; j < numberOfRounds; j++){
if(cards[i][j] != -1){
continue;
}
for(int k = 0; k < numberOfPlayers; k++){
if(k == player){
continue;
}
if(!blockedSuits[i][k]){
availableCards[k]++;
}
}
}
}
int curpos = 0;
for(int i = 0; i < numberOfSuits; i++){
bool suitVerified = false;
if(blockedSuits[i][simP]){
continue;
}
for(int j = 0; j < numberOfRounds; j++){
if(cards[i][j] == -1){ //possible candidate
if(!suitVerified){
int saveSuit = i; //the suit bool value before the simulated move
if(currentTrick[0][0] != -1){
saveSuit = currentTrick[0][0];
}
//pretend you played this card
playSimulatedMove(simP, i, j, cards, currentTrick, blockedSuits);
assignCards[simP]--;
//check if the cards then still works out
bool check = checkValidCards(player, availableCards, assignCards, cards, blockedSuits);
//put everything back
assignCards[simP]++;
cards[i][j] = -1;
currentTrick[trickPos+1][0] = -1;
currentTrick[trickPos+1][1] = -1;
blockedSuits[saveSuit][simP] = false; //(possibly) set it back to false
if(check){
suitVerified = true;
}
else{ //we cant take any cards of this suit
break;
}
}
//if you made it here the card is good!
posmovsim[curpos][0] = i;
posmovsim[curpos][1] = j;
curpos++; //another possible move detected
}
}
}
numMoves = curpos;
}
bool generateSimulatedHands(int player, int assignCards[], int cards[][MAX_ROUNDS],
bool blockedSuits[][MAX_PLAYERS]){
int availableCards[numberOfPlayers] = {};
int cardsToPlace = 0;
for(int i = 0; i < numberOfPlayers; i++){
if(i == player){
continue;
}
cardsToPlace += assignCards[i];
}
for(int j = 0; j < numberOfSuits; j++){
for(int k = 0; k < numberOfRounds; k++){
if(cards[j][k] == -1){
for(int i = 0; i < numberOfPlayers; i++){
if(i == player){
continue;
}
if(!blockedSuits[j][i]){
availableCards[i]++;
}
}
}
}
}
// if(!checkValidCards(player, availableCards, assignCards, cards, blockedSuits)){
// cout << "check valid false in generateSimulatedHands()" << endl;
// return false;
// }
for(int a = 0; a < cardsToPlace; a++){ //
int suit = rand() % numberOfSuits;
int number = rand() % numberOfRounds;
int pSelected = rand() % numberOfPlayers;
while(pSelected == player){
pSelected = rand() % numberOfPlayers;
}
float pRatio;
if(availableCards[pSelected] > 0){
pRatio = (float)assignCards[pSelected] / (float)availableCards[pSelected];
}
else {
pRatio = 0;
}
for(int i = 0; i < numberOfPlayers; i++){ //selected the best candidate
if(i == player){
continue;
}
float curRatio = (float)assignCards[i] / (float)availableCards[i];
if (pRatio < curRatio || (pRatio == curRatio && availableCards[i] < availableCards[pSelected])){ //select the player that needs it the most
pSelected = i;
pRatio = curRatio;
}
}
//select a card for the player
bool foundCard = false;
int test = -1;
while(!foundCard){
test++;
if(test > numberOfCards){ //ran through all the cards and still couldnt find a usefull one
return false;
}
//check if the suit is right
if((number > 0 && number % numberOfRounds == 0) || blockedSuits[suit][pSelected]){
//cout << number % numberOfRounds << endl;
number = 0;
suit = (suit+1) % numberOfSuits;
}
if(cards[suit][number] != -1){ //card is not available
number++;
continue;
}
//calculate if taking this card ruins it for the other players combined
int combinedCards = 0;
for(int i = 0; i < numberOfSuits; i++){
for(int j = 0; j < numberOfRounds; j++){
if(cards[i][j] != -1 || (i == suit && j == number)){
continue;
}
for(int k = 0; k < numberOfPlayers; k++){
if(k == pSelected || k == player){
continue;
}
if(!blockedSuits[i][k]){
combinedCards++;
break;
}
}
}
}
int combinedReq = 0;
for(int i = 0; i < numberOfPlayers; i++){
if(i == pSelected || i == player){
continue;
}
if(!blockedSuits[suit][i] && assignCards[i] == availableCards[i]){ //cant take this suit due to other players
number = 0;
suit = (suit+1) % numberOfSuits;
continue;
}
combinedReq += assignCards[i];
}
if(combinedCards < combinedReq){ //cant take this suit due to other players
number = 0;
suit = (suit+1) % numberOfSuits;
continue;
}
if(!blockedSuits[suit][pSelected] && cards[suit][number] == -1){
break; //found a matching card
}
number++;
}
cards[suit][number] = pSelected;
assignCards[pSelected]--;
for(int j = 0; j < numberOfPlayers; j++){
if(j == player){
continue;
}
if(!blockedSuits[suit][j]){
availableCards[j]--;
}
if (assignCards[j] > availableCards[j]){
return false;
}
}
}
return true;
}
//plays randomly till the end, results are stored in the scoreboard
bool playoutRoundRandom(int player, int curRound, int cards[][MAX_ROUNDS], int tOrder[],
int score[], int trickCards[][CARD_DIM], int tricksMade[],
int bids[], bool suits[][MAX_PLAYERS]){
int possibleCards[curRound][CARD_DIM];
int requiredCards[numberOfPlayers] = {};
int trickNum = 0;
int nextPlayer = -1;
calculateRequiredCardsSim(player, requiredCards, tricksMade, trickCards, tOrder, curRound, trickNum, nextPlayer);
if(!generateSimulatedHands(player, requiredCards, cards, suits)){
return false;
}
for(int numTricks = trickNum; numTricks < curRound; numTricks++){ //play all tricks left in the round
for(int a = nextPlayer; a < numberOfPlayers; a++){ //play a trick
int moves = 0;
int move = -1;
int suitChoice = -1;
int orderChoice = -1;
for(int i = 0; i < curRound; i++){ //clear the possible moves
possibleCards[i][0] = -1;
possibleCards[i][1] = -1;
}
possibleMoves(possibleCards, cards, moves, trickCards, tOrder[a], curRound);
//because the hands are generated we use possibleMoves() instead of simulatePossibleMoves()
if(moves == 0){
cout << "!!!!!!!!!!!!!!!!!!!" << endl;
}
move = rand() % moves;
suitChoice = possibleCards[move][0];