Guest User

Untitled

a guest
Mar 13th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. class GuessingGame
  2.  
  3. attr_reader :tries, :max, :num_of_tries
  4.  
  5. def initialize(tries, max)
  6. @tries, @max, @num_of_tries = tries, max, 0
  7. @num = rand(max + 1)
  8. end
  9.  
  10. def play(guess)
  11. if guess == @num
  12. @num_of_tries += 1
  13. @tries = @num_of_tries
  14. puts "You guessed correct! \nAnswer: #@num \nTries: #@num_of_tries"
  15. else
  16. @num_of_tries += 1
  17. puts "Wrong. \nTries remaining: #{@tries - @num_of_tries}"
  18. puts "\nYou lose." if @num_of_tries == @tries
  19. end
  20. end
  21.  
  22. def game
  23. while @num_of_tries < @tries
  24. puts "Enter a number!"
  25. self.play(Integer(gets))
  26. end
  27. end
  28. end
  29.  
  30. puts "Enter number of tries for this game, followed by the max rand number for this game."
  31. my_game = GuessingGame.new(Integer(gets), Integer(gets))
  32. my_game.game
Add Comment
Please, Sign In to add comment