I am sorry if this is the wrong place. I am a complete noob. I just started a class online and got an idea for my own program. If anyone has time....I can't figure out how to have my program start over. Everything seems to run well, but it wont start over. When I try to put the Rolldie variable into the userRoll method it gives me an error because a constant can't change. I am guessing I need to make a class maybe? def rollD6 1+rand(6) end def rollD4 1+rand(4) end def rollD8 1+rand(8) end def rollD10 1+rand(10) end def rollD12 1+rand(12) end def rollD20 1+rand(20) end def rollD100 1+rand(100) end def userRoll puts "Please choose which die you would like to roll: 1: D4, 2: D6, 3: D8, 4: D10, 5: D12, 6: D20, 7: D100" end userRoll Rolldie = gets.chomp if Rolldie == "1" puts rollD4 elsif Rolldie == "2" puts rollD6 elsif Rolldie == "3" puts rollD8 elsif Rolldie == "4" puts rollD10 elsif Rolldie == "5" puts rollD12 elsif Rolldie == "6" puts rollD20 elsif Rolldie == "7" puts rollD100 else puts "You didn't select a number 1 through 7!" end puts "Would you like to roll again? Y or N" rollAgain = gets.chomp.downcase if rollAgain == "y" userRoll <==============THIS IS THE PROBLEM elsif rollAgain == "n" puts "Thank you for rolling!" else puts "You need to type Y or N" end
on 2016-08-08 00:48
on 2016-08-08 07:54
Hi Oli, you've almost answered your own question- how can I get something to start over? What about a loop? while(true) ... break unless rollAgain end The traditional way of doing this is with a do-while: do # roll dice # want to play again? while (rollAgain == "yes") You might end up turning this into a class like DiceMaster, but you may not have to. What would a DiceMaster do? Ask which dice to use (and use the right dice.) Roll the dice. (Maybe the dice roll themselves, like: d6.roll) Say, Good luck! So, you could have two classes: Dice and DiceMaster: class Dice def initialize(size) @size = size end def roll 1+rand(@size) end end class DiceMaster def initialize which_dice end def which_dice puts "Which dice would you ...?" ... @dice = Dice.new(Rolldie.to_i) end def roll @dice.roll end def play_again? puts "Would you like to ...?" if roll_again == "y" true elsif roll_again == "n" false else puts "Must be Y or N" play_again end end end do dm = DiceMaster.new dm.roll while dm.play_again? Which feels like DiceMaster is not a very useful class.
on 2016-08-08 14:39
Ahhh perfect Joe. Thank you deeply. There are so many good ideas in there. Thanks again and have a good one!
on 2016-09-04 02:22
You can shorten this if you use a case menu by the way. case roll_again when "y" true when "n" false end At the least for long but simple if/else checks, this works very well. I love case/when menus. I'd wish they would become first class objects within objects/classes so that they can be accessed and queried too.
on 2016-09-04 15:03
Robert Heiler wrote in post #1184952: > You can shorten this if you use a case menu by the way. > > case roll_again > when "y" > true > when "n" > false > end > > At the least for long but simple if/else checks, > this works very well. > > I love case/when menus. > > I'd wish they would become first class objects within > objects/classes so that they can be accessed and queried too. Thanks for the reply Robert. I actually just learned the case command and love it. I finally got it to loop by adding a loop towards the end. For some reason I kept trying to loop the whole thing. I also ended up making a class with several methods. I now have a fully functioning D n D dice roller! Too bad its not the 80s...I could have really used it. I intended on adding an interface and then trying to convert it to be a free app on Android. Thanks for all the help guys.
on 2016-09-04 20:55
Well, if you really wanted to shorten the yes/no thing... !!(roll_again =~ /y/i)