-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice_roller_i5.rb
47 lines (34 loc) · 874 Bytes
/
dice_roller_i5.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
class DiceRoller
def initialize(num_of_rolls, expected_rolls_value)
@num_of_rolls = num_of_rolls
@expected_rolls_value = expected_rolls_value
end
def main
loop do
@rolls = []
num_of_rolls.times do
rolls.push(roll)
end
if (1..7).include?(roll(1..30)) && roll(1..30) == 1
remove_rolled_number(rolls.min)
remove_rolled_number(rolls.min)
else
remove_rolled_number(rolls.min)
remove_rolled_number(rolls.max)
end
break if rolls.sum >= expected_rolls_value
end
rolls.push(roll)
puts rolls
end
private
attr_accessor :num_of_rolls, :expected_rolls_value, :rolls
def remove_rolled_number(number)
rolls.delete_at(rolls.index(number))
end
def roll(range=1..20)
rand(range)
end
end
dice_roller = DiceRoller.new(7, 55)
dice_roller.main