Skip to content

Instantly share code, notes, and snippets.

@mattdvhope
Created July 26, 2013 02:03
Show Gist options
  • Save mattdvhope/6085474 to your computer and use it in GitHub Desktop.
Save mattdvhope/6085474 to your computer and use it in GitHub Desktop.
GuessingGame working in my Terminal, but not working in Socrates. I looked through the Specs, but could not find anything wrong.
class GuessingGame
def initialize(guess)
@guess = guess
end
def guess(last_guess) # passing from last_guess = gets.chomp
@last_guess = last_guess
if @last_guess > @guess
return :high
elsif @last_guess < @guess
return :low
elsif @last_guess == @guess
return :correct
end
end
def solved?
if @last_guess == @guess
true
else
false
end
end
end
answer = 1 + rand(10)
game = GuessingGame.new(answer)
last_guess = nil
last_result = nil
until game.solved?
unless last_guess.nil?
puts "Oops! Your last guess (#{last_guess}) was #{last_result}."
puts ""
end
print "Guess a number between 1 and 10, inclusive."
last_guess = gets.chomp.to_i
last_result = game.guess(last_guess)
end
puts "#{last_guess} was correct!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment