Class: Hand

Inherits:
Object
  • Object
show all
Defined in:
lib/blackjack/hand.rb

Direct Known Subclasses

DealerHand, PlayerHand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blackjack) ⇒ Hand

Returns a new instance of Hand.



6
7
8
9
10
# File 'lib/blackjack/hand.rb', line 6

def initialize(blackjack)
  @blackjack = blackjack
  @played = false
  @cards = []
end

Instance Attribute Details

#blackjackObject

Returns the value of attribute blackjack.



4
5
6
# File 'lib/blackjack/hand.rb', line 4

def blackjack
  @blackjack
end

#cardsObject

Returns the value of attribute cards.



4
5
6
# File 'lib/blackjack/hand.rb', line 4

def cards
  @cards
end

#playedObject

Returns the value of attribute played.



4
5
6
# File 'lib/blackjack/hand.rb', line 4

def played
  @played
end

Instance Method Details

#blackjack?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/blackjack/hand.rb', line 20

def blackjack?
  cards.size == 2 && value(:soft) == 21
end

#busted?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/blackjack/hand.rb', line 12

def busted?
  value(:soft) > 21
end

#deal_cardObject



16
17
18
# File 'lib/blackjack/hand.rb', line 16

def deal_card
  cards << blackjack.shoe.next_card
end

#value(count_method, hide_first_card: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blackjack/hand.rb', line 24

def value(count_method, hide_first_card: false)
  total = 0
  cards.each_with_index do |card, index|
    next if index.zero? && hide_first_card

    total += Card.value(card, count_method, total)
  end

  return value(:hard, hide_first_card:) if count_method == :soft && total > 21

  total
end