Class: Hand
- Inherits:
-
Object
- Object
- Hand
- Defined in:
- lib/rora/model/hand.rb
Overview
A subset of cards held at one time by a player during a game.
A hand consists of exactly five cards, and can be created in one of two ways.
A hand can be created with an array of cards. hand = Hand.new()
A hand can be created with string representation of each card. hand = Hand.new(“ACKCJS9H4H”)
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns all cards contained in the hand.
Instance Method Summary collapse
-
#contains(cards) ⇒ Object
Determines if this hand contains the given cards.
-
#flush? ⇒ Boolean
Determines if this hand is a flush.
-
#four_of_a_kind? ⇒ Boolean
Determines if this hand is four of a kind.
-
#full_house? ⇒ Boolean
Determines if this hand is a full house.
-
#high_card? ⇒ Boolean
Determines if this hand is a high card.
-
#initialize(cards) ⇒ Hand
constructor
A new instance of Hand.
-
#key ⇒ Object
Returns the hand key.
-
#name ⇒ Object
Returns the hand name.
-
#one_pair? ⇒ Boolean
Determines if this hand has one pair.
-
#score ⇒ Object
Returns the hand score, from 1 (strongest) to 7462 (weakest).
-
#straight? ⇒ Boolean
Determines if this hand is a straight.
-
#straight_flush? ⇒ Boolean
Determines if this hand is a straight flush.
-
#three_of_a_kind? ⇒ Boolean
Determines if this hand is a three of a kind.
- #to_s ⇒ Object
-
#two_pair? ⇒ Boolean
Determines if this hand is two pair.
-
#type ⇒ Object
Returns the hand type.
Constructor Details
#initialize(cards) ⇒ Hand
Returns a new instance of Hand.
15 16 17 18 19 20 |
# File 'lib/rora/model/hand.rb', line 15 def initialize cards @hand_repository = HandRepository.instance @cards = (cards.kind_of?(Array) ? cards : Card.to_cards(cards)).sort raise ArgumentError, "Exactly 5 cards are required to create a hand, #{cards.size} provided" if @cards.size != 5 raise ArgumentError, "The hand contains duplicate cards" if @cards.uniq.length != @cards.length end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns all cards contained in the hand.
43 44 45 |
# File 'lib/rora/model/hand.rb', line 43 def cards @cards end |
Instance Method Details
#contains(cards) ⇒ Object
Determines if this hand contains the given cards.
96 97 98 |
# File 'lib/rora/model/hand.rb', line 96 def contains cards @cards.contains cards end |
#flush? ⇒ Boolean
Determines if this hand is a flush.
48 49 50 51 52 53 |
# File 'lib/rora/model/hand.rb', line 48 def flush? for i in 0..(@cards.size - 2) do return false if @cards[i].suit != @cards[i+1].suit end true end |
#four_of_a_kind? ⇒ Boolean
Determines if this hand is four of a kind.
61 62 63 |
# File 'lib/rora/model/hand.rb', line 61 def four_of_a_kind? type == HandType::FOUR_OF_A_KIND end |
#full_house? ⇒ Boolean
Determines if this hand is a full house.
66 67 68 |
# File 'lib/rora/model/hand.rb', line 66 def full_house? type == HandType::FULL_HOUSE end |
#high_card? ⇒ Boolean
Determines if this hand is a high card.
91 92 93 |
# File 'lib/rora/model/hand.rb', line 91 def high_card? type == HandType::HIGH_CARD end |
#key ⇒ Object
Returns the hand key.
23 24 25 |
# File 'lib/rora/model/hand.rb', line 23 def key @cards.map { |card| "#{card.key}" }.join end |
#name ⇒ Object
Returns the hand name.
33 34 35 |
# File 'lib/rora/model/hand.rb', line 33 def name resolve_hand_attribute(2) end |
#one_pair? ⇒ Boolean
Determines if this hand has one pair.
86 87 88 |
# File 'lib/rora/model/hand.rb', line 86 def one_pair? type == HandType::ONE_PAIR end |
#score ⇒ Object
Returns the hand score, from 1 (strongest) to 7462 (weakest).
28 29 30 |
# File 'lib/rora/model/hand.rb', line 28 def score resolve_hand_attribute(0) end |
#straight? ⇒ Boolean
Determines if this hand is a straight.
71 72 73 |
# File 'lib/rora/model/hand.rb', line 71 def straight? type == HandType::STRAIGHT end |
#straight_flush? ⇒ Boolean
Determines if this hand is a straight flush.
56 57 58 |
# File 'lib/rora/model/hand.rb', line 56 def straight_flush? type == HandType::STRAIGHT_FLUSH end |
#three_of_a_kind? ⇒ Boolean
Determines if this hand is a three of a kind.
76 77 78 |
# File 'lib/rora/model/hand.rb', line 76 def three_of_a_kind? type == HandType::THREE_OF_A_KIND end |
#to_s ⇒ Object
100 101 102 |
# File 'lib/rora/model/hand.rb', line 100 def to_s "#{@cards.inject('') { |string, card| string << card.key }} (#{name})" end |