-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.rb
More file actions
61 lines (41 loc) · 983 Bytes
/
player.rb
File metadata and controls
61 lines (41 loc) · 983 Bytes
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require_relative 'card_game_utils.rb'
require_relative 'card.rb'
class Player
attr_accessor :name
attr_accessor :cards
attr_accessor :funds
def initialize(name)
@cards = []
@name = name
@funds = CardGameUtils::PLAYER_INITIAL_FUNDS
end
def hit(card)
if @cards.empty?
@cards[0] = card
else
@cards.push(card)
end
end
def clear_cards
if !@cards.empty?
@cards.clear
end
end
def total_score
score = 0
@cards.each { |card| score += card.value }
return score if score <= CardGameUtils::BLACKJACK_SCORE
# Have gone bust
# - check if we have any Aces with high value and change to low value
@cards.count {|card| card.value == 11 }.times do
score -= 10
break if score <= CardGameUtils::BLACKJACK_SCORE
end
score
end
def to_s
str = "#{@name} Cards: "
@cards.each { |card| str += card.to_s + ' '}
str += "Total score: #{total_score}"
end
end