Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment