-
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 all 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 |
---|---|---|
@@ -1,65 +1,36 @@ | ||
Bowling Challenge in Ruby | ||
================= | ||
|
||
* Feel free to use google, your notes, books, etc. but work on your own | ||
* If you refer to the solution of another coach or student, please put a link to that in your README | ||
* If you have a partial solution, **still check in a partial solution** | ||
* You must submit a pull request to this repo with your code by 9am Monday week | ||
A weekend challenge as part of the Makers Academy software engineering bootcamp. | ||
|
||
## The Task | ||
|
||
We were given the following brief: | ||
|
||
**THIS IS NOT A BOWLING GAME, IT IS A BOWLING SCORECARD PROGRAM. DO NOT GENERATE RANDOM ROLLS. THE USER INPUTS THE ROLLS.** | ||
|
||
Count and sum the scores of a bowling game for one player. For this challenge, you do _not_ need to build a web app with a UI, instead, just focus on the logic for bowling (you also don't need a database). Next end-of-unit challenge, you will have the chance to translate the logic to Javascript and build a user interface. | ||
Count and sum the scores of a bowling game for one player. | ||
|
||
A bowling game consists of 10 frames in which the player tries to knock down the 10 pins. In every frame the player can roll one or two times. The actual number depends on strikes and spares. The score of a frame is the number of knocked down pins plus bonuses for strikes and spares. After every frame the 10 pins are reset. | ||
|
||
As usual please start by | ||
|
||
* Forking this repo | ||
|
||
* Finally submit a pull request before Monday week at 9am with your solution or partial solution. However much or little amount of code you wrote please please please submit a pull request before Monday week at 9am. | ||
|
||
___STRONG HINT, IGNORE AT YOUR PERIL:___ Bowling is a deceptively complex game. Careful thought and thorough diagramming — both before and throughout — will save you literal hours of your life. | ||
|
||
## Focus for this challenge | ||
The focus for this challenge is to write high-quality code. | ||
|
||
In order to do this, you may pay particular attention to the following: | ||
* Using diagramming to plan your approach to the challenge | ||
* TDD your code | ||
* Focus on testing behaviour rather than state | ||
* Commit often, with good commit messages | ||
* Single Responsibility Principle and encapsulation | ||
* Clear and readable code | ||
|
||
## Bowling — how does it work? | ||
|
||
### Strikes | ||
|
||
The player has a strike if he knocks down all 10 pins with the first roll in a frame. The frame ends immediately (since there are no pins left for a second roll). The bonus for that frame is the number of pins knocked down by the next two rolls. That would be the next frame, unless the player rolls another strike. | ||
|
||
### Spares | ||
|
||
The player has a spare if the knocks down all 10 pins with the two rolls of a frame. The bonus for that frame is the number of pins knocked down by the next roll (first roll of next frame). | ||
|
||
### 10th frame | ||
## Getting started | ||
|
||
If the player rolls a strike or spare in the 10th frame they can roll the additional balls for the bonus. But they can never roll more than 3 balls in the 10th frame. The additional rolls only count for the bonus not for the regular frame count. | ||
`git clone https://github.com/tomcarmichael/bowling-challenge-ruby.git` | ||
|
||
10, 10, 10 in the 10th frame gives 30 points (10 points for the regular first strike and 20 points for the bonus). | ||
1, 9, 10 in the 10th frame gives 20 points (10 points for the regular spare and 10 points for the bonus). | ||
Install dependencies: | ||
|
||
### Gutter Game | ||
`bundle install` | ||
|
||
A Gutter Game is when the player never hits a pin (20 zero scores). | ||
Run the tests: | ||
|
||
### Perfect Game | ||
`rspec` | ||
|
||
A Perfect Game is when the player rolls 12 strikes (10 regular strikes and 2 strikes for the bonus in the 10th frame). The Perfect Game scores 300 points. | ||
Run the program: | ||
|
||
In the image below you can find some score examples. | ||
`ruby app.rb` | ||
|
||
More about ten pin bowling here: http://en.wikipedia.org/wiki/Ten-pin_bowling | ||
Enter the score for each roll. A strike will immediately move the program onto the next frame. A strike or a spare in frame 10 results in additional bonus balls for the player. | ||
|
||
![Ten Pin Score Example](images/example_ten_pin_scoring.png) | ||
At the end of the game the score will be calculated and printed to the terminal. |
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 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