Class: Ventiuna::Hand
- Inherits:
-
Object
- Object
- Ventiuna::Hand
- Defined in:
- lib/ventiuna/hand.rb
Instance Attribute Summary collapse
-
#cards ⇒ Object
Returns the value of attribute cards.
Instance Method Summary collapse
- #<<(another_card) ⇒ Object (also: #hit)
- #active? ⇒ Boolean
- #blackjack? ⇒ Boolean
- #bust? ⇒ Boolean
-
#initialize(card = [], *other_cards) ⇒ Hand
constructor
A new instance of Hand.
- #pair? ⇒ Boolean
- #soft? ⇒ Boolean
- #split ⇒ Object
- #stand ⇒ Object
- #to_db ⇒ Object
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(card = [], *other_cards) ⇒ Hand
Returns a new instance of Hand.
5 6 7 8 9 |
# File 'lib/ventiuna/hand.rb', line 5 def initialize(card=[], *other_cards) @cards = Array(card) + other_cards @soft = false @active = true end |
Instance Attribute Details
#cards ⇒ Object
Returns the value of attribute cards.
3 4 5 |
# File 'lib/ventiuna/hand.rb', line 3 def cards @cards end |
Instance Method Details
#<<(another_card) ⇒ Object Also known as: hit
11 12 13 |
# File 'lib/ventiuna/hand.rb', line 11 def << (another_card) self.cards << another_card end |
#active? ⇒ Boolean
36 37 38 |
# File 'lib/ventiuna/hand.rb', line 36 def active? @active end |
#blackjack? ⇒ Boolean
40 41 42 |
# File 'lib/ventiuna/hand.rb', line 40 def blackjack? self.value == 21 && self.cards.size == 2 end |
#bust? ⇒ Boolean
44 45 46 |
# File 'lib/ventiuna/hand.rb', line 44 def bust? self.value > 21 #&& !self.soft? end |
#pair? ⇒ Boolean
48 49 50 |
# File 'lib/ventiuna/hand.rb', line 48 def pair? self.cards.size == 2 && (self.cards.first.value == self.cards.last.value) end |
#soft? ⇒ Boolean
52 53 54 55 |
# File 'lib/ventiuna/hand.rb', line 52 def soft? self.value @soft end |
#split ⇒ Object
20 21 22 23 |
# File 'lib/ventiuna/hand.rb', line 20 def split return false unless self.pair? [Ventiuna::Hand.new(self.cards.first), Ventiuna::Hand.new(self.cards.last)] end |
#stand ⇒ Object
16 17 18 |
# File 'lib/ventiuna/hand.rb', line 16 def stand @active = false end |
#to_db ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/ventiuna/hand.rb', line 61 def to_db s = "#{value}" s += "I" if cards.size == 2 s += "S" if soft? s += "P" if pair? s end |
#to_s ⇒ Object
57 58 59 |
# File 'lib/ventiuna/hand.rb', line 57 def to_s "#{self.cards.collect{|c| c.to_s}} = #{self.value}" end |
#value ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ventiuna/hand.rb', line 25 def value sum = 0 self.cards.sort.each_with_index do |card, i| # hit low if haven't reached 11 or there are more cards left hit_low = (sum >= 11) || (i+1 != self.cards.size) @soft = card.ace? && !hit_low sum += card.value(low: hit_low) end sum end |