Friday, June 15, 2007

Deck Class

//----------------------------------------------------------------------------
import java.util.*;
//----------------------------------------------------------------------------
/**
A Deck object represents a generic deck of playing cards. It also exhibits
a containment relationship with the Card class. Deck objects contain Cards in the Deck,
stored in an array list.
@author: Tommy Flewwelling
@version: 1.00
Creation Date: 2007/07/17
*/
public class Deck
{
// The standard 52-card deck, no jokers.
private static final List STANDARD_DECK;

static{
List standardDeck = new LinkedList();
for(int suit = 0; suit < 4; suit++) // -- knowledge of Card
for(int face = 2; face < 15; face++) // -- knowledge of Card
standardDeck.add(new Card(face, suit));
STANDARD_DECK = Collections.unmodifiableList(standardDeck);
}

// The cards in the deck.
private List cards;
//----------------------------------------------------------------------------
/**
Creates a new Deck of the standard 52-card variety.
*/
public Deck(){this(STANDARD_DECK);}
//----------------------------------------------------------------------------
/**
Creates a new Deck consisting of the provided
Cards.
@param cards Contains the Cards to be placed in the
Deck.
*/
public Deck(List cards){
this.cards = new LinkedList();
this.cards.addAll(cards);
}
//----------------------------------------------------------------------------
/**
Shuffle this deck.
*/
public void shuffle(){Collections.shuffle(cards);}
//----------------------------------------------------------------------------
/**
Indicates the number of cards in this deck.
@return The number of cards in this deck.
*/
public int size(){return cards.size();}
//----------------------------------------------------------------------------
/**
Look at the top card.
@return The Card on the top of this deck.
*/
public Card peekCard(){return peekCard(0);}
//----------------------------------------------------------------------------
/**
Look at the card idx positions from the top.
@param idx The number of cards from the top of this deck.
@return The Card at the idxth position.
*/
public Card peekCard(int idx){return (Card)cards.get(idx);}
//----------------------------------------------------------------------------
/**
Removes the top card.
@return The Card on the top of this deck.
*/
public Card takeCard(){return takeCard(0);}
//----------------------------------------------------------------------------
/**
Removes the card idx positions from the top.
@param idx The number of cards from the top of this deck.
@return The Card at the idxth position.
*/
public Card takeCard(int idx){return (Card)cards.remove(idx);}
//----------------------------------------------------------------------------
/**
Adds a card to the top of this deck.
@param c The Card to add.
*/
public void addCard(Card c){addCard(0, c);}
//----------------------------------------------------------------------------
/**
Adds a card to this deck at the specified position.
@param idx The number of cards from the top of this deck.
@param c The Card to add.
*/
public void addCard(int idx, Card c){cards.add(idx, c);}
//----------------------------------------------------------------------------
/**
Returns a description of the Deck.
@return A list of cards present in the Deck.
*/
public String toString(){return getClass().getName() + cards;}
//----------------------------------------------------------------------------
}
//----------------------------------------------------------------------------

No comments: