-
Notifications
You must be signed in to change notification settings - Fork 499
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
Bowling challenge in Ruby #391
base: main
Are you sure you want to change the base?
Changes from 4 commits
21a354f
3e30f6f
481ee69
a4561bd
91b8af9
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 @@ | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
# gem "rails" | ||
|
||
gem "rspec", "~> 3.12" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
diff-lcs (1.5.0) | ||
rspec (3.12.0) | ||
rspec-core (~> 3.12.0) | ||
rspec-expectations (~> 3.12.0) | ||
rspec-mocks (~> 3.12.0) | ||
rspec-core (3.12.2) | ||
rspec-support (~> 3.12.0) | ||
rspec-expectations (3.12.3) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.12.0) | ||
rspec-mocks (3.12.5) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.12.0) | ||
rspec-support (3.12.0) | ||
|
||
PLATFORMS | ||
arm64-darwin-22 | ||
|
||
DEPENDENCIES | ||
rspec (~> 3.12) | ||
|
||
BUNDLED WITH | ||
2.4.12 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require_relative 'lib/game' | ||
require_relative 'lib/frame' | ||
|
||
game = Game.new | ||
game.run_game | ||
game.calculate_score | ||
puts "Total score: #{game.grand_total}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class Frame | ||
attr_accessor :rolls, :bonus_points | ||
|
||
def initialize(io=Kernel) | ||
@io = io | ||
@rolls = [] | ||
@bonus_points = 0 | ||
end | ||
|
||
def play_regular_frame | ||
roll_first_ball | ||
if @rolls.first == 10 | ||
print_strike_message | ||
return | ||
end | ||
roll_second_ball | ||
print_spare_message if @rolls.inject(:+) == 10 | ||
end | ||
|
||
def play_last_frame | ||
roll_first_ball | ||
if @rolls.first == 10 | ||
print_strike_message | ||
roll_bonus_ball | ||
roll_second_bonus_ball | ||
return | ||
end | ||
roll_second_ball | ||
if @rolls.inject(:+) == 10 | ||
print_spare_message | ||
roll_bonus_ball | ||
end | ||
end | ||
|
||
private | ||
|
||
def roll_first_ball | ||
@io.puts "Roll first ball:" | ||
roll = @io.gets.chomp.to_i | ||
@rolls << roll | ||
end | ||
|
||
def print_strike_message | ||
@io.puts "Strike!" | ||
end | ||
|
||
def roll_second_ball | ||
@io.puts "Roll second ball:" | ||
roll = @io.gets.chomp.to_i | ||
@rolls << roll | ||
end | ||
|
||
def print_spare_message | ||
@io.puts "Spare!" | ||
end | ||
|
||
def roll_bonus_ball | ||
@io.puts "Roll bonus ball:" | ||
bonus_roll = @io.gets.chomp.to_i | ||
@rolls << bonus_roll | ||
end | ||
|
||
def roll_second_bonus_ball | ||
@io.puts "Roll second bonus ball:" | ||
bonus_roll = @io.gets.chomp.to_i | ||
@rolls << bonus_roll | ||
end | ||
end |
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. This looks good, comments are helpful and you've included methods for calculating bonus points in frams 1 to 9 and in the final frame. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require_relative './frame' | ||
|
||
class Game | ||
attr_accessor :frames | ||
attr_reader :grand_total | ||
|
||
def initialize(io=Kernel) | ||
@io = io | ||
@frames = [] | ||
10.times { @frames << Frame.new(@io) } | ||
@gutter_game = false | ||
@perfect_game = false | ||
@grand_total = 0 | ||
end | ||
|
||
def gutter_game | ||
@gutter_game = true | ||
end | ||
|
||
def gutter_game? | ||
@gutter_game | ||
end | ||
|
||
def perfect_game | ||
@perfect_game = true | ||
end | ||
|
||
def perfect_game? | ||
@perfect_game | ||
end | ||
|
||
def run_game | ||
for i in 1..9 do | ||
@io.puts "Frame #{i}:" | ||
@frames[i-1].play_regular_frame | ||
end | ||
|
||
@io.puts "Frame 10:" | ||
@frames.last.play_last_frame | ||
end | ||
|
||
def calculate_score | ||
award_bonus_points | ||
award_bonus_points_in_penultimate_frame | ||
sum_all_points | ||
@gutter_game = true if @grand_total == 0 | ||
@perfect_game = true if @grand_total == 300 | ||
end | ||
|
||
def award_bonus_points | ||
for i in 0..7 do | ||
# If player made a strike | ||
if @frames[i].rolls.include?(10) | ||
@frames[i].bonus_points += @frames[i+1].rolls.first | ||
# If subsequent frame had two rolls | ||
if @frames[i+1].rolls.length == 2 | ||
@frames[i].bonus_points += @frames[i+1].rolls.last | ||
# Else they must have bowled a strike | ||
else | ||
@frames[i].bonus_points += @frames[i+2].rolls.first | ||
end | ||
# If player made a spare | ||
elsif @frames[i].rolls.inject(:+) == 10 | ||
@frames[i].bonus_points += @frames[i+1].rolls.first | ||
end | ||
end | ||
end | ||
|
||
def award_bonus_points_in_penultimate_frame | ||
# If player made a strike | ||
if @frames[8].rolls.include?(10) | ||
@frames[8].bonus_points += (@frames.last.rolls.first + @frames.last.rolls[1]) | ||
# If player made a spare | ||
elsif @frames[8].rolls.inject(:+) == 10 | ||
@frames[8].bonus_points += @frames.last.rolls.first | ||
end | ||
end | ||
|
||
def sum_all_points | ||
@frames.each do |frame| | ||
@grand_total += frame.rolls.inject(0, :+) | ||
@grand_total += frame.bonus_points | ||
end | ||
end | ||
end |
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. The only suggestion I can make is to add more tests here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'frame' | ||
|
||
|
||
describe Frame do | ||
let(:frame) { Frame.new } | ||
it "initializes with an empty array @rolls" do | ||
expect(frame.rolls).to be_an_instance_of Array | ||
expect(frame.rolls.empty?).to be_truthy | ||
end | ||
|
||
it "initializes with bonus points set to 0" do | ||
expect(frame.bonus_points).to eq 0 | ||
end | ||
|
||
end |
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. Good idea using the example game from Github in the test. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
require 'game' | ||
require 'frame' | ||
|
||
describe Game do | ||
let(:io) { double(:io) } | ||
let(:game) { Game.new(double(:io)) } | ||
|
||
it "intializes with an array of 10 frames" do | ||
expect(game.frames.first).to be_an_instance_of Frame | ||
expect(game.frames.first.rolls.empty?).to eq true | ||
expect(game.frames.length).to eq 10 | ||
end | ||
|
||
it "intializes with gutter_game? and perfect_game? set to false" do | ||
expect(game.gutter_game?).to eq false | ||
expect(game.perfect_game?).to eq false | ||
end | ||
|
||
it "scores a perfect game 300" do | ||
io = double(:io) | ||
for i in 1..10 | ||
expect(io).to receive(:puts).with("Frame #{i}:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('10') | ||
expect(io).to receive(:puts).with("Strike!") | ||
end | ||
expect(io).to receive(:puts).with("Roll bonus ball:") | ||
expect(io).to receive(:gets).and_return('10') | ||
expect(io).to receive(:puts).with("Roll second bonus ball:") | ||
expect(io).to receive(:gets).and_return('10') | ||
game = Game.new(io) | ||
game.run_game | ||
game.calculate_score | ||
expect(game.grand_total).to eq 300 | ||
expect(game.perfect_game?).to eq true | ||
end | ||
|
||
it "scores a gutter game 0" do | ||
io = double(:io) | ||
for i in 1..10 | ||
expect(io).to receive(:puts).with("Frame #{i}:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('0') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('0') | ||
end | ||
game = Game.new(io) | ||
game.run_game | ||
game.calculate_score | ||
expect(game.grand_total).to eq 0 | ||
expect(game.perfect_game?).to eq false | ||
expect(game.gutter_game?).to eq true | ||
end | ||
|
||
it "scores an example game correctly" do | ||
io = double(:io) | ||
expect(io).to receive(:puts).with("Frame 1:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('1') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('4') | ||
|
||
expect(io).to receive(:puts).with("Frame 2:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('4') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('5') | ||
|
||
expect(io).to receive(:puts).with("Frame 3:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('6') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('4') | ||
expect(io).to receive(:puts).with("Spare!") | ||
|
||
expect(io).to receive(:puts).with("Frame 4:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('5') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('5') | ||
expect(io).to receive(:puts).with("Spare!") | ||
|
||
expect(io).to receive(:puts).with("Frame 5:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('10') | ||
expect(io).to receive(:puts).with("Strike!") | ||
|
||
expect(io).to receive(:puts).with("Frame 6:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('0') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('1') | ||
|
||
expect(io).to receive(:puts).with("Frame 7:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('7') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('3') | ||
expect(io).to receive(:puts).with("Spare!") | ||
|
||
expect(io).to receive(:puts).with("Frame 8:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('6') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('4') | ||
expect(io).to receive(:puts).with("Spare!") | ||
|
||
expect(io).to receive(:puts).with("Frame 9:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('10') | ||
expect(io).to receive(:puts).with("Strike!") | ||
|
||
expect(io).to receive(:puts).with("Frame 10:") | ||
expect(io).to receive(:puts).with("Roll first ball:") | ||
expect(io).to receive(:gets).and_return('2') | ||
expect(io).to receive(:puts).with("Roll second ball:") | ||
expect(io).to receive(:gets).and_return('8') | ||
expect(io).to receive(:puts).with("Spare!") | ||
expect(io).to receive(:puts).with("Roll bonus ball:") | ||
expect(io).to receive(:gets).and_return('6') | ||
|
||
|
||
game = Game.new(io) | ||
game.run_game | ||
game.calculate_score | ||
expect(game.grand_total).to eq 133 | ||
expect(game.perfect_game?).to eq false | ||
expect(game.gutter_game?).to eq false | ||
|
||
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.
The frame class looks good, you've considered first and second rolls and how they interact with bonus rolls