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

- (Array) cards

Returns Cards in the hand

Returns:

  • (Array)

    Cards in the hand



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

def cards
  @cards ||= []
end

Instance Method Details

- (Card) <<(card)

Add a card

Returns:

  • (Card)

    the card added



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

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

- (Integer) <=>(other_hand)

Compares the hand with another hand.

Parameters:

  • other_hand (Hand)

    the hand to compare the receiver to

Returns:

  • (Integer)

    -1 if other_hand is less than the receiver, 0 for equal to, and 1 for greater than

See Also:



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

- (Hash) best_hand

Returns A hash with :type and :cards keys.

Returns:

  • (Hash)

    A hash with :type and :cards keys.



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

- (Array, Nil) flush

Returns Array of Card objects with the flush in decending order or nil if there isn't a flush in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the flush in decending order or nil if there isn't a flush in the hand



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

- (Array, Nil) four_of_a_kind

Returns Array of Card objects with the four of a kind first the kicker in decending order or nil if there isn't four of a kind in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the four of a kind first the kicker in decending order or nil if there isn't four of a kind in the hand



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

def four_of_a_kind
  self.kinds(4)
end

- (Array, Nil) full_house

Returns Array of Card objects with the full house in decending order or nil if there isn't a full house in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the full house in decending order or nil if there isn't a full house in the hand



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

- (Integer) hand_index

Hand's index

Mainly used for internal reasons when comparing hand.

Returns:

  • (Integer)

    index of the hand's rank

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

- (Array) high_card

Returns Array of Card objects with the highest card first

Returns:

  • (Array)

    Array of Card objects with the highest card first



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

def high_card
  self.cards.sort.reverse
end

- (Array, Nil) pair

Returns Array of Card objects with the pair first and kickers in decending order or nil if there isn't a pair in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the pair first and kickers in decending order or nil if there isn't a pair in the hand



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

def pair
  self.pairs(1)
end

- (Array, Nil) straight

Returns Array of Card objects with the straight in decending order or nil if there isn't a straight in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the straight in decending order or nil if there isn't a straight in the hand



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

- (Array, Nil) straight_flush

Returns Array of Card objects with the straight flush in decending order or nil if there isn't a straight flush in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the straight flush in decending order or nil if there isn't a straight flush in the hand



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

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

- (Array) suites

Returns All of the suites contained in the hand

Returns:

  • (Array)

    All of the suites contained in the hand



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

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

- (Array, Nil) three_of_a_kind

Returns Array of Card objects with the three of a kind first and kickers in decending order or nil if there isn't three of a kind in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the three of a kind first and kickers in decending order or nil if there isn't three of a kind in the hand



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

def three_of_a_kind
  self.kinds(3)
end

- (Array, Nil) two_pair

Returns Array of Card objects with the pairs first and kickers in decending order or nil if there isn't two pair in the hand

Returns:

  • (Array, Nil)

    Array of Card objects with the pairs first and kickers in decending order or nil if there isn't two pair in the hand



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

def two_pair
  self.pairs(2)
end