de.uni_paderborn.robots.robotArenaInterface
Class CardData

java.lang.Object
  |
  +--de.uni_paderborn.robots.robotArenaInterface.ItemData
        |
        +--de.uni_paderborn.robots.robotArenaInterface.CardData
All Implemented Interfaces:
CardConstants, java.lang.Cloneable, java.lang.Comparable

public final class CardData
extends ItemData
implements java.lang.Comparable, java.lang.Cloneable, CardConstants


This class represents the cards of a common poker game.
In a poker game a card has a color and a value.
Colors: DIAMOND, HEART, SPADE, CLUB
Values: TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, LADY, KING, ACE

Poker has 10 card combinations. If the card combinations of two hands differ then the hand with the higher card combination has the higher score.
Card combinations in ascending order from lowest to highest :

  • NOTHING
  • ONE_PAIR
  • TWO_PAIRS
  • THREE_OF_A_KIND
  • STRAIGHT
  • FLUSH
  • FULL_HOUSE
  • FOUR_OF_A_KIND
  • STRAIGHT_FLUSH
  • ROYAL_FLUSH
    All these values are defined in the interface CardConstants. Implement the interface in your class to use these values.

    The static method getHandValue(CardData[]) can be used to calculate a poker score.

    See Also:
    PutAction, ExchangeAction, RobotData

    Field Summary
    private static byte CARD_BASE
              base used to calculate the score
    private  byte color
              color of card
    private static boolean DEBUG
              constant controlling DEBUG output
    private  byte value
              value of the card
     
    Fields inherited from interface de.uni_paderborn.robots.robotArenaInterface.CardConstants
    ACE, CLUB, DIAMOND, EIGHT, FIVE, FLUSH, FOUR, FOUR_OF_A_KIND, FULL_HOUSE, HEART, JACK, KING, LADY, NINE, NOTHING, ONE_PAIR, ROYAL_FLUSH, SEVEN, SIX, SPADE, STRAIGHT, STRAIGHT_FLUSH, TEN, THREE, THREE_OF_A_KIND, TWO, TWO_PAIRS
     
    Constructor Summary
    CardData(byte color, byte value)
              
    Constructs a new CardData object with specified value and color.

     
    Method Summary
    static CardData[] cardSort(CardData[] cards)
              Constructs a sorted and cloned array of CardData objects.
     java.lang.Object clone()
              Constructs an exact copy of this CardData object.
     int compareTo(java.lang.Object o)
              Compares two CardData objects.
    private static byte getCardScore(CardData card)
              Calculates the score of a reference to CardData.
     byte getColor()
              Returns the color of this card.
    static int getHandValue(CardData[] hand)
              Calculates a score which is based on poker rules.
     byte getValue()
              Returns the value of this card.
    static java.lang.String stringTypeOfScore(int score)
              Converts a score value into a typeOfScore which is converted into a readable String.
     java.lang.String toString()
              Returns the string representation of this CardData object.
    static int typeOfScore(int score)
              Calculate the type of the score.
     
    Methods inherited from class java.lang.Object
    , equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
     

    Field Detail

    CARD_BASE

    private static final byte CARD_BASE
    base used to calculate the score

    DEBUG

    private static final boolean DEBUG
    constant controlling DEBUG output

    value

    private byte value
    value of the card

    color

    private byte color
    color of card
    Constructor Detail

    CardData

    public CardData(byte color,
                    byte value)
             throws java.lang.IllegalArgumentException

    Constructs a new CardData object with specified value and color.

    Parameters:
    color - color of the new card ( HEART, DIAMOND, SPADE or CLUB )
    value - value of the new card ( TWO, ..., TEN, JACK, LADY, KING, ACE )
    Throws:
    java.lang.IllegalArgumentException - If color or value are out of bounds.
    See Also:
    CardConstants
    Method Detail

    getColor

    public byte getColor()
    Returns the color of this card.
    Returns:
    color of the card ( HEART, DIAMOND, SPADE or CLUB )
    See Also:
    CardConstants

    getValue

    public byte getValue()
    Returns the value of this card.
    Returns:
    value of the card ( TWO, ..., TEN, JACK, LADY, KING, ACE )
    See Also:
    CardConstants

    getCardScore

    private static byte getCardScore(CardData card)
    Calculates the score of a reference to CardData. If card is null then 0 is returned. Else the value of the CardData object minus one is returned.
    Parameters:
    card - CardData reference to be checked.
    Returns:
    0..ACE-1
    See Also:
    getHandValue(de.uni_paderborn.robots.robotArenaInterface.CardData[]), CardConstants

    compareTo

    public int compareTo(java.lang.Object o)
    Compares two CardData objects.

    a.compareTo(b) is
    negativefor a < b
    0for a == b
    positivefor a > b

    CardDatais ordered by Value and then by Color.

    Specified by:
    compareTo in interface java.lang.Comparable
    Returns:
    a negative integer, zero, or a positive integer as this CardData is less than, equal to, or greater than the specified object.
    See Also:
    Comparable

    clone

    public java.lang.Object clone()
    Constructs an exact copy of this CardData object.
    Overrides:
    clone in class java.lang.Object
    Returns:
    An exact copy of this CardData object.
    See Also:
    Cloneable

    cardSort

    public static CardData[] cardSort(CardData[] cards)
    Constructs a sorted and cloned array of CardData objects.
    Note : Only the array is cloned not the CardData objects. compareTo(Object) is used to sort the cards in ascending order.
    Parameters:
    cards - Must not be null. The first 5, different CardData objects in the array are sorted and returned.
    Returns:
    A sorted array containing a maximum of 5 CardData objects. The array contains no null references. If no cards are found null is returned.
    See Also:
    compareTo(java.lang.Object)

    getHandValue

    public static int getHandValue(CardData[] hand)
    Calculates a score which is based on poker rules.

    Enhancement of the poker rules

    In a poker game every player has five cards.
    This method can calculate a well-defined poker-score for less than 5 cards!
    A hand containing less than five cards can still have a higher score than a hand which contains five cards if and only if the latter hand has a lower combination. The combinations always have a bigger effect for the score than the number of cards on the hand.
    If the combinations are the same or there are no combinations at all then the number of cards has a bigger effect than the values of the normal cards.

    Examples
    hand 1 [CLUB,ACE], [DIAMOND,FOUR]
    hand 2 [DIAMOND,THREE], [SPADE,THREE]
    In this example hand 2 wins as it has a ONE_PAIR combination which hand 1 lacks.

    hand 1 [CLUB,ACE]
    hand 2 [DIAMOND,THREE], [DIAMOND,TWO]
    In this example hand 2 wins as it contains more cards !

    Result is the following "Order of importance"
       1) combination
       2) values of the cards in combinations
       3) number of cards
       4) values of the normal cards
    Returns:
    A value between 0 and nearly (ROYAL_FLUSH+1)*14^5
    See Also:
    CardConstants, cardSort(de.uni_paderborn.robots.robotArenaInterface.CardData[])

    typeOfScore

    public static int typeOfScore(int score)
    Calculate the type of the score.
    Returns:
    NOTHING, ONE_PAIR, TWO_PAIRS, ... or ROYAL_FLUSH
    See Also:
    CardConstants

    stringTypeOfScore

    public static java.lang.String stringTypeOfScore(int score)
    Converts a score value into a typeOfScore which is converted into a readable String. The method typeOfScore(int) is called to determine the type of the score.
    Returns:
    "Nothing", "One Pair", "Two Pairs", ... or "Royal Flush"

    toString

    public java.lang.String toString()
    Returns the string representation of this CardData object.
    Overrides:
    toString in class java.lang.Object
    Returns:
    for example "[Diamond,Jack]" or "[Spade,King]"