module CardsLib::Standard::Rules::PokerRule

Public Class Methods

flush(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 31
def self.flush(cards)
  cards if IsSet.verify(cards, [:unique, :suited], {min: 5, max: 5})
end
four_of_a_kind(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 22
def self.four_of_a_kind(cards)
  cards if IsSet.verify(cards, [:unique, :paired], {min: 4, max: 4})
end
full_house(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 26
def self.full_house(cards)
  pair, set = cards.group_by(&:rank).values.sort
  cards if pair.inject(:pair?) && IsSet.verify(set, [:unique, :paired], {min: 3, max: 3})
end
high_card(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 53
def self.high_card(cards)
  cards.detect {|c| c.rank[/\AA/]} || cards.sort.pop
end
one_pair(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 49
def self.one_pair(cards)
  cards if IsSet.verify(cards, [:unique, :paired], {min: 2, max: 2})
end
precedence() click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 5
def self.precedence
  [
    :royal_flush, :straight_flush, :four_of_a_kind,
    :full_house, :flush, :straight,
    :three_of_a_kind, :two_pair, :one_pair,
    :high_card
  ]
end
royal_flush(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 14
def self.royal_flush(cards)
  cards if IsSet.verify(cards, [:suited]) && straight_to_ace(cards)
end
straight(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 35
def self.straight(cards)
  cards if IsSet.verify(cards, [:unique, :ordered], {min: 5, max: 5}) ||
    straight_to_ace(cards)
end
straight_flush(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 18
def self.straight_flush(cards)
  cards if IsSet.verify(cards, [:unique, :ordered, :suited], {min: 5, max: 5})
end
three_of_a_kind(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 40
def self.three_of_a_kind(cards)
  cards if IsSet.verify(cards, [:unique, :paired], {min: 3, max: 3})
end
two_pair(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 44
def self.two_pair(cards)
  pair1, pair2 = cards.group_by(&:rank).values.sort
  cards if pair1.inject(:pair?) && pair2.inject(:pair?)
end

Private Class Methods

straight_to_ace(cards) click to toggle source
# File lib/cards_lib/standard/rules/poker_rule.rb, line 58
def straight_to_ace(cards)
  cards.sort.map(&:rank) == Cards[*%w(As Ks Qs Js Ts)].sort.map(&:rank)
end