Class: PokerEngine::HandIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/poker_engine/hand_index.rb

Constant Summary collapse

RANK_TABLE =

The index is equivalent to the level strength

[HandLevels::HighCard, HandLevels::OnePair,
HandLevels::TwoPairs, HandLevels::ThreeOfAKind,
HandLevels::Straight, HandLevels::Flush,
HandLevels::FullHouse, HandLevels::FourOfAKind,
HandLevels::StraightFlush].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ HandIndex

Returns a new instance of HandIndex.



14
15
16
# File 'lib/poker_engine/hand_index.rb', line 14

def initialize(cards)
  @cards = cards
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



12
13
14
# File 'lib/poker_engine/hand_index.rb', line 12

def cards
  @cards
end

Instance Method Details

#<=>(other) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/poker_engine/hand_index.rb', line 18

def <=>(other)
  outer_level_compare =
    (RANK_TABLE.index(level) <=> RANK_TABLE.index(other.level))

  return outer_level_compare unless outer_level_compare.zero?

  level <=> other.level
end

#>(other) ⇒ Object



27
28
29
# File 'lib/poker_engine/hand_index.rb', line 27

def >(other)
  level <=> other.level
end

#levelObject



31
32
33
34
# File 'lib/poker_engine/hand_index.rb', line 31

def level
  @level ||=
    RANK_TABLE.reverse_each.find { |level| level.owns?(cards) }
end