Forked from dbc-challenges/phase0_template_gist.rb
Last active
January 3, 2016 20:39
-
-
Save carolineartz/eb2d70193aa7fc8bc2c7 to your computer and use it in GitHub Desktop.
Exercise: Build a simple guessing gameCreate 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 if …
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
####################################### PSEUDOCODE ##################################### | |
# INPUT: initialize a game passing the correct integer answer to instantiate; | |
# pass a guess value to #guess | |
# OUPUT: #guess outputs symbol representing | |
# if the most recent guess is too high, too low, or correct | |
# #solved? outputs true if the most recent guess is correct and flase otherwise. | |
# STEPS: #initialize -> set answer to instance variable, answer | |
#guess -> return corresponding symbol for too high, too low, correct | |
#solved? -> return true if the guess is equal the answer and false oherwise. | |
###################################### INITIAL CODE #################################### | |
class GuessingGame | |
def initialize(answer) | |
@answer = answer | |
end | |
def guess(guess) | |
@guess = guess | |
if guess > @answer | |
:high | |
elsif guess == @answer | |
:correct | |
else | |
:low | |
end | |
end | |
def solved? | |
@guess == @answer | |
end | |
end | |
#################################### REFACTORED CODE ################################### | |
class GuessingGame | |
def initialize(answer) | |
@answer = answer | |
end | |
def guess(guess) | |
@guess = guess | |
if @guess == @answer | |
:correct | |
else @guess > @answer ? :high : :low | |
end | |
end | |
def solved? | |
@guess == @answer | |
end | |
end | |
###################################### DRIVER CODE ##################################### | |
game = GuessingGame.new(6) | |
p game.guess(1) == :low | |
p game.guess(7) == :high | |
p game.solved? == false | |
p game.guess(6) == :correct | |
p game.solved? == true | |
p game.guess(7) == :high | |
p game.solved? == false | |
####################################### REFLECTION ##################################### | |
#I thought this challenge was pretty easy...during refactoring I tried to play | |
#around with some things, but it was mostly around with returning true for | |
#solved? when the instance of the game has been solved, regardless of subsequent | |
#incorrect guesses, while still returning :high, :low, :correct for those | |
#guesses. If we defined the game instance as solved once guessed correctly even | |
#with future incorrect guesses and solved? didn't have a question mark in the | |
#method, we could initialize the game with an instance variable called solved | |
#set to false and include an assignment of solved = true before returning | |
#:correct within the guess. I also played with instantiating an array and | |
#pushing to it any incorrect guesses, then returning false but also printing the | |
#incorrect guess history when solved? is called before a correct guess. I know | |
#this isn't in line with the instructions of the challenge, but it made more | |
#sense to me so I played around and solved variations just for shits. Other than | |
#that, I couldn't find too much to refactor...need to check out others' code to | |
#see if there is any very different approach. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment