Created
September 29, 2013 14:33
-
-
Save cnocon/6753028 to your computer and use it in GitHub Desktop.
Creating a class of Die in ruby and allowing users to cheat or play fair :) From Chris Pine's Learn to Program book
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Die | |
| def initialize #this will run as soon as a new instance of the Die class (a Die object) is called | |
| puts "Do you want to cheat, or play fair? To cheat, type 'cheat' and press enter. Otherwise just press enter." | |
| response = gets.chomp | |
| if response == 'cheat' | |
| cheat | |
| else | |
| roll | |
| end | |
| end | |
| def roll | |
| @number_showing = 1 + rand(6) | |
| end | |
| def showing | |
| puts "You rolled a #{@number_showing}" | |
| end | |
| def cheat | |
| puts "What do you want to roll? (1-6)" | |
| number = gets.chomp.to_i | |
| if number <= 6 && number >= 1 | |
| @number_showing = number | |
| else | |
| puts "That isn't between 1 and 6 you stupid cheater." | |
| end | |
| @number_showing | |
| end | |
| end | |
| Die.new.showing #lets us know what we rolled, even if we cheated and we already know :) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment