Class: Card

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ruby-poker/card.rb

Constant Summary collapse

SUITS =
"cdhs"
FACES =
"L23456789TJQKA"
SUIT_LOOKUP =
{
  'c' => 0,
  'd' => 1,
  'h' => 2,
  's' => 3
}
FACE_VALUES =
{
  'L' =>  0,   # this is a low ace
  '2' =>  1,
  '3' =>  2,
  '4' =>  3,
  '5' =>  4,
  '6' =>  5,
  '7' =>  6,
  '8' =>  7,
  '9' =>  8,
  'T' =>  9,
  'J' => 10,
  'Q' => 11,
  'K' => 12,
  'A' => 13
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Card

Returns a new instance of Card.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

Instance Attribute Details

#faceObject (readonly)

Returns the value of attribute face.



84
85
86
# File 'lib/ruby-poker/card.rb', line 84

def face
  @face
end

#suitObject (readonly)

Returns the value of attribute suit.



84
85
86
# File 'lib/ruby-poker/card.rb', line 84

def suit
  @suit
end

Class Method Details

.face_value(face) ⇒ Object



27
28
29
# File 'lib/ruby-poker/card.rb', line 27

def Card.face_value(face)
  FACE_VALUES[face.upcase]
end

Instance Method Details

#<=>(card2) ⇒ Object

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



107
108
109
# File 'lib/ruby-poker/card.rb', line 107

def <=> card2
  @face <=> card2.face
end

#==(card2) ⇒ Object Also known as: eql?

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



113
114
115
# File 'lib/ruby-poker/card.rb', line 113

def == card2
  value == card2.value
end

#hashObject

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



120
121
122
# File 'lib/ruby-poker/card.rb', line 120

def hash
  value.hash
end

#natural_valueObject

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


134
135
136
137
138
# 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_cardObject

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



99
100
101
# File 'lib/ruby-poker/card.rb', line 99

def to_card
  self
end

#to_sObject

Returns a string containing the representation of Card

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



94
95
96
# File 'lib/ruby-poker/card.rb', line 94

def to_s
  FACES[@face].chr + SUITS[@suit].chr
end

#valueObject



87
88
89
# File 'lib/ruby-poker/card.rb', line 87

def value
  (@suit * 13) + @face
end