Class: Hand
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
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
Instance Method Summary collapse
- #<=>(other_hand) ⇒ Object
- #card_ranks ⇒ Object
- #card_suits ⇒ Object
- #card_values ⇒ Object
- #display ⇒ Object
- #flush? ⇒ Boolean
- #form ⇒ Object
- #four_of_a_kind? ⇒ Boolean
- #full_house? ⇒ Boolean
-
#initialize(cards) ⇒ Hand
constructor
A new instance of Hand.
- #pair? ⇒ Boolean
- #rank ⇒ Object
- #rank_idx ⇒ Object
-
#sort! ⇒ Object
sort cards by their rank multiplicity in descending order e.g.
- #straight? ⇒ Boolean
- #straight_flush? ⇒ Boolean
- #three_of_a_kind? ⇒ Boolean
- #two_pair? ⇒ Boolean
- #wheel? ⇒ Boolean
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
#cards ⇒ Object (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_ranks ⇒ Object
45 46 47 |
# File 'lib/rpoker/hand.rb', line 45 def card_ranks cards.map(&:rank) end |
#card_suits ⇒ Object
41 42 43 |
# File 'lib/rpoker/hand.rb', line 41 def card_suits cards.map(&:suit) end |
#card_values ⇒ Object
49 50 51 |
# File 'lib/rpoker/hand.rb', line 49 def card_values wheel? ? [5,4,3,2,1] : cards.map(&:to_i) end |
#display ⇒ Object
37 38 39 |
# File 'lib/rpoker/hand.rb', line 37 def display puts cards.join(" ") end |
#flush? ⇒ Boolean
57 58 59 |
# File 'lib/rpoker/hand.rb', line 57 def flush? card_suits.uniq.size == 1 end |
#form ⇒ Object
91 92 93 |
# File 'lib/rpoker/hand.rb', line 91 def form @form ||= compute_form end |
#four_of_a_kind? ⇒ Boolean
71 72 73 |
# File 'lib/rpoker/hand.rb', line 71 def four_of_a_kind? form == :AAAAx end |
#full_house? ⇒ Boolean
75 76 77 |
# File 'lib/rpoker/hand.rb', line 75 def full_house? form == :AAABB end |
#pair? ⇒ Boolean
87 88 89 |
# File 'lib/rpoker/hand.rb', line 87 def pair? form == :AAxxx end |
#rank ⇒ Object
29 30 31 |
# File 'lib/rpoker/hand.rb', line 29 def rank @rank ||= RANKS.find { |rank| send("#{rank}?") } || :high_card end |
#rank_idx ⇒ Object
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
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
53 54 55 |
# File 'lib/rpoker/hand.rb', line 53 def straight_flush? flush? && straight? end |
#three_of_a_kind? ⇒ Boolean
79 80 81 |
# File 'lib/rpoker/hand.rb', line 79 def three_of_a_kind? form == :AAAxx end |
#two_pair? ⇒ Boolean
83 84 85 |
# File 'lib/rpoker/hand.rb', line 83 def two_pair? form == :AABBx end |
#wheel? ⇒ Boolean
67 68 69 |
# File 'lib/rpoker/hand.rb', line 67 def wheel? card_ranks.sort == %w(A 2 3 4 5).sort end |