-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand.rb
46 lines (35 loc) · 788 Bytes
/
hand.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require_relative 'card'
require_relative 'card_deck'
require_relative 'black_jack'
class Hand
include BlackJack
attr_reader :cards
attr_accessor :visible
def initialize(visible = true)
@deck = CardDeck.instance
@cards = @deck.draw_initial_hand
@visible = visible
end
def pull_card(card)
raise 'Cannot pull cards no more: hand is full.' if full?
@cards << card
end
def points
result = @cards.sum(&:value)
ace_correction(result)
end
def full?
@cards.size == 3
end
def to_s
return @cards.join(' ') if visible
@cards.map { '*' }.join(' ')
end
private
def ace_correction(points)
@cards.each do |card|
points -= Card::ACE_CORRECTION if card.ace? && points > BLACK_JACK
end
points
end
end