Class: TexasHoldem::Combinations::Straight

Inherits:
Combination
  • Object
show all
Defined in:
lib/texas-holdem/combinations/straight.rb

Constant Summary

Constants inherited from Combination

Combination::MAX_COMBINATION_LENGTH

Instance Attribute Summary

Attributes inherited from Combination

#cards, #cards_by_rank, #cards_by_suit, #combination_cards, #kicker_cards

Instance Method Summary collapse

Methods inherited from Combination

#<=>, #has_combination?, #initialize, #to_s

Constructor Details

This class inherits a constructor from TexasHoldem::Combinations::Combination

Instance Method Details

#add_ace_low(ranks) ⇒ Object



20
21
22
23
# File 'lib/texas-holdem/combinations/straight.rb', line 20

def add_ace_low(ranks)
  ranks << Card::ACE_LOW if ranks.index(Card::ACE_HIGH)
  ranks
end

#compare_same_rank(other) ⇒ Object



16
17
18
# File 'lib/texas-holdem/combinations/straight.rb', line 16

def compare_same_rank(other)
  combination_cards.max <=> other.combination_cards.max
end

#get_all_sequences(ranks) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/texas-holdem/combinations/straight.rb', line 25

def get_all_sequences(ranks)
  sequences, current_sequence = [], []
  ranks.each do |rank|
    if current_sequence.empty? || rank - current_sequence.last == 1
      current_sequence << rank
    else
      sequences << current_sequence
      current_sequence = [rank]
    end
  end
  sequences << current_sequence
end

#get_combination_cards(cards) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/texas-holdem/combinations/straight.rb', line 5

def get_combination_cards(cards)
  ranks = add_ace_low(cards_by_rank.keys).sort
  return [] if ranks.length < 5
  straight = get_all_sequences(ranks).find {|seq| seq.length >= 5}
  return [] unless straight
  straight.last(5).map do |rank|
    rank = Card::ACE_HIGH if rank == Card::ACE_LOW
    cards_by_rank[rank][0]
  end
end