cardgame
Class Deck

java.lang.Object
  |
  +--cardgame.Deck

public class Deck
extends java.lang.Object

The Deck class represents a deck of cards which can be used for various kinds of card games. It implements a shuffle method that can put the deck in a random order. It provides methods to deal the cards out to players and to keep track of the number of cards dealt and remaining.

Version:
1.0, 4 August 2001
Author:
James A. Webb, www.uberkode.com

Field Summary
private  java.lang.String deckBackImageFileName
          The private variable deckBackImageFileName stores the file name that represents the back of each PlayingCard in a Deck.
private  int numberOfCards
          The private variable numberOfCards represents the initial number of cards in a Deck.
private  int numberOfDecks
          The private variable numberOfDecks represents the number of standard decks in a Deck.
private  java.util.Stack stack
          The private variable stack is a java Stack (which extends Vector) that stores all PlayingCards created for a Deck.
private  boolean standardDeck
          The private variable standardDeck indicates whether a Deck is a standard deck or not.
 
Constructor Summary
Deck()
          The no-argument constructor creates a single standard deck with 52 cards.
Deck(boolean standard)
          This constructor allows for the creation of non-standard decks of cards; e.g., decks of less than 52 cards.
Deck(int deckMultiple)
          This constructor creates a deck composed of multiples of a standard deck.
 
Method Summary
 void addPlayingCard(PlayingCard cardToBeAdded)
          The public method addPlayingCard aids in creating non-standard decks.
 void buildStandardDeck()
          The public method buildStandardDeck assembles a standard Deck of 52 PlayingCards.
 PlayingCard dealPlayingCard()
          The public method dealPlayingCard simulates the dealing of a card from the top of the deck.
 int getCardsDealt()
          The public method getCardsDealt returns the current number of cards that have been dealt from a Deck.
 int getCardsLeft()
          The public method getCardsLeft returns the current number of cards that are left in a Deck.
 java.lang.String getDeckBackImageFileName()
          The public method getDeckBackImageFileName specifies a file name containing an image to use when constructing all of a Deck's PlayingCards.
 java.lang.String getDeckState()
          The public method getDeckState returns a useful String representation of the current state of a Deck.
 int getNumberOfCards()
          The public method getNumberOfCards returns the initial number of cards that a Deck had when created.
 int getNumberOfDecks()
          The public method getNumberOfDecks indicates the number of decks created by a Deck constructor.
 boolean getStandardDeck()
          The public method getStandardDeck indicates whether a Deck is a standard deck or not.
 void setDeckBackImageFileName(java.lang.String deckBackImageFile)
          The public method setDeckBackImageFileName specifies a file name containing an image to use when constructing all of a Deck's PlayingCards.
 void shuffle()
          The public method shuffle reorders the PlayingCards in a Deck to be in random order.
 java.lang.String toString()
          The public method toString returns a complete specification of a Deck's current state in a convenient tagged format.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

stack

private java.util.Stack stack
The private variable stack is a java Stack (which extends Vector) that stores all PlayingCards created for a Deck. The default order can be changed by invoking the shuffle method. PlayingCards are dealt off a Deck using the dealPlayingCard method.
See Also:
shuffle(), dealPlayingCard()

numberOfCards

private int numberOfCards
The private variable numberOfCards represents the initial number of cards in a Deck. If the standard Deck constructor is used, it will be set to 52. This number corresponds to the number of elements in the stack which holds the PlayingCards. This number does not change during play.
See Also:
getNumberOfCards()

numberOfDecks

private int numberOfDecks
The private variable numberOfDecks represents the number of standard decks in a Deck. If the standard Deck constructor is used, it will be set to 1. If the multiple Deck constructor is used, it will be set to the deckMultiple parameter passed to the constructor. This number does not change during use of a Deck.
See Also:
getNumberOfDecks()

standardDeck

private boolean standardDeck
The private variable standardDeck indicates whether a Deck is a standard deck or not. It is set to false if the Deck(false) constructor is called, and true otherwise.
See Also:
getStandardDeck()

deckBackImageFileName

private java.lang.String deckBackImageFileName
The private variable deckBackImageFileName stores the file name that represents the back of each PlayingCard in a Deck. This file name will be passed to each PlayingCard constructor as a deck is built.
See Also:
getDeckBackImageFileName(), setDeckBackImageFileName(String deckBackImageFile)
Constructor Detail

Deck

public Deck()
The no-argument constructor creates a single standard deck with 52 cards.
See Also:
buildStandardDeck()

Deck

public Deck(int deckMultiple)
This constructor creates a deck composed of multiples of a standard deck. The total number of cards resulting will be the standard number of cards per deck times the number of decks specified.
Parameters:
deckMultiple - the number of decks as an int.
See Also:
numberOfDecks, getNumberOfDecks(), buildStandardDeck()

Deck

public Deck(boolean standard)
This constructor allows for the creation of non-standard decks of cards; e.g., decks of less than 52 cards. The constructor takes a single boolean argument which specifies whether to create a standard or non-standard deck. A standard deck gets the standard 52 cards, and is the same as calling the default (no argument) constructor. A non-standard deck gets no cards automatically created; they must be created and added individually with the addPlayingCard method.
Parameters:
standard - true for standard, false for non-standard.
See Also:
addPlayingCard(PlayingCard cardToBeAdded)
Method Detail

getNumberOfCards

public int getNumberOfCards()
The public method getNumberOfCards returns the initial number of cards that a Deck had when created.
Returns:
the number of cards as an int.
See Also:
numberOfCards

getCardsDealt

public int getCardsDealt()
The public method getCardsDealt returns the current number of cards that have been dealt from a Deck.
Returns:
the number of cards as an int.

getCardsLeft

public int getCardsLeft()
The public method getCardsLeft returns the current number of cards that are left in a Deck.
Returns:
the number of cards as an int.

shuffle

public void shuffle()
The public method shuffle reorders the PlayingCards in a Deck to be in random order. This method works regardless of the number of cards currently in a deck. The VectorRandomizer and RandomIntGenerator classes are used to accomplish the randomization.
See Also:
VectorRandomizer, RandomIntGenerator

dealPlayingCard

public PlayingCard dealPlayingCard()
The public method dealPlayingCard simulates the dealing of a card from the top of the deck. The top PlayingCard in a Deck is actually the last element in Stack stack.
Returns:
the last element of stack, cast as a PlayingCard object.
See Also:
stack

buildStandardDeck

public void buildStandardDeck()
The public method buildStandardDeck assembles a standard Deck of 52 PlayingCards. The PlayingCards are placed on the stack in the order in which they are constructed, i.e., not shuffled. The last PlayingCard constructed (the 52nd) is considered to be the "top" of the stack. This method is called by Deck's constructors. Each PlayingCard gets a default value of 1, a standard front image, and a default back image set by the setDeckBackImageFileName method.
See Also:
stack, Deck(), Deck(int deckMultiple), Deck(boolean standard), setDeckBackImageFileName(String deckBackImageFile)

addPlayingCard

public void addPlayingCard(PlayingCard cardToBeAdded)
The public method addPlayingCard aids in creating non-standard decks. When a non-standard deck is constructed, it contains no cards. They must be constructed and added with this method. The numberOfCards parameter is updated each time a new PlayingCard is added. No defaults are provided for value, or front/back images.
Parameters:
cardToBeAdded - the PlayingCard to be added to a Deck.
See Also:
Deck(boolean standard)

getNumberOfDecks

public int getNumberOfDecks()
The public method getNumberOfDecks indicates the number of decks created by a Deck constructor.
Returns:
number of decks in a Deck as an int.
See Also:
numberOfDecks, Deck(int deckMultiple)

getStandardDeck

public boolean getStandardDeck()
The public method getStandardDeck indicates whether a Deck is a standard deck or not.
Returns:
true for a standard deck, false for a non-standard deck.
See Also:
standardDeck

getDeckBackImageFileName

public java.lang.String getDeckBackImageFileName()
The public method getDeckBackImageFileName specifies a file name containing an image to use when constructing all of a Deck's PlayingCards.
Returns:
the String value of a Deck's back image file name.
See Also:
deckBackImageFileName, setDeckBackImageFileName(String deckBackImageFile)

setDeckBackImageFileName

public void setDeckBackImageFileName(java.lang.String deckBackImageFile)
The public method setDeckBackImageFileName specifies a file name containing an image to use when constructing all of a Deck's PlayingCards.
Parameters:
deckBackImageFile - the String value of the back image file name.
See Also:
deckBackImageFileName, getDeckBackImageFileName()

getDeckState

public java.lang.String getDeckState()
The public method getDeckState returns a useful String representation of the current state of a Deck.
Returns:
the state of a Deck as a String.

toString

public java.lang.String toString()
The public method toString returns a complete specification of a Deck's current state in a convenient tagged format.
Overrides:
toString in class java.lang.Object
Returns:
the state of a Deck as a formatted String.