Skip to content

Instantly share code, notes, and snippets.

@Raynes
Created October 12, 2009 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynes/208549 to your computer and use it in GitHub Desktop.
Save Raynes/208549 to your computer and use it in GitHub Desktop.
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