public class RandomDeck {
  //Create a well shuffled deck
  public RandomDeck() {
    num_remaining_cards = 0;
    cards = new PlayingCard[52];
    for (int i = 0; i <= 3; ++i)
      for (int j = 2; j <= 14; ++j)
        cards[num_remaining_cards ++] = new PlayingCard(i, j);
    shuffleDeck();
  }
  
  // Get a playing card
  public PlayingCard getCard() {
    if (num_remaining_cards <= 0)
      return null;
    --num_remaining_cards;
    return cards[num_remaining_cards];
  }
  
  // Get the number of remaining cards
  public int numRemains() {
    return num_remaining_cards;
  }
  
  // Internal method to shuffle a deck
  private void shuffleDeck() {
    for (int i = cards.length - 1; i >= 1; --i) {
      int rand = (int) (Math.random() * (i+1));
      PlayingCard rand_card = cards[rand];
      cards[rand] = cards[i];
      cards[i] = rand_card;
    }
  }
  
  // Test the class
  public static void main(String[] args) {
    RandomDeck d = new RandomDeck();
    PlayingCard c = d.getCard();
    while (c != null) {
      System.out.println(c.toString());
      System.out.println("Remaining: " + d.numRemains());
      c = d.getCard();
    }
  }
  
  private PlayingCard[] cards;
  private int num_remaining_cards;
}

