Class: Hands::Hand

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/hands/hand.rb

Overview

Represents a poker hand.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cardsArray



7
8
9
# File 'lib/hands/hand.rb', line 7

def cards
  @cards ||= []
end

Instance Method Details

#<<(card) ⇒ Card

Add a card



33
34
35
# File 'lib/hands/hand.rb', line 33

def <<(card)
  self.cards << card
end

#<=>(other_hand) ⇒ Integer

Compares the hand with another hand.



19
20
21
22
23
24
25
26
27
28
# File 'lib/hands/hand.rb', line 19

def <=>(other_hand)
  response = (self.hand_index <=> other_hand.hand_index)

  # If the hands tie, see which is higher (i.e. higher pair)
  if response == 0
    self.cards <=> other_hand.cards
  else
    response
  end
end

#best_handHash



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hands/hand.rb', line 43

def best_hand
  response = {}
  HAND_ORDER.reverse.each do |type|
    cards = self.send(type.to_sym)
    next unless cards
    response[:type] = type
    response[:cards] = cards
    break
  end
  response
end

#flushArray, Nil



108
109
110
111
112
# File 'lib/hands/hand.rb', line 108

def flush
  # If all of the cards are the same suite, we have a flush
  return nil unless self.suites.length == 1
  self.cards.sort.reverse
end

#four_of_a_kindArray, Nil



135
136
137
# File 'lib/hands/hand.rb', line 135

def four_of_a_kind
  self.kinds(4)
end

#full_houseArray, Nil



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hands/hand.rb', line 115

def full_house
  dupes = self.duplicates
  return nil unless dupes.length == 2

  a = []
  b = []

  hand = self.cards.select do |card|
    if dupes.first == card.value
      a << card
    elsif dupes.last == card.value
      b << card
    end
  end

  return nil unless a.length + b.length == 5
  self.cards.sort.reverse
end

#hand_indexInteger

Hand's index

Mainly used for internal reasons when comparing hand.

See Also:



151
152
153
154
155
# File 'lib/hands/hand.rb', line 151

def hand_index
  best = self.best_hand
  return -1 if best.nil?
  HAND_ORDER.index(best[:type].to_s)
end

#high_cardArray



56
57
58
# File 'lib/hands/hand.rb', line 56

def high_card
  self.cards.sort.reverse
end

#pairArray, Nil



61
62
63
# File 'lib/hands/hand.rb', line 61

def pair
  self.pairs(1)
end

#straightArray, Nil



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hands/hand.rb', line 76

def straight
  return nil unless self.cards.length == 5
  cs = self.cards.sort.reverse

  # Ace's low
  if cs.first.value == 'a' and cs[1].value == 5
    # Move ace to end
    ace = cs.first
    cs = cs[1..4]
    cs << ace

    # Check succession
    csr = cs.reverse
    4.times do |i|
      next if i == 0
      return nil unless csr[i].value_index == i - 1
    end

  # Normal
  else
    # Check range
    return nil unless cs.first.value_index - cs.last.value_index == 4

    # Check succession
    4.times do |i|        
      return nil unless cs[i].value_index == cs[i + 1].value_index + 1
    end
  end
  cs
end

#straight_flushArray, Nil



140
141
142
143
# File 'lib/hands/hand.rb', line 140

def straight_flush
  return nil unless self.flush
  self.straight
end

#suitesArray



38
39
40
# File 'lib/hands/hand.rb', line 38

def suites
  self.cards.collect(&:suite).uniq
end

#three_of_a_kindArray, Nil



71
72
73
# File 'lib/hands/hand.rb', line 71

def three_of_a_kind
  self.kinds(3)
end

#two_pairArray, Nil



66
67
68
# File 'lib/hands/hand.rb', line 66

def two_pair
  self.pairs(2)
end