Class: Hand

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rpoker/hand.rb

Constant Summary collapse

RANKS =
%i{
  straight_flush
  four_of_a_kind
  full_house
  flush
  straight
  three_of_a_kind
  two_pair
  pair
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards) ⇒ Hand

Returns a new instance of Hand.



16
17
18
19
20
# File 'lib/rpoker/hand.rb', line 16

def initialize(cards)
  @cards = parse_cards(cards)

  validate_cards!
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



3
4
5
# File 'lib/rpoker/hand.rb', line 3

def cards
  @cards
end

Instance Method Details

#<=>(other_hand) ⇒ Object



22
23
24
25
26
27
# File 'lib/rpoker/hand.rb', line 22

def <=>(other_hand)
  winner = Matchup.new(self, other_hand).winner
  return  1 if winner == self
  return  0 if winner == nil
  return -1 if winner == other_hand
end

#card_ranksObject



45
46
47
# File 'lib/rpoker/hand.rb', line 45

def card_ranks
  cards.map(&:rank)
end

#card_suitsObject



41
42
43
# File 'lib/rpoker/hand.rb', line 41

def card_suits
  cards.map(&:suit)
end

#card_valuesObject



49
50
51
# File 'lib/rpoker/hand.rb', line 49

def card_values
  wheel? ? [5,4,3,2,1] : cards.map(&:to_i)
end

#displayObject



37
38
39
# File 'lib/rpoker/hand.rb', line 37

def display
  puts cards.join(" ")
end

#flush?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rpoker/hand.rb', line 57

def flush?
  card_suits.uniq.size == 1
end

#formObject



91
92
93
# File 'lib/rpoker/hand.rb', line 91

def form
  @form ||= compute_form
end

#four_of_a_kind?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rpoker/hand.rb', line 71

def four_of_a_kind?
  form == :AAAAx
end

#full_house?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rpoker/hand.rb', line 75

def full_house?
  form == :AAABB
end

#pair?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rpoker/hand.rb', line 87

def pair?
  form == :AAxxx
end

#rankObject



29
30
31
# File 'lib/rpoker/hand.rb', line 29

def rank
  @rank ||= RANKS.find { |rank| send("#{rank}?") } || :high_card
end

#rank_idxObject



33
34
35
# File 'lib/rpoker/hand.rb', line 33

def rank_idx
  RANKS.index(rank) || RANKS.size
end

#sort!Object

sort cards by their rank multiplicity in descending order e.g. sort 2s Jh 4s Js 2c as Jh Js 2s 2c 4s



97
98
99
# File 'lib/rpoker/hand.rb', line 97

def sort!
  @cards.sort_by! { |card| [-card_rank_to_count[card.rank], -card.to_i] }
end

#straight?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/rpoker/hand.rb', line 61

def straight?
  return true if wheel?
  sorted_values = card_values.sort
  sorted_values.last - sorted_values.first == 4 && form == :xxxxx
end

#straight_flush?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rpoker/hand.rb', line 53

def straight_flush?
  flush? && straight?
end

#three_of_a_kind?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rpoker/hand.rb', line 79

def three_of_a_kind?
  form == :AAAxx
end

#two_pair?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rpoker/hand.rb', line 83

def two_pair?
  form == :AABBx
end

#wheel?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rpoker/hand.rb', line 67

def wheel?
  card_ranks.sort == %w(A 2 3 4 5).sort
end