Class: Blackjack::Hand
- Inherits:
-
Object
- Object
- Blackjack::Hand
- Defined in:
- lib/blackjack/hand.rb
Instance Attribute Summary collapse
-
#cards ⇒ Object
readonly
Returns the value of attribute cards.
Instance Method Summary collapse
- #account_for_aces(value) ⇒ Object
- #bust? ⇒ Boolean
- #cards_value ⇒ Object
- #contains_ace? ⇒ Boolean
-
#initialize(cards = []) ⇒ Hand
constructor
A new instance of Hand.
Constructor Details
#initialize(cards = []) ⇒ Hand
Returns a new instance of Hand.
4 5 6 |
# File 'lib/blackjack/hand.rb', line 4 def initialize(cards = []) @cards = cards end |
Instance Attribute Details
#cards ⇒ Object (readonly)
Returns the value of attribute cards.
3 4 5 |
# File 'lib/blackjack/hand.rb', line 3 def cards @cards end |
Instance Method Details
#account_for_aces(value) ⇒ Object
13 14 15 |
# File 'lib/blackjack/hand.rb', line 13 def account_for_aces(value) value < 12 && contains_ace? ? value + 10 : value end |
#bust? ⇒ Boolean
21 22 23 |
# File 'lib/blackjack/hand.rb', line 21 def bust? cards_value > 21 end |
#cards_value ⇒ Object
8 9 10 11 |
# File 'lib/blackjack/hand.rb', line 8 def cards_value value = cards.inject(0) { |sum, card| sum + card.value } account_for_aces(value) end |
#contains_ace? ⇒ Boolean
17 18 19 |
# File 'lib/blackjack/hand.rb', line 17 def contains_ace? cards.any? { |card| card.is_ace? } end |