import java.util.ArrayList; import java.util.Collections; /** * A DeckOfCards object represents a standard deck of 52 playing cards. * @author Dave Reed * @version 11/20/06 */ public class DeckOfCards { private ArrayList deck; /** * Creates a deck of 52 cards in order. */ public DeckOfCards() { this.deck = new ArrayList(); String suits = "SHDC"; String ranks = "23456789TJQKA"; for (int s = 0; s < suits.length(); s++) { for (int r = 0; r < ranks.length(); r++) { Card c = new Card("" + ranks.charAt(r) + suits.charAt(s)); this.deck.add(c); } } } /** * Shuffles the deck so that the locations of the cards are random. */ public void shuffle() { Collections.shuffle(this.deck); } /** * Deals a card from the top of the deck, removing it from the deck. * @return the card that was at the top (i.e., end of the ArrayList) */ public Card dealCard() { return this.deck.remove(this.deck.size()-1); } /** * Adds a card to the bottom of the deck. * @param c the card to be added to the bottom (i.e., beginning of the ArrayList) */ public void addCard(Card c) { this.deck.add(0, c); } /** * Determines the number of cards remaining in the deck. * @return the number of cards */ public int cardsRemaining() { return this.deck.size(); } /** * Returns a string representation of the deck. * @return a String containing all of the cards in order */ public String toString() { return this.deck.toString(); } }