//------------------------------------------------------------------------------
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();}
//------------------------------------------------------------------------------
}
Friday, June 15, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment