smithers.cards
Class CardStack

java.lang.Object
  extended by smithers.cards.CardStack

public class CardStack
extends java.lang.Object

Class to store a stack of Card objects. Includes methods for creating, sorting, shuffling and dealing cards.


Constructor Summary
CardStack()
          Constructs an empty stack.
CardStack(Card[] cards)
          Creates a stack from an array of cards.
CardStack(int length)
          Creates a stack of a given number of new cards.
 
Method Summary
 void addCard(Card newCard)
          Adds a card to the top of the stack.
 void deal(CardStack target)
          Deals one card into the specified stack.
 void deal(CardStack[] targets)
          Deals all of the cards in this stack into the specified stacks.
 int deal(CardStack[] targets, int count)
          Deals the specified number of cards into the specified stacks.
 void deal(CardStack target, int count)
          Deals the specified number of cards into the specified stack.
 Card getCard(int index)
          Returns a specific card from the list.
 int getLength()
          Returns the number of cards in the list.
 void move(CardStack target, int count)
          Moves the specified number of cards into the specified stack.
 void removeCard()
          Removes a card from the top of the stack.
 void setSeed(long seed)
          Sets the random seed used for shuffling this stack.
 void shuffle()
          Shuffles this stack of cards.
 void sort()
          Sorts this stack by suit then rank using quicksort with aces low.
 void sort(boolean acesHigh)
          Sorts this stack by suit then rank using quicksort, optionally ranking aces as high or low.
 Card[] toArray()
          Returns an array containing all the cards in this stack.
 Card topCard()
          Returns the top card in the stack.
 java.lang.String toString()
          Returns a String representation of the entire stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CardStack

public CardStack()
Constructs an empty stack.


CardStack

public CardStack(int length)
Creates a stack of a given number of new cards. Creates the first length cards in the deck (0 and 52 are useful values). Values >52 produce multiple decks.
eg. 0=empty, 13=all the clubs, 52=whole deck, 104=two decks.

Parameters:
length - the number of Cards to create.

CardStack

public CardStack(Card[] cards)
Creates a stack from an array of cards.

Parameters:
cards - the Cards to put into the stack
Method Detail

getLength

public int getLength()
Returns the number of cards in the list.

Returns:
the number of cards in the list

topCard

public Card topCard()
Returns the top card in the stack.

Returns:
the card on the top of the stack

getCard

public Card getCard(int index)
Returns a specific card from the list.

Parameters:
index - the index of the card to get
Returns:
the card

toString

public java.lang.String toString()
Returns a String representation of the entire stack.

Overrides:
toString in class java.lang.Object
Returns:
a String containing a 2-character representation of each card in this stack

toArray

public Card[] toArray()
Returns an array containing all the cards in this stack. This array will be a copy of this stack so that modifications of either will not affect the other.

Returns:
an array containing a copy of this stack

addCard

public void addCard(Card newCard)
Adds a card to the top of the stack.

Parameters:
newCard - the card to add

removeCard

public void removeCard()
Removes a card from the top of the stack.


move

public void move(CardStack target,
                 int count)
Moves the specified number of cards into the specified stack. If count == 0, moves all of them. This differs from the method deal(CardStack, count) in that the cards are kept in the same order.

Parameters:
target - the stack to deal to
count - the number of cards to move, or 0 for all of them

deal

public void deal(CardStack target)
Deals one card into the specified stack.

Parameters:
target - the stack to deal to

deal

public void deal(CardStack target,
                 int count)
Deals the specified number of cards into the specified stack. If count == 0, deals all of them.

Parameters:
target - the stack to deal to
count - the number of cards to deal, or 0 for all of them

deal

public void deal(CardStack[] targets)
Deals all of the cards in this stack into the specified stacks.

Parameters:
targets - the stacks to deal to

deal

public int deal(CardStack[] targets,
                int count)
Deals the specified number of cards into the specified stacks. If count < 0 then deal -count cards to each stack, if count > 0 then deal count cards in total, if count == 0 then deal all the cards.

Parameters:
targets - the stacks to deal to
count - the number of cards to deal (as above)
Returns:
the number of cards dealt

setSeed

public void setSeed(long seed)
Sets the random seed used for shuffling this stack.

Parameters:
seed - the new seed

shuffle

public void shuffle()
Shuffles this stack of cards.


sort

public void sort()
Sorts this stack by suit then rank using quicksort with aces low.


sort

public void sort(boolean acesHigh)
Sorts this stack by suit then rank using quicksort, optionally ranking aces as high or low.

Parameters:
acesHigh - true iff aces are to be ranked as high