int checkVillage(int p, struct gameState *post, int handPos) { struct gameState pre; int i; int currentPlayer; bool fail = false; int newCard1; int deckCard1; int postVillage, preVillage=0; //copy the game state to compare pre and post values memcpy(&pre, post, sizeof(struct gameState)); villageAction(p, post, handPos); //expected hand count printf("Hand Count\t Expected: %d\t Result:%d\n", pre.handCount[p], post->handCount[p]); //check deck count printf("Deck Count\t Expected: %d\t Result:%d\n", pre.deckCount[p]-1, post->deckCount[p]); //check expected cards added to hand //assumes no shuffling is done newCard1 = post->hand[p][handPos]; deckCard1 = pre.deck[p][pre.deckCount[p]-1]; printf("Added Card\t Expected: %d\t Result:%d\n", deckCard1, newCard1); //check number of actions printf("Actions\t Expected: %d\t Result:%d\n", pre.numActions+2, post->numActions); //check card count for all other players currentPlayer = p; for(i=0; i< MAX_PLAYERS; i++){ if(i != currentPlayer){ printf("HandCount p%d\t Expected: %d\t Result:%d\n", i+1, pre.handCount[i], post->handCount[i]); printf("DeckCount p%d\t Expected: %d\t Result:%d\n", i+1, pre.deckCount[i], post->deckCount[i]); } } //check count of coins has not changed printf("Coins\t Expected: %d\t Result:%d\n", pre.coins, post->coins); //check village Card has been discarded preVillage=0; postVillage=0; for(i=0; i< pre.handCount[p]; i++){ // printf("prehand[%d][%d]: %d\n", p, i, pre.hand[p][i]); if(pre.hand[p][i] == village){ preVillage++; } } //iterate through post hand - NOTE: no new smithy cards were added from deck to hand for(i=0; i< post->handCount[p]; i++){ // printf("posthand[%d][%d]: %d\n", p, i, post->hand[p][i]); if(post->hand[p][i] == village){ postVillage++; } } printf("Village count\t Expected: %d\t Result:%d\n", preVillage-1, postVillage); //check discard count incremenetd printf("Discard count \t Expected: %d\t Result:%d\n", pre.discardCount[p]+1, post->discardCount[p]); /***CHECK RESULTS***/ if(pre.handCount[p] != post->handCount[p]){ printf("Unexpected handcount\n"); fail = true; } if(pre.deckCount[p]-1 != post->deckCount[p]){ printf("Unexpected deckCount\n"); fail = true; } if((deckCard1 != newCard1)){ printf("Unexpected card added to hand\n"); fail = true; } if(pre.numActions+2 !=post->numActions){ printf("Unexpected number of actions\n"); fail = true; }; //check card count for other players for(i=0; i< MAX_PLAYERS; i++){ if(i != currentPlayer){ if(pre.handCount[i] !=post->handCount[i]){ printf("Unexpected Handcount p%d\n", i+1); fail = true; }; if(pre.deckCount[i] != post->deckCount[i]){ printf("Unexpected Deckcount p%d\n", i+1); fail = true; } } } //check coin count has not changed if(pre.coins != post->coins){ printf("Unexpected change in coin count\n"); fail = true; } //check Village count if(preVillage-1 != postVillage){ printf("Village card not discarded\n"); fail = true; } //check discard pile incremented if(pre.discardCount[p]+1 != post->discardCount[p]){ printf("Discard count not incremented\n"); fail = true; } if(fail ==true){ return -1; } return 0; }
int main() { int seed = 1000; int numPlayer = 2; int r; int k[10] = {adventurer, council_room, feast, gardens, mine , remodel, smithy, village, baron, great_hall}; struct gameState G; printf ("TESTING village():\n"); memset(&G, 23, sizeof(struct gameState)); // clear the game state r = initializeGame(numPlayer, k, seed, &G); // initialize a new game G.handCount[0] = 5; // set the number of cards on hand to 5 for player 0 #if (NOISY_TEST == 1) printf("Starting value for G.handCount = %d\n", G.handCount[0]); printf("Starting value for G.numActions = %d\n", G.numActions); printf("Starting value for G.discardCount = %d\n", G.discardCount[0]); printf("Starting value for G.playedCardCount = %d\n\n", G.playedCardCount); printf("Calling village card function\n"); #endif villageAction(0, 0, &G); #if (NOISY_TEST == 1) printf("G.handCount = %d, expected = %d\n", G.handCount[0], 5); printf("G.numActions = %d, expected = %d\n", G.numActions, 3); printf("G.playedCardCount = %d, expected = %d\n\n", G.playedCardCount, 1); #endif assert(G.handCount[0] == 5); assert(G.numActions == 3); //assertion fails as Actions is incorrectly incremented by 1 assert(G.playedCardCount == 1); #if (NOISY_TEST == 1) printf("Calling village card function\n"); #endif villageAction(0, 0, &G); #if (NOISY_TEST == 1) printf("G.handCount = %d, expected = %d\n", G.handCount[0], 5); printf("G.numActions = %d, expected = %d\n", G.numActions, 5); printf("G.playedCardCount = %d, expected = %d\n\n", G.playedCardCount, 2); #endif assert(G.handCount[0] == 5); assert(G.numActions == 5); //assertion fails as Actions is incorrectly incremented by 1 assert(G.playedCardCount == 2); #if (NOISY_TEST == 1) printf("Calling village card function with different hand position\n"); #endif villageAction(1, 0, &G); #if (NOISY_TEST == 1) printf("G.handCount = %d, expected = %d\n", G.handCount[0], 5); printf("G.numActions = %d, expected = %d\n", G.numActions, 7); printf("G.playedCardCount = %d, expected = %d\n\n", G.playedCardCount, 3); #endif assert(G.handCount[0] == 5); assert(G.numActions == 7); //assertion fails as Actions is incorrectly incremented by 1 assert(G.playedCardCount == 3); #if (NOISY_TEST == 1) printf("Calling village card function with different hand position\n"); #endif villageAction(2, 0, &G); #if (NOISY_TEST == 1) printf("G.handCount = %d, expected = %d\n", G.handCount[0], 5); printf("G.numActions = %d, expected = %d\n", G.numActions, 9); printf("G.playedCardCount = %d, expected = %d\n\n", G.playedCardCount, 4); #endif assert(G.handCount[0] == 5); assert(G.numActions == 9); //assertion fails as Actions is incorrectly incremented by 1 assert(G.playedCardCount == 4); printf("All tests passed!\n"); return 0; }