class Card

Constants

FACES
FACE_VALUES
SUITS
SUIT_LOOKUP

Attributes

face[R]
suit[R]

Public Class Methods

face_value(face) click to toggle source
# File lib/ruby-poker/card.rb, line 27
def Card.face_value(face)
  FACE_VALUES[face.upcase]
end
new(*args) click to toggle source
# File lib/ruby-poker/card.rb, line 62
def initialize(*args)
  if (args.size == 1)
    arg = args.first
    if (arg.respond_to?(:to_card))
      build_from_card(arg)
    elsif (arg.respond_to?(:to_str))
      build_from_string(arg)
    elsif (arg.respond_to?(:to_int))
      build_from_value(arg)
    end
  elsif (args.size == 2)
    arg1, arg2 = args
    if (arg1.respond_to?(:to_str) &&
        arg2.respond_to?(:to_str))
      build_from_face_suit(arg1, arg2)
    elsif (arg1.respond_to?(:to_int) &&
           arg2.respond_to?(:to_int))
      build_from_face_suit_values(arg1, arg2)
    end
  end
end

Public Instance Methods

<=>(card2) click to toggle source

Compare the face value of this card with another card. Returns: -1 if self is less than card2 0 if self is the same face value of card2 1 if self is greater than card2

# File lib/ruby-poker/card.rb, line 107
def <=> card2
  @face <=> card2.face
end
==(card2) click to toggle source

Returns true if the cards are the same card. Meaning they have the same suit and the same face value.

# File lib/ruby-poker/card.rb, line 113
def == card2
  value == card2.value
end
Also aliased as: eql?
eql?(card2)
Alias for: ==
hash() click to toggle source

Compute a hash-code for this Card. Two Cards with the same content will have the same hash code (and will compare using eql?).

# File lib/ruby-poker/card.rb, line 120
def hash
  value.hash
end
natural_value() click to toggle source

A card’s natural value is the closer to it’s intuitive value in a deck in the range of 1 to 52. Aces are low with a value of 1. Uses the bridge order of suits: clubs, diamonds, hearts, and spades. The formula used is: If the suit is clubs, the natural value is the face value (remember Aces are low). If the suit is diamonds, it is the clubs value plus 13. If the suit is hearts, it is plus 26. If it is spades, it is plus 39.

Card.new("Ac").natural_value    # => 1
Card.new("Kc").natural_value    # => 12
Card.new("Ad").natural_value    # => 13
# File lib/ruby-poker/card.rb, line 134
def natural_value
  natural_face = @face == 13 ? 1 : @face+1  # flip Ace from 13 to 1 and
                                            # increment everything else by 1
  natural_face + @suit * 13
end
to_card() click to toggle source

If to_card is called on a ‘Card` it should return itself

# File lib/ruby-poker/card.rb, line 99
def to_card
  self
end
to_s() click to toggle source

Returns a string containing the representation of Card

Card.new(“7c”).to_s # => “7c”

# File lib/ruby-poker/card.rb, line 94
def to_s
  FACES[@face].chr + SUITS[@suit].chr
end
value() click to toggle source
# File lib/ruby-poker/card.rb, line 87
def value
  (@suit * 13) + @face
end

Private Instance Methods

build_from_card(card) click to toggle source

Constructs this card object from another card object

# File lib/ruby-poker/card.rb, line 55
def build_from_card(card)
  @suit = card.suit
  @face = card.face
end
build_from_face_suit(face, suit) click to toggle source
# File lib/ruby-poker/card.rb, line 38
def build_from_face_suit(face, suit)
  suit.downcase!
  @face  = Card::face_value(face)
  @suit  = SUIT_LOOKUP[suit]
  raise ArgumentError, "Invalid card: \"#{face}#{suit}\"" unless @face and @suit
end
build_from_face_suit_values(face_int, suit_int) click to toggle source
# File lib/ruby-poker/card.rb, line 45
def build_from_face_suit_values(face_int, suit_int)
  @face = face_int
  @suit = suit_int
end
build_from_string(card) click to toggle source
# File lib/ruby-poker/card.rb, line 50
def build_from_string(card)
  build_from_face_suit(card[0,1], card[1,1])
end
build_from_value(given_value) click to toggle source
# File lib/ruby-poker/card.rb, line 33
def build_from_value(given_value)
  @suit  = given_value / 13
  @face  = given_value % 13
end