Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. class Dice
  4. attr_accessor :a
  5.  
  6. def initialize
  7. @a = ARGV.first
  8. end
  9.  
  10. def welcome
  11. puts "Simple dice roll emulator"
  12. puts "--------------------------"
  13.  
  14. if @a == nil
  15. puts "Type '--help' for commands list"
  16. elsif @a == "--help"
  17. puts "to roll a certain dice type in:"
  18. puts "d4, d6, d8, d10, d12, d16, d18, d20"
  19. puts "to quit the program type in 'q'"
  20. puts "..................................."
  21. end
  22. end
  23.  
  24. def roll
  25. @d4 = (1..4).to_a
  26. @d6 = (1..6).to_a
  27. @d8 = (1..8).to_a
  28. @d10 = (1..10).to_a
  29. @d12 = (1..12).to_a
  30. @d16 = (1..16).to_a
  31. @d18 = (1..18).to_a
  32. @d20 = (1..20).to_a
  33. end
  34.  
  35. def roll_desc
  36. puts "The roll is '#{@dice}'"
  37. puts "\n"
  38. end
  39.  
  40. def emulator
  41. welcome
  42. roll
  43. loop do
  44. puts "Pick a dice to roll:"
  45. @d = STDIN.gets.chomp
  46. @d = @d.downcase
  47. if @d.match(/[d](4|6|8|10|12|14|16|18|20)|q/i)
  48. if @d == 'd4'
  49. @dice = @d4.sample
  50. roll_desc
  51. elsif @d == 'd6'
  52. @dice = @d6.sample
  53. roll_desc
  54. elsif @d == 'd8'
  55. @dice = @d8.sample
  56. roll_desc
  57. elsif @d == 'd10'
  58. @dice = @d10.sample
  59. roll_desc
  60. elsif @d == 'd12'
  61. @dice = @d12.sample
  62. roll_desc
  63. elsif @d == 'd16'
  64. @dice = @d16.sample
  65. roll_desc
  66. elsif @d == 'd18'
  67. @dice = @d18.sample
  68. roll_desc
  69. elsif @d == 'd20'
  70. @dice = @d20.sample
  71. roll_desc
  72. elsif @d == 'q'
  73. exit
  74. end
  75. else
  76. puts "Wrong input, try again, or see 'dice.rb --help' for reference"
  77. puts "\n"
  78. end
  79. end
  80. end
  81. end
  82.  
  83. if __FILE__ == $0
  84. d = Dice.new
  85. d.emulator
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement