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

On it's way #404

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
Bowling Challenge in Ruby
=================
## Preface

- Below are the instructions we used to create a Bowling Score Calculator using Ruby and RSpec
- There are some bugs with high score (multiple stikes) so don't do too well ;)

## The Task

* 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.
Expand Down Expand Up @@ -63,3 +67,10 @@ 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)


If spare:
Add the next roll to the turn you scored the spare

If strike:
Add the next 2 rolls to the turn you scored the strike
10 changes: 10 additions & 0 deletions lib/bowling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Bowling
def initialize
@strike = []
@spare = []
@turn = 0
end

def total
end
end
59 changes: 59 additions & 0 deletions lib/score_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class ScoreInput
def initialize
@strike = Array.new(10, 0)
@spare = Array.new(10, 0)
@turn = -1
@score = Array.new(10, 0)
end

def check_score(pos)
return @score[pos]
end

def total
end

def add_score(roll_1, roll_2)
raise "You can't hit more than 10 pins a turn" if roll_1 + roll_2 > 10
@turn += 1
total = roll_1 + roll_2
case total
when 10 && roll_2 == 0
@strike[@turn] = 1
@score[@turn] = total
call_strike_or_spare(@turn, total, roll_1)
p @score
puts "Strike!"

when 10
@spare[@turn] = 1
@score[@turn] = total
call_strike_or_spare(@turn, total, roll_1)
p @score
puts "Spare!"

else
@score[@turn] = total
call_strike_or_spare(@turn, total, roll_1)
puts "You missed a spot"

end
end

def call_strike_or_spare(current_turn, total, roll_1)
if @spare[@turn-1] == 1
@score[current_turn] = @score[current_turn] + roll_1

elsif @strike[@turn-1] == 1
@score[current_turn] = @score[current_turn] + total

else

end
end

def add_score_10(roll_1, roll_2, roll_3)
raise "Score impossibly high" if roll_1 + roll_2 + roll_3 > 30

end
end
34 changes: 34 additions & 0 deletions spec/score_input_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'score_input'

describe ScoreInput do
context 'add_score' do
it 'correctly counts a turn lower than 10 points' do
repo = ScoreInput.new
response = repo.add_score(7,3)
# expect(response).to eq "Spare!"
expect(repo.check_score(0)).to eq(10)
end

xit 'inputs a spare on the first turn, 7 on the second' do
repo = ScoreInput.new
turn = repo.add_score(4,6)
response = turn.add_score(7,0)
expect(response.check_score(0)).to eq 17
expect(response.check_score(1)).to eq 24
end

it 'inputs a spare on the first turn, 7 on the second (2)' do
repo = ScoreInput.new
repo.add_score(4,6)
response = repo.add_score(7,0)
expect(response.check_score(0)).to eq 17
expect(response.check_score(1)).to eq 24
end

xit 'raises error from scoring more more than ten a turn' do
repo = ScoreInput.new
sudo = repo.add_score(6,7)
expect { sudo }.to raise_error("You can't hit more than 10 pins a turn")
end
end
end