-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubydice.rb
executable file
·96 lines (81 loc) · 2.09 KB
/
rubydice.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
# Copyright (c) 2013 AJ Acevedo | http://ajacevedo.com
# This content is released under the MIT License.
# http://www.opensource.org/licenses/mit-license.php
# Version: 0.2.3
# Load the library files
$LOAD_PATH << './lib'
require 'colorize.rb'
# Receive input for number of sides to the dice
puts `clear`
puts 'RubyDice - Let\'s roll some dice!'
puts ''
puts 'How many sides should the dice have?'.green
@sides = nil
@sides = gets.chomp
# Begins the sides validation
def sides_empty_validate
while @sides.empty? do
puts 'No sides specified.'.red
puts 'How many sides should the dice have?'.green
@sides = nil
@sides = gets.chomp
sides_invalid_validate
end
end
def sides_invalid_validate
while @sides =~ /\D/ do
puts 'Invalid character'.red
puts 'Please specify the number of sides on each dice (ie: 3)'.green
@sides = nil
@sides = gets.chomp
sides_empty_validate
end
end
sides_empty_validate
sides_invalid_validate
# Receive input for the number of dice to roll
puts ''
puts "How many #{@sides} sided dice will you be rolling?".green
@dice = nil
@dice = gets.chomp
# Begins the dice validation
def dice_empty_validate
while @dice.empty? do
puts 'No dice specified.'.red
puts "How many #{@sides} sided dice will you be rolling?".green
@dice = nil
@dice = gets.chomp
dice_invalid_validate
end
end
def dice_invalid_validate
while @dice =~ /\D/ do
puts 'Invalid character'.red
puts 'Please specify the number of dice to roll (ie: 3)'.green
@dice = nil
@dice = gets.chomp
dice_empty_validate
end
end
dice_empty_validate
dice_invalid_validate
puts ''
puts "When you are ready to roll #{@dice}, #{@sides} sided dice, press Enter to continue:"
waiting = gets.chomp
# Here is the magic behind the dice generator
puts 'Your Roll'.underline + ':'
dice_count = 1
total = 0
@sides = @sides.to_i
@dice = @dice.to_i
while dice_count <= @dice
roll = 1 + rand(@sides)
puts roll
dice_count += 1
total += roll.to_i
end
puts ''
# Calculate the total sum of all dice
puts 'Total Score'.underline + ': ' + "#{total}"
puts ''