class Holdem::PokerRank
Attributes
cards[R]
rank[R]
score[R]
Public Class Methods
new(cards)
click to toggle source
# File lib/holdem/poker_rank.rb, line 5 def initialize(cards) @cards = cards @score, @rank = build_ranking end
Public Instance Methods
build_flush()
click to toggle source
# File lib/holdem/poker_rank.rb, line 130 def build_flush [ [5, highest_flush_card], "Flush" ] end
build_four_kind()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
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.
# File lib/holdem/poker_rank.rb, line 108 def build_high_card [ [0] + sorted_values, "#{high_card.rank} high" ] end
build_pair()
click to toggle source
# File lib/holdem/poker_rank.rb, line 112 def build_pair [ [1] + pair_card_values, "Pair of #{pair_card.rank}s" ] end
build_ranking()
click to toggle source
# 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()
click to toggle source
# File lib/holdem/poker_rank.rb, line 126 def build_straight [ [4, highest_straight_card], "Straight" ] end
build_straight_flush()
click to toggle source
# File lib/holdem/poker_rank.rb, line 146 def build_straight_flush [ [8, highest_straight_card], "Straight Flush" ] end
build_three_kind()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# File lib/holdem/poker_rank.rb, line 45 def flush? suits.any? { |_,count| count >= 5 } end
flush_suit()
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# File lib/holdem/poker_rank.rb, line 37 def four_of_a_kind? kinds?(4) end
Also aliased as: quads?
full_house?()
click to toggle source
# File lib/holdem/poker_rank.rb, line 41 def full_house? three_of_a_kind? && pair? end
Also aliased as: boat?
high_card()
click to toggle source
HELPER METHODS
# File lib/holdem/poker_rank.rb, line 151 def high_card sorted_cards.first end
highest_flush_card()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/holdem/poker_rank.rb, line 80 def kinds?(size) pairs.any? { |count,_cards| count == size } end
open_ended?()
click to toggle source
# 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?()
click to toggle source
# File lib/holdem/poker_rank.rb, line 76 def pair? kinds?(2) end
straight?()
click to toggle source
# 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?()
click to toggle source
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.
# 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?()
click to toggle source
# File lib/holdem/poker_rank.rb, line 67 def three_of_a_kind? kinds?(3) end
Also aliased as: trips?
two_pairs?()
click to toggle source
# 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
Also aliased as: two_pair?
Private Instance Methods
pair_card(num=1)
click to toggle source
helper method to retrieve card value/rank for each pair
# File lib/holdem/poker_rank.rb, line 173 def pair_card(num=1) pairs.take(num).map { |_count, cards| cards.last }.last end
pair_card_values()
click to toggle source
helper method to calculate poker scores
# File lib/holdem/poker_rank.rb, line 178 def pair_card_values pairs.map { |_count, cards| cards.first.value } end
pairs()
click to toggle source
# File lib/holdem/poker_rank.rb, line 186 def pairs @pairs ||= cards.group_by { |card| card.value } .map { |_val,cards| [cards.count, cards] } .sort_by { |count,cards| [count*-1, cards.first.value*-1] } end
sorted_cards()
click to toggle source
# File lib/holdem/poker_rank.rb, line 182 def sorted_cards @sorted_cards ||= cards.sort_by { |card| card.value * -1 } end
sorted_values()
click to toggle source
# File lib/holdem/poker_rank.rb, line 198 def sorted_values @sorted_values ||= sorted_cards.map(&:value) end
suits()
click to toggle source
# File lib/holdem/poker_rank.rb, line 192 def suits @suits ||= cards.group_by { |card| card.suit } .map { |k,v| [k, v.count] } .sort_by { |k,count| [count*-1] } end