Last active
December 13, 2015 17:48
-
-
Save nibanez80/4950293 to your computer and use it in GitHub Desktop.
Exercise: Build a simple guessing game
Create a GuessingGame class which is initialized with an integer called answer. Define an instance method GuessingGame#guess which takes an integer called guess as its input. guess should return the symbol :high if the guess is larger than the answer, :correct if the guess is equal to the answer, and :low i…
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_accessor :answer, :high, :low, :correct, :guess, :last_guess, :last_result | |
| def initialize(answer) | |
| @answer = answer | |
| @last_result = nil | |
| @last_guess = nil | |
| end | |
| def guess(guess) | |
| if guess > self.answer | |
| self.last_guess = 'incorrect' | |
| return :high | |
| elsif guess == self.answer and guess != self.last_guess | |
| self.last_result = 'correct' | |
| return :correct | |
| elsif guess < self.answer | |
| self.last_guess = 'incorrect' | |
| self.last_result = 'incorrect' | |
| return :low | |
| end | |
| end | |
| def solved? | |
| if self.last_result == 'correct' | |
| true | |
| else | |
| false | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment