Created
October 12, 2009 17:18
-
-
Save Raynes/208549 to your computer and use it in GitHub Desktop.
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 GuessingGame | |
attr_reader :tries, :max, :num_of_tries | |
def initialize(tries, max) | |
@tries, @max, @num_of_tries = tries, max, 0 | |
@num = rand(max + 1) | |
end | |
def play(guess) | |
if guess == @num | |
@num_of_tries += 1 | |
@tries = @num_of_tries | |
puts "You guessed correct! \nAnswer: #@num \nTries: #@num_of_tries" | |
else | |
@num_of_tries += 1 | |
puts "Wrong. \nTries remaining: #{@tries - @num_of_tries}" | |
puts "\nYou lose." if @num_of_tries == @tries | |
end | |
end | |
def game | |
while @num_of_tries < @tries | |
puts "Enter a number!" | |
self.play(Integer(gets)) | |
end | |
end | |
end | |
puts "Enter number of tries for this game, followed by the max rand number for this game." | |
my_game = GuessingGame.new(Integer(gets), Integer(gets)) | |
my_game.game |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment