Class: TexasHoldem::Combinations::Combination

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/texas-holdem/combinations/combination.rb

Direct Known Subclasses

Flush, HighestCard, Pair, Straight, ThreeOfAKind, TwoPairs

Constant Summary collapse

MAX_COMBINATION_LENGTH =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ Combination

Returns a new instance of Combination.



9
10
11
12
13
14
15
# File 'lib/texas-holdem/combinations/combination.rb', line 9

def initialize(cards)
  @cards = cards
  @cards_by_rank = get_cards_by_rank
  @cards_by_suit = get_cards_by_suit
  @combination_cards = get_combination_cards(cards)
  @kicker_cards = get_kicker_cards
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



7
8
9
# File 'lib/texas-holdem/combinations/combination.rb', line 7

def cards
  @cards
end

#cards_by_rankObject (readonly)

Returns the value of attribute cards_by_rank.



7
8
9
# File 'lib/texas-holdem/combinations/combination.rb', line 7

def cards_by_rank
  @cards_by_rank
end

#cards_by_suitObject (readonly)

Returns the value of attribute cards_by_suit.



7
8
9
# File 'lib/texas-holdem/combinations/combination.rb', line 7

def cards_by_suit
  @cards_by_suit
end

#combination_cardsObject (readonly)

Returns the value of attribute combination_cards.



7
8
9
# File 'lib/texas-holdem/combinations/combination.rb', line 7

def combination_cards
  @combination_cards
end

#kicker_cardsObject (readonly)

Returns the value of attribute kicker_cards.



7
8
9
# File 'lib/texas-holdem/combinations/combination.rb', line 7

def kicker_cards
  @kicker_cards
end

Instance Method Details

#<=>(other) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/texas-holdem/combinations/combination.rb', line 21

def <=>(other)
  compared_by_rank = RANKS[self.class] <=> RANKS[other.class]
  return compared_by_rank unless compared_by_rank == 0
  compared_by_same_rank = compare_same_rank(other)
  return compared_by_same_rank unless compared_by_same_rank == 0
  compare_kickers(other)
end

#has_combination?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/texas-holdem/combinations/combination.rb', line 17

def has_combination?
  combination_cards.length > 0
end

#to_sObject



29
30
31
# File 'lib/texas-holdem/combinations/combination.rb', line 29

def to_s
  "#{combination_cards.map(&:to_s)}, kickers: #{kicker_cards.map(&:to_s)}"
end