-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdominion.c
executable file
·1333 lines (1122 loc) · 34.6 KB
/
dominion.c
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 "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int compare(const void* a, const void* b) {
if (*(int*)a > *(int*)b)
return 1;
if (*(int*)a < *(int*)b)
return -1;
return 0;
}
struct gameState* newGame() {
struct gameState* g = malloc(sizeof(struct gameState));
return g;
}
int* kingdomCards(int k1, int k2, int k3, int k4, int k5, int k6, int k7,
int k8, int k9, int k10) {
int* k = malloc(10 * sizeof(int));
k[0] = k1;
k[1] = k2;
k[2] = k3;
k[3] = k4;
k[4] = k5;
k[5] = k6;
k[6] = k7;
k[7] = k8;
k[8] = k9;
k[9] = k10;
return k;
}
int initializeGame(int numPlayers, int kingdomCards[10], int randomSeed,
struct gameState *state) {
int i;
int j;
int it;
//set up random number generator
SelectStream(1);
PutSeed((long)randomSeed);
//check number of players
if (numPlayers > MAX_PLAYERS || numPlayers < 2)
{
return -1;
}
//set number of players
state->numPlayers = numPlayers;
//check selected kingdom cards are different
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (j != i && kingdomCards[j] == kingdomCards[i])
{
return -1;
}
}
}
//initialize supply
///////////////////////////////
//set number of Curse cards
if (numPlayers == 2)
{
state->supplyCount[curse] = 10;
}
else if (numPlayers == 3)
{
state->supplyCount[curse] = 20;
}
else
{
state->supplyCount[curse] = 30;
}
//set number of Victory cards
if (numPlayers == 2)
{
state->supplyCount[estate] = 8;
state->supplyCount[duchy] = 8;
state->supplyCount[province] = 8;
}
else
{
state->supplyCount[estate] = 12;
state->supplyCount[duchy] = 12;
state->supplyCount[province] = 12;
}
//set number of Treasure cards
state->supplyCount[copper] = 60 - (7 * numPlayers);
state->supplyCount[silver] = 40;
state->supplyCount[gold] = 30;
//set number of Kingdom cards
for (i = adventurer; i <= treasure_map; i++) //loop all cards
{
for (j = 0; j < 10; j++) //loop chosen cards
{
if (kingdomCards[j] == i)
{
//check if card is a 'Victory' Kingdom card
if (kingdomCards[j] == great_hall || kingdomCards[j] == gardens)
{
if (numPlayers == 2){
state->supplyCount[i] = 8;
}
else{ state->supplyCount[i] = 12; }
}
else
{
state->supplyCount[i] = 10;
}
break;
}
else //card is not in the set choosen for the game
{
state->supplyCount[i] = -1;
}
}
}
////////////////////////
//supply intilization complete
//set player decks
for (i = 0; i < numPlayers; i++)
{
state->deckCount[i] = 0;
for (j = 0; j < 3; j++)
{
state->deck[i][j] = estate;
state->deckCount[i]++;
}
for (j = 3; j < 10; j++)
{
state->deck[i][j] = copper;
state->deckCount[i]++;
}
}
//shuffle player decks
for (i = 0; i < numPlayers; i++)
{
if ( shuffle(i, state) < 0 )
{
return -1;
}
}
//draw player hands
for (i = 0; i < numPlayers; i++)
{
//initialize hand size to zero
state->handCount[i] = 0;
state->discardCount[i] = 0;
//draw 5 cards
// for (j = 0; j < 5; j++)
// {
// drawCard(i, state);
// }
}
//set embargo tokens to 0 for all supply piles
for (i = 0; i <= treasure_map; i++)
{
state->embargoTokens[i] = 0;
}
//initialize first player's turn
state->outpostPlayed = 0;
state->phase = 0;
state->numActions = 1;
state->numBuys = 1;
state->playedCardCount = 0;
state->whoseTurn = 0;
state->handCount[state->whoseTurn] = 0;
//int it; move to top
//Moved draw cards to here, only drawing at the start of a turn
for (it = 0; it < 5; it++){
drawCard(state->whoseTurn, state);
}
updateCoins(state->whoseTurn, state, 0);
return 0;
}
int shuffle(int player, struct gameState *state) {
int newDeck[MAX_DECK];
int newDeckPos = 0;
int card;
int i;
if (state->deckCount[player] < 1)
return -1;
qsort ((void*)(state->deck[player]), state->deckCount[player], sizeof(int), compare);
/* SORT CARDS IN DECK TO ENSURE DETERMINISM! */
while (state->deckCount[player] > 0) {
card = (int) floor(Random() * state->deckCount[player]);
newDeck[newDeckPos] = state->deck[player][card];
newDeckPos++;
for (i = card; i < state->deckCount[player]-1; i++) {
state->deck[player][i] = state->deck[player][i+1];
}
state->deckCount[player]--;
}
for (i = 0; i < newDeckPos; i++) {
state->deck[player][i] = newDeck[i];
state->deckCount[player]++;
}
return 0;
}
int playCard(int handPos, int choice1, int choice2, int choice3, struct gameState *state)
{
int card;
int coin_bonus = 0; //tracks coins gain from actions
//check if it is the right phase
if (state->phase != 0)
{
return -1;
}
//check if player has enough actions
if ( state->numActions < 1 )
{
return -1;
}
//get card played
card = handCard(handPos, state);
//check if selected card is an action
if ( card < adventurer || card > treasure_map )
{
return -1;
}
//play card
if ( cardEffect(card, choice1, choice2, choice3, state, handPos, &coin_bonus) < 0 ){
return -1;
}
//reduce number of actions
state->numActions--;
//update coins (Treasure cards may be added with card draws)
updateCoins(state->whoseTurn, state, coin_bonus);
return 0;
}
int buyCard(int supplyPos, struct gameState *state) {
int who;
if (DEBUG){
printf("Entering buyCard...\n");
}
// I don't know what to do about the phase thing.
who = state->whoseTurn;
if (state->numBuys < 1){
if (DEBUG)
printf("You do not have any buys left\n");
return -1;
} else if (supplyCount(supplyPos, state) <1){
if (DEBUG)
printf("There are not any of that type of card left\n");
return -1;
} else if (state->coins < getCost(supplyPos)){
if (DEBUG)
printf("You do not have enough money to buy that. You have %d coins.\n", state->coins);
return -1;
} else {
state->phase=1;
//state->supplyCount[supplyPos]--;
gainCard(supplyPos, state, 0, who); //card goes in discard, this might be wrong.. (2 means goes into hand, 0 goes into discard)
state->coins = (state->coins) - (getCost(supplyPos));
state->numBuys--;
if (DEBUG)
printf("You bought card number %d for %d coins. You now have %d buys and %d coins.\n", supplyPos, getCost(supplyPos), state->numBuys, state->coins);
}
//state->discard[who][state->discardCount[who]] = supplyPos;
//state->discardCount[who]++;
return 0;
}
int numHandCards(struct gameState *state) {
return state->handCount[ whoseTurn(state) ];
}
int handCard(int handPos, struct gameState *state) {
int currentPlayer = whoseTurn(state);
return state->hand[currentPlayer][handPos];
}
int supplyCount(int card, struct gameState *state) {
return state->supplyCount[card];
}
int fullDeckCount(int player, int card, struct gameState *state) {
int i;
int count = 0;
for (i = 0; i < state->deckCount[player]; i++)
{
if (state->deck[player][i] == card) count++;
}
for (i = 0; i < state->handCount[player]; i++)
{
if (state->hand[player][i] == card) count++;
}
for (i = 0; i < state->discardCount[player]; i++)
{
if (state->discard[player][i] == card) count++;
}
return count;
}
int whoseTurn(struct gameState *state) {
return state->whoseTurn;
}
int endTurn(struct gameState *state) {
int k;
int i;
int currentPlayer = whoseTurn(state);
//Discard hand
for (i = 0; i < state->handCount[currentPlayer]; i++){
state->discard[currentPlayer][state->discardCount[currentPlayer]++] = state->hand[currentPlayer][i];//Discard
state->hand[currentPlayer][i] = -1;//Set card to -1
}
state->handCount[currentPlayer] = 0;//Reset hand count
//Code for determining the player
if (currentPlayer < (state->numPlayers - 1)){
state->whoseTurn = currentPlayer + 1;//Still safe to increment
}
else{
state->whoseTurn = 0;//Max player has been reached, loop back around to player 1
}
state->outpostPlayed = 0;
state->phase = 0;
state->numActions = 1;
state->coins = 0;
state->numBuys = 1;
state->playedCardCount = 0;
state->handCount[state->whoseTurn] = 0;
//int k; move to top
//Next player draws hand
for (k = 0; k < 5; k++){
drawCard(state->whoseTurn, state);//Draw a card
}
//Update money
updateCoins(state->whoseTurn, state , 0);
return 0;
}
int isGameOver(struct gameState *state) {
int i;
int j;
//if stack of Province cards is empty, the game ends
if (state->supplyCount[province] == 0)
{
return 1;
}
//if three supply pile are at 0, the game ends
j = 0;
for (i = 0; i < 25; i++)
{
if (state->supplyCount[i] == 0)
{
j++;
}
}
if ( j >= 3)
{
return 1;
}
return 0;
}
int scoreFor (int player, struct gameState *state) {
int i;
int score = 0;
//score from hand
for (i = 0; i < state->handCount[player]; i++)
{
if (state->hand[player][i] == curse) { score = score - 1; };
if (state->hand[player][i] == estate) { score = score + 1; };
if (state->hand[player][i] == duchy) { score = score + 3; };
if (state->hand[player][i] == province) { score = score + 6; };
if (state->hand[player][i] == great_hall) { score = score + 1; };
if (state->hand[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, state) / 10 ); };
}
//score from discard
for (i = 0; i < state->discardCount[player]; i++)
{
if (state->discard[player][i] == curse) { score = score - 1; };
if (state->discard[player][i] == estate) { score = score + 1; };
if (state->discard[player][i] == duchy) { score = score + 3; };
if (state->discard[player][i] == province) { score = score + 6; };
if (state->discard[player][i] == great_hall) { score = score + 1; };
if (state->discard[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, state) / 10 ); };
}
//score from deck
for (i = 0; i < state->discardCount[player]; i++)
{
if (state->deck[player][i] == curse) { score = score - 1; };
if (state->deck[player][i] == estate) { score = score + 1; };
if (state->deck[player][i] == duchy) { score = score + 3; };
if (state->deck[player][i] == province) { score = score + 6; };
if (state->deck[player][i] == great_hall) { score = score + 1; };
if (state->deck[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, state) / 10 ); };
}
return score;
}
int getWinners(int players[MAX_PLAYERS], struct gameState *state) {
int i;
int j;
int highScore;
int currentPlayer;
//get score for each player
for (i = 0; i < MAX_PLAYERS; i++)
{
//set unused player scores to -9999
if (i >= state->numPlayers)
{
players[i] = -9999;
}
else
{
players[i] = scoreFor (i, state);
}
}
//find highest score
j = 0;
for (i = 0; i < MAX_PLAYERS; i++)
{
if (players[i] > players[j])
{
j = i;
}
}
highScore = players[j];
//add 1 to players who had less turns
currentPlayer = whoseTurn(state);
for (i = 0; i < MAX_PLAYERS; i++)
{
if ( players[i] == highScore && i > currentPlayer )
{
players[i]++;
}
}
//find new highest score
j = 0;
for (i = 0; i < MAX_PLAYERS; i++)
{
if ( players[i] > players[j] )
{
j = i;
}
}
highScore = players[j];
//set winners in array to 1 and rest to 0
for (i = 0; i < MAX_PLAYERS; i++)
{
if ( players[i] == highScore )
{
players[i] = 1;
}
else
{
players[i] = 0;
}
}
return 0;
}
int drawCard(int player, struct gameState *state)
{
int count;
int deckCounter;
if (state->deckCount[player] <= 0){//Deck is empty
//Step 1 Shuffle the discard pile back into a deck
int i;
//Move discard to deck
for (i = 0; i < state->discardCount[player];i++){
state->deck[player][i] = state->discard[player][i];
state->discard[player][i] = -1;
}
state->deckCount[player] = state->discardCount[player];
state->discardCount[player] = 0;//Reset discard
//Shufffle the deck
shuffle(player, state);//Shuffle the deck up and make it so that we can draw
if (DEBUG){//Debug statements
printf("Deck count now: %d\n", state->deckCount[player]);
}
state->discardCount[player] = 0;
//Step 2 Draw Card
count = state->handCount[player];//Get current player's hand count
if (DEBUG){//Debug statements
printf("Current hand count: %d\n", count);
}
deckCounter = state->deckCount[player];//Create a holder for the deck count
if (deckCounter == 0)
return -1;
state->hand[player][count] = state->deck[player][deckCounter - 1];//Add card to hand
state->deckCount[player]--;
state->handCount[player]++;//Increment hand count
}
else{
count = state->handCount[player];//Get current hand count for player
// Duplicate Declaration. int deckCounter;
if (DEBUG){//Debug statements
printf("Current hand count: %d\n", count);
}
deckCounter = state->deckCount[player];//Create holder for the deck count
state->hand[player][count] = state->deck[player][deckCounter - 1];//Add card to the hand
state->deckCount[player]--;
state->handCount[player]++;//Increment hand count
}
return 0;
}
int getCost(int cardNumber)
{
switch( cardNumber )
{
case curse:
return 0;
case estate:
return 2;
case duchy:
return 5;
case province:
return 8;
case copper:
return 0;
case silver:
return 3;
case gold:
return 6;
case adventurer:
return 6;
case council_room:
return 5;
case feast:
return 4;
case gardens:
return 4;
case mine:
return 5;
case remodel:
return 4;
case smithy:
return 4;
case village:
return 3;
case baron:
return 4;
case great_hall:
return 3;
case minion:
return 5;
case steward:
return 3;
case tribute:
return 5;
case ambassador:
return 3;
case cutpurse:
return 4;
case embargo:
return 2;
case outpost:
return 5;
case salvager:
return 4;
case sea_hag:
return 4;
case treasure_map:
return 4;
}
return -1;
}
//--- Refactored Card Functions --//
int adventurerCard(int *drawntreasure, struct gameState *state, int *cardDrawn, int *currentPlayer, int *temphand, int *z){
while(*drawntreasure<2){
if (state->deckCount[*currentPlayer] <1){//if the deck is empty we need to shuffle discard and add to deck
shuffle(*currentPlayer, state);
}
drawCard(*currentPlayer, state);
*cardDrawn = state->hand[*currentPlayer][state->handCount[*currentPlayer]-1];//top card of hand is most recently drawn card.
if (*cardDrawn == copper || *cardDrawn == silver || *cardDrawn == gold)
*drawntreasure++;
else{
temphand[*z]=*cardDrawn;
state->handCount[*currentPlayer]--; //this should just remove the top card (the most recently drawn one).
*z = *z + 1;
}
}
while(*z-1>=0){
state->discard[*currentPlayer][state->discardCount[*currentPlayer]++]=temphand[*z-1]; // discard all cards in play that have been drawn
*z=*z-1;
}
return 0;
}
int feastCard(struct gameState *state, int *temphand, int *currentPlayer, int *choice1){
//gain card with cost up to 5
//Backup hand
int choice1_feast = *choice1;
int i;
int x;
for (i = 0; i <= state->handCount[*currentPlayer]; i++){
temphand[i] = state->hand[*currentPlayer][i];//Backup card
state->hand[*currentPlayer][i] = -1;//Set to nothing
}
//Backup hand
//Update Coins for Buy
updateCoins(*currentPlayer, state, 5);
x = 1;//Condition to loop on
while(x == 1) {//Buy one card
if (supplyCount(choice1_feast, state) <= 0){
if (DEBUG){
printf("None of that card left, sorry!\n");
printf("Cards Left: %d\n", supplyCount(choice1_feast, state));
}
}
else if (state->coins < getCost(choice1_feast)){
printf("That card is too expensive!\n");
if (DEBUG){
printf("Coins: %d < %d\n", state->coins, getCost(choice1_feast));
}
}
else{
if (DEBUG){
printf("Deck Count: %d\n", state->handCount[*currentPlayer] + state->deckCount[*currentPlayer] + state->discardCount[*currentPlayer]);
}
gainCard(choice1_feast, state, 0, *currentPlayer);//Gain the card
x = 0;//No more buying cards
if (DEBUG){
printf("Deck Count: %d\n", state->handCount[*currentPlayer] + state->deckCount[*currentPlayer] + state->discardCount[*currentPlayer]);
}
}
}
//Reset Hand
for (i = 0; i <= state->handCount[*currentPlayer]; i++){
state->hand[*currentPlayer][i] = temphand[i];
temphand[i] = -1;
}
//Reset Hand
return 0;
}
int mineCard(struct gameState *state, int *currentPlayer, int *choice1, int *choice2, int *handPos){
int choice1_mine = *choice1;
int choice2_mine = *choice2;
int j;
int i;
j = state->hand[*currentPlayer][choice1_mine]; //store card we will trash
if (state->hand[*currentPlayer][choice1_mine] < copper || state->hand[*currentPlayer][choice1_mine] > gold)
return -1;
if (choice2_mine > treasure_map || choice2_mine < curse)
return -1;
if ( (getCost(state->hand[*currentPlayer][choice1_mine]) + 3) > getCost(choice2_mine) )
return -1;
gainCard(choice2_mine, state, 2, *currentPlayer);
//discard card from hand
discardCard(*handPos, *currentPlayer, state, 0);
//discard trashed card
for (i = 0; i < state->handCount[*currentPlayer]; i++){
if (state->hand[*currentPlayer][i] == j){
discardCard(i, *currentPlayer, state, 0);
break;
}
}
return 0;
}
int remodelCard(struct gameState *state, int *currentPlayer, int *choice1, int *choice2, int *handPos){
int j;
int i;
int choice1_remodel = *choice1;
int choice2_remodel = *choice2;
j = state->hand[*currentPlayer][choice1_remodel]; //store card we will trash
if ( (getCost(state->hand[*currentPlayer][choice1_remodel]) + 2) > getCost(choice2_remodel) )
return -1;
gainCard(choice2_remodel, state, 0, *currentPlayer);
//discard card from hand
discardCard(*handPos, *currentPlayer, state, 0);
//discard trashed card
for (i = 0; i < state->handCount[*currentPlayer]; i++){
if (state->hand[*currentPlayer][i] == j){
discardCard(i, *currentPlayer, state, 0);
break;
}
}
return 0;
}
int smithyCard(struct gameState *state, int *currentPlayer, int *handPos){
int i;
//+3 Cards
for (i = 0; i < 3; i++)
drawCard(*currentPlayer, state);
//discard card from hand
discardCard(*handPos, *currentPlayer, state, 0);
return 0;
}
int villageCard(struct gameState *state, int *currentPlayer, int *handPos){
//+1 Card
drawCard(*currentPlayer, state);
//+2 Actions
state->numActions = state->numActions + 2;
//discard played card from hand
discardCard(*handPos, *currentPlayer, state, 0);
return 0;
}
int baronCard(struct gameState *state, int *currentPlayer, int *choice1){
int choice1_baron = *choice1;
state->numBuys++;//Increase buys by 1!
if (choice1_baron > 0){//Boolean true or going to discard an estate
int p;//Iterator for hand!
int card_not_discarded = 1;//Flag for discard set!
p = 0;
while(card_not_discarded){
if (state->hand[*currentPlayer][p] == estate){//Found an estate card!
state->coins += 4;//Add 4 coins to the amount of coins
state->discard[*currentPlayer][state->discardCount[*currentPlayer]] = state->hand[*currentPlayer][p];
state->discardCount[*currentPlayer]++;
for (p = 0; p < state->handCount[*currentPlayer]; p++)
state->hand[*currentPlayer][p] = state->hand[*currentPlayer][p+1];
state->hand[*currentPlayer][state->handCount[*currentPlayer]] = -1;
state->handCount[*currentPlayer]--;
card_not_discarded = 0;//Exit the loop
}
else if (p > state->handCount[*currentPlayer]){
if(DEBUG) {
printf("No estate cards in your hand, invalid choice\n");
printf("Must gain an estate if there are any\n");
}
if (supplyCount(estate, state) > 0){
gainCard(estate, state, 0, *currentPlayer);
state->supplyCount[estate]--;//Decrement estates
if (supplyCount(estate, state) == 0)
isGameOver(state);
}
card_not_discarded = 0;//Exit the loop
}
else{
p++;//Next card
}
}
}
else {
if (supplyCount(estate, state) > 0){
gainCard(estate, state, 0, *currentPlayer);//Gain an estate
state->supplyCount[estate]--;//Decrement Estates
if (supplyCount(estate, state) == 0)
isGameOver(state);
}
}
return 0;
}
int great_hallCard(struct gameState *state, int *currentPlayer, int *handPos){
//+1 Card
drawCard(*currentPlayer, state);
//+1 Actions
state->numActions++;
//discard card from hand
discardCard(*handPos, *currentPlayer, state, 0);
return 0;
}
int minionCard(struct gameState *state, int *currentPlayer, int *handPos, int *choice1, int *choice2){
int choice1_minion = *choice1;
int choice2_minion = *choice2;
int i;
int j;
//+1 action
state->numActions++;
//discard card from hand
discardCard(*handPos, *currentPlayer, state, 0);
if (choice1_minion) //+2 coins
state->coins = state->coins + 2;
else if (choice2_minion){ //discard hand, redraw 4, other players with 5+ cards discard hand and draw 4
//discard hand
while(numHandCards(state) > 0)
discardCard(*handPos, *currentPlayer, state, 0);
//draw 4
for (i = 0; i < 4; i++)
drawCard(*currentPlayer, state);
//other players discard hand and redraw if hand size > 4
for (i = 0; i < state->numPlayers; i++){
if (i != *currentPlayer){
if ( state->handCount[i] > 4 ){
//discard hand
while( state->handCount[i] > 0 ){
discardCard(*handPos, i, state, 0);
}
//draw 4
for (j = 0; j < 4; j++)
drawCard(i, state);
}
}
}
}
return 0;
}
int stewardCard(struct gameState *state, int *currentPlayer, int *handPos, int *choice1, int *choice2, int *choice3){
int choice1_steward = *choice1;
int choice2_steward = *choice2;
int choice3_steward = *choice3;
if (choice1_steward == 1){
//+2 cards
drawCard(*currentPlayer, state);
drawCard(*currentPlayer, state);
}
else if (choice1_steward == 2){
//+2 coins
state->coins = state->coins + 2;
}
else {
//trash 2 cards in hand
discardCard(choice2_steward, *currentPlayer, state, 1);
discardCard(choice3_steward, *currentPlayer, state, 1);
}
//discard card from hand
discardCard(*handPos, *currentPlayer, state, 0);
return 0;
}
int tributeCard(struct gameState *state, int *currentPlayer, int* handPos, int *nextPlayer, int *tributeRevealedCards){
int i;
if ((state->discardCount[*nextPlayer] + state->deckCount[*nextPlayer]) <= 1){
if (state->deckCount[*nextPlayer] > 0){
tributeRevealedCards[0] = state->deck[*nextPlayer][state->deckCount[*nextPlayer]-1];
state->deckCount[*nextPlayer]--;
}
else if (state->discardCount[*nextPlayer] > 0){
tributeRevealedCards[0] = state->discard[*nextPlayer][state->discardCount[*nextPlayer]-1];
state->discardCount[*nextPlayer]--;
}
else{
//No Card to Reveal
if (DEBUG)
printf("No cards to reveal\n");
}
}
else {
if (state->deckCount[*nextPlayer] == 0){
for (i = 0; i < state->discardCount[*nextPlayer]; i++){
state->deck[*nextPlayer][i] = state->discard[*nextPlayer][i];//Move to deck
state->deckCount[*nextPlayer]++;
state->discard[*nextPlayer][i] = -1;
state->discardCount[*nextPlayer]--;
}
shuffle(*nextPlayer,state);//Shuffle the deck
}
tributeRevealedCards[0] = state->deck[*nextPlayer][state->deckCount[*nextPlayer]-1];
state->deck[*nextPlayer][state->deckCount[*nextPlayer]--] = -1;
state->deckCount[*nextPlayer]--;
tributeRevealedCards[1] = state->deck[*nextPlayer][state->deckCount[*nextPlayer]-1];
state->deck[*nextPlayer][state->deckCount[*nextPlayer]--] = -1;
state->deckCount[*nextPlayer]--;
}
if (tributeRevealedCards[0] == tributeRevealedCards[1]){//If we have a duplicate card, just drop one
state->playedCards[state->playedCardCount] = tributeRevealedCards[1];
state->playedCardCount++;
tributeRevealedCards[1] = -1;
}
//discard played card from hand
discardCard(*handPos, *currentPlayer, state, 0);
for (i = 0; i <= 2; i ++){
if (tributeRevealedCards[i] == copper || tributeRevealedCards[i] == silver || tributeRevealedCards[i] == gold) //Treasure cards
state->coins += 2;
else if (tributeRevealedCards[i] == estate || tributeRevealedCards[i] == duchy || tributeRevealedCards[i] == province || tributeRevealedCards[i] == gardens || tributeRevealedCards[i] == great_hall){//Victory Card Found
drawCard(*currentPlayer, state);
drawCard(*currentPlayer, state);
}
else //Action Card
state->numActions = state->numActions + 2;
}
return 0;
}
int ambassadorCard(struct gameState *state, int *currentPlayer, int *handPos, int *choice1, int *choice2){
int j;