Class: Holdem::PokerRank
- Inherits:
-
Object
- Object
- Holdem::PokerRank
- Defined in:
- lib/holdem/poker_rank.rb
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
Instance Method Summary collapse
- #build_flush ⇒ Object
- #build_four_kind ⇒ Object
- #build_full_house ⇒ Object
-
#build_high_card ⇒ Object
BUILD HAND RANKINGS Score are first ranked by type of hand [0-9].
- #build_pair ⇒ Object
- #build_ranking ⇒ Object
- #build_straight ⇒ Object
- #build_straight_flush ⇒ Object
- #build_three_kind ⇒ Object
- #build_two_pairs ⇒ Object
- #five_card_straight?(list) ⇒ Boolean
- #flush? ⇒ Boolean
- #flush_suit ⇒ Object
- #four_card_straight?(list) ⇒ Boolean
- #four_of_a_kind? ⇒ Boolean (also: #quads?)
- #full_house? ⇒ Boolean (also: #boat?)
-
#high_card ⇒ Object
HELPER METHODS.
- #highest_flush_card ⇒ Object
- #highest_straight_card ⇒ Object
-
#initialize(cards) ⇒ PokerRank
constructor
A new instance of PokerRank.
- #kinds?(size) ⇒ Boolean
- #open_ended? ⇒ Boolean
- #pair? ⇒ Boolean
- #straight? ⇒ Boolean
-
#straight_flush? ⇒ Boolean
POKER HAND RANKINGS These methods determine type of poker hand.
- #three_of_a_kind? ⇒ Boolean (also: #trips?)
- #two_pairs? ⇒ Boolean (also: #two_pair?)
Constructor Details
#initialize(cards) ⇒ PokerRank
Returns a new instance of PokerRank.
5 6 7 8 |
# File 'lib/holdem/poker_rank.rb', line 5 def initialize(cards) @cards = cards @score, @rank = build_ranking end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
3 4 5 |
# File 'lib/holdem/poker_rank.rb', line 3 def cards @cards end |
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
3 4 5 |
# File 'lib/holdem/poker_rank.rb', line 3 def rank @rank end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
3 4 5 |
# File 'lib/holdem/poker_rank.rb', line 3 def score @score end |
Instance Method Details
#build_flush ⇒ Object
130 131 132 |
# File 'lib/holdem/poker_rank.rb', line 130 def build_flush [ [5, highest_flush_card], "Flush" ] end |
#build_four_kind ⇒ Object
140 141 142 143 144 |
# File 'lib/holdem/poker_rank.rb', line 140 def build_four_kind [ [7] + pair_card_values, "Four of a kind (quad #{pair_card(1).rank}s)", ] end |
#build_full_house ⇒ Object
134 135 136 137 138 |
# File 'lib/holdem/poker_rank.rb', line 134 def build_full_house [ [6] + pair_card_values, "Full House (#{pair_card(1).rank}s over #{pair_card(2).rank}s)", ] end |
#build_high_card ⇒ Object
BUILD HAND RANKINGS Score are first ranked by type of hand [0-9]. If equal, then compared by pairs, high cards, and/or remaining cards. Also includes description of hand.
108 109 110 |
# File 'lib/holdem/poker_rank.rb', line 108 def build_high_card [ [0] + sorted_values, "#{high_card.rank} high" ] end |
#build_pair ⇒ Object
112 113 114 |
# File 'lib/holdem/poker_rank.rb', line 112 def build_pair [ [1] + pair_card_values, "Pair of #{pair_card.rank}s" ] end |
#build_ranking ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/holdem/poker_rank.rb', line 10 def build_ranking case when straight_flush? then build_straight_flush when four_of_a_kind? then build_four_kind when full_house? then build_full_house when flush? then build_flush when straight? then build_straight when three_of_a_kind? then build_three_kind when two_pairs? then build_two_pairs when pair? then build_pair else build_high_card end end |
#build_straight ⇒ Object
126 127 128 |
# File 'lib/holdem/poker_rank.rb', line 126 def build_straight [ [4, highest_straight_card], "Straight" ] end |
#build_straight_flush ⇒ Object
146 147 148 |
# File 'lib/holdem/poker_rank.rb', line 146 def build_straight_flush [ [8, highest_straight_card], "Straight Flush" ] end |
#build_three_kind ⇒ Object
122 123 124 |
# File 'lib/holdem/poker_rank.rb', line 122 def build_three_kind [ [3] + pair_card_values, "Three of a Kind (trip #{pair_card.rank}s)" ] end |
#build_two_pairs ⇒ Object
116 117 118 119 120 |
# File 'lib/holdem/poker_rank.rb', line 116 def build_two_pairs [ [2] + pair_card_values, "Two Pairs (#{pair_card(1).rank}s and #{pair_card(2).rank}s)", ] end |
#five_card_straight?(list) ⇒ Boolean
63 64 65 |
# File 'lib/holdem/poker_rank.rb', line 63 def five_card_straight?(list) (list.first - list.last).abs == 4 && list.uniq.length == 5 end |
#flush? ⇒ Boolean
45 46 47 |
# File 'lib/holdem/poker_rank.rb', line 45 def flush? suits.any? { |_,count| count >= 5 } end |
#flush_suit ⇒ Object
165 166 167 168 |
# File 'lib/holdem/poker_rank.rb', line 165 def flush_suit suit, count = suits.first return suit if count >= 5 end |
#four_card_straight?(list) ⇒ Boolean
59 60 61 |
# File 'lib/holdem/poker_rank.rb', line 59 def four_card_straight?(list) (list.first - list.last).abs == 3 && list.uniq.length == 4 end |
#four_of_a_kind? ⇒ Boolean Also known as: quads?
37 38 39 |
# File 'lib/holdem/poker_rank.rb', line 37 def four_of_a_kind? kinds?(4) end |
#full_house? ⇒ Boolean Also known as: boat?
41 42 43 |
# File 'lib/holdem/poker_rank.rb', line 41 def full_house? three_of_a_kind? && pair? end |
#high_card ⇒ Object
HELPER METHODS
151 152 153 |
# File 'lib/holdem/poker_rank.rb', line 151 def high_card sorted_cards.first end |
#highest_flush_card ⇒ Object
161 162 163 |
# File 'lib/holdem/poker_rank.rb', line 161 def highest_flush_card cards.select { |card| card.suit == flush_suit }.max.value end |
#highest_straight_card ⇒ Object
155 156 157 158 159 |
# File 'lib/holdem/poker_rank.rb', line 155 def highest_straight_card sorted_values.each_cons(5) do |vals| return vals.first if five_card_straight?(vals) end end |
#kinds?(size) ⇒ Boolean
80 81 82 |
# File 'lib/holdem/poker_rank.rb', line 80 def kinds?(size) pairs.any? { |count,_cards| count == size } end |
#open_ended? ⇒ Boolean
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/holdem/poker_rank.rb', line 84 def open_ended? return false if straight? card_values = sorted_values # Add ace to end of sorted values if present card_values.push(1) if card_values.include?(14) card_values.each_cons(4) do |vals| if four_card_straight?(vals) && vals.last != 1 && vals.first != 14 return true end end false end |
#pair? ⇒ Boolean
76 77 78 |
# File 'lib/holdem/poker_rank.rb', line 76 def pair? kinds?(2) end |
#straight? ⇒ Boolean
49 50 51 52 53 54 55 56 57 |
# File 'lib/holdem/poker_rank.rb', line 49 def straight? card_values = sorted_values # check for low ace straight card_values.push(1) if card_values.include?(14) card_values.each_cons(5) do |vals| return true if five_card_straight?(vals) end false end |
#straight_flush? ⇒ Boolean
POKER HAND RANKINGS These methods determine type of poker hand. Hands are sorted by pair, then by values in descending order using the #pairs method. The hand ranking methods below filter the pairs array to determine type of hand.
30 31 32 33 34 35 |
# File 'lib/holdem/poker_rank.rb', line 30 def straight_flush? sorted_cards.each_cons(5) do |check_cards| return true if five_card_straight?(check_cards.map(&:value)) && check_cards.map(&:suit).uniq.one? end false end |
#three_of_a_kind? ⇒ Boolean Also known as: trips?
67 68 69 |
# File 'lib/holdem/poker_rank.rb', line 67 def three_of_a_kind? kinds?(3) end |
#two_pairs? ⇒ Boolean Also known as: two_pair?
71 72 73 74 |
# File 'lib/holdem/poker_rank.rb', line 71 def two_pairs? num_of_pairs = pairs.select { |count,_cards| count == 2 }.size num_of_pairs > 1 end |