Class: PokerRanking::HandType::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hand_type/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/hand_type/base.rb', line 6

def initialize(cards)
  @cards = cards
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



4
5
6
# File 'lib/hand_type/base.rb', line 4

def cards
  @cards
end

Instance Method Details

#cards_for_values_and_kickers(*values) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/hand_type/base.rb', line 55

def cards_for_values_and_kickers(*values)
  result = cards.select { |card| not values.include? card.value }
  values.reverse.each do |value|
    result += cards.select { |card| card.value == value }
  end
  result[-5..-1]
end

#cards_in_straight(card_set) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hand_type/base.rb', line 69

def cards_in_straight(card_set)
  return [] if card_set.empty?

  cards_in_streak = []
  cards_in_straight = []
  last_value = 0

  if card_set[-1].rank == 'Ace'
    cards_in_streak << card_set[-1]
    last_value = 1
  end

  card_set.each do |card|
    if cards_in_streak.empty? or card.value == last_value + 1
      cards_in_streak << card
    elsif card.value > last_value
      cards_in_streak = [card]
    end
    last_value = cards_in_streak[-1].value

    if cards_in_streak.length >= 5
      cards_in_straight = cards_in_streak.clone
    end
  end
  cards_in_straight
end

#highest_same_value(n) ⇒ Object



18
19
20
# File 'lib/hand_type/base.rb', line 18

def highest_same_value(n)
  highest_same_value_except n, 0
end

#highest_same_value_except(n, skipped_values) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hand_type/base.rb', line 30

def highest_same_value_except(n, skipped_values)
  skipped_values = [skipped_values] unless skipped_values.respond_to? :include?
  value = 0
  count = 1
  last_value = 0
  @cards.each do |card|
    if card.value == last_value && !(skipped_values.include? card.value)
      count += 1
      value = last_value if count == n
    else
      count = 1
      last_value = card.value
    end
  end
  value
end

#kickersObject



47
48
49
50
51
52
53
# File 'lib/hand_type/base.rb', line 47

def kickers
  kickers = []
  @cards.map(&:value).reverse.each do |value|
    kickers << value if @cards.map(&:value).count(value) == 1
  end
  kickers[0..number_of_kickers-1]
end

#n_of_a_kind?(n) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/hand_type/base.rb', line 14

def n_of_a_kind?(n)
  highest_same_value(n) > 0
end

#number_of_kickersObject



22
23
24
# File 'lib/hand_type/base.rb', line 22

def number_of_kickers
  5
end

#second_valueObject



26
27
28
# File 'lib/hand_type/base.rb', line 26

def second_value
  value
end

#straight_value_of(card_set) ⇒ Object



63
64
65
66
67
# File 'lib/hand_type/base.rb', line 63

def straight_value_of(card_set)
  cards_in_straight = cards_in_straight(card_set)

  cards_in_straight.empty? ? 0 : cards_in_straight[-1].value
end

#valueObject



10
11
12
# File 'lib/hand_type/base.rb', line 10

def value
  cards[-1].value
end