Friday, June 15, 2007

Card Class

//-----------------------------------------------------------------------------
import java.io.*;
import java.util.*;
//-----------------------------------------------------------------------------
/**
A Card object represents a playing card, such as "Ace of Hearts". A card has a suit(Diamond, Heart, Spade or Club) and a face-value(2,...10, 11 = Jack, 12 = Queen, 13 = king).
A card object is immutable; once instantiated, the values cannot change.
@author: Tommy Flewwelling
@version: 1.00
Creation Date: 2007/07/17
*/
public class Card{
// Private instance variables.
// Card's face-value and suit.
private int faceValue, suit;

/** String representation of card suits - position in array indicates corresponding integral value. */
public static final String[] FACE_VALUE = {"", "", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
public static final String[] SUIT = {"Spades", "Hearts", "Clubs", "Diamonds"};
//---------------------------------------------------------------------------
/** Create a new Card instance, in * "uninitialized" state (requiring face and value settings).
*/
public Card(){
this.faceValue = -1;
this.suit = -1;
}
//---------------------------------------------------------------------------
/**
Creates a new Card instance.
@param face The card's face-value.
@param suit The card's suit.
*/
public Card(int face, int suit){
validateFace(face);
validateSuit(suit);
this.faceValue = face;
this.suit = suit;
}
//---------------------------------------------------------------------------
/**
Validates the card's face-value.
@param f The card's face-value.
@throws IllegalArgumentException If invalid face-value is provided.
*/
private void validateFace(int f){
if (f < 2 || f > 14) throw new IllegalArgumentException (f + " is an invalid face value. Valid face values are 2(Two) through 14(Ace).");
}
//---------------------------------------------------------------------------
/**
Validates the card's suit.
@param s The card's suit.
@throws IllegalArgumentException If invalid suit is provided.
*/
private void validateSuit(int s){
if (s < 0 || s > 3) throw new IllegalArgumentException (s + " is an invalid suit value. Valid suit values are 0(Spades), 1(Hearts), 2(Clubs), 3(Diamonds).");
}
//---------------------------------------------------------------------------
/**
Returns the card's face-value.
@return faceValue The card's face-value.
*/
public int getFaceValue(){return this.faceValue;}
//---------------------------------------------------------------------------
/**
Updates the card's face-value.
@param fv The card's face-value.
*/
public void setFaceValue(int fv){
validateFace(fv);
this.faceValue = fv;
}
//---------------------------------------------------------------------------
/**
Returns the card's suit.
@return suit The card's suit.
*/
public int getSuit(){return this.suit;}
//---------------------------------------------------------------------------
/**
Updates the card's suit.
@param s The card's suit.
*/
public void setSuit(int s){
validateSuit(s);
this.suit = s;
}
//--------------------------- Overridden Object methods ---------------
/**
Returns a string representation of the card's faceValue and suit.
@return A string representation of card's faceValue and suit(i.e. Two of Hearts).
*/
public String toString ()
{return(faceValue == -1 || suit == -1) ? "Unitialized Card" : (FACE_VALUE[faceValue] + " of " + SUIT[suit]);}
//---------------------------------------------------------------------------
/**
Overrides the Equals method than compares suit then face-value.
@return A boolean to determine if the objects are equal.
*/
public boolean equals(Object o){
boolean eq = false;
if (o instanceof Card)
{
Card c = (Card) o;
eq = this.faceValue == c.faceValue && this.suit == c.suit;
}
return eq;
}
//---------------------------------------------------------------------------
/**
Overrides the hashCode() method.
@return The hash code of the lenght of the face-value array(FACE_VALUE.length)
multiplied by instance field suit and added to the face-value(faceValue) instance field.
*/
public int hashCode(){return FACE_VALUE.length * suit + faceValue;}
//---------------------------------------------------------------------------
}
//---------------------------------------------------------------------------

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;}
//----------------------------------------------------------------------------
}
//----------------------------------------------------------------------------

Hand Class

//------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Collections;
//------------------------------------------------------------------------------
/**
A Hand object represents a generic playing hand of Cards using {@link java.util.ArrayList}. It also exhibits a containment relationship with the Card class. Hand objects contain Cards in the Deck, stored in an array list.
@author: Tommy Flewwelling
@version: 1.00
Creation Date: 2007/6/02
*/
public class Hand
{
//------------------------------------------------------------------------------
// Private instance variable.
private ArrayListhand;
//------------------------------------------------------------------------------
/**
Creates a new Hand instance, in "uninitialized" state.
*/
public Hand(){hand = new ArrayList();}
//------------------------------------------------------------------------------
/**
Initializes a Hand given an array of Cards.
@param cards[] An array of Cards.
*/
public Hand(Card[] cards){
hand = new ArrayList();

for(int i = 0; i < cards.length; i++)
addCard(cards[i]);
}
//-----------------------------------------------------------------------------
/**
Adds a Card to the present Hand.
@param card The card to be added to the present Hand.
*/
public void addCard(Card card){
if(card != null)
hand.add(card);
}
//------------------------------------------------------------------------------
/**
Seeks for the specified Card stored at the specified location in the present Hand.
@param index Position of Card to be accessed.
@return The specified Card in the present Hand or the null reference if index is out of bounds.
*/
public Card findCard(int index){
return (Card)hand.get(index);
}
//------------------------------------------------------------------------------
/**
Removes the specified Card in the present Hand. If the Card exists in this Hand more than once, only the first occurrance is removed.
@param card The Card to be removed in the present Hand.
*/
public Card removeCard(Card card) {
int index = hand.indexOf(card);
if(index >= 0 && index < hand.size())
return hand.remove(index);
else
return null;
}
//------------------------------------------------------------------------------
/**
Removes the specified Card at the specified location in the Hand.
@param index An index of the Card to be removed in the present Hand.
*/
public Card removeCard(int index){
if(index >= 0 && index < hand.size())
return hand.remove(index);
else
return null;
}
//------------------------------------------------------------------------------
/**
Determines if the specified Card is current in the Hand.
@param card The Card being searched for in the Hand.
@return true If the Hand contains the Card, otherwise return false.
*/
public boolean containsCard(Card card){
return hand.contains(card);
}
//-----------------------------------------------------------------------------
/**
Returns the number of cards present in the Hand.
@return The number of cards present in the Hand.
*/
public int getNumberOfCards(){return hand.size();}
//------------------------------------------------------------------------------
/**
Determines if the Hand is Empty.
@return true If the Hand is empty, otherwise return false.
*/
public boolean isEmpty(){
return hand.isEmpty();
}
//------------------------------------------------------------------------------
/**
Removes all the cards present in the Hand.
*/
public void clearCards(){
hand.clear();
}
//------------------------------------------------------------------------------
/**
Returns a description of the Hand. The list is part of an enclosing object of type Hand.
@return A list of cards present in the Hand.
*/
public String toString() {return getClass().getName() + hand.toString();}
//------------------------------------------------------------------------------
}