Class: Poker::Hand
- Inherits:
-
Object
- Object
- Poker::Hand
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/poker/hand.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(hand) ⇒ Object
- #[](index) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(*str_or_ary) ⇒ Hand
constructor
A new instance of Hand.
- #rank ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(*str_or_ary) ⇒ Hand
Returns a new instance of Hand.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/poker/hand.rb', line 17 def initialize(*str_or_ary) if str_or_ary.size == 1 @cards = str_or_ary[0].split(/ /).map { |s| Card.new(s) } else if str_or_ary[0].kind_of?(Card) @cards = str_or_ary else @cards = str_or_ary.map { |s| Card.new(s) } end end end |
Class Method Details
Instance Method Details
#<=>(hand) ⇒ Object
47 48 49 |
# File 'lib/poker/hand.rb', line 47 def <=>(hand) - (value <=> hand.value) end |
#[](index) ⇒ Object
51 52 53 |
# File 'lib/poker/hand.rb', line 51 def [](index) @cards[index] end |
#each(&block) ⇒ Object
55 56 57 |
# File 'lib/poker/hand.rb', line 55 def each(&block) @cards.each(&block) end |
#rank ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/poker/hand.rb', line 33 def rank v = value return "High Card" if v > 6185 return "Pair" if v > 3325 return "Two Pair" if v > 2467 return "Three of a Kind" if v > 1609 return "Straight" if v > 1599 return "Flush" if v > 322 return "Full House" if v > 166 return "Four of a Kind" if v > 10 return "Straight Flush" if v > 1 return "Royal Flush" if v == 1 end |
#size ⇒ Object
59 60 61 |
# File 'lib/poker/hand.rb', line 59 def size @cards.size end |
#to_s ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/poker/hand.rb', line 63 def to_s r = rank if r == "Pair" || r == "Two Pair" || r == "Three of a Kind" || r == "Full House" || r == "Four of a Kind" h = @cards.inject({}) { |h, c| r = c.rank_value + 1 h[r] = h[r] ? h[r] + 1 : 0 h } return @cards.sort_by { |c| if h[c.rank_value + 1] > 0 - ((h[c.rank_value + 1] * 10_000) + (c.rank_value + 1) * 100) + c.suit_value else - c.rank_value + c.suit_value end }.join(" ") elsif r == "Straight Flush" || r == "Straight" return @cards.sort_by { |c| if c.rank_value == 12 10_000 else - (c.rank_value + 1) * 100 - c.suit_value end }.join(" ") end @cards.sort_by { |c| - (c.rank_value + 1) * 100 - c.suit_value }. join(" ") end |
#to_str ⇒ Object
93 94 95 |
# File 'lib/poker/hand.rb', line 93 def to_str to_s end |