Example #1
0
int main(){
   int seed = 1234;
   int numPlayers = 2;
   int i, r, p;
   int t = 0;
   int k[10] = {adventurer, council_room, feast, gardens, mine,
   		remodel, smithy, village, baron, great_hall};
   struct gameState G;

   int handCount, playedCount;

   printf("Testing playSmithy():\n\n");

   //Test from initial state
   r = initializeGame(numPlayers, k, seed, &G);

   //build hand
   G.hand[0][0] = smithy;
   G.hand[0][1] = estate;
   G.hand[0][2] = copper;
   G.hand[0][3] = copper;
   G.hand[0][4] = mine;
   G.handCount[0] = 5;

   handCount = G.handCount[0];
   playedCount = G.playedCardCount;

   i = playSmithy(t, &G, 0, 0);
   
   //check that there are two more cards in hand
   if (G.handCount[0] == handCount + 2){
      printf("PASSED: Three cards added to hand\n");
   } else {
      printf("FAILED: Three cards added to hand\n");
   }

   //played pile increases by one
   if (G.playedCardCount == playedCount + 1 ){
      printf("PASSED: Card added to played pile\n");
   } else {
      printf("FAILED: Card added to played pile\n");
   }

   p = 0;
   for (t = 0; t < G.playedCardCount; t++){
      if (G.playedCards[i] == smithy){
	 p = 1;
      }
   }

   if (p == 1){
      printf("PASSED: Smithy in played card pile\n");
   } else {
      printf("FAILED: Smithy in payed card pile\n");
   }

   printf("Tests complete\n\n");
   
   return 0;
}
Example #2
0
void testPlaySmithy(int handpos, struct gameState *post){
	struct gameState pre;
	memcpy (&pre, post, sizeof(struct gameState));
	int currentPlayer = whoseTurn(post);
	int r;  
	r = playSmithy(post, handpos);

	if (r != 0)
		printf("r != 0\n");
	if  (pre.numActions - 1 != post->numActions)
		printf ("pre.numActions - 1 != post->numActions\n");
	if (pre.handCount[currentPlayer] + 2 != post->handCount[currentPlayer])
		printf("pre.handCount[currentPlayer] + 2 != post->handCount[currentPlayer]\n");
	if (pre.playedCardCount + 1 != post->playedCardCount)
		printf("pre.playedCardCount + 1 != post->playedCardCount\n");
	if (post->playedCards[post->playedCardCount - 1] != smithy)
		printf("post->playedCards[post->playedCardCount - 1] != smithy\n");
	
	if (pre.deckCount[currentPlayer] >= 3){
		if (pre.deckCount[currentPlayer] - 3 != post->deckCount[currentPlayer])
			printf("pre.deckCount[currentPlayer] - 3 != post->deckCount[currentPlayer]\n");
	} else {
		if (pre.deckCount[currentPlayer] + pre.discardCount[currentPlayer] - 3 != post->deckCount[currentPlayer])
			printf("pre.deckCount[currentPlayer] + pre.discardCount[currentPlayer] - 3 != post->deckCount[currentPlayer]\n");
	}
}
Example #3
0
int main() {
	struct gameState G;
	int runSuccess;
	int player = 0;
	int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall}; //kingdom cards we will play with
	int copyHandCount;

	printf("\n======Testing playSmithy()======\n\n");

	//run initializeGame() with 2 players, the kingdom cards we have selected, 1 as the random seed and the gameState struct
	runSuccess = initializeGame(2, k, 1, &G);
	printf("initializeGame() ran, and returned %d. Expected value: %d\n", runSuccess, 0);

	//save original handCount
	copyHandCount = G.handCount[player];

	//put Smithy at front of player's hand
	G.hand[player][0] = smithy;

	//run playSmithy()
	runSuccess = playSmithy(&G, 0);
	printf("\nplaySmithy() ran, and returned %d. Expected value: %d\n", runSuccess, 0);

	printf("\nAfter running playSmithy():\n");

	//compare handCount
	printf("\nG.handCount = %d. Expected value:%d\n", G.handCount[player], copyHandCount + 3);

	//compare discardCount
	printf("\nG.discardCount= %d. Expected value:%d\n", G.discardCount[player], 1);

	return 0;
}
Example #4
0
int main()
{
	printf("Testing playSmithy()...\n");
	struct gameState* state = newGame();
	int player = 0;
	int failCount = 0;
	state->numPlayers = 4;
	
	// start each player with a total of 15 cards
	int i;
	for (i = 0; i < state->numPlayers; i++)
	{
		state->handCount[i] = 5;
		state->deckCount[i] = 5;
		state->discardCount[i] = 5;
	}
	
	int nonCurrPlayerTotalCount = 15, currPlayerTotalCount = 15;
	
	playSmithy(state, 3, player);
	
	for (i = 0; i < state->numPlayers; i++)
	{
		if (i == player)
		{
			// 3 cards should move from deck to hand, 1 discarded from hand
			if (state->handCount[i] != 7 ||
				state->deckCount[i] != 2 ||
				state->discardCount[i] != 5)
			{
				printf("Error: Player 1 did not move 3 cards from deck to hand and discard one from hand\n");
				failCount++;
			}
		}
		else
		{
			// check that no other player's cards were affected
			int currCardCount = (state->handCount[i] + state->deckCount[i] + state->discardCount[i]);
			if (currCardCount != 15)
			{
				printf("Error: Player %d's card count is %d instead of 15\n", i + 1, currCardCount);
				failCount++;
			}
		}
	}
	
	printf("%d tests failed!\n", failCount);
	printf("Testing completed!\n");
	return 0;
}
Example #5
0
int main()
{
    //s1 will hold state after initialization, s2 will be state after draw card
    struct gameState *gs1 = malloc(sizeof(struct gameState));
    struct gameState *gs2 = malloc(sizeof(struct gameState));
    //Cards used in test instance of game
    int gameCards[10] = {smithy, adventurer, council_room, feast, gardens, mine, remodel,
        village, baron, great_hall};
    int numPlayers;
    int seed = 55;
    int p;
    int passed = 0;
    
    //Run tests for all game sizes and for each player in game
    printf("Testing for playSmithy beginning.\n");
    for (numPlayers = 2; numPlayers <= 4; numPlayers++)
    {
        printf("Testing a %d player game.\n", numPlayers);
        
        //Initialize game and test for success
        if (initializeGame(numPlayers, gameCards, seed, gs1) == -1)
        {
            printf("Game state failed to initialize. No testing completed.\n");
            return -1;
        }
        
        //Test for each player
        for (p = 0; p < numPlayers; p++)
        {
            printf("Testing for player %d playing smithy card.\n", p + 1);
            //A random card will be assigned to smith
            int card = rand() % 5;
            gs1->hand[p][card] = smithy;
            
            //Save state of game for comparison before and after playSmithy()
            memcpy(gs2, gs1, sizeof(struct gameState));
            playSmithy(p, card, gs1);
            //Test changes that occurred during playSmith()
            if (!testSmithy(gs2, gs1, p))
                passed = -1;
                
        }
    }
    if (passed != -1)
        printf("PASS all tests for playSmithy().\n");
    
    
    return 0;
}
Example #6
0
 int main() {
 	printf("Testing smithy\n");
    int i;
    int seed = 1000;
    int numPlayer = 2;
    int p, r;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;
    memset(&G, 23, sizeof(struct gameState));
    r = initializeGame(numPlayer, k, seed, &G);
    p = G.whoseTurn;
    int handPos = 0;

    G.hand[p][0] = smithy;
    G.hand[p][1] = gold;
    G.hand[p][2] = gold;
    G.hand[p][3] = estate;
    G.hand[p][4] = estate;
    i=0;
    playSmithy(handPos, p, &G);
    //Test all post conditions after playing smithy.  Removed from hand, Cards drawn, smithy discarded
    if(G.hand[p][0] == smithy)
    {
    	printf("Test Failed: Smithy Was not Played");
    	i++;
    }
    if(G.handCount[p] != 7)
    {
    	printf("Test Failed: Hand Count should = 7 actual is %d\n", G.handCount[p]);
    	i++;
    }
    if(G.discard[p][G.discardCount[p]-1] != smithy)
    {
    	printf("Test Failed: Smithy was not discarded properly");
    	i++;
    }
    if(i>0)
    {
    	printf("Tests Failed\n");
    }
    else
    {
    	printf("All Tests Passed");
    }
    return 0;
}
Example #7
0
//returns the number of tests passed
int testSmithyCard(struct gameState *state, int pos)
{
  int priorHand, postHand;
  int priorDiscard, postDiscard;
  int testsPassed = 0;

  //set up pre conditions
  priorHand = state->handCount[0];
  priorDiscard = state->discardCount[0];

  //play the smithy
  playSmithy(state, 0, pos);

  //set up post conditions
  postHand = state->handCount[0];
  postDiscard = state->discardCount[0];

  //check that 3 cards were added to hand
  if (postHand == (priorHand + 2)) {
    printf("PASS -> 3 cards were added to hand.\n");
    testsPassed++;
  }
  else {
    printf("FAIL -> Did not add 3 cards to hand.\n");
  }

  //check that smithy was removed from hand
  if (state->hand[0][pos] != smithy) {
    printf("PASS -> Smithy was removed from hand.\n");
    testsPassed++;
  }
  else {
    printf("FAIL -> Smithy was not removed from hand.\n");
  }

  //check that smithy was discarded
  if (postDiscard == (priorDiscard + 1)) {
    printf("PASS -> Smithy was discarded.\n");
    testsPassed++;
  }
  else {
    printf("FAIL -> Smithy was not discarded.\n");
  }

  return testsPassed;
}
Example #8
0
int randomTestSmithy() {
    int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
    int i, result, handCount, deckCount, playedCount;
    struct gameState g;
    int failure = 0;
    // initializing game  with random seed
    memset(&g, 23, sizeof(struct gameState));
    initializeGame(2, k, ((rand() % 999999999) + 1), &g);
    // adding 30-99 random cards to game, at least 3 cards to deck
    for (i = 0; i < ((rand() % 70) + 30); i++) {
        gainCard((rand() % 28), &g, (rand() % 3), 0);
    }
    gainCard((rand() % 28), &g, 1, 0);
    gainCard((rand() % 28), &g, 1, 0);
    gainCard((rand() % 28), &g, 1, 0);
    // shuffling deck
    shuffle(0, &g);
    // adding Smithy to hand
    g.hand[0][g.handCount[0]] = smithy;
    g.handCount[0]++;
    // saving current relevant data
    handCount = g.handCount[0];
    deckCount = g.deckCount[0];
    playedCount = g.playedCardCount;
    // playing Smithy
    playSmithy(0, handCount, &g);
    // comparing hand count
    result = g.handCount[0];
    printf("Original Hand Card Count: %d | New Count: %d, Expected: %d\n", handCount, result, handCount + 2);
    if (handCount != result - 2){failure = 1;}
    // comparing deck count
    result = g.deckCount[0];
    printf("Original Deck Count: %d | New Count: %d, Expected: %d\n", deckCount, result, deckCount - 3);
    if (deckCount != result + 3){failure = 1;}
    // comparing played count
    result = g.playedCardCount;
    printf("Original Played Count: %d | New Count: %d, Expected: %d\n", playedCount, result, playedCount + 1);
    if (playedCount != result - 1){failure = 1;}

    if (failure == 1) {
        return 1;
    } else {
        return 0;
    }
}
Example #9
0
int main() {

  int seed = 1000;
  int numPlayer = 2;
  int p, r, handCount, initHandCount, initPlayedCount;
  int k[10] = {adventurer, council_room, feast, gardens, mine
             , remodel, smithy, village, baron, great_hall};
  struct gameState G;
  int maxHandCount = 5;

  printf ("TESTING playSmithy():\n");
  
  p = 0; // set player 0
  
  memset(&G, 23, sizeof(struct gameState));   // clear the game state
  r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
  r = gainCard(smithy, &G, 2, p); // add smithy to player 0s hand


  initPlayedCount = G.playedCardCount;
  initHandCount = G.handCount[p];

  r = playSmithy(&G, 0, initHandCount - 1);
  
  //+1 played card count
  printf("G.playedCardCount = %d, expected = %d\n", G.playedCardCount, initPlayedCount + 1);
  assert(G.playedCardCount == initPlayedCount + 1); // check if the played card count is correct

  // +3 cards - Discard Smithy = Net +2
  printf("G.handCount[p] = %d, expected = %d\n", G.handCount[p], initHandCount + 2);
  assert(G.handCount[p] == initHandCount + 2); // check if the handcount is correct
  
  printf("All tests passed!\n");

  return 0;

}
Example #10
0
int main(){
    struct gameState originalGame, testGame;
    
    int cards[10] = {adventurer, embargo, village, minion, mine, cutpurse,
        sea_hag, tribute, smithy, council_room};
    int numberOfPlayers = 2;
    int player = 0;
    int seed = 1000;
    int handPos = 0;
    int handCountAfter;
    int firstCard = estate;
    int secondCard = duchy;
    int thirdCard = province;
    
    
    // initialize game state and player cards
    initializeGame(numberOfPlayers, cards, seed, &originalGame);
    
    printf("----------------- Testing Card: %s ----------------\n", TESTCARD);
    
    // ----------- TEST 1: There are no cards in the deck --------------
    memcpy(&testGame, &originalGame, sizeof(struct gameState));
    testGame.handCount[player] = 1;
    testGame.hand[player][handPos] = smithy;
    testGame.deckCount[player] = 0;
    playSmithy(player, &testGame, handPos);
    handCountAfter = testGame.handCount[player];
    
    printf("Test 1: There are no cards in the deck\n");
    printf("Hand count = %d, expected = %d\n\n", handCountAfter, 0);
    assert(handCountAfter == 0);
    
    
    // ----------- TEST 2: one card in hand --------------
    memcpy(&testGame, &originalGame, sizeof(struct gameState));
    testGame.handCount[player] = 1;
    testGame.hand[player][0] = smithy;
    testGame.deckCount[player] = 1;
    
    testGame.deck[player][0] = firstCard;
    
    playSmithy(player, &testGame, handPos);
    handCountAfter = testGame.handCount[player];
    
    printf("Test 2: one card in hand\n");
    printf("Hand count = %d, expected = %d\n", handCountAfter, 1);
    printf("Card in hand = %d, expected = %d\n\n", testGame.hand[player][0], firstCard);
    assert(handCountAfter == 1);
    assert(testGame.hand[player][0] == firstCard);
    
    // ----------- TEST 3: two cards in hand --------------
    memcpy(&testGame, &originalGame, sizeof(struct gameState));
    testGame.handCount[player] = 1;
    testGame.hand[player][0] = smithy;
    testGame.deckCount[player] = 2;
    
    testGame.deck[player][0] = firstCard;
    testGame.deck[player][1] = secondCard;
    
    playSmithy(player, &testGame, handPos);
    handCountAfter = testGame.handCount[player];
    
    printf("Test 3: two cards in hand\n");
    printf("Hand count = %d, expected = %d\n", handCountAfter, 2);
    printf("Card in first position = %d, expected = %d\n", testGame.hand[player][0], firstCard);
    printf("Card in second position = %d, expected = %d\n\n", testGame.hand[player][1], secondCard);
    assert(handCountAfter == 2);
    assert(testGame.hand[player][0] == firstCard);
    assert(testGame.hand[player][1] == secondCard);
    
    // ----------- TEST 4: three cards in hand --------------
    memcpy(&testGame, &originalGame, sizeof(struct gameState));
    testGame.handCount[player] = 1;
    testGame.hand[player][0] = smithy;
    testGame.deckCount[player] = 3;
    
    testGame.deck[player][0] = firstCard;
    testGame.deck[player][1] = secondCard;
    testGame.deck[player][2] = thirdCard;
    
    playSmithy(player, &testGame, handPos);
    handCountAfter = testGame.handCount[player];
    
    printf("Test 4: three cards in hand\n");
    printf("Hand count = %d, expected = %d\n", handCountAfter, 3);
    
    int firstCardFound = foundCardInHand(&testGame, player, firstCard);
    int secondCardFound = foundCardInHand(&testGame, player, secondCard);
    int thirdCardFound = foundCardInHand(&testGame, player, thirdCard);

    
    printf("Card %d in hand = %d, expected = %d\n", firstCard, firstCardFound, 1);
    printf("Card %d in hand = %d, expected = %d\n", secondCard, secondCardFound, 1);
    printf("Card %d in hand = %d, expected = %d\n\n", thirdCard, thirdCardFound, 1);
    assert(handCountAfter == 3);
    assert(firstCardFound);
    assert(secondCardFound);
    assert(thirdCardFound);
    
    printf("\n >>>>> SUCCESS: Testing complete %s <<<<<\n\n", TESTCARD);

    
    return 0;
}
Example #11
0
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus)
{
   //uses switch to select card and perform actions
  switch( card ) 
    {
    case adventurer:
       return (playAdventurer(state));
	
    case council_room:
       return (playCouncil_Room(state, handPos));
			
    case feast:
       return(playFeast(state, choice1)); 
	
    case gardens:
       return -1;

    case smithy:
      return (playSmithy(state, handPos));
		
    case village:
      return (playVillage(state, handPos));
			
    case mine:
      return(playMine(state, choice1, choice2, handPos));
			
    case remodel:
      return(playRemodel(state, choice1, choice2, handPos));
 
    case baron:
      return(playBaron(state, choice1, choice2, handPos));
   
    case great_hall:
      return(playGreat_hall(state, handPos));
 
    case minion:
      return(playMinion(state, choice1, choice2, handPos));

    case steward:
      return(playSteward(state, choice1, choice2, choice3, handPos));
		
    case tribute:
      return(playTribute(state));

    case ambassador:
      return(playAmbassador(state,choice1, choice2, handPos));
		
    case cutpurse:
      return(playCutpurse(state, handPos));
		
    case embargo:
      return(playEmbargo(state, choice1, handPos));
		
    case outpost:
      return(playOutpost(state, handPos));
		
    case salvager:
      return(playSalvager(state, choice1, handPos));
		
    case sea_hag:
      return(playSea_hag(state));
		
    case treasure_map:
      return(playTreasure_map(state, handPos));
 
    }
  return -1;
}
Example #12
0
int main()
{
	int i, j;
	int choice1, choice2, choice3, handPos, bonus, player;
	choice1 = choice2 = choice3 = handPos = bonus = player = 0;

	struct gameState state, controlState;

	printf("\nTESTING Smithy cardEffect\n");
	printf("Testing Deck Count\n");
	for(i = 3; i < 12; i++)
	{
		smithyInit(&state);
		smithyInit(&controlState);
		state.deckCount[player] = i;
		printf("Starting Deck Count %d ", i);
		for(j = 0; j < i; j++)
		{
			state.deck[player][i] = copper;
		}
		handPos = state.handCount[player] - 1;
		memcpy(&controlState, &state, sizeof(struct gameState));
		//cardEffect(smithy, choice1, choice2, choice3, &state, handPos, &bonus);
		playSmithy(&state, state.whoseTurn, handPos);
		compareDeckCount(player, &state, &controlState);
	}

	printf("Testing Hand Count\n");
	for(i = 1; i < 5; i++)
	{
		smithyInit(&state);
		smithyInit(&controlState);
		state.handCount[player] = i;
		printf("Starting Hand Count %d ", i);
		for(j = 0; j < i; j++)
		{
			state.hand[player][j] = copper;
		}
		state.hand[player][i - 1] = smithy;
		handPos = state.handCount[player] - 1;
		memcpy(&controlState, &state, sizeof(struct gameState));
		//cardEffect(smithy, choice1, choice2, choice3, &state, handPos, &bonus);
		playSmithy(&state, state.whoseTurn, handPos);
		compareHandCount(player, &state, &controlState);
	}
	
	printf("Testing Smithy Discard\n");
	for(i = 0; i < 5; i++)
	{
		smithyInit(&state);
		smithyInit(&controlState);
		for(j = 0; j < 5; j++)
		{
			state.hand[player][j] = copper;
		}
		state.hand[player][i] = smithy;
		handPos = i;
		memcpy(&controlState, &state, sizeof(struct gameState));
		//cardEffect(smithy, choice1, choice2, choice3, &state, handPos, &bonus);
		playSmithy(&state, state.whoseTurn, handPos);
		smithyInHand(player, &state);
	}

	printf("Testing Other Players' State\n");
	for(i = 0; i < 4; i++)
	{
		smithyInit(&state);
		smithyInit(&controlState);
		handPos = state.handCount[player] - 1;
		//cardEffect(smithy, choice1, choice2, choice3, &state, handPos, &bonus);
		playSmithy(&state, state.whoseTurn, handPos);
		comparePlayerState(player, &state, &controlState);
	}

	printf("Testing Supply Counts\n");
	smithyInit(&state);
	smithyInit(&controlState);
	handPos = state.handCount[player] - 1;
	//cardEffect(smithy, choice1, choice2, choice3, &state, handPos, &bonus);
	playSmithy(&state, state.whoseTurn, handPos);
	compareSupplyCount(player, &state, &controlState);

	return 0;
}
Example #13
0
void testSmithy()
{
    printf("Beginning random tests for smithy:\n");

    int i;
    int j;
    int passed = 0;
    int numTests = 1000;
    int player;

    for (i = 0; i < numTests; i++)
    {
        struct gameState state;
        if (setUp(&state) < 0)
        {
            printf("ERROR: setup failed\n");
            continue;
        }
        struct gameState expected;
        memcpy(&expected, &state, sizeof(struct gameState));

        // Add smithy as last card in hand and play it.
        player = state.whoseTurn;
        int handPos = state.handCount[player];
        state.hand[player][handPos] = smithy;
        state.handCount[player]++;
        if (playSmithy(&state, handPos) != 0)
        {
            printf("Failed: playSmithy() failed.\n");
            continue;
        }

        // Setup expected state.
        for (j = 0; j < 3; j++)
        {
            expected.hand[player][expected.handCount[player]] = expected.deck[player][expected.deckCount[player]];
            expected.handCount[player]++;
            expected.deckCount[player]--;
        }
        expected.handCount[player] += 3;
        expected.playedCards[expected.playedCardCount] = smithy;
        expected.playedCardCount++;

        // Compare states
        if (state.handCount[(player + 1) % 2] != expected.handCount[(player + 1) % 2])
        {
            printf("Non-active player's hand changed.\n");
            passed += 0;
            continue;
        }
        else if (state.deckCount[(player + 1) % 2] != expected.deckCount[(player + 1) % 2])
        {
            printf("Non-active player's deck changed.\n");
            passed += 0;
            continue;
        }
        else if (state.discardCount[(player + 1) % 2] != expected.discardCount[(player + 1) % 2])
        {
            printf("Non-active player's discard pile changed.\n");
            passed += 0;
            continue;
        }
        else if (state.handCount[player] != expected.handCount[player])
        {
            // Remove print statement since we intentionally introduced this bug
            // printf("Player's hand count not as expected.\n");
            passed += 0;
            continue;
        }
        else if (state.playedCardCount != expected.playedCardCount)
        {
            printf("Player's played card count not as expected.\n");
            passed += 0;
            continue;
        }
        else if (state.discardCount[player] != expected.discardCount[player])
        {
            printf("Player's discard card count not as expected.\n");
            passed += 0;
            continue;
        }
        else if (state.deckCount[player] != expected.deckCount[player])
        {
            printf("Player's deck card count not as expected.\n");
            passed += 0;
            continue;
        }
        // Compare expected to actual hands
        else
        {
            qsort(state.hand[player], state.handCount[player], sizeof(int), compare);
            qsort(expected.hand[player], expected.handCount[player], sizeof(int), compare);
            for (j = 0; j < expected.handCount[player]; j++)
            {
                if (state.hand[player][j] != expected.hand[player][j])
                {
                    printf("Failed: hands do not match.\n");
                    passed += 0;
                    continue;
                }
            }
        }
        
        passed += 1;
    }

    printf("End of test. %d out of %d tests passed.\n\n", passed, i);
}
Example #14
0
int main(int argc, char * argv[]) {
	int test_trials = 10000;
	
	if (argc > 1) {
		test_trials = atoi(argv[1]);
		if (test_trials < 1) {
			printf("Usage: cardtest1 <trials>\r\n");
			exit(1);
		}
	}
	
	srand(time(NULL));
	
	//new gameState
	struct gameState * gs = malloc(sizeof(struct gameState));
	struct gameState * stateCopy = malloc(sizeof(struct gameState));
	
	int trial;
	int returnValue;
	int numberOfErrors = 0;
	int playerNum;
	
	int emptyHand;
	int emptyDiscard;
	int emptyDeck;
	int emptyThree;	//not enough cards?
	int handPos;
	int drawCards;
	
	printf("Card Test 1\r\n");
	printf("Conducting %d random trials.\r\n", test_trials);
	
	for (trial = 0; trial < test_trials; trial++) {
		
		printf("TRIAL %d\r\n", trial);
		
		
		fuzzState(gs);

		//semi-randomize inputs (within reason)
		playerNum = randomNumber(2, MAX_PLAYERS) - 2;
		emptyDeck = percentChanceIsOne(5);
		emptyDiscard = percentChanceIsOne(5);
		emptyHand = percentChanceIsOne(5);
		emptyThree = percentChanceIsOne(1);
		
		if (emptyDeck == 1 || emptyThree == 1) {
			gs->deckCount[playerNum] = 0;
		} else { 
			gs->deckCount[playerNum] = randomNumber(1, 300);
		}
		
		if (emptyHand == 1 || emptyThree == 1) {
			gs->handCount[playerNum] = 1;					//leave room for Smithy card
		} else { 
			gs->handCount[playerNum] = randomNumber(2, 300);
		}
		
		if (emptyDiscard == 1 || emptyThree == 1) {
			gs->discardCount[playerNum] = 0;
		} else {
			gs->discardCount[playerNum] = randomNumber(1, 300);
		}
		
		gs->playedCardCount = randomNumber(0,gs->handCount[playerNum]);
		
		/*gs->deckCount[playerNum] = randomNumber(5, 300);
		gs->discardCount[playerNum] = randomNumber(5, 300);
		gs->handCount[playerNum] = randomNumber(5, 300);
		*/
		
		
		//set smithy card
		handPos = randomNumber(0, gs->handCount[playerNum]-1);
		gs->hand[playerNum][handPos] = smithy;
		
		//create copy for comparison later
		memcpy(stateCopy, gs, sizeof(struct gameState));
		
		//RUN FUNCTION
		returnValue = playSmithy(handPos, playerNum, gs);
		
		//Check state
		if (stateCopy->deckCount[playerNum] < 3) {
			if (gs->discardCount[playerNum] != 1) {
				printf("discardCount is not as expected.  Expected: 1, Actual: %d\r\n", gs->discardCount[playerNum]);
				numberOfErrors++;
			}
		} else {
			if (gs->discardCount[playerNum] != stateCopy->discardCount[playerNum] + 1) {
				printf("discardCount is not as expected.  Expected: %d, Actual: %d\r\n",stateCopy->discardCount[playerNum] + 1, gs->discardCount[playerNum]);
				numberOfErrors++;
			}			
		}
		
		//check top of discard for smithy
		if (gs->discard[playerNum][ gs->discardCount[playerNum] - 1 ] != smithy){
			printf("top of discard mismatch. Expected: %d, Actual %d\r\n", smithy, gs->discard[playerNum][ gs->discardCount[playerNum] - 1 ]);
			numberOfErrors++;
		}
		
		
		if (stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum] >= 3) {
			drawCards = 3;
		} else {
			drawCards = stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum];
		}
		
		//drawCards should be drawCards less pluss smithy
		if (gs->discardCount[playerNum] + gs->deckCount[playerNum] - drawCards + 1 != stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum]) {
			printf("deck + discard count is not as expected.  Expected: %d, Actual: %d\r\n",stateCopy->discardCount[playerNum] + stateCopy->deckCount[playerNum] - drawCards + 1, gs->discardCount[playerNum] + gs->deckCount[playerNum]);
			numberOfErrors++;
		}
		
		//Hand Should have drawCards extra cards minus smithy
		if (gs->handCount[playerNum] != stateCopy->handCount[playerNum] + drawCards - 1) {
			printf("handCount is not as expected.  Expected: %d, Actual: %d\r\n",stateCopy->handCount[playerNum] + drawCards - 1, gs->handCount[playerNum]);
			numberOfErrors++;
		}


	}	
	
	printf("Card Test 1 Complete\r\n");
	printf("Number of errors found: %d\r\n", numberOfErrors);
	

	free(gs);
	free(stateCopy);
	return 0;
}
Example #15
0
int main() {
	int runSuccess;
	int player = 0;
	int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall}; //kingdom cards we will play with
	int copyHandCount;
	int i, num_cards, j, card, pos;

	/* initialize random seed: */
	srand ( time(NULL) );

	printf("\n======Random Testing playSmithy()======\n\n");

	//create 2000 random gamestates
	for(i=0; i<2000; i++){
		copyHandCount = 0;

		struct gameState* G = malloc(sizeof(struct gameState));
		
		//get a random seed
		int random_seed = rand()% 1000000;
		
		//run initializeGame() with 2 players, the kingdom cards we have selected, the random seed and the gameState struct
		runSuccess = initializeGame(2, k, random_seed, G);
		printf("initializeGame() ran, and returned %d. Expected value: %d\n", runSuccess, 0);

		//randomly initialize deck, hand and discard
		//random number of cards in hand
		num_cards = rand() % 20 + 1;
		//select num_cards cards and randomly place in hand
		for(j = 0; j < num_cards; j++){
			//randomly select card
			card = rand() % 28;
			//randomly place in either deck, hand or discard
			pos = rand() % 3;
			gainCard(card, G, pos, player);
		}	
		
		//put smithy card at the front of the player's hand
		//replace what was there
		G->hand[player][0] = smithy;

		//save original handCount
		copyHandCount = G->handCount[player];

		//run playSmithy()
		runSuccess = playSmithy(G, 0);
		printf("\nplaySmithy() ran, and returned %d. Expected value: %d\n", runSuccess, 0);

		printf("\nAfter running playSmithy():\n");

		//compare handCount
		printf("\nG.handCount = %d. Expected value:%d\n", G->handCount[player], copyHandCount + 3);

		//compare discardCount
		printf("\nG.discardCount= %d. Expected value:%d\n", G->discardCount[player], 1);

		free(G);
	}

	return 0;
	}
Example #16
0
void testSmithy(struct gameState *G, int cardsInPlay[10]) {
    int result;
    int handPos = 2;
    int player = 0;
    int numberInDiscards = G->discardCount[player];
    int numberInHand = G->handCount[player];
    int numberInDeck = G->deckCount[player];
    int playedCards = G->playedCardCount;
    int numInDeck2 = G->deckCount[1];
    int numInHand2 = G->handCount[1];
    int numInDiscard2 = G->discardCount[1];
    int kc[10];
    int i;
    int sc[4];
    
    for (i = 0; i < 10; i++) {
        kc[i] = supplyCount(cardsInPlay[i], G);
    }
    for (i = 0; i < 4; i++) {
        sc[i] = G->supplyCount[i];
    }
    
    if (numberInHand >= 5) {
        G->hand[player][0] = copper;
        G->hand[player][1] = silver;
        G->hand[player][2] = smithy;
        G->hand[player][3] = adventurer;
        G->hand[player][4] = gold;
    }
    
    //test with smithy in mid hand
    result = playSmithy(player, G, handPos);
    
    assert(result == 0);
    printf("testSmithy(): PASS didn't produce an error.\n");
    assert(numInDeck2 == G->deckCount[1]);
    printf("testSmithy(): PASS other player's deck count unchanged.\n");
    assert(numInHand2 == G->handCount[1]);
    printf("testSmithy(): PASS other player's hand count unchanged.\n");
    assert(numInDiscard2 == G->discardCount[1]);
    printf("testSmithy(): PASS other player's discard count unchanged.\n");
    if (numberInHand != G->handCount[player] -2) {
        printf("testSmithy(): FAIL number in hand is %d instead of %d.\n", G->handCount[player], numberInHand +2  );
    } else {
        printf("testSmithy(): PASS number in hand is +3 cards and -1 discard.\n");
    }
    //assert(numberInDiscards == G->discardCount[player] +1);
    if (numberInDiscards != G->discardCount[player] +1) {
        printf("testSmithy(): FAIL number in discards is %d, was previously %d. Should have incremented.\n", G->discardCount[player], numberInDiscards);
    } else {
        printf("testSmithy(): PASS discard is incremented correctly.\n");
    }
    if (numberInDeck != G->deckCount[player] +2) { 
        printf("testSmithy(): FAIL number in deckCount is %d, was previously %d. Should have decremented by 2.\n", G->deckCount[player], numberInDeck);
    } else {
        printf("testSmithy(): PASS deckCount decremented correctly.\n");
    }
    if (G->playedCardCount -1 != playedCards) {
        printf("testSmithy(): FAIL playedCardCount is %d, was previously %d. Should have incremented by 1.\n", G->playedCardCount, playedCards);
    } else {
        printf("testSmithy(): PASS smithy added to playedCards.\n");
    }
    
    //test with smithy as only card in hand
    while (G->handCount[player] > 1) {
        discardCard(0, player, G, 0);
    }
    
    G->hand[player][0] = smithy;
    numberInDiscards = G->discardCount[player];
    numberInHand = G->handCount[player];
    numberInDeck = G->deckCount[player];
    playedCards = G->playedCardCount;
    
    result = playSmithy(player, G, 0);
    
    assert(result == 0);
    printf("testSmithy(): PASS didn't produce an error.\n");
    if (numberInHand != G->handCount[player] -2) {
        printf("testSmithy(): FAIL number in hand is %d instead of %d.\n", G->handCount[player], numberInHand +2  );
    } else {
        printf("testSmithy(): PASS number in hand is +3 cards and -1 discard.\n");
    }
    if (numberInDiscards != G->discardCount[player] +1) {
        printf("testSmithy(): FAIL number in discards is %d, was previously %d. Should have incremented.\n", G->discardCount[player], numberInDiscards);
    } else {
        printf("testSmithy(): PASS discard is incremented correctly.\n");
    }
    if (numberInDeck != G->deckCount[player] +1) {
        printf("testSmithy(): FAIL number in deckCount is %d, was previously %d. Should have decremented by 2.\n", G->deckCount[player], numberInDeck);
    } else {
        printf("testSmithy(): PASS deckCount decremented correctly.\n");
    }
    if (G->playedCardCount -1 != playedCards) {
        printf("testSmithy(): FAIL playedCardCount is %d, was previously %d. Should have incremented by 1.\n", G->playedCardCount, playedCards);
    } else {
        printf("testSmithy(): PASS smithy added to playedCards.\n");
    }
    
    int altered = 1;
    for (i = 0; i < 10; i++) {
        if (kc[i] != supplyCount(cardsInPlay[i], G)){
            printf("testSmithy(): FAIL kingdom cards supply has been altered.\n");
            altered = 0;
        }  
    }
    if (altered == 1)
        printf("testSmithy(): PASS kingdom cards supply has not been altered.\n");
    
    altered = 1;
    for (i = 1; i < 4; i++) {
        if (sc[i] != G->supplyCount[i]){
            printf("testSmithy(): FAIL victory card supply has been altered.\n");
            altered = 0;
        }  
    }
    if (altered == 1)
        printf("testSmithy(): PASS victory card supply has not been altered.\n");
}
Example #17
0
int main() {
    int i;
    int seed = 1000;

    int numPlayer = 2;
    int p, r;
    int k[10] = {adventurer, great_hall, cutpurse, gardens, mine
               , remodel, smithy, village, sea_hag, embargo};

    struct gameState G;
    int count;
    bool pass = true;

    // Use for checking state change
    int victoryCount, kingdomCount;
    int victory_kingdom[13];
    int index = 0;
    bool equal = true;

    memset(&G, 23, sizeof(struct gameState));   // clear the game state
    r = initializeGame(numPlayer, k, seed, &G); // initialize a new game

    printf("----------------- Testing smithy\n");

    // Before testing, check victory and kingdom cards
    // victory_kingdom[] saves pre-test pile counts
    printf("----------------- BEFORE PLAYER ACTIONS AND BUYS\n");

    victoryCount = G.supplyCount[estate];
    victory_kingdom[index] = victoryCount;
    index++;
    printf("estate count: %d\n", victoryCount);

    victoryCount = G.supplyCount[duchy];
    victory_kingdom[index] = victoryCount;
    index++;
    printf("duchy count: %d\n", victoryCount);

    victoryCount = G.supplyCount[province];
    victory_kingdom[index] = victoryCount;
    index++;
    printf("province count: %d\n", victoryCount);

    for (i = 0; i < 10; i++) {
        kingdomCount = G.supplyCount[ k[i] ];
        victory_kingdom[index] = kingdomCount;
        index++;
        printf("k[%d] count: %d\n", i, kingdomCount);
    }

    // Start testing
    p = 0;
    printf("----------------- Player %d:\n", p);

    // Put smithy in hand
    G.hand[p][ G.handCount[p] ] = smithy;
    G.handCount[p]++;

    // Check pile counts
    printf("----------------- AFTER PUT SMITHY IN HAND\n");

    printf("DECK COUNT\n");
    for (i = 0; i < G.deckCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.deck[p][i]);
    }

    printf("DISCARD COUNT\n");
    for (i = 0; i < G.discardCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.discard[p][i]);
    }

    printf("HAND COUNT\n");
    for (i = 0; i < G.handCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.hand[p][i]);
    }

    // Put cards in deck:
    // adventurer = 7
    // cutpurse = 21
    // remodel = 12
    // sea_hag = 25
    // village = 14
    G.deck[p][ G.deckCount[p] ] = adventurer;
    G.deckCount[p]++;
    G.deck[p][ G.deckCount[p] ] = cutpurse;
    G.deckCount[p]++;
    G.deck[p][ G.deckCount[p] ] = remodel;
    G.deckCount[p]++;
    G.deck[p][ G.deckCount[p] ] = sea_hag;
    G.deckCount[p]++;
    G.deck[p][ G.deckCount[p] ] = village;
    G.deckCount[p]++;

    // Check pile counts
    printf("----------------- AFTER PUT 5 MORE CARDS IN DECK\n");

    printf("DECK COUNT\n");
    for (i = 0; i < G.deckCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.deck[p][i]);
    }

    printf("DISCARD COUNT\n");
    for (i = 0; i < G.discardCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.discard[p][i]);
    }

    printf("HAND COUNT\n");
    for (i = 0; i < G.handCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.hand[p][i]);
    }

    // smithy should draw 3 cards from player's own discard
    // and add them to player's own hand
    // 14, 25, and 12 should be drawn and added to hand
    // 7 and 21 should stay in the deck
    // smithy should be added to discard
    playSmithy(&G, p, 5);

    // Check pile counts
    printf("----------------- AFTER PLAY SMITHY\n");

    printf("DECK COUNT\n");
    for (i = 0; i < G.deckCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.deck[p][i]);
    }

    // Verify
    int check1[] = {estate, estate, copper, copper, copper, adventurer, cutpurse};
    count = sizeof(check1)/sizeof(check1[0]);

    printf("Deck count: %d, Expected: %d\n", G.deckCount[p], count);
    if (G.deckCount[p] != count) {
        printf("----------------- TEST FAILED!\n");
        pass = false;
    }

    printf("DISCARD COUNT\n");
    for (i = 0; i < G.discardCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.discard[p][i]);
    }

    // Verify
    int check2[] = {smithy};
    count = sizeof(check2)/sizeof(check2[0]);

    printf("Discard count: %d, Expected: %d\n", G.discardCount[p], count);
    if (G.discardCount[p] != count) {
        printf("----------------- TEST FAILED!\n");
        pass = false;
    }

    printf("HAND COUNT\n");
    for (i = 0; i < G.handCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.hand[p][i]);
    }

    // Verify
    int check3[] = {copper, copper, estate, copper, copper, village, sea_hag, remodel};
    count = sizeof(check3)/sizeof(check3[0]);

    printf("Hand count: %d, Expected: %d\n", G.handCount[p], count);
    if (G.handCount[p] != count) {
        printf("----------------- TEST FAILED!\n");
        pass = false;
    }

    // Should be NO state changes made to next player's decks
    p = 1;
    printf("----------------- Player %d:\n", p);
    printf("----------------- Check that no state changes were made to other player's deck\n");

    // Check pile counts
    int copperCount = 0;
    int estateCount = 0;

    printf("DECK COUNT\n");
    for (i = 0; i < G.deckCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.deck[p][i]);
        if (G.deck[p][i] == copper) {
            copperCount++;
        }
        if (G.deck[p][i] == estate) {
            estateCount++;
        }
    }

    // Verify
    printf("Copper count: %d, Expected: 7\n", copperCount);
    printf("Estate count: %d, Expected: 3\n", estateCount);
    assert(copperCount == 7);
    assert(estateCount == 3);

    printf("Deck count: %d, Expected: 10\n", G.deckCount[p]);
    assert(G.deckCount[p] == 10);

    printf("DISCARD COUNT\n");
    for (i = 0; i < G.discardCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.discard[p][i]);
    }

    // Verify
    printf("Discard count: %d, Expected: 0\n", G.discardCount[p]);
    assert(G.discardCount[p] == 0);

    printf("HAND COUNT\n");
    for (i = 0; i < G.handCount[p]; i++)
    {
        printf("Position %d, Card: %d\n", i, G.hand[p][i]);
    }

    // Verify
    printf("Hand count: %d, Expected: 0\n", G.handCount[p]);
    assert(G.handCount[p] == 0);

    // Should be NO state changes made to victory and kingdom cards
    // (besides the changes intentionally made outside of playSmithy)
    printf("----------------- Check that no state changes were made to victory and kingdom card piles\n");

    victoryCount = G.supplyCount[estate];
    printf("estate count: %d\n", victoryCount);
    assert(victoryCount == victory_kingdom[0]);

    victoryCount = G.supplyCount[duchy];
    printf("duchy count: %d\n", victoryCount);
    assert(victoryCount == victory_kingdom[1]);

    victoryCount = G.supplyCount[province];
    printf("province count: %d\n", victoryCount);
    assert(victoryCount == victory_kingdom[2]);

    // index 0 to 2 = victory
    // index 3 to 12 = kingdom
    int x = 3;

    for (i = 0; i < 10; i++) {
        kingdomCount = G.supplyCount[ k[i] ];
        printf("k[%d] count: %d\n", i, kingdomCount);
        if (victory_kingdom[x] != kingdomCount) {
            equal = false;
            break;
        }
        x++;
    }

    printf("No state changes for victory and kingdom card piles: %d\n", equal);

    if (pass) {
        printf("\nAll tests passed!\n");
    }
    else {
        printf("\nSome test(s) failed!\n");
    }
    
    return 0;
}
Example #18
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:
      return playAdventurer(state);
			
    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:
      //+3 Cards
      return playSmithy(state, handPos);
	
	case village:
			return playVillage(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:
      //+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:
      return playSteward(state, choice1, choice2, choice3, handPos);
		
    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:
      return playSalvager(state, handPos, choice1);
		
    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;
}
Example #19
0
int main()
{
     int r;
     int player;
     int numPlayer = 2;

     int seed = 1000;
     int k[10] = { adventurer, council_room, feast, gardens, mine
          , remodel, smithy, village, baron, great_hall };

#if (NOISY_TEST == 1)
     const char* cards[] = //All 27 cards in game
     {
          "curse",
          "estate",
          "duchy",
          "province",
          "copper",
          "silver",
          "gold",
          "adventurer",
          "council_room",
          "feast",
          "gardens",
          "mine",
          "remodel",
          "smithy",
          "village",
          "baron",
          "great_hall",
          "minion",
          "steward",
          "tribute",
          "ambassador",
          "cutpurse",
          "embargo",
          "outpost",
          "salvager",
          "sea_hag",
          "treasure_map"
     };
#endif

     struct gameState prevState; //untouched game state to compare with after running tests
     struct gameState postState; //game state that will be used for tests


     int testResult;

     int numTests = 1; //how many test iterations to run.
     int failedTests = 0;
     int passedTests = 0;

     printf("==============================================\r\n");
     printf("    Beginning testing for playSmithy()\r\n");
     printf("==============================================\r\n");


     int i;
     for (i = 0; i < numTests; i++)
     {

          memset(&prevState, 23, sizeof(struct gameState));   // clear the game state
          r = initializeGame(numPlayer, k, seed, &prevState); // initialize a new game

          for (player = 0; player < numPlayer; player++)
          {
    
               prevState.handCount[player] = 1;        // set the number of cards on hand
               int smithyHand[1];
               smithyHand[0] = smithy;
               memcpy(prevState.hand[player], smithyHand, sizeof(int));

               prevState.deckCount[player] = 5;        // set the number of cards in deck
               int mixedDeck[5];
               mixedDeck[0] = copper;
               mixedDeck[1] = silver;
               mixedDeck[2] = gold;
               mixedDeck[3] = gardens;
               mixedDeck[4] = tribute;
               memcpy(prevState.deck[player], mixedDeck, sizeof(int) * 5);

               memcpy(&postState, &prevState, sizeof(struct gameState)); //create clone of game state to run tests on
               playSmithy(player, &postState, 0);

               //Check that exactly 3 cards were indeed added to current player's hand by comparing handCount value
#if (NOISY_TEST == 1)
               printf("---------------------------------------------------------------------------------------------------------------------------\r\n");
               printf("Testing that player[%d] handCount has increased by 2...\r\n", player);
#endif

               testResult = postState.handCount[player]; 

               if (testResult == prevState.handCount[player] + 2) //+2 due to smithy needing being discarded
               {
                    passedTests++;
               }
               else//failed
               {
                    failedTests++;
               }
#if (NOISY_TEST == 1)
               printf("handCount = %d, Expected = %d\r\n", testResult, prevState.handCount[player] + 2);
               printf("Testing that player[%d] playedCardCount has increased by 1..\r\n", player);
#endif
               //Check that playedCardCount increased by 1

               testResult = postState.playedCardCount;

               if (testResult == prevState.playedCardCount + 1) //passed
               {
                    passedTests++;
               }
               else//failed
               {
                    failedTests++;
               }
#if (NOISY_TEST == 1)
               printf("playedCardCount = %d, Expected = %d\r\n", testResult, prevState.playedCardCount + 1);
               printf("Checking that the smithy card was added to the played cards pile...\r\n");
#endif
               //Check that the smithy card was added to the played cards pile

               testResult = findCard(player, &postState, smithy, 0);

               if (testResult == -1) //failed, if variable is still -1, smithy was not found
               {
                    failedTests++;
               }
               else//passed
               {
                    passedTests++;
               }

#if (NOISY_TEST == 1)
               if (testResult == -1) //failed
               {
                    printf("Failed! Smithy card was not found in played cards pile!\r\n");
               }
               else//passed
               {
                    printf("Passed! Smithy card was found in played cards pile at position %d!\r\n", testResult);
               }
               printf("Checking that smithy card is not in player[%d]'s hand...\r\n", player);
#endif
               //Check that smithy card is not in player's hand

               testResult = findCard(player, &postState, smithy, 1);

               if (testResult == -1) //passed, if variable is still -1, smithy was not found
               {
                    passedTests++;
               }
               else//failed
               {
                    failedTests++;
               }

#if (NOISY_TEST == 1)
               if (testResult == -1) //passed
               {
                    printf("Passsed! Smithy card was not found in the player's hand!\r\n");
               }
               else//failed
               {
                    printf("Failed! Smithy card was found in player's hand at position %d!\r\n", testResult);
               }
               printf("Checking that the 3 cards added came from the player's own deck...\r\n");
#endif
               //Check that the 3 cards added came from the player's own deck


               //debug
               //int y;
               //for (y = 0; y < postState.handCount[player]; y++)
               //{
                    //printf("%s Card at position %d \r\n", cards[postState.hand[player][y]], y);
              // }

               int card;
               int cardCount = 0;
               int foundCards[MAX_DECK];
               int j;
               for (j = 0; j < 5; j++)
               {

                    if (j == 0) 
                    { 
                         card = copper; 
                    }
                    if (j == 1) 
                    { 
                         card = silver;
                    }
                    if (j == 2)
                    { 
                         card = gold;
                    }
                    if (j == 3)
                    { 
                         card = gardens;
                    }
                    if (j == 4)
                    { 
                         card = tribute;
                    }

                    testResult = findCard(player, &postState,card, 1);

                    if (testResult != -1) //card found
                    {
                         cardCount++;
                         foundCards[cardCount - 1] = card;

#if (NOISY_TEST == 1)
                         printf("Found %s card in player[%d]'s hand at position %d! Number of cards found: %d\r\n", cards[card], player, testResult, cardCount);
#endif
                    }
               }

               if (cardCount == 3) //passed
               {
                    passedTests++;
               }
               else//failed
               {
                   failedTests++;
               }

#if (NOISY_TEST == 1)
               printf("cardCount = %d, Expected = 3\r\n", cardCount);
#endif

               //check that the found cards are not in the deck anymore
               for (j = 0; j < cardCount; j++)
               {
                    testResult = findCard(player, &postState, foundCards[j], 2);

                    if (testResult == -1) //card not found
                    {
                         passedTests++;                         
                    }
                    else
                    {
                         failedTests++;
#if (NOISY_TEST == 1)
                         printf("Found %s card in player[%d]'s deck at position %d\r\n", cards[card], player, testResult);
#endif
                    }
               }
          }
     }
     
     printf("---------------------------------------------------------------------------------------------------------------------------\r\n");
     printf("%d out of %d tests passed!\r\n", passedTests, passedTests + failedTests);
     printf("%d out of %d tests failed!\r\n", failedTests, passedTests + failedTests);

     return 0;
}