Class: Holdem::Card

Inherits:
Object
  • Object
show all
Defined in:
lib/card.rb

Constant Summary collapse

SUITS =
"cdhs"
RANKS =
"23456789TJQKA"
SUITS_LOOKUP =
{
  'c' => 0,
  'd' => 1,
  'h' => 2,
  's' => 3,
}
RANKS_LOOKUP =
{
  '2' =>  0,
  '3' =>  1,
  '4' =>  2,
  '5' =>  3,
  '6' =>  4,
  '7' =>  5,
  '8' =>  6,
  '9' =>  7,
  'T' =>  8,
  'J' =>  9,
  'Q' => 10,
  'K' => 11,
  'A' => 12,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card) ⇒ Card

Returns a new instance of Card.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/card.rb', line 31

def initialize(card)
  if card.is_a?(String) and card.length == 2
    @rank = RANKS_LOOKUP[card[0, 1]]
    @suit = SUITS_LOOKUP[card[1, 1].downcase]
  else
    raise "Received bad constructor: #{card.to_s} which is a #{card.class}"
  end

  if @rank.nil? or @suit.nil?
    raise "Invalid rank '#{card[0, 1]}' or invalid suit '#{card[1, 1]}'"
  end
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



29
30
31
# File 'lib/card.rb', line 29

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



29
30
31
# File 'lib/card.rb', line 29

def suit
  @suit
end

Instance Method Details

#<(compare_to) ⇒ Object



68
69
70
# File 'lib/card.rb', line 68

def <(compare_to)
  return compare_to > self
end

#<=>(compare_to) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/card.rb', line 54

def <=>(compare_to)
  if(compare_to.is_a?(Card))
    return 0 if self == compare_to
    return -1 if self.rank < compare_to.rank
    return -1 if self.rank == compare_to.rank && self.suit < compare_to.suit
    return 1
  end
end

#==(compare_to) ⇒ Object



48
49
50
51
52
# File 'lib/card.rb', line 48

def ==(compare_to)
  if(compare_to.is_a?(Card))
    return self.rank == compare_to.rank && self.suit == compare_to.suit
  end
end

#>(compare_to) ⇒ Object



63
64
65
66
# File 'lib/card.rb', line 63

def >(compare_to)
  return true if (self <=> compare_to) == 1
  false
end

#equiv?(compare_to) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/card.rb', line 72

def equiv?(compare_to)
  return true if self.rank == compare_to.rank
  return false
end

#higher?(compare_to) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/card.rb', line 77

def higher?(compare_to)
  return true if self.rank > compare_to.rank
  false
end

#lower?(compare_to) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/card.rb', line 82

def lower?(compare_to)
  return compare_to.higher?(self)
end

#to_sObject



44
45
46
# File 'lib/card.rb', line 44

def to_s
  RANKS[@rank].chr + SUITS[@suit].chr
end