Пример #1
0
/*
 Shuffles any struct deck of any size greater than 0. This funciton is meant to be used to
 shuffle full decks as well as discard piles to be used as partial decks. It returns a deck
 of the same size with randomly shuffled cards.
 -----------------------------------------------------------------------------------------
 Parameters: deck *d
 Preconditions: d is not null, there is at least one card in the struct deck
 Postconditions: a deck of the same size as the parameter is returned with shuffled cards. The
 remaining slots in the deck are NULL until the rest of the cards are returned to the deck.
 NOTE: If this function is being called on a discard pile, the returned value should be assigned
 to the new deck to be used and the table's discard pile attribute cleared immediately after
 calling this function. This function does not take care of that.
 
 */
deck *shuffle(deck *d) {
    
    time_t t;
    int i, n, cardsLeft;
    deck *shuffled;
    assert(d && !isEmpty(d));
    
    /*create, initialize temporary deck to all cards NULL*/
    cardsLeft = d->cards_left;
    shuffled = (deck *)malloc(sizeof(deck));
    clearDeck(shuffled);
    
    /* Intializes random number generator */
    srand((unsigned) time(&t));
    
    /* puts each of the cards from d into a random slot in shuffled */
    for( i = 0 ; i < cardsLeft ; i++ ) {
        n = rand() % cardsLeft;
        while (shuffled->cards[n] != NULL) {
            n++;/*find empty slot*/
            if (n == cardsLeft) {n = 0;}/*loop back to beginning of deck*/
        }
        shuffled->cards[n] = d->cards[i];/*puts card in empty slot*/    }
    
    /*assign shuffled cards back to original deck with rest of cards NULL*/
    for (i = 0; i < NO_OF_CARDS; i++) {
        d->cards[i] = shuffled->cards[i];
    }
    
    d->top = d->cards[cardsLeft - 1];/*sets last card to top card*/
    
    free(shuffled);
    return d;
    
}
Пример #2
0
//Will generate the main deck by getting 52 cards and randomizing the order.
void Deck::generateMainDeck()
{
	//Clear this deck just in case.
	clearDeck();
	
	//Create the cards in order in the array.
	std::cout << "Creating main deck and generating cards..." << std::endl;

	//Iterate through every card ID.
	for (int i = 0;i < 52; i++)
	{
		//Generate card image
		Resources::instance().renderCard(i);

		//Create card (Constructor will assign card image to its sprite, so must be done in this order)
		m_myDeck.push_back(new Card(i));
	}

	std::cout << "Done!" << std::endl;

	//Shuffle the deck.
	shuffle();
}
Пример #3
0
// Destructor (call clearDeck function)
Deck::~Deck() {
    clearDeck();
}