diff --git a/README.md b/README.md index 15dc4762..b0ed272e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/lib/bowling.rb b/lib/bowling.rb new file mode 100644 index 00000000..ffcfc557 --- /dev/null +++ b/lib/bowling.rb @@ -0,0 +1,10 @@ +class Bowling + def initialize + @strike = [] + @spare = [] + @turn = 0 + end + + def total + end +end \ No newline at end of file diff --git a/lib/score_input.rb b/lib/score_input.rb new file mode 100644 index 00000000..df14817b --- /dev/null +++ b/lib/score_input.rb @@ -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 \ No newline at end of file diff --git a/spec/score_input_spec.rb b/spec/score_input_spec.rb new file mode 100644 index 00000000..0f7bbbcd --- /dev/null +++ b/spec/score_input_spec.rb @@ -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