-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice_roller_i8.rb
60 lines (46 loc) · 1.17 KB
/
dice_roller_i8.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
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
self.results = []
# num_of_rolls.times { results.push(roll) }
roller(7)
if (1..7).include?(roll(30)) && roll(30) == 1
2.times { remove_rolled_number(results.min) }
else
remove_rolled_number(results.min)
remove_rolled_number(results.max)
end
break if results.sum >= expected_rolls_value
end
results.push(roll)
puts "Your randomly generated set: #{results.join(' ')}. Are you happy?"
if gets.chomp != 'YES!'
self.main
else
return
end
end
private
attr_accessor :num_of_rolls, :expected_rolls_value, :results
def remove_rolled_number(number)
results.delete_at(results.index(number))
end
def roll(max=20)
rand(1..max)
end
def roller(rolls, max_range=20, iterator=1)
if iterator >= rolls
return
else
results.push(roll(max_range))
iterator += 1
self.roller(rolls, max_range, iterator)
end
end
end
dice_roller = DiceRoller.new(7, 55)
dice_roller.main