#include "deck.h" #include "randgen.h" // see A Computer Science Tapestry for this file const int SIZE = 52; Deck::Deck() : myCards(SIZE), myIndex(0) { int rank; int suit; int num = 0; for(rank=1; rank <= SIZE/4; rank++) { for (suit = Card::spades; suit <= Card::clubs; suit++) { myCards[num] = Card(rank,Card::Suit(suit)); num++; } } } void Deck::Shuffle() { myIndex = 0; RandGen gen; int k; for(k=0; k < SIZE-1; k++) { int swapIndex = gen.RandInt(k,SIZE-1); Card temp = myCards[swapIndex]; myCards[swapIndex] = myCards[k]; myCards[k] = temp; } } Card Deck::GetCard() { Card c; if (0 <= myIndex && myIndex < SIZE) { c = myCards[myIndex]; myIndex++; } else { c = Card(0,Card::spades); // make a joker } return c; } int Deck::Size() const { return SIZE - myIndex; }