int totalCardCountTest(struct gameState* state){ state->numPlayers = 2; state->deckCount[0] = 27; int i; //Deck will contain one of each card for(i = 0; i < state->deckCount[0]; i++){ state->deck[0][i] = i; } //Fill hand with village cards state->handCount[0] = 5; for(i = 5; i < 5 + state->handCount[0]; i++){ state->hand[0][i-5] = village; } //Fill discard pile with arbitrary 10 cards state->discardCount[0] = 10; for(i = 0; i < state->discardCount[0]; i++){ state->discard[0][i] = i; } struct gameState* cpy = (struct gameState*)(malloc(sizeof(struct gameState))); memcpy(cpy, state, sizeof(struct gameState)); villageCard(state, 0, 0); int cardCount = 0; cardCount += state->deckCount[0] + state->handCount[0] + state->discardCount[0]; if(cardCount != cpy->deckCount[0] + cpy->handCount[0] + cpy->discardCount[0]){ return -1; } //Card should exit normally as expected free(cpy); return 0; }
void checkVillage(int thisPlayer, struct gameState *G, int handPos) { int otherPlayer; int result; int discardCount = 0; struct gameState testG; memcpy (&testG, G, sizeof(struct gameState)); if (thisPlayer == 1) otherPlayer = 0; else otherPlayer = 1; result = villageCard(&testG, thisPlayer, handPos); // do to G what we think village should do drawCard(thisPlayer, G); G->numActions += 2; // discard village from hand discardCard(handPos, thisPlayer, G, 0); discardCount++; // -- TESTS --// printf("Village returned successfully - return value %d, expected %d\n", result, 0); assert (result == 0); // village returned correctly printf("Number of actions incremented properly - actions %d, expected %d\n", testG.numActions, G->numActions); // assert (testG.numActions == G->numActions); if (testG.numActions != G->numActions) // replace assert since it always catches bug printf("Failure: incorrect number of actions added\n"); printf("Played card discarded as expected - played card pile %d, expected %d\n", testG.playedCardCount, G->playedCardCount); assert(testG.playedCardCount == G->playedCardCount); printf("Discarded card is village - played card value %d, expected %d\n", testG.playedCards[testG.playedCardCount - 1], village); assert(testG.playedCards[testG.playedCardCount - 1] == village); // check for state change to other player printf("No state change to other player:\n"); printf(" Other player deck count - actual %d, expected %d\n", testG.deckCount[otherPlayer], G->deckCount[otherPlayer]); assert(testG.deckCount[otherPlayer] == G->deckCount[otherPlayer]); printf(" Other player hand count - actual %d, expected %d\n", testG.handCount[otherPlayer], G->handCount[otherPlayer]); assert(testG.handCount[otherPlayer] == G->handCount[otherPlayer]); printf(" Other player discard count - actual %d, expected %d\n", testG.discardCount[otherPlayer], G->discardCount[otherPlayer]); assert(testG.discardCount[otherPlayer] == G->discardCount[otherPlayer]); // test game states have been altered the same way printf("Testing for identical game states...\n"); if (memcmp(G, &testG, sizeof(struct gameState)) == 0) printf("Success: both game states identical\n"); else printf("Fail: game states are not identical\n"); //assert(memcmp(&G, testG, sizeof(struct gameState)) == 0); }
int main () { struct gameState actual, expected; int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall}; int num_players = 2; initializeGame(num_players, k, 1, &actual); int player = 0; //Set up player's deck. actual.hand[player][0] = village; actual.hand[player][1] = gold; //+1 card from the deck -1 village card = the same. actual.handCount[player] = 2; memcpy(&expected, &actual, sizeof(struct gameState)); //Copy the game state to the pre game state test case. assert(memcmp(&expected, &actual, sizeof(struct gameState)) == 0); printf("The state of the actual and expected both initialized the same.\n"); //Set up expected results. expected.deckCount[player] = actual.deckCount[player] - 1; expected.numActions = 2; int handpos = 0; if( villageCard(player, &actual, handpos) == 0){ printf("The village card was called successfully.\n"); }else{ printf("The village card failed to be called.i\n"); } //Check results. //a card was taken from the deck successfully. assert( expected.deckCount[player] == actual.deckCount[player] ); //+ 1 card from the deck -1 village card. Hand Count should be the same. assert( expected.handCount[player] == actual.handCount[player] ); //Check that the player has two actions left. assert( expected.numActions == actual.numActions ); //Check that the village card is discarded. assert( actual.hand[player][0] != village ); printf("The function executed successfully for the player.\n"); int player2 = 1; //Check that no other players were not altered. assert( expected.deckCount[player2] == actual.deckCount[player2] ); assert( expected.handCount[player2] == actual.handCount[player2] ); printf("The function didn't alter other player's state.\n"); return 0; }
int main() { int i, j, m; int actions; int success = 1; int failed = 0; int seed = 1000; int count = 0; int numPlayer = 2; int maxBonus = 10; int p = 0, r, deckC; int shuffled = 0; int originalDeck[100]; int k[10] = {adventurer, council_room, feast, gardens, mine , remodel, smithy, village, baron, great_hall }; struct gameState G; printf ("TESTING villlageCard():\n"); for (i = 0; i < 10; i++) { memset(&G, 23, sizeof(struct gameState)); // clear the game state r = initializeGame(numPlayer, k, seed, &G); // initialize a new game G.numActions = i; for (m = 0; m < 5; m++) { #if (NOISY_TEST == 1) printf("Actions in players hand before village: %d\n", G.numActions); #endif actions = G.numActions; villageCard(p, &G, G.handCount[p] - 1); #if (NOISY_TEST == 1) printf("Actions in players hand after village: %d\n", G.numActions); printf("Should be: %d\n", (m+1)*2); #endif if ((m+1)*2 != G.numActions) { printf("TEST FAILED\n"); success = 0; } } } if (success) { printf("All tests passed!\n"); } return 0; }
/* void villageCard(struct gameState* state, int currentPlayer, int handPos){ drawCard(currentPlayer, state); state->numActions = 2; //discard played card from hand discardCard(handPos, currentPlayer, state, 0); return; } */ int basicTest(struct gameState* state){ state->numPlayers = 2; state->deckCount[0] = 27; int i; //Deck should include at one copper, one silver, and one gold to be found for(i = 0; i < state->deckCount[0]; i++){ state->deck[0][i] = i; } state->handCount[0] = 5; for(i = 5; i < 5 + state->handCount[0]; i++){ state->hand[0][i-5] = village; } state->discardCount[0] = 10; for(i = 0; i < state->discardCount[0]; i++){ state->discard[0][i] = i; } struct gameState* cpy = (struct gameState*)(malloc(sizeof(struct gameState))); memcpy(cpy, state, sizeof(struct gameState)); villageCard(state, 0, 0); //Use 1 action, add 2 actions if(state->numActions != cpy->numActions +1){ free(cpy); return -1; } //Discard village card, add one new card if(state->handCount[0] != cpy->handCount[0]){ free(cpy); return -2; } //Discard only the village card if(state->discardCount[0] != cpy->discardCount[0] +1){ free(cpy); return -3; } free(cpy); return 0; }
int discardTest(struct gameState* state){ state->numPlayers = 2; state->deckCount[0] = 27; int i; for(i = 0; i < state->deckCount[0]; i++){ state->deck[0][i] = i; } state->handCount[0] = 5; for(i = 5; i < 5 + state->handCount[0]; i++){ state->hand[0][i-5] = village; } state->discardCount[0] = 10; for(i = 0; i < state->discardCount[0]; i++){ state->discard[0][i] = i; } struct gameState* cpy = (struct gameState*)(malloc(sizeof(struct gameState))); memcpy(cpy, state, sizeof(struct gameState)); villageCard(state, 0, 0); if(state->discardCount[0] != cpy->discardCount[0]+1){ free(cpy); return -1; } if(state->discard[0][state->discardCount[0]-1] != village){ free(cpy); return -2; } //Card should exit normally as expected free(cpy); return 0; }
int main(int argc, char *argv[]){ PutSeed(-1); printf("cardtest2.c\n"); struct gameState state; struct gameState stateCopy; memset(&state, 0, sizeof(struct gameState)); /* copy of gameState for comparison */ stateCopy = state; /*Init deck with 4 cards.*/ state.deck[0][0] = silver; state.deck[0][1] = province; state.deck[0][2] = gold; state.deck[0][3] = duchy; state.hand[0][0] = village; state.deckCount[0] = 4; state.discardCount[0] = 0; state.handCount[0] = 1; state.numActions = 0; state.playedCardCount = 0; villageCard(&state, 0, 0); printf("villageCard(): %s empty hand (except Village card) case\n", (state.handCount[0] == 1 && state.playedCardCount == 1 && state.deckCount[0] == 3 && state.numActions == 2) ? "PASS" : "FAIL"); /* clear game state */ memset(&state, 0, sizeof(struct gameState)); /*Init deck with 4 cards.*/ state.deck[0][0] = silver; state.deck[0][1] = province; state.deck[0][2] = gold; state.deck[0][3] = duchy; state.hand[0][0] = village; state.handCount[0] = 1; state.deckCount[0] = 0; state.discardCount[0] = 0; state.numActions = 0; state.playedCardCount = 0; villageCard(&state, 0, 0); printf("villageCard(): %s empty deck case\n", (state.handCount[0] == 0 && state.playedCardCount == 1 && state.deckCount[0] == 0 && state.numActions == 2) ? "PASS" : "FAIL"); /* clear verified changes made by villageCard() */ memset(&state.handCount[0], 0, sizeof(int)); memset(&state.deckCount[0], 0, sizeof(int)); memset(&state.discardCount[0], 0, sizeof(int)); memset(&state.playedCardCount, 0, sizeof(int)); memset(&state.hand[0], 0, sizeof(int) * MAX_HAND); memset(&state.deck[0], 0, sizeof(int) * MAX_DECK); memset(&state.discard[0], 0, sizeof(int) * MAX_DECK); memset(&state.playedCards, 0, sizeof(int) * MAX_DECK); memset(&state.numActions, 0, sizeof(int) * MAX_DECK); /* verify that no erroneous changes are made to game state */ printf("villageCard(): %s changes to correct player only\n", (memcmp(&state, &stateCopy, sizeof(struct gameState)) == 0) ? "PASS" : "FAIL"); /* clear game state */ memset(&state, 0, sizeof(struct gameState)); /*Init deck with 4 cards.*/ state.deck[0][0] = silver; state.deck[0][1] = province; state.deck[0][2] = gold; state.deck[0][3] = duchy; state.hand[0][0] = village; state.hand[0][1] = village; state.deckCount[0] = 4; state.discardCount[0] = 0; state.handCount[0] = 2; state.numActions = 0; state.playedCardCount = 0; villageCard(&state, 0, 0); villageCard(&state, 0, 1); printf("villageCard(): %s 2 consecutive Village cards case\n", (state.handCount[0] == 2 && state.playedCardCount == 2 && state.deckCount[0] == 2 && state.numActions == 4) ? "PASS" : "FAIL"); // printf("deckCount: %i handCount: %i playedCardCount: %i\n", // state.deckCount[0], state.handCount[0], state.playedCardCount); return 0; }
void testVillage() { int seed = 1000; int numPlayer = 2; int k[10] = {adventurer, council_room, feast, gardens, mine , remodel, smithy, village, baron, great_hall}; int testPlayer=1; int handCount=5; //initialize gamestate struct gameState G; //loading hand with 5 cards first, hardcoded in int firstHand[5]; //place specific cards in each possible spot firstHand[0] = feast; firstHand[1] = feast; firstHand[2] = estate; firstHand[3] = smithy; firstHand[4] = village; initializeGame(numPlayer, k, seed, &G); //set specifics for new game state G.handCount[testPlayer]= handCount; //place cards in test player's hand memcpy(G.hand[testPlayer], firstHand, sizeof(int) * handCount); //The adventure card has two basic functions //draws cards until two treasures are drawn //discards all other cards //now to test actual function villageCard(&G, testPlayer, 4); //now going to test state after passing correct values //since it adds two treasures, handCount should be at 7 if(G.numActions == 3) { printf ("Passed, game action =3, expected %d\n", G.numActions); } else { printf ("Failed, game action = %d cards, expected 3\n", G.numActions); } }
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus) { int i; int j; int k; int x; int index; int currentPlayer = whoseTurn(state); int nextPlayer = currentPlayer + 1; int tributeRevealedCards[2] = {-1, -1}; int temphand[MAX_HAND];// moved above the if statement int drawntreasure=0; int cardDrawn; int z = 0;// this is the counter for the temp hand if (nextPlayer > (state->numPlayers - 1)){ nextPlayer = 0; } //uses switch to select card and perform actions switch( card ) { case adventurer: adventurerCard(state, currentPlayer, temphand); return 0; case council_room: //+4 Cards for (i = 0; i < 4; i++) { drawCard(currentPlayer, state); } //+1 Buy state->numBuys++; //Each other player draws a card for (i = 0; i < state->numPlayers; i++) { if ( i != currentPlayer ) { drawCard(i, state); } } //put played card in played card pile discardCard(handPos, currentPlayer, state, 0); return 0; case feast: //gain card with cost up to 5 //Backup hand 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, state) <= 0){ if (DEBUG) printf("None of that card left, sorry!\n"); if (DEBUG){ printf("Cards Left: %d\n", supplyCount(choice1, state)); } } else if (state->coins < getCost(choice1)){ printf("That card is too expensive!\n"); if (DEBUG){ printf("Coins: %d < %d\n", state->coins, getCost(choice1)); } } else{ if (DEBUG){ printf("Deck Count: %d\n", state->handCount[currentPlayer] + state->deckCount[currentPlayer] + state->discardCount[currentPlayer]); } gainCard(choice1, 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; case gardens: return -1; case mine: j = state->hand[currentPlayer][choice1]; //store card we will trash if (state->hand[currentPlayer][choice1] < copper || state->hand[currentPlayer][choice1] > gold) { return -1; } if (choice2 > treasure_map || choice2 < curse) { return -1; } if ( (getCost(state->hand[currentPlayer][choice1]) + 3) > getCost(choice2) ) { return -1; } gainCard(choice2, 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; case remodel: j = state->hand[currentPlayer][choice1]; //store card we will trash if ( (getCost(state->hand[currentPlayer][choice1]) + 2) > getCost(choice2) ) { return -1; } gainCard(choice2, 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; case smithy: smithyCard(state, currentPlayer, handPos); return 0; case village: villageCard(state, currentPlayer, handPos); return 0; case baron: baronCard(state, currentPlayer, choice1); return 0; case great_hall: //+1 Card drawCard(currentPlayer, state); //+1 Actions state->numActions++; //discard card from hand discardCard(handPos, currentPlayer, state, 0); return 0; case minion: //+1 action state->numActions++; //discard card from hand discardCard(handPos, currentPlayer, state, 0); if (choice1) //+2 coins { state->coins = state->coins + 2; } else if (choice2) //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; case steward: stewardCard(state, handPos, currentPlayer, choice1, choice2, choice3); return 0; case tribute: 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; } 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; case ambassador: j = 0; //used to check if player has enough cards to discard if (choice2 > 2 || choice2 < 0) { return -1; } if (choice1 == handPos) { return -1; } for (i = 0; i < state->handCount[currentPlayer]; i++) { if (i != handPos && i == state->hand[currentPlayer][choice1] && i != choice1) { j++; } } if (j < choice2) { return -1; } if (DEBUG) printf("Player %d reveals card number: %d\n", currentPlayer, state->hand[currentPlayer][choice1]); //increase supply count for choosen card by amount being discarded state->supplyCount[state->hand[currentPlayer][choice1]] += choice2; //each other player gains a copy of revealed card for (i = 0; i < state->numPlayers; i++) { if (i != currentPlayer) { gainCard(state->hand[currentPlayer][choice1], state, 0, i); } } //discard played card from hand discardCard(handPos, currentPlayer, state, 0); //trash copies of cards returned to supply for (j = 0; j < choice2; j++) { for (i = 0; i < state->handCount[currentPlayer]; i++) { if (state->hand[currentPlayer][i] == state->hand[currentPlayer][choice1]) { discardCard(i, currentPlayer, state, 1); break; } } } return 0; case cutpurse: updateCoins(currentPlayer, state, 2); for (i = 0; i < state->numPlayers; i++) { if (i != currentPlayer) { for (j = 0; j < state->handCount[i]; j++) { if (state->hand[i][j] == copper) { discardCard(j, i, state, 0); break; } if (j == state->handCount[i]) { for (k = 0; k < state->handCount[i]; k++) { if (DEBUG) printf("Player %d reveals card number %d\n", i, state->hand[i][k]); } break; } } } } //discard played card from hand discardCard(handPos, currentPlayer, state, 0); return 0; case embargo: //+2 Coins state->coins = state->coins + 2; //see if selected pile is in play if ( state->supplyCount[choice1] == -1 ) { return -1; } //add embargo token to selected supply pile state->embargoTokens[choice1]++; //trash card discardCard(handPos, currentPlayer, state, 1); return 0; case outpost: //set outpost flag state->outpostPlayed++; //discard card discardCard(handPos, currentPlayer, state, 0); return 0; case salvager: //+1 buy state->numBuys++; if (choice1) { //gain coins equal to trashed card state->coins = state->coins + getCost( handCard(choice1, state) ); //trash card discardCard(choice1, currentPlayer, state, 1); } //discard card discardCard(handPos, currentPlayer, state, 0); return 0; case sea_hag: for (i = 0; i < state->numPlayers; i++){ if (i != currentPlayer){ state->discard[i][state->discardCount[i]] = state->deck[i][state->deckCount[i]--]; state->deckCount[i]--; state->discardCount[i]++; state->deck[i][state->deckCount[i]--] = curse;//Top card now a curse } } return 0; case treasure_map: //search hand for another treasure_map index = -1; for (i = 0; i < state->handCount[currentPlayer]; i++) { if (state->hand[currentPlayer][i] == treasure_map && i != handPos) { index = i; break; } } if (index > -1) { //trash both treasure cards discardCard(handPos, currentPlayer, state, 1); discardCard(index, currentPlayer, state, 1); //gain 4 Gold cards for (i = 0; i < 4; i++) { gainCard(gold, state, 1, currentPlayer); } //return success return 1; } //no second treasure_map found in hand return -1; } return -1; }
int main() { int i; int seed = 1000; int numPlayer = 2; int p, r, handCount; int playedCardCount; int numActions; int k[10] = {adventurer, council_room, feast, gardens, mine , remodel, smithy, village, baron, great_hall}; struct gameState G; int maxHandCount = 5; int passFlag = 1; int maxplayedCardCount = 10; // arrays of all coppers, villages int coppers[MAX_HAND]; int villages[MAX_HAND]; for (i = 0; i < MAX_HAND; i++) { coppers[i] = copper; villages[i] = village; } printf ("TESTING villageCard():\n"); for (p = 0; p < numPlayer; p++) { for (handCount = 1; handCount <= maxHandCount; handCount++) { for (playedCardCount = 0; playedCardCount <= maxplayedCardCount; playedCardCount++) { #if (NOISY_TEST == 1) printf("Test player %d with %d card(s) on hand and %d playedCardCount.\n", p, handCount, playedCardCount); #endif memset(&G, 23, sizeof(struct gameState)); // clear the game state r = initializeGame(numPlayer, k, seed, &G); // initialize a new game G.handCount[p] = handCount; // set the number of cards on hand G.playedCardCount = playedCardCount; //Set the number of played card memcpy(G.hand[p], villages, sizeof(int) * handCount); // set all the cards to copper memcpy(G.playedCards, coppers, sizeof(int) * playedCardCount); // set all the cards to copper numActions = G.numActions; villageCard(0, p, &G, 0); #if (NOISY_TEST == 1) printf("G.handCount = %d, expected = %d\n", G.handCount[p], handCount); #endif if(G.handCount[p] != handCount){ // check if the number of cards on hand is correct printf("FAIL, G.handCount is not as expected.\n"); passFlag = 0; } #if (NOISY_TEST == 1) printf("G.playedCardCount = %d, expected = %d\n", G.playedCardCount, playedCardCount + 1); #endif if(G.playedCardCount != playedCardCount + 1){ // check if the number of played card count is correct printf("FAIL, G.playedCardCount is not as expected.\n"); passFlag = 0; } #if (NOISY_TEST == 1) printf("G.playedCards[playedCardCount] = %d, expected = %d\n", G.playedCards[playedCardCount], village); #endif if(G.playedCards[playedCardCount] != village){ printf("FAIL, village card is not in played card pile\n"); passFlag = 0; } printf("G.numActions = %d, expected = %d\n", G.numActions, numActions + 2); if(G.numActions != numActions + 2){ printf("FAIL, numActions is not as expected.\n"); passFlag = 0; } } } } if(passFlag == 1) printf("All tests passed!\n"); return 0; }
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus) { int i; int j; int k; int x; int index; int currentPlayer = whoseTurn(state); int nextPlayer = currentPlayer + 1; int tributeRevealedCards[2] = {-1, -1}; int temphand[MAX_HAND];// moved above the if statement int drawntreasure=0; int cardDrawn; int z = 0;// this is the counter for the temp hand if (nextPlayer > (state->numPlayers - 1)){ nextPlayer = 0; } //uses switch to select card and perform actions switch( card ) { case adventurer: 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++; } } 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; case council_room: return council_roomCard(currentPlayer, state, handPos); case feast: //gain card with cost up to 5 //Backup hand 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, state) <= 0){ if (DEBUG) printf("None of that card left, sorry!\n"); if (DEBUG){ printf("Cards Left: %d\n", supplyCount(choice1, state)); } } else if (state->coins < getCost(choice1)){ printf("That card is too expensive!\n"); if (DEBUG){ printf("Coins: %d < %d\n", state->coins, getCost(choice1)); } } else{ if (DEBUG){ printf("Deck Count: %d\n", state->handCount[currentPlayer] + state->deckCount[currentPlayer] + state->discardCount[currentPlayer]); } gainCard(choice1, 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; case gardens: return -1; case mine: return mineCard(currentPlayer, state, handPos, choice1, choice2); case remodel: j = state->hand[currentPlayer][choice1]; //store card we will trash if ( (getCost(state->hand[currentPlayer][choice1]) + 2) > getCost(choice2) ) { return -1; } gainCard(choice2, 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; case smithy: return smithyCard(currentPlayer, state, handPos); case village: return villageCard(currentPlayer, state, handPos); case baron: state->numBuys++;//Increase buys by 1! if (choice1 > 0){//Boolean true or going to discard an estate int p = 0;//Iterator for hand! int card_not_discarded = 1;//Flag for discard set! 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 < 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; case great_hall: //+1 Card drawCard(currentPlayer, state); //+1 Actions state->numActions++; //discard card from hand discardCard(handPos, currentPlayer, state, 0); return 0; case minion: return minionCard(currentPlayer, state, handPos, choice1, choice2); case steward: if (choice1 == 1) { //+2 cards drawCard(currentPlayer, state); drawCard(currentPlayer, state); } else if (choice1 == 2) { //+2 coins state->coins = state->coins + 2; } else { //trash 2 cards in hand discardCard(choice2, currentPlayer, state, 1); discardCard(choice3, currentPlayer, state, 1); } //discard card from hand discardCard(handPos, currentPlayer, state, 0); return 0; case tribute: 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; } 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; case ambassador: j = 0; //used to check if player has enough cards to discard if (choice2 > 2 || choice2 < 0) { return -1; } if (choice1 == handPos) { return -1; } for (i = 0; i < state->handCount[currentPlayer]; i++) { if (i != handPos && i == state->hand[currentPlayer][choice1] && i != choice1) { j++; } } if (j < choice2) { return -1; } if (DEBUG) printf("Player %d reveals card number: %d\n", currentPlayer, state->hand[currentPlayer][choice1]); //increase supply count for choosen card by amount being discarded state->supplyCount[state->hand[currentPlayer][choice1]] += choice2; //each other player gains a copy of revealed card for (i = 0; i < state->numPlayers; i++) { if (i != currentPlayer) { gainCard(state->hand[currentPlayer][choice1], state, 0, i); } } //discard played card from hand discardCard(handPos, currentPlayer, state, 0); //trash copies of cards returned to supply for (j = 0; j < choice2; j++) { for (i = 0; i < state->handCount[currentPlayer]; i++) { if (state->hand[currentPlayer][i] == state->hand[currentPlayer][choice1]) { discardCard(i, currentPlayer, state, 1); break; } } } return 0; case cutpurse: updateCoins(currentPlayer, state, 2); for (i = 0; i < state->numPlayers; i++) { if (i != currentPlayer) { for (j = 0; j < state->handCount[i]; j++) { if (state->hand[i][j] == copper) { discardCard(j, i, state, 0); break; } if (j == state->handCount[i]) { for (k = 0; k < state->handCount[i]; k++) { if (DEBUG) printf("Player %d reveals card number %d\n", i, state->hand[i][k]); } break; } } } } //discard played card from hand discardCard(handPos, currentPlayer, state, 0); return 0; case embargo: //+2 Coins state->coins = state->coins + 2; //see if selected pile is in play if ( state->supplyCount[choice1] == -1 ) { return -1; } //add embargo token to selected supply pile state->embargoTokens[choice1]++; //trash card discardCard(handPos, currentPlayer, state, 1); return 0; case outpost: //set outpost flag state->outpostPlayed++; //discard card discardCard(handPos, currentPlayer, state, 0); return 0; case salvager: //+1 buy state->numBuys++; if (choice1) { //gain coins equal to trashed card state->coins = state->coins + getCost( handCard(choice1, state) ); //trash card discardCard(choice1, currentPlayer, state, 1); } //discard card discardCard(handPos, currentPlayer, state, 0); return 0; case sea_hag: for (i = 0; i < state->numPlayers; i++){ if (i != currentPlayer){ state->discard[i][state->discardCount[i]] = state->deck[i][state->deckCount[i]--]; state->deckCount[i]--; state->discardCount[i]++; state->deck[i][state->deckCount[i]--] = curse;//Top card now a curse } } return 0; case treasure_map: //search hand for another treasure_map index = -1; for (i = 0; i < state->handCount[currentPlayer]; i++) { if (state->hand[currentPlayer][i] == treasure_map && i != handPos) { index = i; break; } } if (index > -1) { //trash both treasure cards discardCard(handPos, currentPlayer, state, 1); discardCard(index, currentPlayer, state, 1); //gain 4 Gold cards for (i = 0; i < 4; i++) { gainCard(gold, state, 1, currentPlayer); } //return success return 1; } //no second treasure_map found in hand return -1; } return -1; }
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus) { int currentPlayer = whoseTurn(state); int nextPlayer = currentPlayer + 1; int tributeRevealedCards[2] = {-1, -1}; int temphand[MAX_HAND];// moved above the if statement int drawntreasure=0; int cardDrawn; int z = 0;// this is the counter for the temp hand if (nextPlayer > (state->numPlayers - 1)) { nextPlayer = 0; } switch( card ) { case adventurer: return adventurerCard(&drawntreasure, state, &cardDrawn, ¤tPlayer, temphand, &z); case council_room: return council_roomCard(¤tPlayer, state, &handPos); case feast: return feastCard(state, temphand, ¤tPlayer, &choice1); case gardens: return -1; case mine: return mineCard(state, ¤tPlayer, &choice1, &choice2, &handPos); case remodel: return remodelCard(state, ¤tPlayer, &choice1, &choice2, &handPos); case smithy: return smithyCard(state, ¤tPlayer, &handPos); case village: return villageCard(state, ¤tPlayer, &handPos); case baron: return baronCard(state, ¤tPlayer, &choice1); case great_hall: return great_hallCard(state, ¤tPlayer, &handPos); case minion: return minionCard(state, ¤tPlayer, &handPos, &choice1, &choice2); case steward: return stewardCard(state, ¤tPlayer, &handPos, &choice1, &choice2, &choice3); case tribute: return tributeCard(state, ¤tPlayer, &handPos, &nextPlayer, tributeRevealedCards); case ambassador: return ambassadorCard(state, ¤tPlayer, &handPos, &choice1, &choice2); case cutpurse: return cutpurseCard(state, ¤tPlayer, &handPos); case embargo: return embargoCard(state, ¤tPlayer, &handPos, &choice1); case outpost: return outpostCard(state, ¤tPlayer, &handPos); case salvager: return salvagerCard(state, ¤tPlayer, &handPos, &choice1); case sea_hag: return sea_hagCard(state, ¤tPlayer); case treasure_map: return treasure_mapCard(state, ¤tPlayer, &handPos); } return -1; }
int main() { srand(time(NULL)); int gameSeed = rand() % 1000 + 1; int p = 0; //player 1 int numPlayer = 4; int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall}; struct gameState* GS = newGame(); initializeGame(numPlayer, k, gameSeed, GS); GS->hand[p][4] = village; //5th card in hand is village printf("Playing Village card and testing...\n"); /*Checking handcount and cards in hand before playing village*/ printf("Hand count before village is %d...\n", GS->handCount[p]); int i = 0, cardStatus; char c[25]; for(i = 0; i < GS->handCount[p]; i++){ cardNumToName(GS->hand[p][i], c); //Converts card number to string printf( "%s, ", c); if(strcmp(c, "Village") == 0){ cardStatus = 1; //Card is present in hand } } /*Check if village card is in hand before use*/ if(cardStatus == 1){ printf("\nTest PASSED, card is present in player %d's hand\n\n", p); } else { printf("\nTest FAILED, card is NOT present in player %d's hand\n\n", p); } villageCard(p, GS, 4); //Play village card cardStatus = 0; //Reset to zero /*Checking handcount and cards in hand after playing village*/ printf("Hand count after village is %d...\n", GS->handCount[p]); for(i = 0; i < GS->handCount[p]; i++){ cardNumToName(GS->hand[p][i], c); //Converts card number to string printf( "%s, ", c); if(strcmp(c, "Village") == 0){ cardStatus = 1; //Card is present in hand } } /*Check if village card is in hand*/ if(cardStatus == 0){ printf("\nTest PASSED, card is NOT present in player %d's hand\n\n", p); } else { printf("\nTest FAILED, card is present in player %d's hand\n\n", p); } printf("Testing for correct number of cards drawn...\n"); /*Check current state player's handcount +1 cards after discard is 5 cards*/ if(GS->handCount[p] == 5){ printf("Test PASSED, Player %d drew +1 cards\n\n", p); } else { printf("Test FAILED, Player %d drew incorrect amount of cards\n\n", p); } printf("Testing for correct number of actions...\n"); /*Check if numActions is incremented*/ if(GS->numActions == 3){ printf("Test PASSED, Player %d has +2 numActions\n\n", p); } else { printf("Test FAILED, Player %d has incorrect number of numActions\n\n", p); } printf("Checking if card is in played pile...\n"); /*Check if card is in played pile*/ if(GS->playedCards[p] == village){ printf("Test PASSED, Village is in played pile\n\n"); } else { printf("Test FAILED, Village is NOT in played pile\n\n"); } return 0; }
int stateTest(struct gameState* state, int expectedStateChanges){ int* stateTracker = (int*)(malloc(19*sizeof(int))); memset(stateTracker, 0, 19*sizeof(int)); int i, retVal; state->numPlayers = 2; state->deckCount[0] = 27; for(i = 0; i < state->deckCount[0]; i++){ state->deck[0][i] = i; } state->handCount[0] = 5; for(i = 0; i < state->handCount[0]; i++){ state->hand[0][i] = village; } state->discardCount[0] = 10; for(i = 0; i < state->discardCount[0]; i++){ state->discard[0][i] = i; } struct gameState* cpy = (struct gameState*)(malloc(sizeof(struct gameState))); memcpy(cpy, state, sizeof(struct gameState)); villageCard(state, 0, 0); do{ retVal = _stateTestHelper(state, cpy, 0); if(retVal){ stateTracker[retVal]++; if(stateTracker[retVal] > 1){ printf("stateTest %i: Can't Clear State Error\n", retVal); return -1; } } } while (retVal); int sc = _stateTrackerChecker(stateTracker); //numActions should change if(!stateTracker[8]){ return -1; } //Hand Should change if(!stateTracker[11]){ return -2; } //Hand Count Shouldn't CHange if(stateTracker[12]){ return -3; } //discard should change if(!stateTracker[13]){ return -4; } //discard count if(!stateTracker[14]){ return -5; } //playedCards if(!stateTracker[15]){ return -6; } //PlayedCardCount if(!stateTracker[16]){ return -7; } //deckCount if(!stateTracker[17]){ return -8; } //deck should change if(!stateTracker[18]){ return -9; } if(sc != expectedStateChanges){ printf("stateTest -1: State error, %i Extra Changes\n", sc-expectedStateChanges); return -10; } free(stateTracker); free(cpy); return 0; }
int main() { int i, r, j; int players = 4; int seed = rand() % 1000; int check; struct gameState G; struct gameState pre; int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall}; //Initialize game memset(&G, 0, sizeof(struct gameState)); // zero game state r = initializeGame(players, k, seed, &G); // initializeGame returns 0 on success assert(r == 0); printf("Testing: village() in process...!\n"); //Initialize all player decks and hands for(i = 0; i < players; i++) { //initialize and set hand G.handCount[i] = 5; G.hand[i][0] = village; G.hand[i][1] = feast; G.hand[i][2] = mine; G.hand[i][3] = smithy; G.hand[i][4] = baron; //initialize and set deck G.deckCount[i] = 5; G.deck[i][0] = adventurer; G.deck[i][1] = council_room; G.deck[i][2] = copper; G.deck[i][3] = silver; G.deck[i][3] = gold; } memcpy(&pre, &G, sizeof(struct gameState)); for(j = 0; j < G.numPlayers; j++) { //Set player turn G.whoseTurn = j; //Manually set pre game condition //Increase handCount since village adds +1 cards to hand //Increase numActions since village adds +2 actions to a player's turn pre.numActions = 1; //Always starts at 1 for each turn pre.handCount[j] += 1; pre.numActions += 2; //village adds +2 actions //Test for +1 card, +2 action check = villageCard(&G, 0); if(check != 0) printf("great_hall card function returned unsuccessful!\n"); //Test for the same handCount: great_hall adds +1 cards into the players hand if(G.handCount[j] != pre.handCount[j]) { printf("Fail: pre.handCount = %d post.handCount = %d\n", pre.handCount[j], G.handCount[j]); printf("Player %d\n", j+1); } //Test for the same numActions: great_hall = +1 actions if(G.numActions != pre.numActions) { printf("Fail: pre.numActions = %d post.numActions = %d\n", pre.numActions, G.numActions); printf("Player %d\n", j+1); } } printf("Testing: village() complete!\n"); return 0; }