smithers.cards
Class Card

java.lang.Object
  extended by smithers.cards.Card

public class Card
extends java.lang.Object

Class to store a playing card from a standard 52-card poker or bridge deck. A card consists of a rank (Ace, 2-10, Jack, Queen or King) and a suit (Clubs, Diamonds, Hearts or Spades). Instances of this class are immutable.


Field Summary
static char[] RANKS
           
static char[] SUITS
           
 
Constructor Summary
Card()
          Constructs the Ace of Clubs.
Card(char rank, char suit)
          Creates a card with given rank and suit both specified as char.
Card(int index)
          Creates a card from a single integer in the range 0-51.
Card(int rank, char suit)
          Creates a card with given rank and suit.
Card(int rank, int suit)
          Creates a card with given rank and suit both specified as int.
 
Method Summary
 int compareRank(Card card)
          Finds the signed difference between the ranks of 2 cards.
 boolean equals(java.lang.Object o)
          Checks if another card is the same as this one.
 char getColor()
          Gets the colour of this card.
 char getColour()
          Gets the colour of this card.
 int getRank()
          Gets the rank of this card.
 char getRankAsChar()
          Gets the rank of this card as a char.
 int getRankAsInt()
          Gets the rank of this card as an int.
 java.lang.String getRankAsString()
          Gets the rank of this card as a String.
 char getSuit()
          Gets the suit of this card.
 char getSuitAsChar()
          Gets the suit of this card as a char.
 int getSuitAsInt()
          Gets the suit of this card as an int.
 java.lang.String getSuitAsString()
          Gets the suit of this card as a String.
 boolean sameColor(Card card)
          Checks if another Card is the same colour as this one.
 boolean sameColour(Card card)
          Checks if another Card is the same colour as this one.
 boolean sameRank(Card card)
          Checks if another Card has the same rank as this one.
 boolean sameSuit(Card card)
          Checks if another Card has the same suit as this one.
 int toInt()
          Produces an integer representation of the card based on suit then rank.
 int toInt(boolean aceHigh)
          Produces an integer representation of the card based on suit then rank.
 java.lang.String toString()
          Produces a 2 character String representing this card.
 java.lang.String toStringFull()
          Produces a String representation of this card.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SUITS

public static final char[] SUITS

RANKS

public static final char[] RANKS
Constructor Detail

Card

public Card()
Constructs the Ace of Clubs.


Card

public Card(int rank,
            char suit)
Creates a card with given rank and suit.

Parameters:
rank - the rank of the card (1=A, 11=J, 12=Q, 13=K)
suit - the suit of the card (C, D, H, S)

Card

public Card(int rank,
            int suit)
Creates a card with given rank and suit both specified as int.

Parameters:
rank - the rank of the card (1=A, 11=J, 12=Q, 13=K)
suit - the suit of the card (0=C, 1=D, 2=H, 3=S)

Card

public Card(char rank,
            char suit)
Creates a card with given rank and suit both specified as char.

Parameters:
rank - the rank of the card (A, 2-9, T, J, Q, K)
suit - the suit of the card (C, D, H, S)

Card

public Card(int index)
Creates a card from a single integer in the range 0-51.

Parameters:
index - a the index of the card from 0-51
Method Detail

getRank

public int getRank()
Gets the rank of this card.

Returns:
the rank as an int

getRankAsChar

public char getRankAsChar()
Gets the rank of this card as a char.

Returns:
the rank as a char

getRankAsInt

public int getRankAsInt()
Gets the rank of this card as an int.

Returns:
the rank as an int

getRankAsString

public java.lang.String getRankAsString()
Gets the rank of this card as a String.

Returns:
a String representing the rank

getSuit

public char getSuit()
Gets the suit of this card.

Returns:
the suit as a char

getSuitAsChar

public char getSuitAsChar()
Gets the suit of this card as a char.

Returns:
the suit as a char

getSuitAsInt

public int getSuitAsInt()
Gets the suit of this card as an int.

Returns:
the suit as an int

getSuitAsString

public java.lang.String getSuitAsString()
Gets the suit of this card as a String.

Returns:
a String representing the suit

getColour

public char getColour()
Gets the colour of this card.

Returns:
'B' or 'R' depending on the suit

getColor

public char getColor()
Gets the colour of this card.

Returns:
'B' or 'R' depending on the suit
See Also:
getColour()

toString

public java.lang.String toString()
Produces a 2 character String representing this card. The first character is the rank and the second is the suit (e.g. "5C").

Overrides:
toString in class java.lang.Object
Returns:
a 2 character String represention of this card

toStringFull

public java.lang.String toStringFull()
Produces a String representation of this card. The output is of the form "rank of suit" (e.g. "Five of Clubs").

Returns:
a String represention of this card

toInt

public int toInt()
Produces an integer representation of the card based on suit then rank. Gives aces the lowest value for a given suit.

Returns:
an integer representing this card
See Also:
toInt(boolean)

toInt

public int toInt(boolean aceHigh)
Produces an integer representation of the card based on suit then rank. Optionally gives aces the highest or lowest value in their suit.

Parameters:
aceHigh - true if aces should be ranked high
Returns:
an integer representing this card

equals

public boolean equals(java.lang.Object o)
Checks if another card is the same as this one. Cards are equal iff they have the same suit and rank.

Overrides:
equals in class java.lang.Object
Parameters:
o - the Object to test against
Returns:
true if o is a Card and represents the same card as this one

sameRank

public boolean sameRank(Card card)
Checks if another Card has the same rank as this one.

Parameters:
card - the Card to test against
Returns:
true if the cards have the same rank

sameSuit

public boolean sameSuit(Card card)
Checks if another Card has the same suit as this one.

Parameters:
card - the Card to test against
Returns:
true if the cards have the same suit

sameColour

public boolean sameColour(Card card)
Checks if another Card is the same colour as this one.

Parameters:
card - the Card to test against
Returns:
true if the cards are the same colour

sameColor

public boolean sameColor(Card card)
Checks if another Card is the same colour as this one.

Parameters:
card - the Card to test against
Returns:
true if the cards are the same colour
See Also:
sameColour(smithers.cards.Card)

compareRank

public int compareRank(Card card)
Finds the signed difference between the ranks of 2 cards. The result is positive if this card has the higher rank. Treats aces as low.

Parameters:
card - Card to test against
Returns:
the difference between the ranks