module EulerPoker::Handable
Public Class Methods
new(cards)
click to toggle source
# File lib/euler_poker/handable.rb, line 5 def initialize(cards) @cards = cards end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/euler_poker/handable.rb, line 9 def <=>(other) class_result = class_comparison(other) return class_result unless class_result == 0 return instance_comparison(other) end
class_comparison(other)
click to toggle source
# File lib/euler_poker/handable.rb, line 20 def class_comparison(other) ranking <=> other.ranking end
descending_ranks()
click to toggle source
# File lib/euler_poker/handable.rb, line 32 def descending_ranks ranks.sort.reverse end
ranking()
click to toggle source
# File lib/euler_poker/handable.rb, line 16 def ranking RANKED_HANDS.reverse.index(self.class) end
ranks()
click to toggle source
# File lib/euler_poker/handable.rb, line 24 def ranks @cards.map(&:rank) end
suits()
click to toggle source
# File lib/euler_poker/handable.rb, line 28 def suits @cards.map(&:suit) end
Private Instance Methods
flush?()
click to toggle source
# File lib/euler_poker/handable.rb, line 57 def flush? suits.uniq.length == 1 end
four_of_a_kind?()
click to toggle source
# File lib/euler_poker/handable.rb, line 49 def four_of_a_kind? rank_counts.values.any? { |count| count == 4 } end
full_house?()
click to toggle source
# File lib/euler_poker/handable.rb, line 53 def full_house? rank_counts.values.include?(3) && rank_counts.values.include?(2) end
one_pair?()
click to toggle source
# File lib/euler_poker/handable.rb, line 85 def one_pair? rank_counts.values.any? { |count| count == 2 } end
rank_counts()
click to toggle source
# File lib/euler_poker/handable.rb, line 38 def rank_counts ranks.reduce(Hash.new(0)) do |counts, rank| counts[rank] += 1 counts end end
straight?()
click to toggle source
# File lib/euler_poker/handable.rb, line 61 def straight? # Can't be a straight unless all cards are different ranks return false unless ranks.uniq.count == 5 sorted_cards = ranks.sort smallest = sorted_cards.first largest = sorted_cards.last # Since these are unique and sorted, if the difference between # largest and smallest is 4, then these are consecutive cards largest - smallest == 4 end
straight_flush?()
click to toggle source
# File lib/euler_poker/handable.rb, line 45 def straight_flush? return true if straight? && flush? end
three_of_a_kind?()
click to toggle source
# File lib/euler_poker/handable.rb, line 74 def three_of_a_kind? rank_counts.values.any? { |count| count == 3 } end
two_pair?()
click to toggle source
# File lib/euler_poker/handable.rb, line 78 def two_pair? rank_counts .values .select { |count| count == 2 } .count == 2 end