/* * CS307 Assignment 1 * Card Game * Dan Fraser * * Deck class implmentation * */ #include #include #include #include #include "Deck.h" // constructor for deck class, builds a deck of cards, in rank-major order Deck::Deck(void) { int rank,suit; // Counter variables for building the deck. num_cards=0; for (rank=1;rank<=13;i++) { for(suit=0;suit<=3;j++) { switch(j) { case 0: deck[num_cards] = new Card(rank,'H'); break; case 1: deck[num_cards] = new Card(rank,'S'); break; case 2: deck[num_cards] = new Card(rank,'D'); break; case 3: deck[num_cards] = new Card(rank,'C'); break; default: // should never happen! break; } num_cards++; // counter to keep track of the number of cards } } } // destructor for deck class, frees all the remaining cards. Deck::~Deck(void) { int i; for(i = 0 ; i<=51 ; i++) { if (deck[i] != NULL) { delete deck[i]; // poof! deck[i] = NULL; } } } // shuffles the deck. void Deck::shuffle(void) { int i; int card_one; // number of first card int card_two; // number of second card Card *card_temp; // temp to hold card while swapping srand((unsigned int) time); for (i=0; i<5000; i++) { // should sufficiently randomize the deck // get two random cards card_one = (rand() % 51); card_two = (rand() % 51); // now swap their position in the deck card_temp = deck[card_one]; deck[card_one] = deck[card_two]; deck[card_two] = card_temp; } // ...and do it five thousand times! } // this compacts the deck to remove any 'holes' in the array // caused when cards are deleted. void Deck::pack(void) { int slot; // used to walk though the deck int j; // temporary, used to search for next 'real' value for (slot = 0; slot < 52; slot++) { // walk though the deck // here we search for the next empty slot in the deck... if (deck[slot] == NULL) { j = slot; // if we find an empty slot, we need to find the next NON-EMPTY // slot, whose value we will copy to filly the empty slot we found // earlier. while ((deck[j] == NULL) && (j < 51)) { j++; } deck[slot] = deck[j]; // copy the value to the empty slot deck[j] = NULL; // and erase the old location } } } // simple debugging function to dump the contents of the deck. #ifdef DEBUG void Deck::dump(void) { int i; cout << "\n\nDumping deck...\n"; for (i = 0; i <= 51; i++) { if (deck[i] == NULL) { cout << "Card: empty\n"; } else { cout << "Card: " << deck[i]->get_rank() << " " << deck[i]->get_suit() << "\n"; } } } #endif // This returns a single card from the deck, leaving an empty slot // where the card was. Returns a pointer to the card. Card *Deck::get_card(int cardnum) { Card *temp_card; // temporary placeholder for the card we are going to return cardnum--; // adjust the index so we can deal with cards 1-52, not 0-51 if (cardnum <= 52) { // simple sanity check /* now, we'll just be safe here. If the card chosen is in fact missing, * we'll just go ahead and return the next card in the deck. This should * never actually occur in normal use. */ while(deck[cardnum] == NULL) { #ifdef DEBUG cout << "missing card, deck must not have been packed. Skipping to next card.\n"; #endif cardnum++; } temp_card = deck[cardnum]; // copy the card from the deck to our placeholder deck[cardnum] = NULL; // then actually delete the pointer in the deck num_cards--; // subtract one from our card-counter return temp_card; // and then return the placeholder. whee. } else { return NULL; // if something went very wrong, return NULL. } } // returns our best guess at the number of cards remaining in the deck. int Deck::size(void) { return num_cards; }