Class: AcpcPokerTypes::PileOfCards

Inherits:
Array
  • Object
show all
Defined in:
lib/acpc_poker_types/pile_of_cards.rb

Direct Known Subclasses

Hand

Constant Summary collapse

POKER_HAND_STRENGTHS =
{
  royal_flush: 12115,
  straight_flush: 12106,
  four_of_a_kind: 11934,
  full_house: 10921,
  flush: 9634,
  straight: 9623,
  three_of_a_kind: 8606,
  two_pair: 5291,
  one_pair: 1287,
  high_card: 0
}

Instance Method Summary collapse

Instance Method Details

#all_cards(suit) ⇒ Object



153
154
155
# File 'lib/acpc_poker_types/pile_of_cards.rb', line 153

def all_cards(suit)
  self.class().new(select { |c| c.suit == suit })
end

#largest_rank(number_of_cards = 1, suits = Suit.all_suits) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/acpc_poker_types/pile_of_cards.rb', line 177

def largest_rank(number_of_cards = 1, suits = Suit.all_suits)
  rank = -1
  each do |card|
    if (
      card.rank > rank &&
      suits.include?(card.suit) &&
      count { |c| c.rank == card.rank } >= number_of_cards &&
      card.rank > rank
    )
      rank = card.rank
    end
  end
  rank
end

#remaining_cards(rank_of_cards_to_remove, number_of_cards_to_remove = 1) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/acpc_poker_types/pile_of_cards.rb', line 157

def remaining_cards(
  rank_of_cards_to_remove,
  number_of_cards_to_remove = 1
)
  num_matched_cards = 0
  reduce(self.class().new([])) do |cards, c|
    if (
      c.rank == rank_of_cards_to_remove && (
        num_matched_cards < number_of_cards_to_remove ||
        number_of_cards_to_remove < 0
      )
    )
      num_matched_cards += 1
    else
      cards << c
    end
    cards
  end
end

#to_poker_hand_descriptionObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/acpc_poker_types/pile_of_cards.rb', line 34

def to_poker_hand_description
  case to_poker_hand_key
  when :royal_flush
    'royal flush'
  when :straight_flush
    "#{Rank.new(to_poker_hand_strength - POKER_HAND_STRENGTHS[:straight_flush] + 3).to_sym}-high straight flush"
  when :four_of_a_kind
    rank_with_quad = largest_rank(4)

    m = "quad #{ActiveSupport::Inflector.pluralize(rank_with_quad.to_sym.to_s)}"

    if length > 4
      kicker = remaining_cards(rank_with_quad, 4).largest_rank
      "#{m} with #{kicker.to_sym} kicker"
    else
      m
    end
  when :full_house
    rank_with_trip = largest_rank(3)
    rank_with_pair = remaining_cards(rank_with_trip, -1).largest_rank 2
    "#{ActiveSupport::Inflector.pluralize(rank_with_trip.to_sym.to_s)} full of #{ActiveSupport::Inflector.pluralize(rank_with_pair.to_sym.to_s)}"
  when :flush
    flush_with_largest_rank = nil
    largest_rank_so_far = -1
    Suit.every_suit do |suit|
      if count { |c| c.suit == suit } >= 5
        r = largest_rank 1, [suit]
        if r > largest_rank_so_far
          flush_with_largest_rank = suit
          largest_rank_so_far = r
        end
      end
    end
    cards = all_cards flush_with_largest_rank
    r1 = cards.largest_rank
    cards = cards.remaining_cards(r1)
    r2 = cards.largest_rank
    cards = cards.remaining_cards(r2)
    r3 = cards.largest_rank
    cards = cards.remaining_cards(r3)
    r4 = cards.largest_rank
    cards = cards.remaining_cards(r4)
    r5 = cards.largest_rank

    "#{r1.to_sym}, #{r2.to_sym}, #{r3.to_sym}, #{r4.to_sym}, #{r5.to_sym} flush"
  when :straight
    "#{Rank.new(to_poker_hand_strength - POKER_HAND_STRENGTHS[:straight] + 3).to_sym}-high straight"
  when :three_of_a_kind
    r = largest_rank 3
    m = "trip #{ActiveSupport::Inflector.pluralize(r.to_sym.to_s)}"

    if length > 3
      cards = remaining_cards(r, 3)
      r1 = cards.largest_rank
      m += " with #{r1.to_sym}"

      if length > 4
        cards = cards.remaining_cards r1
        r2 = cards.largest_rank
        m += " and #{r2.to_sym}"
      end
    end
    m
  when :two_pair
    first_pair = largest_rank 2
    cards = remaining_cards first_pair, -1
    second_pair = cards.largest_rank 2

    m = "#{ActiveSupport::Inflector.pluralize(first_pair.to_sym.to_s)} and #{ActiveSupport::Inflector.pluralize(second_pair.to_sym.to_s)}"

    if length > 4
      cards = cards.remaining_cards second_pair, -1
      "#{m} with #{cards.largest_rank.to_sym} kicker"
    else
      m
    end
  when :one_pair
    pair = largest_rank 2

    m = "pair of #{ActiveSupport::Inflector.pluralize(pair.to_sym.to_s)}"

    if length > 2
      cards = remaining_cards pair, -1
      r1 = cards.largest_rank
      m += " with #{r1.to_sym}"

      if length > 3
        cards = cards.remaining_cards r1, -1
        r2 = cards.largest_rank

        if length > 4
          cards = cards.remaining_cards r2, -1
          r3 = cards.largest_rank
          m += ", #{r2.to_sym}, and #{r3.to_sym}"
        else
          m += " and #{r2.to_sym}"
        end
      end
    end
    m
  when :high_card
    cards = self
    hand = [5, length].min.times.inject([]) do |h, i|
      c = cards.largest_rank
      h << c.to_sym
      cards = cards.remaining_cards(c, -1)
      h
    end

    if length == 1
      "#{hand.first}"
    elsif length == 2
      "#{hand.first} and #{hand.last}"
    else
      "#{hand[0..-2].join(', ')}, and #{hand.last}"
    end
  end
end

#to_poker_hand_keyObject



25
26
27
28
29
30
31
32
# File 'lib/acpc_poker_types/pile_of_cards.rb', line 25

def to_poker_hand_key
  flattened_strengths = self.class().poker_hand_strengths_sorted_desc.flatten
  flattened_strengths[
    flattened_strengths.index do |e|
      e.respond_to?(:to_i) && e.to_i <= to_poker_hand_strength
    end - 1
  ]
end

#to_poker_hand_strengthInteger

Returns The strength of the strongest poker hand that can be made from this pile of cards.

Returns:

  • (Integer)

    The strength of the strongest poker hand that can be made from this pile of cards.



21
22
23
# File 'lib/acpc_poker_types/pile_of_cards.rb', line 21

def to_poker_hand_strength
  HandEvaluator.rank_hand map { |card| card.to_i }
end