|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ch.aplu.jcardgame.Hand
public class Hand
Representation of a card set.
You find a general description of the CardGame library in the Card
class documentation.
Hands are very useful containers for cards, not only for the cards in a
player's hand, but also for storing card piles of any kind on the gambling table.
Even when a hand contains a maximum of one card, it is preferable to
individual cards, because the transfer from one hand to another is very simple.
A hand can be empty (and displayed as such without harm). A hand may also
serve as card store without being displayed during the whole game. It is not
rare to use between 10 to 20 hands in a 4-player game application.
A hand stores the cards in an card list and most hand modifications will
modify this list. A hand may be displayed in the card game (game grid)
window in several layouts: stacked (a card pile, only the top card is seen), in a row,
in a column and as fan.
All hand operation methods will no shown the modifications until draw() is
called. (All of them have a boolean doDraw parameter that can be
set true to call draw() automatically.)
By default, the mouse touches are not enabled. To enable them,
use setTouchEnabled(true). For RowLayout, ColumnLayout and FanLayout
the card is automatically pulled on top of other cards when you left click
on a visible part of the card (unless disabled by putOnTopEnabled(false)).
Card
Nested Class Summary | |
---|---|
static class |
Hand.CardAlignment
Card alignment used when hand is drawn using drawRow(), drawColumn() |
static class |
Hand.SortType
Compare type used for the compareTo() implementation. |
Constructor Summary | |
---|---|
Hand(Deck deck)
Creates a hand instance with game cards from the given deck. |
Method Summary | ||
---|---|---|
void |
act()
For internal use only. |
|
void |
addCardListener(CardListener listener)
Registers a card listener to get event notifications when a card is touched or arrives at its target. |
|
boolean |
contains(Card card)
Returns true, if the a card with same suit and rank is found in the card list of current hand. |
|
void |
cut(int nb,
boolean doDraw)
Divides the current hand (as a deck of cards) into two batches and reassembles the batches in reverse order. |
|
void |
draw()
Displays the hand using the current layout. |
|
|
extractCardsWithRank(R rank)
Returns a hand containing card clones from card of the current hand that have the given rank. |
|
|
extractCardsWithSuit(T suit)
Returns a hand containing card clones from card of the current hand that have the given suit. |
|
Hand[] |
extractPairs()
Returns all hands with pairs (two cards with same rank) found in the current hand. |
|
Hand[] |
extractQuads()
Returns all hands with quads (four cards with same rank) found in the current hand. |
|
Hand[] |
extractSequences(int length)
Returns all sequences found in the current hand with mixed suits with given length (>2). |
|
|
extractSequences(T suit,
int length)
Returns all sequences found in the current hand that have given suit with given length (>2). |
|
Hand[] |
extractTrips()
Returns all hands with trips (three cards with same rank) found in the current hand. |
|
Card |
get(int i)
Returns card reference of the card at index i of the card list or null if the hand is empty. |
|
Card |
getCard(int cardNumber)
Returns card reference of card in this hand with given card number. |
|
|
getCard(T suit,
R rank)
Returns card reference of the card in this hand with given suit and rank. |
|
java.util.ArrayList<Card> |
getCardList()
Returns the card list of the current hand. |
|
|
getCardsWithRank(R rank)
Returns a list containing the card references of all cards with given rank. |
|
|
getCardsWithSuit(T suit)
Returns a list containing the card references of all cards with given suit. |
|
Card |
getFirst()
Returns card reference of the first card or null if the hand is empty. |
|
Location |
getHandLocation()
Returns a clone of the hand location. |
|
Card |
getLast()
Returns card reference of the last card or null if the hand is empty. |
|
HandLayout |
getLayout()
Returns the reference of the current HandLayout. |
|
int |
getMaxPosition(Hand.SortType sortType)
Compares the cards in the card list using the given compare type. |
|
int |
getNumberOfCards()
Returns the number of cards in the current hand. |
|
|
getNumberOfCardsWithRank(R rank)
Returns the number of cards of given rank in the current hand. |
|
|
getNumberOfCardsWithSuit(T suit)
Returns the number of cards of given suit in the current hand. |
|
java.util.ArrayList<Card[]> |
getPairs()
Returns all pairs (two cards with same rank) found in the current hand. |
|
java.util.ArrayList<Card[]> |
getQuads()
Returns all quads (four cards with same rank) found in the current hand. |
|
int |
getScore()
Returns the sum of the card values (card points) of all card in the hand. |
|
java.util.ArrayList<Card[]> |
getSequences(int length)
Returns all sequences found in the current hand with mixed suits with given length (>2). |
|
|
getSequences(T suit,
int length)
Returns all sequences found in the current hand that have given suit with given length (>2). |
|
Hand.SortType |
getSortType()
Returns the compare type for cards used for sorting. |
|
TargetArea |
getTargetArea()
Returns a reference to the current target area. |
|
java.util.ArrayList<Card[]> |
getTrips()
Returns all trips (three cards with same rank) found in the current hand. |
|
boolean |
insert(Card card,
boolean doDraw)
Inserts the given card in the current hand (reference copy) and sets the hand of the given card to the current hand. |
|
boolean |
insert(Hand hand,
boolean doDraw)
Inserts all cards of the given hand in the current hand (reference copy) and sets the hand of the cards to the current hand. |
|
boolean |
insert(int cardNumber,
boolean doDraw)
Creates a new card with given card number and inserts it in the current hand. |
|
|
insert(T suit,
R rank,
boolean doDraw)
Creates a new card with given suit and rank and inserts it in the current hand. |
|
boolean |
isEmpty()
Returns true, if the card list of the current hand is empty. |
|
boolean |
isTouchEnabled()
Returns the current state of mouse touches. |
|
void |
mouseTouched(Actor actor,
GGMouse mouse,
java.awt.Point spot)
Implementation of GGMouseTouchListener. |
|
void |
putOnTop(Card topCard)
Draws the given card above each other card (on top) in this hand and rearrages the hand appearence so that all card are shown. |
|
void |
putOnTopEnabled(boolean enable)
Enable/disables automatic putOnTop when the card is left-clicked. |
|
static void |
randomBatchTransfer(int nb,
Hand source,
Hand target,
boolean doDraw)
Moves a randomly selected batch of cards from the source hand to the target hand. |
|
boolean |
remove(Card card,
boolean doDraw)
Removes card from the current hand. |
|
boolean |
remove(int index,
boolean doDraw)
Removes the card at given index from card list. |
|
void |
removeAll(boolean doRefresh)
Removes every card in the current hand and from the game grid. |
|
void |
removeFirst(boolean doDraw)
Removes first card from card list. |
|
void |
removeLast(boolean doDraw)
Removes last card from card list. |
|
Card |
reverse(boolean doDraw)
Reverses the order of the cards in the card list. |
|
Card |
reverseSort(Hand.SortType sortType,
boolean doDraw)
Sorts the card list with given sort type and reverses the list. |
|
void |
setCardGame(CardGame cardGame)
Selects the card game instance where to display the hand's card. |
|
void |
setSortType(Hand.SortType sortType)
Sets the compare type of cards used for sorting to one of the enumeration values defined in the SortType enumeration. |
|
void |
setTargetArea(TargetArea targetArea)
Sets the transferring attributes for all cards in the current hand. |
|
void |
setTouchEnabled(boolean enable)
Enables or disables mouse touch events. |
|
void |
setVerso(boolean isVerso)
Sets the isVerso attribute to all cards in the card list. |
|
void |
setView(CardGame cardGame,
HandLayout handLayout)
Preparing to display the current hand in a game grid window using the given given layout. |
|
Card |
shift(boolean forward,
boolean doDraw)
Shifts (rolls) the card list forward or backward. |
|
Card |
shuffle(boolean doDraw)
Shuffles the card list (random permutation of cards). |
|
Card |
sort(Hand.SortType sortType,
boolean doDraw)
Sorts the card list with given sort type. |
|
java.lang.String |
toString()
Returns a string representation of all cards in the hand. |
|
void |
transfer(Card card,
Hand targetHand,
boolean doDraw)
Transfers the card from the current hand to the given target hand using the currently defined target area. |
|
void |
transferNonBlocking(Card card,
Hand targetHand,
boolean doDraw)
Same as transfer(card, targetHand, doDraw), but the methods returs immediately. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Hand(Deck deck)
deck
- the deck where suit, rank and points are defined.Method Detail |
---|
public void setCardGame(CardGame cardGame)
cardGame
- the reference to the card gamepublic void mouseTouched(Actor actor, GGMouse mouse, java.awt.Point spot)
mouseTouched
in interface GGMouseTouchListener
actor
- the reference of the actor that reports the touchmouse
- the mouse reference to get to get information about the eventspot
- the pixel coordinates relative to the mouse touch area where the event occurredpublic void putOnTopEnabled(boolean enable)
enable
- if true, the card is automatically put on top when clicked;
otherwise the card must be manually put on top using one of the mouse
eventspublic void putOnTop(Card topCard)
topCard
- the card to put on toppublic void addCardListener(CardListener listener)
listener
- the CardListener to registerpublic int getScore()
public java.lang.String toString()
toString
in class java.lang.Object
public boolean insert(Hand hand, boolean doDraw)
hand
- the hand from where to import the cardsdoDraw
- if true, the current hand is drawn
public boolean insert(Card card, boolean doDraw)
card
- the card to insertdoDraw
- if true, the current hand is drawn (showing the inserted card)
public boolean insert(int cardNumber, boolean doDraw)
cardNumber
- the card number of the card to insertdoDraw
- if true, the current hand is drawn (showing the inserted card)
public <T extends java.lang.Enum<T>,R extends java.lang.Enum<R>> boolean insert(T suit, R rank, boolean doDraw)
T
- the Enum type of the suitR
- the Enum type of the ranksuit
- the card suitrank
- the card rankdoDraw
- if true, the current hand is drawn (showing the inserted card)
public void removeFirst(boolean doDraw)
doDraw
- if true, the current hand is drawn
(not showing the card anymore)remove(Card card, boolean doDraw)
public void removeLast(boolean doDraw)
doDraw
- if true, the current hand is drawn
(not showing the card anymore)remove(Card card, boolean doDraw)
public boolean remove(int index, boolean doDraw)
index
- the index of the card to removedoDraw
- if true, the current hand is drawn
(not showing the card anymore)
remove(Card card, boolean doDraw)
public boolean remove(Card card, boolean doDraw)
card
- the card to remove; if the card is not in the hand, nothing happensdoDraw
- if true, the current hand is drawn
(not showing the card anymore)
public void removeAll(boolean doRefresh)
doRefresh
- if true, the game grid is refreshed, so that the
hand becomes immediately invisiblepublic boolean contains(Card card)
card
- the card to check
public boolean isEmpty()
public java.util.ArrayList<Card> getCardList()
public int getNumberOfCards()
public <T extends java.lang.Enum<T>> int getNumberOfCardsWithSuit(T suit)
suit
- the suit of the cards
public <R extends java.lang.Enum<R>> int getNumberOfCardsWithRank(R rank)
rank
- the rank of the cards
public Card get(int i)
public Card getFirst()
public Card getLast()
public void setVerso(boolean isVerso)
isVerso
- if true, all cards are shown face down; otherwise they are
shown face uppublic void setTargetArea(TargetArea targetArea)
targetArea
- the TargetArea used for the transferpublic TargetArea getTargetArea()
public void act()
act
in interface GGActListener
public Card sort(Hand.SortType sortType, boolean doDraw)
doDraw
- if true, the hand is drawn
public Card reverseSort(Hand.SortType sortType, boolean doDraw)
sortType
- the card compare type used for sortingdoDraw
- if true, the hand is drawn
public Card shift(boolean forward, boolean doDraw)
forward
- if true, shifts to the right (forward); otherwise
shifts to the left (backward)doDraw
- if true, the hand is drawn
public Card reverse(boolean doDraw)
doDraw
- if true, the hand is drawn
public Card shuffle(boolean doDraw)
doDraw
- if true, the hand is drawn
public void setTouchEnabled(boolean enable)
enable
- if true, the mouse touches are enabled; otherwise disabledpublic boolean isTouchEnabled()
public void setSortType(Hand.SortType sortType)
sortType
- the compare type used for sortingpublic Hand.SortType getSortType()
public int getMaxPosition(Hand.SortType sortType)
sortType
- the compare type to apply
public void setView(CardGame cardGame, HandLayout handLayout)
cardGame
- the reference of the CardGame where to display the handhandLayout
- the reference to a HandLayout instance; may be null, if
the hand is never drawn, but the CardGame reference should be setpublic void draw()
public <T extends java.lang.Enum<T>> Hand extractCardsWithSuit(T suit)
T
- the type of the suitsuit
- the requested suit
public <T extends java.lang.Enum<T>> java.util.ArrayList<Card> getCardsWithSuit(T suit)
T
- the type of the suitsuit
- the requested suit
public <R extends java.lang.Enum<R>> Hand extractCardsWithRank(R rank)
R
- the type of the rankrank
- the requested rank
public <R extends java.lang.Enum<R>> java.util.ArrayList<Card> getCardsWithRank(R rank)
R
- the type of the rankrank
- the requested rank
public java.util.ArrayList<Card[]> getSequences(int length)
length
- the length of the requested sequence
public Hand[] extractSequences(int length)
length
- the length of the requested sequence
public <T extends java.lang.Enum<T>> java.util.ArrayList<Card[]> getSequences(T suit, int length)
T
- the type of the suitsuit
- the requested suitlength
- the length of the requested sequence
public <T extends java.lang.Enum<T>> Hand[] extractSequences(T suit, int length)
T
- the type of the suitsuit
- the requested suitlength
- the length of the requested sequence
public java.util.ArrayList<Card[]> getPairs()
public java.util.ArrayList<Card[]> getTrips()
public java.util.ArrayList<Card[]> getQuads()
public Hand[] extractPairs()
public Hand[] extractTrips()
public Hand[] extractQuads()
public HandLayout getLayout()
public Location getHandLocation()
public void cut(int nb, boolean doDraw)
nb
- the number of cards in the first part. If less than one or greater
than the number of cards nothing happensdoDraw
- if true, the hand is drawnpublic static void randomBatchTransfer(int nb, Hand source, Hand target, boolean doDraw)
nb
- the number of cards to take out. If nb less or equals zero,
an empty hand is returned; if nb is greater or equal to the number of
cards in the given hand, a hand with all cards is returnedsource
- the hand where to take the cardstarget
- the hand where to insert the cardsdoDraw
- if true, the source hand and the target hand are redrawnpublic void transfer(Card card, Hand targetHand, boolean doDraw)
card
- the card to transfertargetHand
- the hand where to transfer the carddoDraw
- if true, the current hand and the target hand are drawnTargetArea
public void transferNonBlocking(Card card, Hand targetHand, boolean doDraw)
card
- the card to transfertargetHand
- the hand where to transfer the carddoDraw
- if true, the current hand and the target hand are drawnpublic Card getCard(int cardNumber)
cardNumber
- the card number of the requested card
public <T extends java.lang.Enum<T>,R extends java.lang.Enum<R>> Card getCard(T suit, R rank)
T
- the Enum type of the suitR
- the Enum type of the ranksuit
- the card suitrank
- the card rank
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |