-
Notifications
You must be signed in to change notification settings - Fork 0
/
greed.rb
74 lines (60 loc) · 1.03 KB
/
greed.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Greed
def initialize
@counts = [0,0,0,0,0,0]
end
def score_of(*dice)
dice.inject(0) do |running_total, die_value|
running_total + score_of_one(die_value)
end
end
def score_of_one(die_value)
die = DieFactory.create_die(die_value)
score = die.individual_value
if third_of(die_value)
score += die.triplett_bonus
end
score
end
def third_of(die_value)
@counts[die_value-1] += 1
@counts[die_value-1] == 3
end
end
class DieFactory
def self.create_die(die_value)
if die_value == 1
DieOfOne.new
elsif die_value == 5
DieOfFive.new
else
DieOfNonOneOrFive.new(die_value)
end
end
end
class DieOfOne
def individual_value
100
end
def triplett_bonus
700
end
end
class DieOfFive
def individual_value
50
end
def triplett_bonus
350
end
end
class DieOfNonOneOrFive
def initialize(die_value)
@die_value = die_value
end
def individual_value
0
end
def triplett_bonus
@die_value * 100
end
end