Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Nicks-Game/README.md
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
```
42 changes: 42 additions & 0 deletions Nicks-Game/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'optparse'
Copy link
Member

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.

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!'
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

The 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
21 changes: 21 additions & 0 deletions Nicks-Game/game.rb
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
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of the spaceship operator

end
end
8 changes: 8 additions & 0 deletions Nicks-Game/game_settings.rb
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