-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework_solution-006.rb
65 lines (60 loc) · 1.13 KB
/
homework_solution-006.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
class Die
attr_accessor :face
attr_reader :sides
def initialize( args = {} )
args = defaults.merge(args)
@sides = args[:sides]
end
def defaults
{ sides: 4 }
end
def roll
@face = rand(1..sides)
@face
end
def tell_properties
"The die has face #{face} and #{sides} sides.\n"
end
end
class DiceGame
attr_reader :dies
def initialize
@dies = []
end
def add_die(sides)
dies << Die.new(sides: sides)
end
def roll_all
dies.each { |die| die.roll }
end
def on_the_table
if dies == []
return "No dies were added to the game, yet."
else
dies.map(&:tell_properties).join
end
end
end
class DiceGameInterface
def initialize
@dice_game = DiceGame.new
start
return nil
end
def start
while true
p "Enter number of dice sides for your next dice"
input = gets.chomp.to_i
if input >= 4
@dice_game.add_die(input)
"Die with #{input} sides added to game."
elsif input == 0
@dice_game.roll_all
puts @dice_game.on_the_table
break
else
"Wrong input"
end
end
end
end