/stylish_code.rb Secret
Created
August 31, 2014 09:07
-
Star
(0)
You must be signed in to star a gist -
Fork
(154)
You must be signed in to fork a gist
-
-
Save nextacademy-private/05f003697d8207fcf6b6 to your computer and use it in GitHub Desktop.
Stylish Code
This file contains 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 Guessing_game | |
VALID_Numbers = (1..100).to_a # Store valid answers in an array | |
def initialize answer; @answer = answer | |
@solved = false | |
# Validate input | |
raise "Answer must be between 1 and 100" unless VALID_Numbers.include? @answer | |
end | |
def guess ( number ) | |
if number == @answer # Check if the two are equal | |
@solved = true | |
:correct | |
elsif (number > @answer) # Check if the guess is higher | |
@solved = false | |
return :high | |
elsif(number<@answer) # Check if the guess is lower | |
@solved = false | |
:low | |
end | |
end | |
def solved? | |
@solved | |
end | |
end | |
game = Guessing_game.new(10) | |
# This following should print out a whole bunch of "true" | |
puts game.guess(5) == :low | |
puts game.guess(15) == :high | |
puts game.solved? == false | |
puts game.guess(10) == :correct | |
puts game.solved? == true | |
puts game.guess(2) == :low | |
puts game.solved? == false | |
begin | |
Guessing_game.new(200) | |
rescue RuntimeError => e | |
puts e.to_s == "Answer must be between 1 and 100" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment