Module: CardsLib::Standard::Rules::PokerRule

Defined in:
lib/cards_lib/standard/rules/poker_rule.rb

Class Method Summary collapse

Class Method Details

.flush(cards) ⇒ Object



31
32
33
# 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) ⇒ Object



22
23
24
# 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) ⇒ Object



26
27
28
29
# 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) ⇒ Object



53
54
55
# 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) ⇒ Object



49
50
51
# 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

.precedenceObject



5
6
7
8
9
10
11
12
# 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) ⇒ Object



14
15
16
# 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) ⇒ Object



35
36
37
38
# 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) ⇒ Object



18
19
20
# 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

.straight_to_ace(cards) ⇒ Object



58
59
60
# 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

.three_of_a_kind(cards) ⇒ Object



40
41
42
# 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) ⇒ Object



44
45
46
47
# 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