Exemple #1
0
/* Usage: deck hand_size number_of_hands */
int main(int argc, char *argv[]) {

    /* input */
    int nhands;                   /* stores number of hands from input */
    int handSize;                 /* stores hand size from input */
    int canDisplayHands;          /* flag to indicate if the hands can be displayed */

    int foundErrors = NO_ERRORS;  /* indicate whether errors are found */

    /* validate input */
    if (validInput(argc, argv, &handSize, &nhands)) {

        Card deck[DECK_SIZE];     /* the cards deck */
        Hand hands[nhands];       /* the hands */
        Hand sortedHands[nhands];

        canDisplayHands = (handSize && nhands) > 0 ? TRUE : FALSE;

        /* create a deck of cards. */
        printf("\n   Create and display the deck.\n\n");
        createDeck(deck);
        displayDeck(deck, DECK_SIZE);

        /* shuffle deck of cards */
        printf("\n   Shuffle and display the deck.\n\n");
        shuffleDeck(deck, DECK_SIZE);
        displayDeck(deck, DECK_SIZE);

        if (canDisplayHands) {
            /* create and display hands */
            printf("\n   Create and display hands.\n\n");
            createHands(deck, hands, handSize, nhands);
            sortHands(hands, sortedHands, handSize, nhands);

            displayHands(hands, sortedHands, handSize, nhands);

            /* rank hands */
            printf("\n   Rank and display ranked hands.\n\n");
            displayRankedHands(sortedHands, nhands);

            printf("\n   Display winners.\n\n");
            displayWinners(sortedHands, nhands);
        }
        else {
            printf("\nCould not display hands because either the hand size or the number of hands is zero.\n");
        }
    }
    else {
        /* invalid input */
        printf("Usage: deck hand_size number_of_hands\n");
        foundErrors = ERRORS;
    }
    return foundErrors;
}
Exemple #2
0
void testCardDeal()
{
    Player p1, p2;
    p1.name = "Player 1";
    p2.name = "Player 2";
    
    Card deck[DECK_SIZE];
    
    initDeck(deck);
    
    cout << "-------- TESTING CARD FUNCTIONS ---------" << endl;
    
    cout << "--- ORIGINAL SORTED DECK ---" << endl << endl;
    displayDeck(deck);
    
    cout << endl << "--- SHUFFLED DECK ---" << endl << endl;
    shuffleCards(deck, DECK_SIZE);
    displayDeck(deck);
    
    drawHand(deck, p1);
    drawHand(deck, p2);
    
    cout << endl << "--- PLAYER HANDS ---" << endl << endl;
    displayHand(p1);
    cout << endl << endl;
    displayHand(p2);
    cout << endl << endl;
    
    
    
    tossCardFromHand(p1.hand[2], p1);
    tossCardFromHand(p2.hand[2], p2);
    
    cout << endl << "--- PLAYER HANDS ---" << endl << endl;
    displayHand(p1);
    cout << endl << endl;
    displayHand(p2);
    cout << endl << endl;
    
    drawCard(deck, p1);
    drawCard(deck, p2);
    
    
    cout << endl << "--- PLAYER HANDS ---" << endl << endl;
    displayHand(p1);
    cout << endl << endl;
    displayHand(p2);
    cout << endl << endl;
}
int main() {
    int i, j;
    int seed = 1000;

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

    int cards[10] = {copper, smithy, village, silver, mine, gold,
                gardens, remodel, embargo, cutpurse};

    struct gameState G, testG;
    int index, count;
    int treasureCount;
    int treasureDrawn, otherRevealed;
    int preDeck, preDiscard, preHand;
    bool pass = true;

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

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

    // Number of runs
    for (j = 1; j < 1001; j++) {
        // Copy the game state to a test case
        memcpy(&testG, &G, sizeof(struct gameState));

        // Player 0
        p = 0;
        printf("----------------- Random Deck Test %d\n", j);
        printf("----------------- Initial counts\n");

        // Initialize deckCount and treasureCount
        testG.deckCount[p] = 0;
        treasureCount = 0;

        // Create deck of 10 random cards
        for (i = 0; i < 10; i++) {
            index = floor(Random() * 10);
            testG.deck[p][i] = cards[index];
            testG.deckCount[p]++;
        }

        // Check deck for number of treasures
        for (i = 0; i < testG.deckCount[p]; i++) {
            if (testG.deck[p][i] == copper || testG.deck[p][i] == silver || testG.deck[p][i] == gold) {
                treasureCount++;
            }
        }

        // Discard should be empty
        testG.discardCount[p] = 0;

        // Create fixed hand of 5 cards
        testG.handCount[p] = 0;
        for (i = 0; i < 5; i++) {
            testG.hand[p][i] = estate;
            testG.handCount[p]++;
        }

        // Save pre-test values
        preDeck = testG.deckCount[p];
        preDiscard = testG.discardCount[p];
        preHand = testG.handCount[p];

        // Calculate expected values
        // If no treasures in deck, then reveal all cards in deck
        if (treasureCount == 0) {
            treasureDrawn = 0;
            otherRevealed = testG.deckCount[p];
        }
        // If 1 treasure in deck,
        // then reveal all cards in deck in an attempt to get 2 treasures
        else if (treasureCount == 1) {
            treasureDrawn = 1;
            otherRevealed = testG.deckCount[p] - treasureDrawn;
        }
        // If at least 2 treasures in deck
        else {
            treasureDrawn = 0;
            otherRevealed = 0;
            i = testG.deckCount[p] - 1;
            while (treasureDrawn < 2) {
                if (testG.deck[p][i] == copper || testG.deck[p][i] == silver || testG.deck[p][i] == gold) {
                    treasureDrawn++;
                    i--;
                }
                else {
                    otherRevealed++;
                    i--;
                }
            }
        }

        displayAll(&testG, p);
    
        printf("----------------- After playAdventurer\n");

        playAdventurer(&testG, p);

        displayDeck(&testG, p);

        // Check that deck is correct
        count = preDeck - otherRevealed - treasureDrawn;
        printf("Deck count: %d, Expected: %d\n", testG.deckCount[p], count);
        if (testG.deckCount[p] != count) {
            printf("----------------- TEST FAILED!\n");
            pass = false;
        }

        displayDiscard(&testG, p);

        // Check that discard is correct
        count = preDiscard + otherRevealed;
        printf("Discard count: %d, Expected: %d\n", testG.discardCount[p], count);
        if (testG.discardCount[p] != count) {
            printf("----------------- TEST FAILED!\n");
            pass = false;
        }

        displayHand(&testG, p);

        // Check that count of cards is correct
        count = preHand + treasureDrawn;
        printf("Hand count: %d, Expected: %d\n", testG.handCount[p], count);
        if (testG.handCount[p] != count) {
            printf("----------------- TEST FAILED!\n");
            pass = false;
        }
    }

    if (pass) {
        printf("\nAll tests passed!\n");
    }
    else {
        printf("\nSome test(s) failed!\n");
    }
    
    return 0;
}