Created
May 26, 2011 14:21
-
-
Save FrancoB411/993237 to your computer and use it in GitHub Desktop.
number_guesser.rb
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
| MAX_NUMBER = 50 | |
| MIN_NUMBER = 0 | |
| GUESS_LIMIT = 5 | |
| @computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER | |
| def over_guess_limit? | |
| if @guess_count > GUESS_LIMIT | |
| puts "You're over the guess limit!" | |
| true | |
| end | |
| end | |
| def guess_valid? | |
| !over_guess_limit? && !quit? | |
| end | |
| def quit? #checks to see if player has commanded game to quit. | |
| @command == "quit" || @name == "quit" | |
| end | |
| def abort #aborts the game | |
| puts "bye, see ya later" | |
| end | |
| def game #tells you if your guess it too low, too high | |
| loop do | |
| puts "What is your guess?" | |
| @command = gets.chomp | |
| @guess_count += 1 | |
| if guess_valid? | |
| guess = @command.to_i | |
| else | |
| abort | |
| break | |
| end | |
| if guess < @computers_number | |
| puts "Your guess was too low!" | |
| elsif guess > @computers_number | |
| puts "Your guess was too high!" | |
| else | |
| success | |
| break | |
| end | |
| end | |
| end | |
| def start_game #starts the game, game timer, & guess_count | |
| if quit? | |
| abort | |
| return | |
| end | |
| puts "Hello #{@name}. I'm guessing a number between #{MIN_NUMBER} and #{MAX_NUMBER}." | |
| puts "You need to guess the number. I will tell you if you are too low or too high." | |
| @start_time = Time.now | |
| @guess_count = 0 | |
| game | |
| end | |
| def greeter #Gets player's name, calls start_game | |
| puts "Hello player. What is your name?" | |
| @name = gets.chomp | |
| start_game | |
| end | |
| def success #success message | |
| puts "You got the number right! It was #{@computers_number}." | |
| game_length | |
| abort | |
| end | |
| def game_length #times the duration of the game | |
| game_duration= (Time.now - @start_time).to_i | |
| puts "It took you #{game_duration} seconds to guess!" | |
| end | |
| greeter |
Author
error?
oh I see , its when you say quit.
I am not sure what abort does, but clearly not what you want as the code keeps executing. try a break instead, as you do if you guess correctly.
oh, I see, abort is your method. not sure if putting the break in abort would work, but you could put it after you call abort.
MAX_NUMBER = 50
MIN_NUMBER = 49
@computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER
puts "Hello player. What is your name?"
name = gets
name = name.chomp
puts "Hello #{name}. I'm guessing a number between #{MIN_NUMBER} and #{MAX_NUMBER}."
puts "You need to guess the number. I will tell you if you are too low or too high."
puts "Please enter quit if you would like to leave"
def success #success message
puts "You got the number right! It was #{@computers_number}."
# abort
end
def abort #aborts the game
puts "bye, see ya later"
end
loop do
puts "What is your guess?"
command = gets
if command == "quit\n"
abort
break
else
guess = command.to_i
end
if guess < @computers_number
puts "Your guess was too low!"
elsif guess > @computers_number
puts "Your guess was too high!"
else
success
end
end
Author
Thanks Charissa,
I took your abort-break pattern (nice) in the flow and used it in the else as well.
It works now, thanks a lot.
Best,
F
Author
Thanks Charissa!
Curious, if I add a comment to the gist, do you see it?
On May 26, 2011, at 11:44 AM, charissa wrote:
``` ruby
MAX_NUMBER = 50
MIN_NUMBER = 49
@computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER
puts "Hello player. What is your name?"
name = gets
name = name.chomp
puts "Hello #{name}. I'm guessing a number between #{MIN_NUMBER} and #{MAX_NUMBER}."
puts "You need to guess the number. I will tell you if you are too low or too high."
puts "Please enter quit if you would like to leave"
def success #success message
puts "You got the number right! It was ***@***.***_number}."
# abort
end
def abort #aborts the game
puts "bye, see ya later"
end
loop do
puts "What is your guess?"
command = gets
if command == "quit\n"
abort
break
else
guess = command.to_i
end
if guess < @computers_number
puts "Your guess was too low!"
elsif guess > @computers_number
puts "Your guess was too high!"
else
success
end
end
```
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/993237
Best,
Franco
Email:
FrancoB411@gmail.com
Blog:
http://francob411.posterous.com/
Facebook:
http://www.facebook.com/#!/franco.barbeite
Twitter:
http://twitter.com/FrancoB411
LinkedIn:
http://www.linkedin.com/in/francob411
Yes
…Sent from my iPhone
On May 26, 2011, at 12:00 PM, ***@***.*** wrote:
Thanks Charissa!
Curious, if I add a comment to the gist, do you see it?
On May 26, 2011, at 11:44 AM, charissa wrote:
> ``` ruby
> MAX_NUMBER = 50
> MIN_NUMBER = 49
>
>
> @computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER
>
> puts "Hello player. What is your name?"
> name = gets
> name = name.chomp
> puts "Hello #{name}. I'm guessing a number between #{MIN_NUMBER} and #{MAX_NUMBER}."
> puts "You need to guess the number. I will tell you if you are too low or too high."
> puts "Please enter quit if you would like to leave"
> def success #success message
> puts "You got the number right! It was ***@***.***_number}."
> # abort
> end
>
> def abort #aborts the game
> puts "bye, see ya later"
> end
>
> loop do
> puts "What is your guess?"
> command = gets
>
> if command == "quit\n"
> abort
> break
> else
> guess = command.to_i
> end
>
> if guess < @computers_number
> puts "Your guess was too low!"
> elsif guess > @computers_number
> puts "Your guess was too high!"
> else
> success
> end
> end
> ```
> ##
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/993237
Best,
Franco
Email:
***@***.***
Blog:
http://francob411.posterous.com/
Facebook:
http://www.facebook.com/#!/franco.barbeite
Twitter:
http://twitter.com/FrancoB411
LinkedIn:
http://www.linkedin.com/in/francob411
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/993237
I only get an email after I make a comment In the thread. Otherwise i see what you do in my github dashboard. Because I follow you.
…Sent from my iPhone
On May 26, 2011, at 12:00 PM, ***@***.*** wrote:
Thanks Charissa!
Curious, if I add a comment to the gist, do you see it?
On May 26, 2011, at 11:44 AM, charissa wrote:
> ``` ruby
> MAX_NUMBER = 50
> MIN_NUMBER = 49
>
>
> @computers_number = rand(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER
>
> puts "Hello player. What is your name?"
> name = gets
> name = name.chomp
> puts "Hello #{name}. I'm guessing a number between #{MIN_NUMBER} and #{MAX_NUMBER}."
> puts "You need to guess the number. I will tell you if you are too low or too high."
> puts "Please enter quit if you would like to leave"
> def success #success message
> puts "You got the number right! It was ***@***.***_number}."
> # abort
> end
>
> def abort #aborts the game
> puts "bye, see ya later"
> end
>
> loop do
> puts "What is your guess?"
> command = gets
>
> if command == "quit\n"
> abort
> break
> else
> guess = command.to_i
> end
>
> if guess < @computers_number
> puts "Your guess was too low!"
> elsif guess > @computers_number
> puts "Your guess was too high!"
> else
> success
> end
> end
> ```
> ##
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/993237
Best,
Franco
Email:
***@***.***
Blog:
http://francob411.posterous.com/
Facebook:
http://www.facebook.com/#!/franco.barbeite
Twitter:
http://twitter.com/FrancoB411
LinkedIn:
http://www.linkedin.com/in/francob411
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/993237
Author
Added Pascal Levy-Garboua's timer to give you the duration of the time it took you to guess the numbers.
Author
Re-factored for clarity. Added the ability to limit the number of player's guesses.
Author
Removed a good number of code lines thanks to Peter Cooper's suggestions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The error:
'program.rb:32:in
block in <main>': undefined method<' for nil:NilClass (NoMethodError)from program.rb:22:in
loop' from program.rb:22:inFrancos-Desktop:~ julian$