-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson.rb
More file actions
50 lines (40 loc) · 838 Bytes
/
person.rb
File metadata and controls
50 lines (40 loc) · 838 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
# frozen_string_literal: true
require './bank'
class Person
FORMAT_PERSON_NAME = /[a-z]+/i.freeze
BLACKJACK = 21
attr_reader :name, :cards, :deposit
def initialize(name)
@name = name
@cards = []
@deposit = Bank.new
validate!
end
def bet
@deposit.bet!
end
def get_money(amount)
@deposit.add!(amount)
end
def drop_card
cards.clear
end
def show_cards
cards.join(',')
end
def sum_points
value = 0
aces_count = 0
@cards.each do |card|
value += card.cost
aces_count += 1 if card.ace?
end
value -= 10 if aces_count == 1 && value > BLACKJACK
value -= 10 if aces_count == 2
value -= 20 if aces_count == 3
value
end
def validate!
raise ArgumentError, "Name #{name} player is not valid" if name !~ FORMAT_PERSON_NAME
end
end