class Cribbage::Card

AH 5C 6D 7S 8C QD 9C

Constants

INDEX
SUITS

Attributes

card_str[RW]
face_value[RW]
index[RW]
suit[RW]
value[RW]

Str format:

"5h" = five of hearts
"AD" = ace of diamonds
"10c" = ten of clubs

Public Class Methods

new(str) click to toggle source
# File lib/cribbage.rb, line 28
def initialize(str)
  raise "String cannot be blank!" if str.nil?
  raise "Must be a string!" unless str.is_a?(String)
  raise "Invalid length" if (str.size < 2 or str.size > 3)

  @value = self.compute_value(str.chop)
  @face_value = str.chop.upcase
  raise "Invalid face value" unless INDEX.include?(@face_value)
  @index = INDEX.index(@face_value)
  @suit  = str[-1].upcase
  @card_str = str
end

Public Instance Methods

compute_value(char) click to toggle source
# File lib/cribbage.rb, line 45
def compute_value(char)
  return 1 if char.upcase=='A'
  return 10 if %w(J Q K).include?(char.upcase)
  if char.to_i > 0 and char.to_i <= 10
    return char.to_i
  end
  raise "Could not compute value of #{char}"
end
to_s() click to toggle source
# File lib/cribbage.rb, line 41
def to_s
  "[#{@face_value}#{SUITS[@suit]}]"
end