diff --git a/projects/alghanmz/alghanmz-assignment-3/Makefile b/projects/alghanmz/alghanmz-assignment-3/Makefile new file mode 100644 index 000000000..377d44b80 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/Makefile @@ -0,0 +1,46 @@ +CFLAGS= -Wall -fpic -coverage -lm -std=c99 + +unittestresults: unittest1 unittest2 unittest3 unittest4 unittest5 runtests + +rngs.o: rngs.h rngs.c + gcc -c rngs.c $(CFLAGS) + +dominion.o: dominion.h dominion.c rngs.o + gcc -c dominion.c $(CFLAGS) + +unittest1: dominion.o rngs.o unittest1.c + gcc -o unittest1 unittest1.c dominion.o rngs.o $(CFLAGS) + #./unittest1 + +unittest2: dominion.o rngs.o unittest2.c + gcc -o unittest2 unittest2.c dominion.o rngs.o $(CFLAGS) + #./unittest2 + +unittest3: dominion.o rngs.o unittest3.c + gcc -o unittest3 unittest3.c dominion.o rngs.o $(CFLAGS) + #./unittest3 + +unittest4: dominion.o rngs.o unittest4.c + gcc -o unittest4 unittest4.c dominion.o rngs.o $(CFLAGS) + #./unittest4 + +unittest5: dominion.o rngs.o unittest5.c + gcc -o unittest5 unittest5.c dominion.o rngs.o $(CFLAGS) + #./unittest5 + +#ifeq (0,1) +runtests: unittest1 unittest2 unittest3 unittest4 unittest5 + ./unittest1 > unittestresults.out + gcov -b -f dominion.c >> unittestresults.out + ./unittest2 >> unittestresults.out + gcov -b -f dominion.c >> unittestresults.out + ./unittest3 >> unittestresults.out + gcov -b -f dominion.c >> unittestresults.out + ./unittest4 >> unittestresults.out + gcov -b -f dominion.c >> unittestresults.out + ./unittest5 >> unittestresults.out + gcov -b -f dominion.c >> unittestresults.out +#endif + +clean: + rm -f *.o unittest1 unittest2 unittest3 unittest4 unittest5 *.gcov *.gcda *.gcno *.out diff --git a/projects/alghanmz/alghanmz-assignment-3/dominion.c b/projects/alghanmz/alghanmz-assignment-3/dominion.c new file mode 100644 index 000000000..0c865f97e --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/dominion.c @@ -0,0 +1,1420 @@ +#include "dominion.h" +#include "dominion_helpers.h" +#include "rngs.h" +#include +#include +#include + +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 = 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 { + int count = state->handCount[player];//Get current hand count for player + 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; +} + +int doMine(int choice1, int choice2, struct gameState *state, int handPos, int currentPlayer) +{ + int i; + int j; + + 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; +} + +void getEstateCard(struct gameState *state, int currentPlayer) +{ + 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); + } + } +} + +void doBaron(int choice1, struct gameState *state, int currentPlayer) +{ + state->numBuys++;//Increase buys by 1! + state->numBuys++;//Increase buys by 1! + if (choice1 > 0) { //Boolean true or going to discard an estate + int p = 1; //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"); + } + getEstateCard(state, currentPlayer); + card_not_discarded = 0;//Exit the loop + } + + else { + p++;//Next card + } + } + } + + else { + getEstateCard(state, currentPlayer); + } +} + +void doMinion(int choice1, int choice2, struct gameState *state, int handPos, int currentPlayer) +{ + int i; + int j; + + //+1 action + state->numActions++; + + //discard card from hand + discardCard(handPos, currentPlayer, state, 0); + + if (choice1) + { + 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) > 1 /*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); + } + } + } + } + } +} + +void printDeck(struct gameState *state, int p) { + printf("Deck:"); + for (int i=0; i < state->deckCount[p]; i++) + printf(" %d", state->deck[p][i]); + printf("\n"); +} + +void printHand(struct gameState *state, int p) { + printf("Hand:"); + for (int i=0; i < state->handCount[p]; i++) + printf(" %d", state->hand[p][i]); + printf("\n"); +} + +void doTribute(struct gameState *state, int currentPlayer, int nextPlayer, int tributeRevealedCards[2]) +{ + 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]; + tributeRevealedCards[1] = state->deck[nextPlayer][state->deckCount[nextPlayer]-2]; + //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->numActions = state->numActions + 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->coins += 2; + } + } +} + +int doAmbassador(int choice1, int choice2, struct gameState *state, int handPos, int currentPlayer) +{ + int i; + int j; + + j = 0; //used to check if player has enough cards to discard + + if (choice2 > 3 || choice2 < 0) + { + return -1; + } + +/* + if (choice1 == handPos) + { + return -1; + } + */ + + for (i = 0; i < state->handCount[currentPlayer]; i++) + { + if (i != handPos && state->hand[currentPlayer][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; +} + +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: + //+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: + return doMine(choice1, choice2, state, handPos, currentPlayer); + + 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: + //+3 Cards + for (i = 0; i < 3; i++) + { + drawCard(currentPlayer, state); + } + + //discard card from hand + discardCard(handPos, currentPlayer, state, 0); + return 0; + + case village: + //+1 Card + drawCard(currentPlayer, state); + + //+2 Actions + state->numActions = state->numActions + 2; + + //discard played card from hand + discardCard(handPos, currentPlayer, state, 0); + return 0; + + case baron: + doBaron(choice1, state, currentPlayer); + 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: + doMinion(choice1, choice2, state, handPos, currentPlayer); + return 0; + + 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: + doTribute(state, currentPlayer, nextPlayer, tributeRevealedCards); + return 0; + + case ambassador: + return doAmbassador(choice1, choice2, state, handPos, currentPlayer); + + 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 discardCard(int handPos, int currentPlayer, struct gameState *state, int trashFlag) +{ + + //if card is not trashed, added to Played pile + if (trashFlag < 1) + { + //add card to played pile + state->playedCards[state->playedCardCount] = state->hand[currentPlayer][handPos]; + state->playedCardCount++; + } + + //set played card to -1 + state->hand[currentPlayer][handPos] = -1; + + //remove card from player's hand + if ( handPos == (state->handCount[currentPlayer] - 1) ) //last card in hand array is played + { + //reduce number of cards in hand + state->handCount[currentPlayer]--; + } + else if ( state->handCount[currentPlayer] == 1 ) //only one card in hand + { + //reduce number of cards in hand + state->handCount[currentPlayer]--; + } + else + { + //replace discarded card with last card in hand + state->hand[currentPlayer][handPos] = state->hand[currentPlayer][ (state->handCount[currentPlayer] - 1)]; + //set last card to -1 + state->hand[currentPlayer][state->handCount[currentPlayer] - 1] = -1; + //reduce number of cards in hand + state->handCount[currentPlayer]--; + } + + return 0; +} + +int gainCard(int supplyPos, struct gameState *state, int toFlag, int player) +{ + //Note: supplyPos is enum of choosen card + + //check if supply pile is empty (0) or card is not used in game (-1) + if ( supplyCount(supplyPos, state) < 1 ) + { + return -1; + } + + //added card for [whoseTurn] current player: + // toFlag = 0 : add to discard + // toFlag = 1 : add to deck + // toFlag = 2 : add to hand + + if (toFlag == 1) + { + state->deck[ player ][ state->deckCount[player] ] = supplyPos; + state->deckCount[player]++; + } + else if (toFlag == 2) + { + state->hand[ player ][ state->handCount[player] ] = supplyPos; + state->handCount[player]++; + } + else + { + state->discard[player][ state->discardCount[player] ] = supplyPos; + state->discardCount[player]++; + } + + //decrease number in supply pile + state->supplyCount[supplyPos]--; + + return 0; +} + +int updateCoins(int player, struct gameState *state, int bonus) +{ + int i; + + //reset coin count + state->coins = 0; + + //add coins for each Treasure card in player's hand + for (i = 0; i < state->handCount[player]; i++) + { + if (state->hand[player][i] == copper) + { + state->coins += 1; + } + else if (state->hand[player][i] == silver) + { + state->coins += 2; + } + else if (state->hand[player][i] == gold) + { + state->coins += 3; + } + } + + //add bonus + state->coins += bonus; + + return 0; +} + + +//end of dominion.c diff --git a/projects/alghanmz/alghanmz-assignment-3/dominion.h b/projects/alghanmz/alghanmz-assignment-3/dominion.h new file mode 100644 index 000000000..f9d9039b8 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/dominion.h @@ -0,0 +1,131 @@ +#ifndef _DOMINION_H +#define _DOMINION_H + +// Code from various sources, baseline from Kristen Bartosz + +#define MAX_HAND 500 +#define MAX_DECK 500 + +#define MAX_PLAYERS 4 + +#define DEBUG 0 + +/* http://dominion.diehrstraits.com has card texts */ +/* http://dominion.isotropic.org has other stuff */ + +/* hand# means index of a card in current active player's hand */ + +enum CARD +{ curse = 0, + estate, + duchy, + province, + + copper, + silver, + gold, + + adventurer, + /* If no/only 1 treasure found, stop when full deck seen */ + council_room, + feast, /* choice1 is supply # of card gained) */ + gardens, + mine, /* choice1 is hand# of money to trash, choice2 is supply# of + money to put in hand */ + remodel, /* choice1 is hand# of card to remodel, choice2 is supply# */ + smithy, + village, + + baron, /* choice1: boolean for discard of estate */ + /* Discard is always of first (lowest index) estate */ + great_hall, + minion, /* choice1: 1 = +2 coin, 2 = redraw */ + steward, /* choice1: 1 = +2 card, 2 = +2 coin, 3 = trash 2 (choice2,3) */ + tribute, + + ambassador, /* choice1 = hand#, choice2 = number to return to supply */ + cutpurse, + embargo, /* choice1 = supply# */ + outpost, + salvager, /* choice1 = hand# to trash */ + sea_hag, + treasure_map +}; + +struct gameState { + int numPlayers; //number of players + int supplyCount[treasure_map+1]; //this is the amount of a specific type of card given a specific number. + int embargoTokens[treasure_map+1]; + int outpostPlayed; + int outpostTurn; + int whoseTurn; + int phase; + int numActions; /* Starts at 1 each turn */ + int coins; /* Use as you see fit! */ + int numBuys; /* Starts at 1 each turn */ + int hand[MAX_PLAYERS][MAX_HAND]; + int handCount[MAX_PLAYERS]; + int deck[MAX_PLAYERS][MAX_DECK]; + int deckCount[MAX_PLAYERS]; + int discard[MAX_PLAYERS][MAX_DECK]; + int discardCount[MAX_PLAYERS]; + int playedCards[MAX_DECK]; + int playedCardCount; +}; + +/* All functions return -1 on failure, and DO NOT CHANGE GAME STATE; + unless specified for other return, return 0 on success */ + +struct gameState* newGame(); + +int* kingdomCards(int k1, int k2, int k3, int k4, int k5, int k6, int k7, + int k8, int k9, int k10); + +int initializeGame(int numPlayers, int kingdomCards[10], int randomSeed, + struct gameState *state); +/* Responsible for initializing all supplies, and shuffling deck and + drawing starting hands for all players. Check that 10 cards selected + are in fact (different) kingdom cards, and that numPlayers is valid. + +Cards not in game should initialize supply position to -1 */ + +int shuffle(int player, struct gameState *state); +/* Assumes all cards are now in deck array (or hand/played): discard is + empty */ + +int playCard(int handPos, int choice1, int choice2, int choice3, + struct gameState *state); +/* Play card with index handPos from current player's hand */ + +int buyCard(int supplyPos, struct gameState *state); +/* Buy card with supply index supplyPos */ + +int numHandCards(struct gameState *state); +/* How many cards current player has in hand */ + +int handCard(int handNum, struct gameState *state); +/* enum value of indexed card in player's hand */ + +int supplyCount(int card, struct gameState *state); +/* How many of given card are left in supply */ + +int fullDeckCount(int player, int card, struct gameState *state); +/* Here deck = hand + discard + deck */ + +int whoseTurn(struct gameState *state); + +int endTurn(struct gameState *state); +/* Must do phase C and advance to next player; do not advance whose turn + if game is over */ + +int isGameOver(struct gameState *state); + +int scoreFor(int player, struct gameState *state); +/* Negative here does not mean invalid; scores may be negative, + -9999 means invalid input */ + +int getWinners(int players[MAX_PLAYERS], struct gameState *state); +/* Set array position of each player who won (remember ties!) to + 1, others to 0 */ + +#endif diff --git a/projects/alghanmz/alghanmz-assignment-3/hw33.c b/projects/alghanmz/alghanmz-assignment-3/hw33.c new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/hw33.c @@ -0,0 +1 @@ + diff --git a/projects/alghanmz/alghanmz-assignment-3/unittest1.c b/projects/alghanmz/alghanmz-assignment-3/unittest1.c new file mode 100644 index 000000000..de2476384 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/unittest1.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include "dominion.h" + +void doBaron(int choice1, struct gameState *state, int currentPlayer); + +void assert(int a, char *s) { + if (!a) { + printf("Assert failed: %s\n", s); + } +} + +void assertEqual(int a, int b, char *s) { + if (a != b) { + printf("Assert failed: %s\n", s); + } +} + +int containsCard(int *container, int size, int card) { + for (int i=0; i < size; i++) { + if (container[i] == card) { + return 1; + } + } + return 0; +} + +int cardCount(int *container, int size, int card) { + int count = 0; + + for (int i=0; i < size; i++) { + if (container[i] == card) { + count++; + } + } + return count; +} + +int main() { + int seed = 47; + int p; + int choice1; + + // card array + int k[10] = { adventurer, council_room, feast, gardens, mine + , remodel, smithy, village, baron, great_hall }; + + // declare the game state + struct gameState G, G2; + + printf("\nBegin Testing doBaron():\n\n"); + initializeGame(2, k, seed, &G); + + printf("Test 1\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = baron; + choice1 = 0; // choose to gain an estate card + doBaron(choice1, &G2, 0); + assertEqual(G2.numBuys, G.numBuys + 1, "Number of buys not increased by 1"); + assert(containsCard(G2.discard[p], G2.discardCount[p], estate), "Estate card not gained"); + assert(containsCard(G2.playedCards, G2.playedCardCount, baron), "Baron card not discarded"); + + printf("Test 2\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = baron; + G2.hand[p][1] = estate; + choice1 = 1; // choose to discard an estate card + doBaron(choice1, &G2, 0); + assertEqual(G2.numBuys, G.numBuys + 1, "Number of buys not increased by 1"); + assertEqual(G2.coins, G.coins + 4, "Coins not increased by 4"); + assertEqual(cardCount(G2.hand[p], G2.handCount[p], estate), + cardCount(G.hand[p], G.handCount[p], estate) - 1, "Estate card not discarded"); + assertEqual(cardCount(G2.hand[p], G2.handCount[p], baron), + cardCount(G.hand[p], G.handCount[p], baron) - 1, "Baron card not discarded"); + + printf("Test 3\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = estate; + G2.hand[p][1] = baron; + G2.hand[p][2] = copper; + G2.hand[p][3] = copper; + G2.hand[p][4] = copper; + choice1 = 1; // choose to discard an estate card + doBaron(choice1, &G2, 0); + assertEqual(G2.numBuys, G.numBuys + 1, "Number of buys not increased by 1"); + assertEqual(G2.coins, G.coins + 4, "Coins not increased by 4"); + assertEqual(cardCount(G2.hand[p], G2.handCount[p], estate), + cardCount(G.hand[p], G.handCount[p], estate) - 1, "Estate card not discarded"); + assertEqual(cardCount(G2.hand[p], G2.handCount[p], baron), + cardCount(G.hand[p], G.handCount[p], baron) - 1, "Baron card not discarded"); + + printf("End of doBaron() Tests\n\n"); + return 0; +} diff --git a/projects/alghanmz/alghanmz-assignment-3/unittest2.c b/projects/alghanmz/alghanmz-assignment-3/unittest2.c new file mode 100644 index 000000000..c22d721b2 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/unittest2.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include "dominion.h" + +void doMinion(int choice1, int choice2, struct gameState *state, int handPos, int currentPlayer); + +void assert(int a, char *s) { + if (!a) { + printf("Assert failed: %s\n", s); + } +} + +void assertEqual(int a, int b, char *s) { + if (a != b) { + printf("Assert failed: %s\n", s); + } +} + +int containsCard(int *container, int size, int card) { + for (int i=0; i < size; i++) { + if (container[i] == card) { + return 1; + } + } + return 0; +} + +int main() { + int seed = 47; + int p; + int p2; + int choice1; + int choice2; + + // card array + int k[10] = { adventurer, council_room, feast, gardens, mine + , remodel, smithy, village, minion, great_hall }; + + // declare the game state + struct gameState G, G2; + + printf("\nBegin Testing doMinion():\n\n"); + initializeGame(4, k, seed, &G); // 4 players + + printf("Test 1\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = minion; + choice1 = 1; // choose +2 coins + choice2 = 0; + doMinion(choice1, choice2, &G2, 0, p); + assertEqual(G2.numActions, G.numActions + 1, "Number of actions not increased by 1"); + assertEqual(G2.coins, G.coins + 2, "Number of coins not increased by 2"); + assert(containsCard(G2.playedCards, G2.playedCardCount, minion), "Minion card not discarded"); + + printf("Test 2\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + p2 = 2; // another player + G2.hand[p][0] = minion; + G2.handCount[p2] = 3; // change handcount from 5 to 3 + choice1 = 0; + choice2 = 1; // choose to redraw hand + doMinion(choice1, choice2, &G2, 0, p); + assertEqual(G2.numActions, G.numActions + 1, "Number of actions not increased by 1"); + assertEqual(G2.handCount[p], 4, "Current player should have exactly 4 cards"); + assertEqual(G2.handCount[p2], 3, "A player with less than 5 cards discarded hand"); + assert(containsCard(G2.playedCards, G2.playedCardCount, minion), "Minion card not discarded"); + + printf("End of doMinion() Tests\n\n"); + return 0; +} diff --git a/projects/alghanmz/alghanmz-assignment-3/unittest3.c b/projects/alghanmz/alghanmz-assignment-3/unittest3.c new file mode 100644 index 000000000..5de2181f2 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/unittest3.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include "dominion.h" + +int doAmbassador(int choice1, int choice2, struct gameState *state, int handPos, int currentPlayer); + +void assert(int a, char *s) { + if (!a) { + printf("Assert failed: %s\n", s); + } +} + +void assertEqual(int a, int b, char *s) { + if (a != b) { + printf("Assert failed: %s\n", s); + } +} + +int containsCard(int *container, int size, int card) { + for (int i=0; i < size; i++) { + if (container[i] == card) { + return 1; + } + } + return 0; +} + +int cardCount(int *container, int size, int card) { + int count = 0; + + for (int i=0; i < size; i++) { + if (container[i] == card) { + count++; + } + } + return count; +} + +int main() { + int seed = 47; + int p; + int p2; + int handPos; + int choice1; + int choice2; + int ret_val; + + // card array + int k[10] = { adventurer, council_room, feast, gardens, mine + , remodel, smithy, village, ambassador, great_hall }; + + // declare the game state + struct gameState G, G2; + + printf("\nBegin Testing doAmbassador():\n\n"); + initializeGame(2, k, seed, &G); + + printf("Test 1\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + p2 = 1; + G2.hand[p][0] = copper; + G2.hand[p][1] = smithy; + G2.hand[p][2] = copper; + G2.hand[p][3] = copper; + G2.hand[p][4] = ambassador; + choice1 = 0; // choose to discard copper + choice2 = 3; // choose to discard 3 of them + handPos = 4; // ambassador + ret_val = doAmbassador(choice1, choice2, &G2, handPos, p); + assertEqual(ret_val, -1, "Didn't return -1 when trying to discard too many cards"); + assertEqual(cardCount(G2.hand[p], G2.handCount[p], copper), + cardCount(G.hand[p], G.handCount[p], copper) - choice2, "Trashed the wrong number of cards"); + assert(containsCard(G2.playedCards, G2.playedCardCount, ambassador), "Ambassador card not discarded"); + assert(containsCard(G2.discard[p2], G2.discardCount[p2], copper), "Player 2 did not gain a copper"); + + printf("Test 2\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + p2 = 1; + G2.hand[p][0] = ambassador; + G2.hand[p][1] = ambassador; + choice1 = 0; // choose to discard ambassador + choice2 = 1; // choose to discard 1 of them + handPos = 0; // ambassador + ret_val = doAmbassador(choice1, choice2, &G2, handPos, p); + assertEqual(ret_val, -1, "Didn't return -1 when trying to discard ambassador card"); + assert(containsCard(G2.playedCards, G2.playedCardCount, ambassador), "Ambassador card not discarded"); + assert(containsCard(G2.discard[p2], G2.discardCount[p2], ambassador), "Player 2 did not gain an ambassador card"); + + printf("End of doAmbassador() Tests\n\n"); + return 0; +} diff --git a/projects/alghanmz/alghanmz-assignment-3/unittest4.c b/projects/alghanmz/alghanmz-assignment-3/unittest4.c new file mode 100644 index 000000000..0466e2d66 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/unittest4.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include "dominion.h" + +void doTribute(struct gameState *state, int currentPlayer, int nextPlayer, int tributeRevealedCards[2]); + +void assert(int a, char *s) { + if (!a) { + printf("Assert failed: %s\n", s); + } +} + +void assertEqual(int a, int b, char *s) { + if (a != b) { + printf("Assert failed: %s\n", s); + } +} + +int containsCard(int *container, int size, int card) { + for (int i=0; i < size; i++) { + if (container[i] == card) { + return 1; + } + } + return 0; +} + +int cardCount(int *container, int size, int card) { + int count = 0; + + for (int i=0; i < size; i++) { + if (container[i] == card) { + count++; + } + } + return count; +} + +int main() { + int seed = 47; + int p; + int p2; + int tributeRevealedCards[2]; + int result; + + // card array + int k[10] = { adventurer, council_room, feast, gardens, mine + , remodel, smithy, village, tribute, great_hall }; + + // declare the game state + struct gameState G, G2; + + printf("\nBegin Testing doTribute():\n\n"); + initializeGame(2, k, seed, &G); + + printf("Test 1\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + p2 = 1; + G2.hand[p][0] = tribute; + G2.deckCount[p2] = 0; + G2.discardCount[p2] = 2; + G2.discard[p2][G2.discardCount[p2]-2] = copper; + G2.discard[p2][G2.discardCount[p2]-1] = estate; + doTribute(&G2, p, p2, tributeRevealedCards); + result = containsCard(G2.playedCards, G2.playedCardCount, tribute); + assert(result, "Tribute card not discarded"); + if (result) + assertEqual(G2.handCount[p], G.handCount[p]+1, "Didn't draw 2 cards"); + else + assertEqual(G2.handCount[p], G.handCount[p]+2, "Didn't draw 2 cards"); + assertEqual(G2.coins, G.coins+2, "Coins didn't increase by 2"); + + printf("Test 2\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + p2 = 1; + G2.hand[p][0] = tribute; + G2.deck[p2][G2.deckCount[p2]-2] = smithy; + G2.deck[p2][G2.deckCount[p2]-1] = estate; + doTribute(&G2, p, p2, tributeRevealedCards); + result = containsCard(G2.playedCards, G2.playedCardCount, tribute); + assert(result, "Tribute card not discarded"); + if (result) + assertEqual(G2.handCount[p], G.handCount[p]+1, "Didn't draw 2 cards"); + else + assertEqual(G2.handCount[p], G.handCount[p]+2, "Didn't draw 2 cards"); + assertEqual(G2.numActions, G.numActions+2, "Actions didn't increase by 2"); + + printf("Test 3\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + p2 = 1; + G2.hand[p][0] = tribute; + G2.deckCount[p2] = 0; + G2.discardCount[p2] = 1; + G2.discard[p2][0] = estate; + doTribute(&G2, p, p2, tributeRevealedCards); + result = containsCard(G2.playedCards, G2.playedCardCount, tribute); + assert(result, "Tribute card not discarded"); + if (result) + assertEqual(G2.handCount[p], G.handCount[p]+1, "Didn't draw 2 cards"); + else + assertEqual(G2.handCount[p], G.handCount[p]+2, "Didn't draw 2 cards"); + + printf("End of doTribute() Tests\n\n"); + return 0; +} diff --git a/projects/alghanmz/alghanmz-assignment-3/unittest5.c b/projects/alghanmz/alghanmz-assignment-3/unittest5.c new file mode 100644 index 000000000..d5040c060 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/unittest5.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include "dominion.h" + +int doMine(int choice1, int choice2, struct gameState *state, int handPos, int currentPlayer); + +void assert(int a, char *s) { + if (!a) { + printf("Assert failed: %s\n", s); + } +} + +void assertEqual(int a, int b, char *s) { + if (a != b) { + printf("Assert failed: %s\n", s); + } +} + +int containsCard(int *container, int size, int card) { + for (int i=0; i < size; i++) { + if (container[i] == card) { + return 1; + } + } + return 0; +} + +int cardCount(int *container, int size, int card) { + int count = 0; + + for (int i=0; i < size; i++) { + if (container[i] == card) { + count++; + } + } + return count; +} + +int main() { + int seed = 47; + int p; + int handPos; + int choice1; + int choice2; + int ret_val; + + // card array + int k[10] = { adventurer, council_room, feast, gardens, mine + , remodel, smithy, village, ambassador, great_hall }; + + // declare the game state + struct gameState G, G2; + + printf("\nBegin Testing doMine():\n\n"); + initializeGame(2, k, seed, &G); + + printf("Test 1\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = curse; + G2.hand[p][1] = mine; + choice1 = 0; // choose to trash curse + choice2 = silver; // choose to gain a silver + handPos = 1; // mine + ret_val = doMine(choice1, choice2, &G2, handPos, p); + assertEqual(ret_val, -1, "Didn't return -1 when trying to trash a curse"); + + printf("Test 2\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = copper; + G2.hand[p][1] = mine; + G2.hand[p][2] = copper; + G2.hand[p][3] = estate; + G2.hand[p][4] = estate; + choice1 = 0; // choose to trash copper + choice2 = silver; // choose to gain a silver + handPos = 1; // mine + ret_val = doMine(choice1, choice2, &G2, handPos, p); + assert(containsCard(G2.playedCards, G2.playedCardCount, mine), "Mine card not discarded"); + assert(containsCard(G2.hand[p], G2.handCount[p], silver), "Silver not gained to hand"); + assert(containsCard(G2.playedCards, G2.playedCardCount, copper), "Copper card not discarded"); + + printf("Test 3\n"); + memcpy(&G2, &G, sizeof(G)); + p = 0; + G2.hand[p][0] = smithy; + G2.hand[p][1] = mine; + choice1 = 0; // choose to trash smithy + choice2 = silver; // choose to gain a silver + handPos = 1; // mine + ret_val = doMine(choice1, choice2, &G2, handPos, p); + assertEqual(ret_val, -1, "Didn't return -1 when trying to trash a smithy"); + + printf("End of doMine() Tests\n\n"); + return 0; +} diff --git a/projects/alghanmz/alghanmz-assignment-3/unittestresults.out b/projects/alghanmz/alghanmz-assignment-3/unittestresults.out new file mode 100644 index 000000000..e7b35ea86 --- /dev/null +++ b/projects/alghanmz/alghanmz-assignment-3/unittestresults.out @@ -0,0 +1,963 @@ + +Begin Testing doBaron(): + +Test 1 +Assert failed: Number of buys not increased by 1 +Assert failed: Baron card not discarded +Test 2 +Assert failed: Number of buys not increased by 1 +Assert failed: Baron card not discarded +Test 3 +Assert failed: Number of buys not increased by 1 +Assert failed: Coins not increased by 4 +Assert failed: Baron card not discarded +End of doBaron() Tests + +Function 'updateCoins' +Lines executed:81.82% of 11 +Branches executed:100.00% of 8 +Taken at least once:75.00% of 8 +No calls + +Function 'gainCard' +Lines executed:61.54% of 13 +Branches executed:100.00% of 6 +Taken at least once:50.00% of 6 +Calls executed:100.00% of 1 + +Function 'discardCard' +Lines executed:0.00% of 13 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +No calls + +Function 'cardEffect' +Lines executed:0.00% of 133 +Branches executed:0.00% of 93 +Taken at least once:0.00% of 93 +Calls executed:0.00% of 44 + +Function 'doAmbassador' +Lines executed:0.00% of 20 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 3 + +Function 'doTribute' +Lines executed:0.00% of 28 +Branches executed:0.00% of 30 +Taken at least once:0.00% of 30 +Calls executed:0.00% of 2 + +Function 'printHand' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'printDeck' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'doMinion' +Lines executed:0.00% of 17 +Branches executed:0.00% of 16 +Taken at least once:0.00% of 16 +Calls executed:0.00% of 6 + +Function 'doBaron' +Lines executed:100.00% of 22 +Branches executed:100.00% of 10 +Taken at least once:100.00% of 10 +Calls executed:100.00% of 2 + +Function 'getEstateCard' +Lines executed:85.71% of 7 +Branches executed:100.00% of 4 +Taken at least once:50.00% of 4 +Calls executed:75.00% of 4 + +Function 'doMine' +Lines executed:0.00% of 14 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +Calls executed:0.00% of 4 + +Function 'getCost' +Lines executed:0.00% of 30 +Branches executed:0.00% of 28 +Taken at least once:0.00% of 28 +No calls + +Function 'drawCard' +Lines executed:36.36% of 22 +Branches executed:33.33% of 6 +Taken at least once:16.67% of 6 +Calls executed:0.00% of 1 + +Function 'getWinners' +Lines executed:0.00% of 24 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 2 + +Function 'scoreFor' +Lines executed:0.00% of 42 +Branches executed:0.00% of 42 +Taken at least once:0.00% of 42 +Calls executed:0.00% of 3 + +Function 'isGameOver' +Lines executed:0.00% of 10 +Branches executed:0.00% of 8 +Taken at least once:0.00% of 8 +No calls + +Function 'endTurn' +Lines executed:0.00% of 20 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 3 + +Function 'whoseTurn' +Lines executed:0.00% of 2 +No branches +No calls + +Function 'fullDeckCount' +Lines executed:0.00% of 9 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +No calls + +Function 'supplyCount' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'handCard' +Lines executed:0.00% of 3 +No branches +Calls executed:0.00% of 1 + +Function 'numHandCards' +Lines executed:0.00% of 2 +No branches +Calls executed:0.00% of 1 + +Function 'buyCard' +Lines executed:0.00% of 13 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 4 + +Function 'playCard' +Lines executed:0.00% of 14 +Branches executed:0.00% of 10 +Taken at least once:0.00% of 10 +Calls executed:0.00% of 3 + +Function 'shuffle' +Lines executed:93.75% of 16 +Branches executed:100.00% of 8 +Taken at least once:87.50% of 8 +Calls executed:100.00% of 2 + +Function 'initializeGame' +Lines executed:83.87% of 62 +Branches executed:95.65% of 46 +Taken at least once:80.43% of 46 +Calls executed:100.00% of 5 + +Function 'kingdomCards' +Lines executed:0.00% of 13 +No branches +No calls + +Function 'newGame' +Lines executed:0.00% of 3 +No branches +No calls + +Function 'compare' +Lines executed:83.33% of 6 +Branches executed:100.00% of 4 +Taken at least once:75.00% of 4 +No calls + +File 'dominion.c' +Lines executed:21.78% of 583 +Branches executed:21.03% of 409 +Taken at least once:16.87% of 409 +Calls executed:13.40% of 97 +Creating 'dominion.c.gcov' + + +Begin Testing doMinion(): + +Test 1 +Test 2 +Assert failed: Current player should have exactly 4 cards +Assert failed: A player with less than 5 cards discarded hand +End of doMinion() Tests + +Function 'updateCoins' +Lines executed:81.82% of 11 +Branches executed:100.00% of 8 +Taken at least once:75.00% of 8 +No calls + +Function 'gainCard' +Lines executed:61.54% of 13 +Branches executed:100.00% of 6 +Taken at least once:50.00% of 6 +Calls executed:100.00% of 1 + +Function 'discardCard' +Lines executed:92.31% of 13 +Branches executed:100.00% of 6 +Taken at least once:66.67% of 6 +No calls + +Function 'cardEffect' +Lines executed:0.00% of 133 +Branches executed:0.00% of 93 +Taken at least once:0.00% of 93 +Calls executed:0.00% of 44 + +Function 'doAmbassador' +Lines executed:0.00% of 20 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 3 + +Function 'doTribute' +Lines executed:0.00% of 28 +Branches executed:0.00% of 30 +Taken at least once:0.00% of 30 +Calls executed:0.00% of 2 + +Function 'printHand' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'printDeck' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'doMinion' +Lines executed:100.00% of 17 +Branches executed:100.00% of 16 +Taken at least once:93.75% of 16 +Calls executed:100.00% of 6 + +Function 'doBaron' +Lines executed:100.00% of 22 +Branches executed:100.00% of 10 +Taken at least once:100.00% of 10 +Calls executed:100.00% of 2 + +Function 'getEstateCard' +Lines executed:85.71% of 7 +Branches executed:100.00% of 4 +Taken at least once:50.00% of 4 +Calls executed:75.00% of 4 + +Function 'doMine' +Lines executed:0.00% of 14 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +Calls executed:0.00% of 4 + +Function 'getCost' +Lines executed:0.00% of 30 +Branches executed:0.00% of 28 +Taken at least once:0.00% of 28 +No calls + +Function 'drawCard' +Lines executed:36.36% of 22 +Branches executed:33.33% of 6 +Taken at least once:16.67% of 6 +Calls executed:0.00% of 1 + +Function 'getWinners' +Lines executed:0.00% of 24 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 2 + +Function 'scoreFor' +Lines executed:0.00% of 42 +Branches executed:0.00% of 42 +Taken at least once:0.00% of 42 +Calls executed:0.00% of 3 + +Function 'isGameOver' +Lines executed:0.00% of 10 +Branches executed:0.00% of 8 +Taken at least once:0.00% of 8 +No calls + +Function 'endTurn' +Lines executed:0.00% of 20 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 3 + +Function 'whoseTurn' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'fullDeckCount' +Lines executed:0.00% of 9 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +No calls + +Function 'supplyCount' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'handCard' +Lines executed:0.00% of 3 +No branches +Calls executed:0.00% of 1 + +Function 'numHandCards' +Lines executed:100.00% of 2 +No branches +Calls executed:100.00% of 1 + +Function 'buyCard' +Lines executed:0.00% of 13 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 4 + +Function 'playCard' +Lines executed:0.00% of 14 +Branches executed:0.00% of 10 +Taken at least once:0.00% of 10 +Calls executed:0.00% of 3 + +Function 'shuffle' +Lines executed:93.75% of 16 +Branches executed:100.00% of 8 +Taken at least once:87.50% of 8 +Calls executed:100.00% of 2 + +Function 'initializeGame' +Lines executed:93.55% of 62 +Branches executed:100.00% of 46 +Taken at least once:89.13% of 46 +Calls executed:100.00% of 5 + +Function 'kingdomCards' +Lines executed:0.00% of 13 +No branches +No calls + +Function 'newGame' +Lines executed:0.00% of 3 +No branches +No calls + +Function 'compare' +Lines executed:83.33% of 6 +Branches executed:100.00% of 4 +Taken at least once:75.00% of 4 +No calls + +File 'dominion.c' +Lines executed:28.47% of 583 +Branches executed:26.89% of 409 +Taken at least once:22.49% of 409 +Calls executed:20.62% of 97 +Creating 'dominion.c.gcov' + + +Begin Testing doAmbassador(): + +Test 1 +Assert failed: Didn't return -1 when trying to discard too many cards +Test 2 +Assert failed: Didn't return -1 when trying to discard ambassador card +End of doAmbassador() Tests + +Function 'updateCoins' +Lines executed:81.82% of 11 +Branches executed:100.00% of 8 +Taken at least once:75.00% of 8 +No calls + +Function 'gainCard' +Lines executed:61.54% of 13 +Branches executed:100.00% of 6 +Taken at least once:50.00% of 6 +Calls executed:100.00% of 1 + +Function 'discardCard' +Lines executed:92.31% of 13 +Branches executed:100.00% of 6 +Taken at least once:83.33% of 6 +No calls + +Function 'cardEffect' +Lines executed:0.00% of 133 +Branches executed:0.00% of 93 +Taken at least once:0.00% of 93 +Calls executed:0.00% of 44 + +Function 'doAmbassador' +Lines executed:90.00% of 20 +Branches executed:100.00% of 22 +Taken at least once:77.27% of 22 +Calls executed:100.00% of 3 + +Function 'doTribute' +Lines executed:0.00% of 28 +Branches executed:0.00% of 30 +Taken at least once:0.00% of 30 +Calls executed:0.00% of 2 + +Function 'printHand' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'printDeck' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'doMinion' +Lines executed:100.00% of 17 +Branches executed:100.00% of 16 +Taken at least once:93.75% of 16 +Calls executed:100.00% of 6 + +Function 'doBaron' +Lines executed:100.00% of 22 +Branches executed:100.00% of 10 +Taken at least once:100.00% of 10 +Calls executed:100.00% of 2 + +Function 'getEstateCard' +Lines executed:85.71% of 7 +Branches executed:100.00% of 4 +Taken at least once:50.00% of 4 +Calls executed:75.00% of 4 + +Function 'doMine' +Lines executed:0.00% of 14 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +Calls executed:0.00% of 4 + +Function 'getCost' +Lines executed:0.00% of 30 +Branches executed:0.00% of 28 +Taken at least once:0.00% of 28 +No calls + +Function 'drawCard' +Lines executed:36.36% of 22 +Branches executed:33.33% of 6 +Taken at least once:16.67% of 6 +Calls executed:0.00% of 1 + +Function 'getWinners' +Lines executed:0.00% of 24 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 2 + +Function 'scoreFor' +Lines executed:0.00% of 42 +Branches executed:0.00% of 42 +Taken at least once:0.00% of 42 +Calls executed:0.00% of 3 + +Function 'isGameOver' +Lines executed:0.00% of 10 +Branches executed:0.00% of 8 +Taken at least once:0.00% of 8 +No calls + +Function 'endTurn' +Lines executed:0.00% of 20 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 3 + +Function 'whoseTurn' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'fullDeckCount' +Lines executed:0.00% of 9 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +No calls + +Function 'supplyCount' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'handCard' +Lines executed:0.00% of 3 +No branches +Calls executed:0.00% of 1 + +Function 'numHandCards' +Lines executed:100.00% of 2 +No branches +Calls executed:100.00% of 1 + +Function 'buyCard' +Lines executed:0.00% of 13 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 4 + +Function 'playCard' +Lines executed:0.00% of 14 +Branches executed:0.00% of 10 +Taken at least once:0.00% of 10 +Calls executed:0.00% of 3 + +Function 'shuffle' +Lines executed:93.75% of 16 +Branches executed:100.00% of 8 +Taken at least once:87.50% of 8 +Calls executed:100.00% of 2 + +Function 'initializeGame' +Lines executed:93.55% of 62 +Branches executed:100.00% of 46 +Taken at least once:89.13% of 46 +Calls executed:100.00% of 5 + +Function 'kingdomCards' +Lines executed:0.00% of 13 +No branches +No calls + +Function 'newGame' +Lines executed:0.00% of 3 +No branches +No calls + +Function 'compare' +Lines executed:83.33% of 6 +Branches executed:100.00% of 4 +Taken at least once:75.00% of 4 +No calls + +File 'dominion.c' +Lines executed:31.56% of 583 +Branches executed:32.27% of 409 +Taken at least once:26.89% of 409 +Calls executed:23.71% of 97 +Creating 'dominion.c.gcov' + + +Begin Testing doTribute(): + +Test 1 +Assert failed: Tribute card not discarded +Assert failed: Didn't draw 2 cards +Test 2 +Assert failed: Tribute card not discarded +Assert failed: Didn't draw 2 cards +Assert failed: Actions didn't increase by 2 +Test 3 +Assert failed: Tribute card not discarded +Assert failed: Didn't draw 2 cards +End of doTribute() Tests + +Function 'updateCoins' +Lines executed:81.82% of 11 +Branches executed:100.00% of 8 +Taken at least once:75.00% of 8 +No calls + +Function 'gainCard' +Lines executed:61.54% of 13 +Branches executed:100.00% of 6 +Taken at least once:50.00% of 6 +Calls executed:100.00% of 1 + +Function 'discardCard' +Lines executed:92.31% of 13 +Branches executed:100.00% of 6 +Taken at least once:83.33% of 6 +No calls + +Function 'cardEffect' +Lines executed:0.00% of 133 +Branches executed:0.00% of 93 +Taken at least once:0.00% of 93 +Calls executed:0.00% of 44 + +Function 'doAmbassador' +Lines executed:90.00% of 20 +Branches executed:100.00% of 22 +Taken at least once:77.27% of 22 +Calls executed:100.00% of 3 + +Function 'doTribute' +Lines executed:82.14% of 28 +Branches executed:100.00% of 30 +Taken at least once:70.00% of 30 +Calls executed:100.00% of 2 + +Function 'printHand' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'printDeck' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'doMinion' +Lines executed:100.00% of 17 +Branches executed:100.00% of 16 +Taken at least once:93.75% of 16 +Calls executed:100.00% of 6 + +Function 'doBaron' +Lines executed:100.00% of 22 +Branches executed:100.00% of 10 +Taken at least once:100.00% of 10 +Calls executed:100.00% of 2 + +Function 'getEstateCard' +Lines executed:85.71% of 7 +Branches executed:100.00% of 4 +Taken at least once:50.00% of 4 +Calls executed:75.00% of 4 + +Function 'doMine' +Lines executed:0.00% of 14 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +Calls executed:0.00% of 4 + +Function 'getCost' +Lines executed:0.00% of 30 +Branches executed:0.00% of 28 +Taken at least once:0.00% of 28 +No calls + +Function 'drawCard' +Lines executed:36.36% of 22 +Branches executed:33.33% of 6 +Taken at least once:16.67% of 6 +Calls executed:0.00% of 1 + +Function 'getWinners' +Lines executed:0.00% of 24 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 2 + +Function 'scoreFor' +Lines executed:0.00% of 42 +Branches executed:0.00% of 42 +Taken at least once:0.00% of 42 +Calls executed:0.00% of 3 + +Function 'isGameOver' +Lines executed:0.00% of 10 +Branches executed:0.00% of 8 +Taken at least once:0.00% of 8 +No calls + +Function 'endTurn' +Lines executed:0.00% of 20 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 3 + +Function 'whoseTurn' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'fullDeckCount' +Lines executed:0.00% of 9 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +No calls + +Function 'supplyCount' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'handCard' +Lines executed:0.00% of 3 +No branches +Calls executed:0.00% of 1 + +Function 'numHandCards' +Lines executed:100.00% of 2 +No branches +Calls executed:100.00% of 1 + +Function 'buyCard' +Lines executed:0.00% of 13 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 4 + +Function 'playCard' +Lines executed:0.00% of 14 +Branches executed:0.00% of 10 +Taken at least once:0.00% of 10 +Calls executed:0.00% of 3 + +Function 'shuffle' +Lines executed:93.75% of 16 +Branches executed:100.00% of 8 +Taken at least once:87.50% of 8 +Calls executed:100.00% of 2 + +Function 'initializeGame' +Lines executed:93.55% of 62 +Branches executed:100.00% of 46 +Taken at least once:89.13% of 46 +Calls executed:100.00% of 5 + +Function 'kingdomCards' +Lines executed:0.00% of 13 +No branches +No calls + +Function 'newGame' +Lines executed:0.00% of 3 +No branches +No calls + +Function 'compare' +Lines executed:83.33% of 6 +Branches executed:100.00% of 4 +Taken at least once:75.00% of 4 +No calls + +File 'dominion.c' +Lines executed:35.51% of 583 +Branches executed:39.61% of 409 +Taken at least once:32.03% of 409 +Calls executed:25.77% of 97 +Creating 'dominion.c.gcov' + + +Begin Testing doMine(): + +Test 1 +Assert failed: Didn't return -1 when trying to trash a curse +Test 2 +Assert failed: Silver not gained to hand +Test 3 +End of doMine() Tests + +Function 'updateCoins' +Lines executed:81.82% of 11 +Branches executed:100.00% of 8 +Taken at least once:75.00% of 8 +No calls + +Function 'gainCard' +Lines executed:61.54% of 13 +Branches executed:100.00% of 6 +Taken at least once:50.00% of 6 +Calls executed:100.00% of 1 + +Function 'discardCard' +Lines executed:92.31% of 13 +Branches executed:100.00% of 6 +Taken at least once:83.33% of 6 +No calls + +Function 'cardEffect' +Lines executed:0.00% of 133 +Branches executed:0.00% of 93 +Taken at least once:0.00% of 93 +Calls executed:0.00% of 44 + +Function 'doAmbassador' +Lines executed:90.00% of 20 +Branches executed:100.00% of 22 +Taken at least once:77.27% of 22 +Calls executed:100.00% of 3 + +Function 'doTribute' +Lines executed:82.14% of 28 +Branches executed:100.00% of 30 +Taken at least once:70.00% of 30 +Calls executed:100.00% of 2 + +Function 'printHand' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'printDeck' +Lines executed:0.00% of 6 +Branches executed:0.00% of 2 +Taken at least once:0.00% of 2 +Calls executed:0.00% of 3 + +Function 'doMinion' +Lines executed:100.00% of 17 +Branches executed:100.00% of 16 +Taken at least once:93.75% of 16 +Calls executed:100.00% of 6 + +Function 'doBaron' +Lines executed:100.00% of 22 +Branches executed:100.00% of 10 +Taken at least once:100.00% of 10 +Calls executed:100.00% of 2 + +Function 'getEstateCard' +Lines executed:85.71% of 7 +Branches executed:100.00% of 4 +Taken at least once:50.00% of 4 +Calls executed:75.00% of 4 + +Function 'doMine' +Lines executed:85.71% of 14 +Branches executed:100.00% of 12 +Taken at least once:58.33% of 12 +Calls executed:100.00% of 4 + +Function 'getCost' +Lines executed:16.67% of 30 +Branches executed:100.00% of 28 +Taken at least once:10.71% of 28 +No calls + +Function 'drawCard' +Lines executed:36.36% of 22 +Branches executed:33.33% of 6 +Taken at least once:16.67% of 6 +Calls executed:0.00% of 1 + +Function 'getWinners' +Lines executed:0.00% of 24 +Branches executed:0.00% of 22 +Taken at least once:0.00% of 22 +Calls executed:0.00% of 2 + +Function 'scoreFor' +Lines executed:0.00% of 42 +Branches executed:0.00% of 42 +Taken at least once:0.00% of 42 +Calls executed:0.00% of 3 + +Function 'isGameOver' +Lines executed:0.00% of 10 +Branches executed:0.00% of 8 +Taken at least once:0.00% of 8 +No calls + +Function 'endTurn' +Lines executed:0.00% of 20 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 3 + +Function 'whoseTurn' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'fullDeckCount' +Lines executed:0.00% of 9 +Branches executed:0.00% of 12 +Taken at least once:0.00% of 12 +No calls + +Function 'supplyCount' +Lines executed:100.00% of 2 +No branches +No calls + +Function 'handCard' +Lines executed:0.00% of 3 +No branches +Calls executed:0.00% of 1 + +Function 'numHandCards' +Lines executed:100.00% of 2 +No branches +Calls executed:100.00% of 1 + +Function 'buyCard' +Lines executed:0.00% of 13 +Branches executed:0.00% of 6 +Taken at least once:0.00% of 6 +Calls executed:0.00% of 4 + +Function 'playCard' +Lines executed:0.00% of 14 +Branches executed:0.00% of 10 +Taken at least once:0.00% of 10 +Calls executed:0.00% of 3 + +Function 'shuffle' +Lines executed:93.75% of 16 +Branches executed:100.00% of 8 +Taken at least once:87.50% of 8 +Calls executed:100.00% of 2 + +Function 'initializeGame' +Lines executed:93.55% of 62 +Branches executed:100.00% of 46 +Taken at least once:89.13% of 46 +Calls executed:100.00% of 5 + +Function 'kingdomCards' +Lines executed:0.00% of 13 +No branches +No calls + +Function 'newGame' +Lines executed:0.00% of 3 +No branches +No calls + +Function 'compare' +Lines executed:83.33% of 6 +Branches executed:100.00% of 4 +Taken at least once:75.00% of 4 +No calls + +File 'dominion.c' +Lines executed:38.42% of 583 +Branches executed:49.39% of 409 +Taken at least once:34.47% of 409 +Calls executed:29.90% of 97 +Creating 'dominion.c.gcov' + diff --git a/projects/alghanmz/p.txt b/projects/alghanmz/p.txt new file mode 100644 index 000000000..44ee501f8 --- /dev/null +++ b/projects/alghanmz/p.txt @@ -0,0 +1 @@ +cout << "H" diff --git a/projects/alghanmz/quiz/Makefile b/projects/alghanmz/quiz/Makefile new file mode 100644 index 000000000..05c227e31 --- /dev/null +++ b/projects/alghanmz/quiz/Makefile @@ -0,0 +1,9 @@ +testme: testme.c + gcc -o testme -coverage testme.c + +runtests: testme + ./testme + gcov testme.c + +clean: + rm -f *.o testme *.gcov *.gcda *.gcno diff --git a/projects/alghanmz/quiz/hw.c b/projects/alghanmz/quiz/hw.c new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/projects/alghanmz/quiz/hw.c @@ -0,0 +1 @@ + diff --git a/projects/alghanmz/quiz/testme b/projects/alghanmz/quiz/testme new file mode 100644 index 000000000..8b40b8efc Binary files /dev/null and b/projects/alghanmz/quiz/testme differ diff --git a/projects/alghanmz/quiz/testme.c b/projects/alghanmz/quiz/testme.c new file mode 100644 index 000000000..c193162a9 --- /dev/null +++ b/projects/alghanmz/quiz/testme.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +char inputChar() +{ + // TODO: rewrite this function + int c = rand() % 96 + 32; + return (char) c; +} + +char *inputString() +{ + // TODO: rewrite this function + static char str[6]; + int i; + + for (i=0; i < 5; i++) + str[i] = inputChar(); + str[5] = '\0'; + return str; +} + +void testme() +{ + int tcCount = 0; + char *s; + char c; + int state = 0; + time_t start = time(NULL); + while (time(NULL) - start < 5) + { + tcCount++; + c = inputChar(); + s = inputString(); + printf("Iteration %d: c = %c, s = %s, state = %d\n", tcCount, c, s, state); + + if (c == '[' && state == 0) state = 1; + if (c == '(' && state == 1) state = 2; + if (c == '{' && state == 2) state = 3; + if (c == ' '&& state == 3) state = 4; + if (c == 'a' && state == 4) state = 5; + if (c == 'x' && state == 5) state = 6; + if (c == '}' && state == 6) state = 7; + if (c == ')' && state == 7) state = 8; + if (c == ']' && state == 8) state = 9; + if (s[0] == 'r' && s[1] == 'e' + && s[2] == 's' && s[3] == 'e' + && s[4] == 't' && s[5] == '\0' + && state == 9) + { + printf("error "); + exit(200); + } + } +} + + +int main(int argc, char *argv[]) +{ + srand(time(NULL)); + testme(); + return 0; +} diff --git a/projects/alghanmz/quiz/testme.c.gcov b/projects/alghanmz/quiz/testme.c.gcov new file mode 100644 index 000000000..b06c4bb33 --- /dev/null +++ b/projects/alghanmz/quiz/testme.c.gcov @@ -0,0 +1,70 @@ + -: 0:Source:testme.c + -: 0:Graph:testme.gcno + -: 0:Data:testme.gcda + -: 0:Runs:3 + -: 0:Programs:1 + -: 1:#include + -: 2:#include + -: 3:#include + -: 4:#include + -: 5: + 7296396: 6:char inputChar() + -: 7:{ + -: 8: // TODO: rewrite this function + 7296396: 9: int c = rand() % 96 + 32; + 7296396: 10: return (char) c; + -: 11:} + -: 12: + 1216066: 13:char *inputString() + -: 14:{ + -: 15: // TODO: rewrite this function + -: 16: static char str[6]; + -: 17: int i; + -: 18: + 7296396: 19: for (i=0; i < 5; i++) + 6080330: 20: str[i] = inputChar(); + 1216066: 21: str[5] = '\0'; + 1216066: 22: return str; + -: 23:} + -: 24: + 3: 25:void testme() + -: 26:{ + 3: 27: int tcCount = 0; + -: 28: char *s; + -: 29: char c; + 3: 30: int state = 0; + 3: 31: time_t start = time(NULL); + 3: 32: while (time(NULL) - start < 7) + -: 33: { + 1216066: 34: tcCount++; + 1216066: 35: c = inputChar(); + 1216066: 36: s = inputString(); + 1216066: 37: printf("Iteration %d: c = %c, s = %s, state = %d\n", tcCount, c, s, state); + -: 38: + 1216066: 39: if (c == '[' && state == 0) state = 1; + 1216066: 40: if (c == '(' && state == 1) state = 2; + 1216066: 41: if (c == '{' && state == 2) state = 3; + 1216066: 42: if (c == ' '&& state == 3) state = 4; + 1216066: 43: if (c == 'a' && state == 4) state = 5; + 1216066: 44: if (c == 'x' && state == 5) state = 6; + 1216066: 45: if (c == '}' && state == 6) state = 7; + 1216066: 46: if (c == ')' && state == 7) state = 8; + 1216066: 47: if (c == ']' && state == 8) state = 9; + 1216066: 48: if (s[0] == 'r' && s[1] == 'e' + 111: 49: && s[2] == 's' && s[3] == 'e' + #####: 50: && s[4] == 't' && s[5] == '\0' + #####: 51: && state == 9) + -: 52: { + #####: 53: printf("error "); + #####: 54: exit(200); + -: 55: } + -: 56: } + 3: 57:} + -: 58: + -: 59: + 3: 60:int main(int argc, char *argv[]) + -: 61:{ + 3: 62: srand(time(NULL)); + 3: 63: testme(); + 3: 64: return 0; + -: 65:} diff --git a/projects/alghanmz/quiz/testme.gcda b/projects/alghanmz/quiz/testme.gcda new file mode 100644 index 000000000..237cfdc55 Binary files /dev/null and b/projects/alghanmz/quiz/testme.gcda differ diff --git a/projects/alghanmz/quiz/testme.gcno b/projects/alghanmz/quiz/testme.gcno new file mode 100644 index 000000000..c60697222 Binary files /dev/null and b/projects/alghanmz/quiz/testme.gcno differ