carolineartz
1/20/2014 - 7:50 AM

Exercise: Build a simple guessing gameCreate a GuessingGame class which is initialized with an integer called answer.Define an instance meth

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 the guess is lower than the answer.Define an instance method GuessingGame#solved? which returns true if the most recent guess was correct and false otherwise.For example,game = GuessingGame.new(10)game.solved? # => falsegame.guess(5) # => :lowgame.guess(20) # => :highgame.solved? # => falsegame.guess(10) # => :correctgame.solved? # => true


####################################### 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.