Skip to content

Instantly share code, notes, and snippets.

@Raynes Raynes/gist:208549
Created Oct 12, 2009

Embed
What would you like to do?
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
You can’t perform that action at this time.