ch.aplu.jcardgame
Class Deck

java.lang.Object
  extended by ch.aplu.jcardgame.Deck

public class Deck
extends java.lang.Object

A class to bundle information about the card suits and ranks and their card actors (sprite images).

You find a general description of the JCardGame library in the Card class documentation.

A deck maintains a store of available cards and contains the information about available card suits and card ranks as enumerations. Each card may have an individual integer card value called card point. Suits and ranks are ordered in a priority list defined by the order of enums when the deck is created. Each card is linked to a JGameGrid actor that uses two sprite images, one for the face and one for the cover (back). Each sprite image should have the same standard dimension. The scaling and rotation of card images is done dynamically when the cards are drawn in the game grid window.

The sprite images are loaded from disk files in the subdirectory "sprites" using the enum string values of the suits with an increasing number appended starting from 0.
For instance

enum Suit
{
  SPADES, HEARTS, DIAMONDS, CLUBS
}

enum Rank
{
  ACE, KING, QUEEN, JACK, TEN, NINE, EIGHT, SEVEN, SIX
}


the disk files

  sprites/clubs0.gif for Clubs-Ace, sprites/clubs1.gif for Clubs-King, etc. (all lowercase)

will be used. Another set of sprites images can be loaded by providing a sprites enumeration in the deck constructor. E.g. If the following enumeration is passed to the constructor

enum Sprites
{
  A, B, C, D
}


the sprite images are loaded from

  sprites/a0.gif for Clubs-Ace, sprites/a1.gif for Clubs-King, etc. (all lowercase)

but the card suits and ranks names remain unchanged.

See Also:
Card

Nested Class Summary
static interface Deck.CardValues
          Interface for the construction of integer arrays with card values (card points, card values) of each card in each suit.
 
Field Summary
 Card[][] cards
          Public array of all cards in this deck in the order cards[suitId][rankId].
 
Constructor Summary
Deck(T[] suits, R[] ranks, S[] sprites, java.lang.String cover)
          Creates a deck from given suit and rank and sprites enumerations with card values set to zero.
Deck(T[] suits, R[] ranks, S[] sprites, java.lang.String cover, Deck.CardValues cardValues)
          Creates a deck from given suit and rank and sprites enumerations with given card values.
Deck(T[] suits, R[] ranks, java.lang.String cover)
          Creates a deck from given suit and rank enumerations with card values set to zero using the values of the suits enumeration for the sprite names.
Deck(T[] suits, R[] ranks, java.lang.String cover, Deck.CardValues cardValues)
          Creates a deck from given suit and rank enumerations with given card values using the values of the suits enumeration for the sprite names.
 
Method Summary
 Hand[] dealingOut(int nbPlayers, int nbCardsPerPlayer)
          Same as dealingOut(nbPlayers, nbCardsPerPlayer, shuffle) with shuffle = true.
 Hand[] dealingOut(int nbPlayers, int nbCardsPerPlayer, boolean shuffle)
          Returns an array with nbPlayers + 1 hands.
static
<T extends java.lang.Enum<T>>
java.lang.String[]
enumToStringArray(T[] values)
          Returns the enumeration values as string array for the given enumeration.
 java.awt.Dimension getCardDimension()
          Returs the card dimensions of the card with suit and rank id zero.
<T extends java.lang.Enum<T>,R extends java.lang.Enum<R>>
int
getCardNumber(T suit, R rank)
          Returns the card number of the card with given suit and rank.
 int[] getCardValues(java.lang.Enum suit)
          Returns an integer array of the card values in the given suit.
static double getHeapSize()
          Returns the current memory heap size.
 int getNumberOfCards()
          Returns the total number of cards in this deck.
 int getNumberOfRanks()
          Returns the number of ranks in this deck.
 int getNumberOfSuits()
          Returns the number of suits in this deck.
 java.lang.Enum getRank(int rankId)
          Returns the rank corresponding the given rank id.
 int getRankId(int cardNb)
          Returns the rank id for the given card number.
<R extends java.lang.Enum<R>>
int
getRankId(R rank)
          Returns an integer id of the given rank (0: for rank with highest priority)
 java.lang.String getRankName(int cardNb)
          Returns the rank name as string for the given card number The cards are numbered from 0 in the suit and rank priority order given when the deck is created (the following numbering is used: all card from high to low rank of the cards in the highest priority suit, in the next suit, etc.)
<T extends java.lang.Enum<T>,R extends java.lang.Enum<R>>
Actor
getSeedActor(T suit, R rank)
          Returns the JGameGrid actor attributed to the card with the specified suit and rank.
 java.lang.Enum getSuit(int suitId)
          Returns the suit corresponding the given suit id.
 int getSuitId(int cardNb)
          Returns the suit id for the card with the given card number.
<T extends java.lang.Enum<T>>
int
getSuitId(T suit)
          Returns an integer id of the given suit (0: for suit with highest priority)
 java.lang.String getSuitName(int cardNb)
          Returns the suit name as string for the card with the given card number.
static void showHeapSize()
          Displays the current memory heap size in System.out.
 Hand toHand()
          Returns the complete shuffled deck as a hand.
 Hand toHand(boolean shuffle)
          Returns the complete deck as a hand.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cards

public Card[][] cards
Public array of all cards in this deck in the order cards[suitId][rankId].

Constructor Detail

Deck

public Deck(T[] suits,
            R[] ranks,
            java.lang.String cover)
Creates a deck from given suit and rank enumerations with card values set to zero using the values of the suits enumeration for the sprite names.

Type Parameters:
T - the Enum type of the suit
R - the Enum type of the rank
Parameters:
suits - an array of suits
ranks - an array of ranks
cover - the partial filename of the card cover sprite image. The file must reside in the sprites subdirectory and have the extension .gif

Deck

public Deck(T[] suits,
            R[] ranks,
            java.lang.String cover,
            Deck.CardValues cardValues)
Creates a deck from given suit and rank enumerations with given card values using the values of the suits enumeration for the sprite names.

Type Parameters:
T - the Enum type of the suit
R - the Enum type of the rank
Parameters:
suits - an array of suits
ranks - an array of ranks
cover - the partial filename of the card cover sprite image. The file must reside in the sprites subdirectory and have the extension .gif
cardValues - the integer array of card values for each suit; if null is passed, all card values are set to zero

Deck

public Deck(T[] suits,
            R[] ranks,
            S[] sprites,
            java.lang.String cover)
Creates a deck from given suit and rank and sprites enumerations with card values set to zero. The size of the suit and the sprites enumerations must be equal.

Type Parameters:
T - the Enum type of the suit
R - the Enum type of the rank
S - the Enum type of the sprites
Parameters:
suits - an array of suits
ranks - an array of ranks
sprites - an array of sprites; if null is passed, the suits values are used for creating the filenames
cover - the partial filename of the card cover sprite image. The file must reside in the sprites subdirectory and have the extension .gif

Deck

public Deck(T[] suits,
            R[] ranks,
            S[] sprites,
            java.lang.String cover,
            Deck.CardValues cardValues)
Creates a deck from given suit and rank and sprites enumerations with given card values. The size of the suit and the sprites enumerations must be equal.

Type Parameters:
T - the Enum type of the suit
R - the Enum type of the rank
S - the Enum type of the sprites
Parameters:
suits - an array of suits
ranks - an array of ranks
sprites - an array of sprites; if null is passed, the suits values are used for creating the filenames
cover - the partial filename of the card cover sprite image. The file must reside in the sprites subdirectory and have the extension .gif
cardValues - the integer array of card values for each suit; if null is passed, all card values are set to zero
Method Detail

getSeedActor

public <T extends java.lang.Enum<T>,R extends java.lang.Enum<R>> Actor getSeedActor(T suit,
                                                                                    R rank)
Returns the JGameGrid actor attributed to the card with the specified suit and rank.

Parameters:
suit - the suit of the card
rank - the rank of the card
Returns:
a reference to the Actor or null, if the card of given suit and rank is not found

getSuitId

public <T extends java.lang.Enum<T>> int getSuitId(T suit)
Returns an integer id of the given suit (0: for suit with highest priority)

Type Parameters:
T - the type of the suit
Parameters:
suit - the requested suit
Returns:
a priority index starting from 0

getSuit

public java.lang.Enum getSuit(int suitId)
Returns the suit corresponding the given suit id.

Parameters:
suitId - the id of the suit in the order the suit enum is defined.
Returns:
the suit enum value

getRankId

public <R extends java.lang.Enum<R>> int getRankId(R rank)
Returns an integer id of the given rank (0: for rank with highest priority)

Type Parameters:
R - the type of the suit
Parameters:
rank - the requested rank
Returns:
a priority index starting from 0

getRank

public java.lang.Enum getRank(int rankId)
Returns the rank corresponding the given rank id.

Parameters:
rankId - the id of the rank in the order the rank enum is defined.
Returns:
the suit enum value

getSuitId

public int getSuitId(int cardNb)
Returns the suit id for the card with the given card number. The cards are numbered from 0 in the suit and rank priority order given when the deck is created (the following numbering is used: all card from high to low rank of the cards in the highest priority suit, in the next suit, etc.)

Parameters:
cardNb - the card number
Returns:
the suit id of the card

getSuitName

public java.lang.String getSuitName(int cardNb)
Returns the suit name as string for the card with the given card number. The cards are numbered from 0 in the suit and rank priority order given when the deck is created (the following numbering is used: all card from high to low rank of the cards in the highest priority suit, in the next suit, etc.)

Parameters:
cardNb - the card number
Returns:
the suit name of the card

getRankId

public int getRankId(int cardNb)
Returns the rank id for the given card number. The cards are numbered from 0 in the suit and rank priority order given when the deck is created (the following numbering is used: all card from high to low rank of the cards in the highest priority suit, in the next suit, etc.)

Parameters:
cardNb - the card number
Returns:
the rank id of the card

getRankName

public java.lang.String getRankName(int cardNb)
Returns the rank name as string for the given card number The cards are numbered from 0 in the suit and rank priority order given when the deck is created (the following numbering is used: all card from high to low rank of the cards in the highest priority suit, in the next suit, etc.)

Parameters:
cardNb - the card number
Returns:
the rank name of the card

getCardDimension

public java.awt.Dimension getCardDimension()
Returs the card dimensions of the card with suit and rank id zero. All card should have the same dimensions.

Returns:
the dimension of the cards in this deck

toHand

public Hand toHand()
Returns the complete shuffled deck as a hand.

Returns:
all cards of the shuffled deck in one hand

toHand

public Hand toHand(boolean shuffle)
Returns the complete deck as a hand.

Parameters:
shuffle - if true, the cards are shuffle; otherwise they are ordered by rank and then by suit
Returns:
all cards of the deck in one hand

dealingOut

public Hand[] dealingOut(int nbPlayers,
                         int nbCardsPerPlayer)
Same as dealingOut(nbPlayers, nbCardsPerPlayer, shuffle) with shuffle = true.

Parameters:
nbPlayers - the number of players
nbCardsPerPlayer - the number of cards for each player
Returns:
an array with nbPlayers elements of hands. If nbPlayers * nbCardsPerPlayer exceeds the total number of cards, null is returned

dealingOut

public Hand[] dealingOut(int nbPlayers,
                         int nbCardsPerPlayer,
                         boolean shuffle)
Returns an array with nbPlayers + 1 hands. The number of cards for hand[0] to hand[nbPlayers - 1] is equal to the given nbCardsPerPlayer. The remaining cards (if any) are returned at array index nbPlayer. This hand may be used as a card talon (card stock, drawing hand or dead hand). When nbPlayers is zero, all cards are in the talon. If shuffle = false, the the cards are ordered by the priority order given by the enums when the deck is created; otherwise the cards are randomly shuffled.

Parameters:
nbPlayers - the number of players
nbCardsPerPlayer - the number of cards for each player
shuffle - if true, the cards are shuffled; otherwise they are ordered by rank and then by suit
Returns:
an array with nbPlayers elements of hands. If nbPlayers * nbCardsPerPlayer exceeds the total number of cards, null is returned

getCardValues

public int[] getCardValues(java.lang.Enum suit)
Returns an integer array of the card values in the given suit.

Parameters:
suit - the suit from where to get the card values
Returns:
an integer array of the card values in the given suit

enumToStringArray

public static <T extends java.lang.Enum<T>> java.lang.String[] enumToStringArray(T[] values)
Returns the enumeration values as string array for the given enumeration.

Type Parameters:
T - the enumeration type
Parameters:
values - the values of the given enumeration
Returns:
a string array with the enumeration values

getNumberOfSuits

public int getNumberOfSuits()
Returns the number of suits in this deck.

Returns:
the number of suits

getNumberOfRanks

public int getNumberOfRanks()
Returns the number of ranks in this deck.

Returns:
the number of ranks

getNumberOfCards

public int getNumberOfCards()
Returns the total number of cards in this deck.

Returns:
the number of cards

getCardNumber

public <T extends java.lang.Enum<T>,R extends java.lang.Enum<R>> int getCardNumber(T suit,
                                                                                   R rank)
Returns the card number of the card with given suit and rank. Cards are numbered in rank order from suit to suit in the given suit priority:

First suit from 0..nbCardsInSuit-1

Second suit from nbCardsInSuit..2*nbCardsInSuit-1

etc.

Parameters:
suit - the suit of the card
rank - the rank of the card
Returns:
the card number

getHeapSize

public static double getHeapSize()
Returns the current memory heap size.

Returns:
the curren heap size

showHeapSize

public static void showHeapSize()
Displays the current memory heap size in System.out.