-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
852 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Instructions | ||
|
||
Score a bowling game. | ||
|
||
Bowling is a game where players roll a heavy ball to knock down pins arranged in a triangle. | ||
Write code to keep track of the score of a game of bowling. | ||
|
||
## Scoring Bowling | ||
|
||
The game consists of 10 frames. | ||
A frame is composed of one or two ball throws with 10 pins standing at frame initialization. | ||
There are three cases for the tabulation of a frame. | ||
|
||
- An open frame is where a score of less than 10 is recorded for the frame. | ||
In this case the score for the frame is the number of pins knocked down. | ||
|
||
- A spare is where all ten pins are knocked down by the second throw. | ||
The total value of a spare is 10 plus the number of pins knocked down in their next throw. | ||
|
||
- A strike is where all ten pins are knocked down by the first throw. | ||
The total value of a strike is 10 plus the number of pins knocked down in the next two throws. | ||
If a strike is immediately followed by a second strike, then the value of the first strike cannot be determined until the ball is thrown one more time. | ||
|
||
Here is a three frame example: | ||
|
||
| Frame 1 | Frame 2 | Frame 3 | | ||
| :--------: | :--------: | :--------------: | | ||
| X (strike) | 5/ (spare) | 9 0 (open frame) | | ||
|
||
Frame 1 is (10 + 5 + 5) = 20 | ||
|
||
Frame 2 is (5 + 5 + 9) = 19 | ||
|
||
Frame 3 is (9 + 0) = 9 | ||
|
||
This means the current running total is 48. | ||
|
||
The tenth frame in the game is a special case. | ||
If someone throws a spare or a strike then they get one or two fill balls respectively. | ||
Fill balls exist to calculate the total of the 10th frame. | ||
Scoring a strike or spare on the fill ball does not give the player more fill balls. | ||
The total value of the 10th frame is the total number of pins knocked down. | ||
|
||
For a tenth frame of X1/ (strike and a spare), the total value is 20. | ||
|
||
For a tenth frame of XXX (three strikes), the total value is 30. | ||
|
||
## Requirements | ||
|
||
Write code to keep track of the score of a game of bowling. | ||
It should support two operations: | ||
|
||
- `roll(pins : int)` is called each time the player rolls a ball. | ||
The argument is the number of pins knocked down. | ||
- `score() : int` is called only at the very end of the game. | ||
It returns the total score for that game. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"m-dango" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/Bowling.rakumod" | ||
], | ||
"test": [ | ||
"t/bowling.rakutest" | ||
], | ||
"example": [ | ||
".meta/solutions/lib/Bowling.rakumod" | ||
] | ||
}, | ||
"blurb": "Score a bowling game.", | ||
"source": "The Bowling Game Kata from UncleBob", | ||
"source_url": "https://web.archive.org/web/20221001111000/http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata" | ||
} |
106 changes: 106 additions & 0 deletions
106
exercises/practice/bowling/.meta/solutions/lib/Bowling.rakumod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
my class X::Bowling::GameOver is Exception { | ||
method message {'Cannot roll after game is over'} | ||
} | ||
|
||
my class X::Bowling::GameInProgress is Exception { | ||
method message {'Score cannot be taken until the end of the game'} | ||
} | ||
|
||
my class X::Bowling::TooManyPins is Exception { | ||
method message {'Pin count exceeds pins on the lane'} | ||
} | ||
|
||
my class X::Bowling::NegativePins is Exception { | ||
method message {'Negative roll is invalid'} | ||
} | ||
|
||
class Bowling { | ||
my class Frame { | ||
has @.rolls is List; | ||
has Bool:D $.is-final = False; | ||
has UInt:D $!max-roll = 10; | ||
|
||
multi method add-roll ($pins where * < 0) { | ||
X::Bowling::NegativePins.new.throw; | ||
} | ||
|
||
multi method add-roll ($pins where * > $!max-roll) { | ||
X::Bowling::TooManyPins.new.throw; | ||
} | ||
|
||
multi method add-roll ($pins) { | ||
if $.is-final { | ||
if @.rolls == 2 { | ||
$!max-roll = 0; | ||
} | ||
elsif @.rolls == 1 && @.rolls[0] != 10 { | ||
$!max-roll = @.rolls[0] + $pins == 10 ?? 10 !! 0; | ||
} | ||
elsif @.rolls == 1 && @.rolls[0] == 10 && $pins < 10 { | ||
$!max-roll -= $pins; | ||
} | ||
} | ||
elsif !@.rolls { | ||
$!max-roll -= $pins; | ||
} | ||
elsif @.rolls == 1 { | ||
$!max-roll = 0; | ||
} | ||
|
||
@!rolls := |@!rolls, $pins; | ||
} | ||
|
||
method is-strike { | ||
return Nil if $.is-final || $.is-complete.not; | ||
return @.rolls == 1 && @.rolls[0] == 10; | ||
} | ||
|
||
method is-spare { | ||
return Nil if $.is-final || $.is-complete.not; | ||
return @.rolls == 2 && @.rolls.sum == 10; | ||
} | ||
|
||
method is-complete { | ||
return $!max-roll == 0; | ||
} | ||
} | ||
|
||
has Frame @!frames = Frame.new; | ||
|
||
method roll ($pins) { | ||
X::Bowling::GameOver.new.throw if $.is-complete; | ||
|
||
given @!frames[*-1] -> $current-frame { | ||
$current-frame.add-roll($pins); | ||
|
||
if $current-frame.is-complete && $current-frame.is-final.not { | ||
@!frames.push(Frame.new(:is-final(@!frames.elems == 9))); | ||
} | ||
} | ||
} | ||
|
||
method score { | ||
X::Bowling::GameInProgress.new.throw if $.is-complete.not; | ||
|
||
return sum gather { | ||
for ^@!frames -> $i { | ||
given @!frames[$i] { | ||
.take for .rolls; | ||
|
||
when .is-strike { | ||
.take for @!frames[$i+1].rolls[* > 2 ?? ^2 !! *]; | ||
take @!frames[$i+2].rolls[0] if @!frames[$i+1].rolls == 1; | ||
} | ||
|
||
when .is-spare { | ||
take @!frames[$i+1].rolls[0]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
method is-complete { | ||
return .is-final && .is-complete given @!frames[*-1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../t/bowling.rakutest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
unit: false | ||
tests: | | ||
my Bowling $game; | ||
properties: | ||
roll: | ||
test: |- | ||
my Str $prev = '$game.=new;' ~ "\n"; | ||
if %case<input><previousRolls> { | ||
$prev ~= sprintf(q:to/END/, %case<input><previousRolls><>.List.raku); | ||
for %s -> $pins { | ||
$game.roll($pins); | ||
} | ||
END | ||
} | ||
my $error = do given %case<expected><error> { | ||
when /^Pin/ {'X::Bowling::TooManyPins'} | ||
when /^Cannot/ {'X::Bowling::GameOver'} | ||
when /^Neg/ {'X::Bowling::NegativePins'} | ||
} | ||
$prev ~ sprintf(q:to/END/, %case<input><roll>, $error, %case<description>.raku, %case<expected><error>.raku); | ||
throws-like( | ||
{ $game.roll(%s) }, | ||
%s, | ||
%s, | ||
:message(/^ %s $/), | ||
); | ||
END | ||
score: | ||
test: |- | ||
my Str $prev = '$game.=new;' ~ "\n"; | ||
if %case<input><previousRolls> { | ||
$prev ~= sprintf(q:to/END/, %case<input><previousRolls><>.List.raku); | ||
for %s -> $pins { | ||
$game.roll($pins); | ||
} | ||
END | ||
} | ||
if %case<expected><error>:exists { | ||
my $error = do given %case<expected><error> { | ||
when /^Score/ {'X::Bowling::GameInProgress'} | ||
} | ||
$prev ~ sprintf(q:to/END/, $error, %case<description>.raku, %case<expected><error>.raku); | ||
throws-like( | ||
{ $game.score }, | ||
%s, | ||
%s, | ||
:message(/^ %s $/), | ||
); | ||
END | ||
} | ||
else { | ||
$prev ~ sprintf(q:to/END/, %case<expected>, %case<description>.raku); | ||
cmp-ok( | ||
$game.score, | ||
"==", | ||
%s, | ||
%s, | ||
); | ||
END | ||
} | ||
example: |- | ||
my class X::Bowling::GameOver is Exception { | ||
method message {'Cannot roll after game is over'} | ||
} | ||
my class X::Bowling::GameInProgress is Exception { | ||
method message {'Score cannot be taken until the end of the game'} | ||
} | ||
my class X::Bowling::TooManyPins is Exception { | ||
method message {'Pin count exceeds pins on the lane'} | ||
} | ||
my class X::Bowling::NegativePins is Exception { | ||
method message {'Negative roll is invalid'} | ||
} | ||
class Bowling { | ||
my class Frame { | ||
has @.rolls is List; | ||
has Bool:D $.is-final = False; | ||
has UInt:D $!max-roll = 10; | ||
multi method add-roll ($pins where * < 0) { | ||
X::Bowling::NegativePins.new.throw; | ||
} | ||
multi method add-roll ($pins where * > $!max-roll) { | ||
X::Bowling::TooManyPins.new.throw; | ||
} | ||
multi method add-roll ($pins) { | ||
if $.is-final { | ||
if @.rolls == 2 { | ||
$!max-roll = 0; | ||
} | ||
elsif @.rolls == 1 && @.rolls[0] != 10 { | ||
$!max-roll = @.rolls[0] + $pins == 10 ?? 10 !! 0; | ||
} | ||
elsif @.rolls == 1 && @.rolls[0] == 10 && $pins < 10 { | ||
$!max-roll -= $pins; | ||
} | ||
} | ||
elsif [email protected] { | ||
$!max-roll -= $pins; | ||
} | ||
elsif @.rolls == 1 { | ||
$!max-roll = 0; | ||
} | ||
@!rolls := |@!rolls, $pins; | ||
} | ||
method is-strike { | ||
return Nil if $.is-final || $.is-complete.not; | ||
return @.rolls == 1 && @.rolls[0] == 10; | ||
} | ||
method is-spare { | ||
return Nil if $.is-final || $.is-complete.not; | ||
return @.rolls == 2 && @.rolls.sum == 10; | ||
} | ||
method is-complete { | ||
return $!max-roll == 0; | ||
} | ||
} | ||
has Frame @!frames = Frame.new; | ||
method roll ($pins) { | ||
X::Bowling::GameOver.new.throw if $.is-complete; | ||
given @!frames[*-1] -> $current-frame { | ||
$current-frame.add-roll($pins); | ||
if $current-frame.is-complete && $current-frame.is-final.not { | ||
@!frames.push(Frame.new(:is-final(@!frames.elems == 9))); | ||
} | ||
} | ||
} | ||
method score { | ||
X::Bowling::GameInProgress.new.throw if $.is-complete.not; | ||
return sum gather { | ||
for ^@!frames -> $i { | ||
given @!frames[$i] { | ||
.take for .rolls; | ||
when .is-strike { | ||
.take for @!frames[$i+1].rolls[* > 2 ?? ^2 !! *]; | ||
take @!frames[$i+2].rolls[0] if @!frames[$i+1].rolls == 1; | ||
} | ||
when .is-spare { | ||
take @!frames[$i+1].rolls[0]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
method is-complete { | ||
return .is-final && .is-complete given @!frames[*-1]; | ||
} | ||
} | ||
stub: |- | ||
my class X::Bowling::GameOver is Exception { | ||
method message {'Cannot roll after game is over'} | ||
} | ||
my class X::Bowling::GameInProgress is Exception { | ||
method message {'Score cannot be taken until the end of the game'} | ||
} | ||
my class X::Bowling::TooManyPins is Exception { | ||
method message {'Pin count exceeds pins on the lane'} | ||
} | ||
my class X::Bowling::NegativePins is Exception { | ||
method message {'Negative roll is invalid'} | ||
} | ||
class Bowling { | ||
method roll ($pins) { | ||
} | ||
method score { | ||
} | ||
} |
Oops, something went wrong.