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.
-
#hand_repository ⇒ Object
readonly
Returns the value of attribute hand_repository.
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.
-
#hash_key ⇒ Object
Returns the hand hash key.
-
#high_card? ⇒ Boolean
Determines if this hand is a high card.
-
#id ⇒ Object
Returns the hand id.
-
#initialize(cards) ⇒ Hand
constructor
A new instance of Hand.
-
#name ⇒ Object
Returns the hand name.
-
#one_pair? ⇒ Boolean
Determines if this hand has one pair.
-
#probability ⇒ Object
Returns the probability of this hand being dealt.
-
#score ⇒ Object
Returns the hand strength, 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.
-
#two_pair? ⇒ Boolean
Determines if this hand is two pair.
-
#type ⇒ Object
Returns the hand type.
-
#value ⇒ Object
Returns the hand value.
Constructor Details
#initialize(cards) ⇒ Hand
Returns a new instance of Hand.
15 16 17 18 19 20 21 |
# 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) 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.
63 64 65 |
# File 'lib/rora/model/hand.rb', line 63 def cards @cards end |
#hand_repository ⇒ Object (readonly)
Returns the value of attribute hand_repository.
13 14 15 |
# File 'lib/rora/model/hand.rb', line 13 def hand_repository @hand_repository end |
Instance Method Details
#contains(cards) ⇒ Object
Determines if this hand contains the given cards.
116 117 118 |
# File 'lib/rora/model/hand.rb', line 116 def contains cards @cards.contains cards end |
#flush? ⇒ Boolean
Determines if this hand is a flush.
68 69 70 71 72 73 |
# File 'lib/rora/model/hand.rb', line 68 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.
81 82 83 |
# File 'lib/rora/model/hand.rb', line 81 def four_of_a_kind? type == HandType::FOUR_OF_A_KIND end |
#full_house? ⇒ Boolean
Determines if this hand is a full house.
86 87 88 |
# File 'lib/rora/model/hand.rb', line 86 def full_house? type == HandType::FULL_HOUSE end |
#hash_key ⇒ Object
Returns the hand hash key.
31 32 33 34 35 |
# File 'lib/rora/model/hand.rb', line 31 def hash_key j = 1 @cards.each { |card| j = j * card.rank.id } flush? ? (j * 67) : j end |
#high_card? ⇒ Boolean
Determines if this hand is a high card.
111 112 113 |
# File 'lib/rora/model/hand.rb', line 111 def high_card? type == HandType::HIGH_CARD end |
#id ⇒ Object
Returns the hand id.
24 25 26 27 28 |
# File 'lib/rora/model/hand.rb', line 24 def id i = 1 @cards.each { |card| i = i * card.id } i end |
#name ⇒ Object
Returns the hand name.
53 54 55 |
# File 'lib/rora/model/hand.rb', line 53 def name resolve_hand_attribute(2) end |
#one_pair? ⇒ Boolean
Determines if this hand has one pair.
106 107 108 |
# File 'lib/rora/model/hand.rb', line 106 def one_pair? type == HandType::ONE_PAIR end |
#probability ⇒ Object
Returns the probability of this hand being dealt.
48 49 50 |
# File 'lib/rora/model/hand.rb', line 48 def probability resolve_hand_attribute(3) end |
#score ⇒ Object
Returns the hand strength, from 1 (strongest) to 7462 (weakest).
43 44 45 |
# File 'lib/rora/model/hand.rb', line 43 def score resolve_hand_attribute(0) end |
#straight? ⇒ Boolean
Determines if this hand is a straight.
91 92 93 |
# File 'lib/rora/model/hand.rb', line 91 def straight? type == HandType::STRAIGHT end |
#straight_flush? ⇒ Boolean
Determines if this hand is a straight flush.
76 77 78 |
# File 'lib/rora/model/hand.rb', line 76 def straight_flush? type == HandType::STRAIGHT_FLUSH end |
#three_of_a_kind? ⇒ Boolean
Determines if this hand is a three of a kind.
96 97 98 |
# File 'lib/rora/model/hand.rb', line 96 def three_of_a_kind? type == HandType::THREE_OF_A_KIND end |
#two_pair? ⇒ Boolean
Determines if this hand is two pair.
101 102 103 |
# File 'lib/rora/model/hand.rb', line 101 def two_pair? type == HandType::TWO_PAIR end |
#type ⇒ Object
Returns the hand type.
58 59 60 |
# File 'lib/rora/model/hand.rb', line 58 def type HandType.get resolve_hand_attribute(1) end |
#value ⇒ Object
Returns the hand value.
38 39 40 |
# File 'lib/rora/model/hand.rb', line 38 def value @cards.map { |card| "#{card.key}" }.join(",") end |