I did this to see how much Ruby I could remeber. Turns out this is pretty simple.
- puts "What sort of dice do you want to roll (I.E. How many sides)?"
- sides = gets.chomp.to_i
- puts "How many times should the roll be repeated?"
- rolls = gets.chomp.to_i
- puts "Should the output be logged to a text file? (if so please speicify a filename now, minus the file extension.)"
- fname = gets.chomp
- if fname == ""
- if sides == 1
- puts "Rolling a die with " + sides.to_s + " sides " + rolls.to_s + " times."
- else
- puts "Rolling dice with " + sides.to_s + " sides " + rolls.to_s + " times."
- end
- result = rand(sides) + 1
- while rolls > 0
- result = rand(sides) + 1
- puts result.to_s
- rolls = rolls - 1
- end
- else
- logf = fname + ".txt"
- log = File.open(logf, 'w')
- if sides == 1
- puts "Rolling a die with " + sides.to_s + " sides " + rolls.to_s + " times and logging it to a file called " + fname + ".txt."
- else
- puts "Rolling dice with " + sides.to_s + " sides " + rolls.to_s + " times and logging it to a file called " + fname + ".txt."
- end
- log.write("Rolling dice with " + sides.to_s + " sides " + rolls.to_s + " times and logging it to a file called " + fname + ".txt.\n")
- while rolls > 0
- result = rand(sides) + 1
- puts result.to_s
- log.write(result.to_s + ", ")
- rolls = rolls - 1
- end
- log.close
- end
I forgot, of course, to comment it at all.