Class Card

java.lang.Object
com.github.jmp.poker.Card

public class Card extends Object
Represents an immutable playing card from a standard 52-card deck.

Each card consists of a Rank and a Suit, and maintains an internal integer value for efficient poker hand evaluation. The internal value uses a specific bit layout for fast comparison and evaluation operations.

Cards can be created from their rank and suit components, or parsed from a two-character string representation (e.g., "Ks" for King of Spades).

String format examples:

  • "As" - Ace of Spades
  • "Kc" - King of Clubs
  • "Th" - Ten of Hearts
  • "2d" - Two of Diamonds
See Also:
  • Constructor Details

    • Card

      public Card(Rank rank, Suit suit)
      Creates a new card with the specified rank and suit.

      The constructor computes an internal bit-packed value for efficient poker hand evaluation operations.

      Parameters:
      rank - the rank of the card
      suit - the suit of the card
      Throws:
      NullPointerException - if rank or suit is null
  • Method Details

    • fromString

      public static Card fromString(String string)
      Creates a new Card instance from its string representation.

      The string must be exactly two characters: the first character represents the rank, and the second character represents the suit.

      Rank characters: "2", "3", "4", "5", "6", "7", "8", "9", "T" (Ten), "J" (Jack), "Q" (Queen), "K" (King), "A" (Ace)

      Suit characters: "s" (Spades), "h" (Hearts), "d" (Diamonds), "c" (Clubs)

      Parameters:
      string - the two-character string representation (e.g., "Ks", "2h")
      Returns:
      a new Card instance corresponding to the given string
      Throws:
      IllegalArgumentException - if string is null, not exactly 2 characters, or contains invalid rank/suit characters
    • getRank

      public Rank getRank()
      Returns the rank of this card.
      Returns:
      the rank of this card
      See Also:
    • getSuit

      public Suit getSuit()
      Returns the suit of this card.
      Returns:
      the suit of this card
      See Also:
    • toString

      public String toString()
      Returns the string representation of this card.

      The format is a two-character string where the first character represents the rank and the second represents the suit.

      Examples: "Ks" (King of Spades), "Jh" (Jack of Hearts), "2c" (Two of Clubs)

      Overrides:
      toString in class Object
      Returns:
      the string representation of this card