Class: TexasHoldem::PlayerHand
- Inherits:
-
Object
- Object
- TexasHoldem::PlayerHand
- Includes:
- Comparable
- Defined in:
- lib/player_hand.rb
Direct Known Subclasses
Flush, FourOfAKind, FullHouse, HighCard, OnePair, Straight, ThreeOfAKind, TwoPair
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
Class Method Summary collapse
-
.create(cards) ⇒ Object
TODO: investigate Builder / Factory pattern.
- .inherited(subclass) ⇒ Object
Instance Method Summary collapse
- #<=>(players_hand) ⇒ Object
-
#initialize(cards) ⇒ PlayerHand
constructor
A new instance of PlayerHand.
- #remaining_cards ⇒ Object
- #score ⇒ Object
Constructor Details
#initialize(cards) ⇒ PlayerHand
Returns a new instance of PlayerHand.
16 17 18 19 20 21 22 23 |
# File 'lib/player_hand.rb', line 16 def initialize(cards) @cards = cards @cards.gsub!(/J/, '11') @cards.gsub!(/Q/, '12') @cards.gsub!(/K/, '13') @cards.gsub!(/A/, '14') @cards = @cards.split.sort_by {|card| card.gsub(/\D/,'').to_i }.join ' ' end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
3 4 5 |
# File 'lib/player_hand.rb', line 3 def cards @cards end |
Class Method Details
.create(cards) ⇒ Object
TODO: investigate Builder / Factory pattern
11 12 13 14 |
# File 'lib/player_hand.rb', line 11 def self.create(cards) # cards could be both OnePair and a FullHouse, so return the highest raking hand @hand_types.map {|hand| hand.create(cards) }.compact.sort_by(&:score).last end |
.inherited(subclass) ⇒ Object
6 7 8 |
# File 'lib/player_hand.rb', line 6 def self.inherited(subclass) (@hand_types || @hand_types = []) << subclass end |
Instance Method Details
#<=>(players_hand) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/player_hand.rb', line 25 def <=>(players_hand) other_players_hand_score = players_hand.score this_players_score = score if this_players_score == other_players_hand_score remaining_cards <=> players_hand.remaining_cards else this_players_score <=> other_players_hand_score end end |
#remaining_cards ⇒ Object
40 41 42 |
# File 'lib/player_hand.rb', line 40 def remaining_cards @cards.gsub /\D/, '' end |
#score ⇒ Object
36 37 38 |
# File 'lib/player_hand.rb', line 36 def score base_score * 1000 + relative_score end |