ks.common.model
Class Card

java.lang.Object
  extended byks.common.model.Element
      extended byks.common.model.Card

public class Card
extends Element

Representation of a Card in the model.

The valid suits for a Card are {CLUBS, DIAMONDS, HEARTS, SPADES}, and the valid ranks are 1 (ACE) through 13 (KING). Note that the rank for ACE is lower than other cards. There is a method boolean isAce() that can be used to see if a Card object is an ACE. boolean isFaceCard() returns whether a Card object is a JACK, QUEEN, or KING.

Once created, a Card may have several state changes: Rank, Suit, FaceUp, Selected. In each case, the hasChanged() method for Element is invoked after the change to announce the change to any viewers.

The name for a Card element is determined by its value. There is no way to set the name for a card except by altering its suit and/or rank.


Field Summary
static int ACE
          Specific rank value of an ACE
static java.lang.String ACEabbreviation
          String abbreviation of Ace.
static int CLUBS
          Specific suit value of Clubs
static java.lang.String CLUBSabbreviation
          String abbreviation of Clubs.
static java.lang.String CLUBSname
          String name of Clubs.
static int DIAMONDS
          Specific suit value of Diamonds
static java.lang.String DIAMONDSabbreviation
          String abbreviation of Diamonds.
static java.lang.String DIAMONDSname
          String name of Diamonds.
static int EIGHT
          Specific rank value of an Eight.
static int FIVE
          Specific rank value of a Five
static int FOUR
          Specific rank value of a Four
static int HEARTS
          Specific suit value of Hearts.
static java.lang.String HEARTSabbreviation
          String abbreviation of Hearts.
static java.lang.String HEARTSname
          String name of HEARTS.
static int JACK
          Specific rank value of a Jack
static java.lang.String JACKabbreviation
          String abbreviation of Jack.
static int KING
          Specific rank value of a King
static java.lang.String KINGabbreviation
          String abbreviation of King.
static java.lang.String KINGstring
          String representation of King.
static int NINE
          Specific rank value of a Nine
static int QUEEN
          Specific rank value of a Queen
static java.lang.String QUEENabbreviation
          String abbreviation of Queen.
static char selectedChar
          Character used in string representation to denote selected Card.
static int SEVEN
          Specific rank value of a Seven
static int SIX
          Specific rank value of a Six
static int SPADES
          Specific suit value of Spades.
static java.lang.String SPADESabbreviation
          String abbreviation of Spades.
static java.lang.String SPADESname
          String name of Spades.
static int TEN
          Specific rank value of a Ten
static int THREE
          Specific rank value of a Three
static int TWO
          Specific rank value of a Two
 
Constructor Summary
Card(Card c)
          Construct a card that is a copy of the given Card.
Card(int rank, int suit)
          Construct a card with the given rank and suit.
 
Method Summary
 int compareTo(Card c)
          Compares cards based on ranks.
 java.lang.String getName()
          Return a string reflective of this Card.
 int getRank()
          Return the rank for this card.
 int getSuit()
          Return the suit for this card.
static java.lang.String getSuitName(int suit)
          Static method for converting a specific suit identifier into its String representation.
 boolean isAce()
          Determine whether the card is an ACE.
 boolean isFaceCard()
          Determine whether the Card is a Face Card (Jack, Queen, King).
 boolean isFaceUp()
          Determine whether the Card is face up.
 boolean isSelected()
          Determine whether the card has been selected.
 boolean oppositeColor(Card c)
          Determines whether the two cards have opposite colors.
 boolean oppositeColor(int otherSuit)
          Determines whether the Card has an opposite color than the given suit.
 boolean sameColor(Card c)
          Determine whether the two cards have the same color.
 boolean sameColor(int otherSuit)
          Determines whether the Card has the same color as the given suit.
 boolean sameRank(Card c)
          Determine whether two cards have the same rank.
 boolean sameSuit(Card c)
          Determine whether the two cards have the same suit.
 void setFaceUp(boolean newFaceUp)
          Set the faceUp value of this card.
 void setName(java.lang.String s)
          Deny any request to change the name of a card.
 void setRank(int newRank)
          Set the rank value of this card.
 void setSelected(boolean newSelected)
          Set the selected status of this card.
 void setSuit(int newSuit)
          Set the suit value of this card.
 java.lang.String toString()
          Return a string reflective of this Card.
static java.lang.String toString(int rank, int suit)
          Static method for converting a specific rank/suit into its String representation.
 
Methods inherited from class ks.common.model.Element
getListener, setListener
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ACE

public static final int ACE
Specific rank value of an ACE

See Also:
Constant Field Values

TWO

public static final int TWO
Specific rank value of a Two

See Also:
Constant Field Values

THREE

public static final int THREE
Specific rank value of a Three

See Also:
Constant Field Values

FOUR

public static final int FOUR
Specific rank value of a Four

See Also:
Constant Field Values

FIVE

public static final int FIVE
Specific rank value of a Five

See Also:
Constant Field Values

SIX

public static final int SIX
Specific rank value of a Six

See Also:
Constant Field Values

SEVEN

public static final int SEVEN
Specific rank value of a Seven

See Also:
Constant Field Values

EIGHT

public static final int EIGHT
Specific rank value of an Eight.

See Also:
Constant Field Values

NINE

public static final int NINE
Specific rank value of a Nine

See Also:
Constant Field Values

TEN

public static final int TEN
Specific rank value of a Ten

See Also:
Constant Field Values

JACK

public static final int JACK
Specific rank value of a Jack

See Also:
Constant Field Values

QUEEN

public static final int QUEEN
Specific rank value of a Queen

See Also:
Constant Field Values

KING

public static final int KING
Specific rank value of a King

See Also:
Constant Field Values

CLUBS

public static final int CLUBS
Specific suit value of Clubs

See Also:
Constant Field Values

DIAMONDS

public static final int DIAMONDS
Specific suit value of Diamonds

See Also:
Constant Field Values

HEARTS

public static final int HEARTS
Specific suit value of Hearts.

See Also:
Constant Field Values

SPADES

public static final int SPADES
Specific suit value of Spades.

See Also:
Constant Field Values

ACEabbreviation

public static final java.lang.String ACEabbreviation
String abbreviation of Ace.

See Also:
Constant Field Values

KINGstring

public static final java.lang.String KINGstring
String representation of King.

See Also:
Constant Field Values

KINGabbreviation

public static final java.lang.String KINGabbreviation
String abbreviation of King.

See Also:
Constant Field Values

QUEENabbreviation

public static final java.lang.String QUEENabbreviation
String abbreviation of Queen.

See Also:
Constant Field Values

JACKabbreviation

public static final java.lang.String JACKabbreviation
String abbreviation of Jack.

See Also:
Constant Field Values

CLUBSabbreviation

public static final java.lang.String CLUBSabbreviation
String abbreviation of Clubs.

See Also:
Constant Field Values

CLUBSname

public static final java.lang.String CLUBSname
String name of Clubs.

See Also:
Constant Field Values

DIAMONDSabbreviation

public static final java.lang.String DIAMONDSabbreviation
String abbreviation of Diamonds.

See Also:
Constant Field Values

DIAMONDSname

public static final java.lang.String DIAMONDSname
String name of Diamonds.

See Also:
Constant Field Values

HEARTSabbreviation

public static final java.lang.String HEARTSabbreviation
String abbreviation of Hearts.

See Also:
Constant Field Values

HEARTSname

public static final java.lang.String HEARTSname
String name of HEARTS.

See Also:
Constant Field Values

SPADESabbreviation

public static final java.lang.String SPADESabbreviation
String abbreviation of Spades.

See Also:
Constant Field Values

SPADESname

public static final java.lang.String SPADESname
String name of Spades.

See Also:
Constant Field Values

selectedChar

public static final char selectedChar
Character used in string representation to denote selected Card.

See Also:
Constant Field Values
Constructor Detail

Card

public Card(int rank,
            int suit)
Construct a card with the given rank and suit.

By default, the card is face up and not selected. Since v1.7, the name is calculated on demand in getName(), overridden by this class

Throws:
java.lang.IllegalArgumentException - if rank or suit is an invalid value.

Card

public Card(Card c)
Construct a card that is a copy of the given Card.

This includes faceup and selected Information (since V1.5.1). Since v1.7, the name is calculated on demand by getName(), overriden by this class.

Parameters:
c - ks.common.model.Card
Method Detail

compareTo

public int compareTo(Card c)
Compares cards based on ranks. If returns zero, then cards have the same rank. If a negative number is returned, existing card is lesser than target by that amount; similar for positive results.

If c is a null card, then Integer.MAX_VALUE is returned.

Parameters:
c - ks.common.model.Card
Returns:
int

getName

public java.lang.String getName()
Return a string reflective of this Card.

This method overrides getName() method in Element.

Overrides:
getName in class Element
Returns:
String
Since:
V1.7

getRank

public int getRank()
Return the rank for this card.

Returns:
int

getSuit

public int getSuit()
Return the suit for this card.

Returns:
int

isAce

public boolean isAce()
Determine whether the card is an ACE.

Returns:
boolean
Since:
v1.7

isFaceCard

public boolean isFaceCard()
Determine whether the Card is a Face Card (Jack, Queen, King).

Returns:
boolean
Since:
V1.7

isFaceUp

public boolean isFaceUp()
Determine whether the Card is face up.

Returns:
boolean

isSelected

public boolean isSelected()
Determine whether the card has been selected.

Returns:
boolean

oppositeColor

public boolean oppositeColor(int otherSuit)
Determines whether the Card has an opposite color than the given suit.

If suit is not a valid suit, then an IllegalArgumentException is thrown.

Parameters:
otherSuit - suit to which we are being compared.
Returns:
boolean
Since:
V2.0

oppositeColor

public boolean oppositeColor(Card c)
Determines whether the two cards have opposite colors.

Parameters:
c - ks.common.model.Card
Returns:
boolean

sameColor

public boolean sameColor(Card c)
Determine whether the two cards have the same color.

Parameters:
c - ks.common.model.Card
Returns:
boolean

sameColor

public boolean sameColor(int otherSuit)
Determines whether the Card has the same color as the given suit.

Parameters:
otherSuit - other suit to which we are being compared.
Returns:
boolean
Since:
V2.0

sameRank

public boolean sameRank(Card c)
Determine whether two cards have the same rank.

If the card passed in is null, then false is returned.

Parameters:
c - ks.common.model.Card
Returns:
boolean

sameSuit

public boolean sameSuit(Card c)
Determine whether the two cards have the same suit.

If the card passed in is null, then false is returned.

Parameters:
c - ks.common.model.Card
Returns:
boolean

setFaceUp

public void setFaceUp(boolean newFaceUp)
Set the faceUp value of this card.

Generates modelChanged action if new faceUp status is different from old.

Parameters:
newFaceUp - boolean

setName

public void setName(java.lang.String s)
Deny any request to change the name of a card.

This is here because we override the setName() method of Element.

Overrides:
setName in class Element
Parameters:
s - proposed new name.
Since:
V1.7

setRank

public void setRank(int newRank)
Set the rank value of this card.

Generates modelChanged action if new rank is different from old rank.

Parameters:
newRank - int

setSelected

public void setSelected(boolean newSelected)
Set the selected status of this card.

Generates modelChanged action if new selected state is different from old selected state.

Parameters:
newSelected - boolean

setSuit

public void setSuit(int newSuit)
Set the suit value of this card.

Generates modelChanged action if new suit is different from old suit.

Parameters:
newSuit - int

toString

public java.lang.String toString()
Return a string reflective of this Card. If the card is faceDown, then the name of card is returned in brackets, i.e., "[10H]". If the card is selected, then the string is appended with a "*" character.

Creation date: (10/1/01 8:50:08 PM)

Overrides:
toString in class Element
Returns:
java.lang.String
Since:
V1.7 returns state information also. To return just the name, use getName().

getSuitName

public static java.lang.String getSuitName(int suit)
Static method for converting a specific suit identifier into its String representation.

Parameters:
suit - int
Returns:
java.lang.String
Since:
V2.0

toString

public static java.lang.String toString(int rank,
                                        int suit)
Static method for converting a specific rank/suit into its String representation. Creation date: (10/1/01 8:53:13 PM)

Parameters:
rank - int
suit - int
Returns:
java.lang.String
Since:
V1.5.1 throws IllegalArgumentException on improper input