-
Notifications
You must be signed in to change notification settings - Fork 30
Nick's game #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Nick's game #4
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Nick's Game: Guessing Game | ||
| ``` | ||
| Usage: ruby cli.rb [options] | ||
| -m, --max MAX set the maximum number to guess from | ||
| -g, --guess_limit GUESS_LIMIT set the number of guesses allowed | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require 'optparse' | ||
| require_relative 'game' | ||
| require_relative 'game_settings' | ||
|
|
||
| options = {} | ||
| OptionParser.new do |opts| | ||
| opts.banner = 'Usage: ruby cli.rb [options]' | ||
|
|
||
| opts.on('-m', '--max MAX', 'set the maximum number to guess from') do |v| | ||
| options[:max] = v.to_i | ||
| end | ||
|
|
||
| opts.on('-g', '--guess_limit GUESS_LIMIT', 'set the number of guesses allowed') do |v| | ||
| options[:guess_limit] = v.to_i | ||
| end | ||
| end.parse! | ||
|
|
||
| game = Game.new options | ||
|
|
||
| puts 'Welcome to the guessing game!' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. though these lines are perfectly fine. remember you can also do interpolation from within heredocs in ruby when working with multiline strings.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I totally forgot about that, good point. |
||
| puts "You have #{game.settings.guess_limit || 'unlimited'} tries to guess a number from 1 to #{game.settings.max}." | ||
| loop do | ||
| if game.guesses_remaining == 0 | ||
| puts 'You ran out of tries... You lose!' | ||
| exit | ||
| else | ||
| if game.guesses_remaining | ||
| print "Guess #{game.guesses_used + 1} of #{game.settings.guess_limit}: " | ||
| else | ||
| print "Guess #{game.guesses_used + 1}: " | ||
| end | ||
|
|
||
| result = game.guess gets.to_i | ||
| case result | ||
| when 0 | ||
| puts 'You won!' | ||
| exit | ||
| when -1 then puts 'Higher...' | ||
| when 1 then puts 'Lower...' | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| require_relative 'game_settings' | ||
|
|
||
| class Game | ||
| attr_reader :settings, :guesses_used, :guesses_remaining | ||
| def initialize(options = {}) | ||
| @settings = GameSettings.new(options) | ||
| @winning_number = rand([email protected]) | ||
| @guesses_used = 0 | ||
| @guesses_remaining = @settings.guess_limit | ||
| end | ||
|
|
||
| def guess(number) | ||
| if @guesses_remaining.is_a? Numeric | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my assumption is that you're checking for nil here? there are better ways to accomplish this in ruby.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that there was another edge case I might have been aiming for, but it's probably just the nil check. |
||
| raise RunTimeError, 'No guesses remaining' unless @guesses_remaining > 0 | ||
| @guesses_remaining -= 1 | ||
| @guesses_used += 1 | ||
| end | ||
|
|
||
| number <=> @winning_number | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of the spaceship operator |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class GameSettings | ||
| attr_reader :max, :guess_limit | ||
|
|
||
| def initialize(max: 10, guess_limit: nil) | ||
| @max = max | ||
| @guess_limit = guess_limit | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool. you could have also used ARGV here and rolled your own.