Skip to content
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 scorecard challenge #390

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
76 changes: 13 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,15 @@
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

## The Task

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

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

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.

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

### Gutter Game

A Gutter Game is when the player never hits a pin (20 zero scores).

### Perfect Game

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.

In the image below you can find some score examples.

More about ten pin bowling here: http://en.wikipedia.org/wiki/Ten-pin_bowling

![Ten Pin Score Example](images/example_ten_pin_scoring.png)
The program is designed in a single class for simplicity and does not include UI handling as per the specs.
BowlingScorer class holds the frame records, which is then iterated over when scores need to be counted.

Frames
----
First 10 frames are added to the @frames array. The amount of bonus shots player can take depend on their score on the 10th frame (effectively giving 3 different options) and its scoring is different than the regular game, so bonus shots are handled with an array of many args. The bonus array is also added to the @frames.

Counting Scores
----
This method iterates over the entire @frames array (starting from index 1 and not 0).
It relies on another method to check if the previous frame was a spare or not (that is why it starts from index 1).
It adds the bonus points to the current frame if the player has scored a spare or strike, not the previous one.
It stops the count after frame 10, and checks the length of the @frames whether the player has produced and bonus shots.
71 changes: 71 additions & 0 deletions lib/bowl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class BowlingScorer
def initialize
@player_score = 0
@frames = []
end

def add_frame(shot1, shot2)
fail "Looks like you hit the next lane or something, cant knock more than 10" if shot1 + shot2 > 10
fail "Smells like invalid input" if shot1 < 0 || shot2 < 0
## Line below is irrelevant for the current specs, but this is done assuming when UI is developed
## the user will input their shots sequentially as opposed to passing them in together
shot1 == 10 ? current_frame = [10,0] : current_frame = [shot1, shot2]
@frames << current_frame
return current_frame
end

def check_for_specials(frame)
message = ""
if frame[0] == 10
message = "strike"
elsif frame.sum == 10
message = "spare"
end
return message
end

def add_bonus_frame(*args)

Choose a reason for hiding this comment

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

Based on the focus points provided to us I would suggest encapsulating the Frame logic into a seperate class. This would make it more modular and the bowlingscorer class would be only responsible for managing the Frames rather than the logic.

@frames << [*args]
return [*args]
end

def count_player_bonus_scores
fail "Player has not scored any bonuses!" if @frames.length <= 10
@player_score += @frames[-1].flatten.sum
end

def count_frame_score(index)

Choose a reason for hiding this comment

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

Would be good to split the responsibilities out here as it currently calculates the frame score and the special cases.

current_frame = @frames[index]
previous_frame = @frames[index-1]
frame_score = current_frame.flatten.sum
if check_for_specials(previous_frame) == "spare"
frame_score += current_frame[0]
elsif check_for_specials(previous_frame) == "strike"
frame_score += current_frame.sum
end
return frame_score
end

def count_player_score
i = 1
@player_score += @frames[0].sum
### The line below is added to be able to test small chunks of frames
frame_count = [@frames.length, 10].min
while i < frame_count
@player_score += count_frame_score(i)
i += 1
end
count_player_bonus_scores if @frames.length > 10
return @player_score
end

## Helper methods - Used in testing and not relevant to gameplay ##
def frames
@frames

Choose a reason for hiding this comment

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

I assume if you had multiple classes you would've used accessor methods to access these instance variables instead?

end

def reset_scores_and_frames
@player_score = 0
@frames = []
end
end
Loading