-
-
Notifications
You must be signed in to change notification settings - Fork 200
bowling: Add exercise #611
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,20 @@ | ||
{ | ||
"authors": [], | ||
"contributors": [ | ||
"vmichalak" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/kotlin/BowlingGame.kt" | ||
], | ||
"test": [ | ||
"src/test/kotlin/BowlingGameTest.kt" | ||
], | ||
"example": [ | ||
".meta/src/reference/kotlin/BowlingGame.kt" | ||
] | ||
}, | ||
"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" | ||
} |
40 changes: 40 additions & 0 deletions
40
exercises/practice/bowling/.meta/src/reference/kotlin/BowlingGame.kt
This file contains hidden or 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,40 @@ | ||
class BowlingGame { | ||
private var builder = Frame.Builder() | ||
private val frames = mutableListOf<Frame>() | ||
|
||
private val isLastFrame: Boolean | ||
get() = frames.size == 11 || (frames.size == 10 && !frames[9].isStrike) | ||
|
||
private val isGameOver: Boolean | ||
get() = when { | ||
frames.size < 10 -> false | ||
frames.size == 10 -> !frames[9].isSpare | ||
frames.size == 11 -> !frames[9].isStrike || !frames[10].isStrike | ||
else -> true | ||
} | ||
|
||
fun roll(pins: Int) { | ||
if(isGameOver) { | ||
throw IllegalStateException("Cannot roll after game is over") | ||
} | ||
builder.withRoll(pins); | ||
if (builder.isComplete) { | ||
frames.add(builder.build()); | ||
builder = Frame.Builder(); | ||
} else if (isLastFrame) { | ||
frames.add(builder.build()); | ||
} | ||
} | ||
|
||
fun score(): Int { | ||
if(!isGameOver) { | ||
throw IllegalStateException("Score cannot be taken until the end of the game") | ||
} | ||
return (0 until 10).sumOf { index -> | ||
val current = frames[index] | ||
val next = frames.getOrElse(index + 1) { Frame.EMPTY } | ||
val nextNext = frames.getOrElse(index + 2) { Frame.EMPTY } | ||
current.score(next, nextNext) | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
exercises/practice/bowling/.meta/src/reference/kotlin/Frame.kt
This file contains hidden or 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,50 @@ | ||
class Frame(val first: Int, val second: Int) { | ||
companion object { | ||
val EMPTY = Frame(0, 0) | ||
} | ||
|
||
val frameScore: Int | ||
get() = first + second | ||
|
||
val isStrike: Boolean | ||
get() = (first == 10) | ||
|
||
val isSpare: Boolean | ||
get() = (frameScore == 10) | ||
|
||
fun score(next: Frame, nextNext: Frame) = frameScore + when { | ||
isStrike -> nextNextRoll(next, nextNext) + next.first | ||
isSpare -> next.first | ||
else -> 0 | ||
} | ||
|
||
private fun nextNextRoll(next: Frame, nextNext: Frame) = | ||
if(next.isStrike) { nextNext.first } else { next.second } | ||
|
||
class Builder { | ||
private val rolls = mutableListOf<Int>() | ||
|
||
val count: Int | ||
get() = rolls.sum() | ||
|
||
val isStrike: Boolean | ||
get() = rolls.size == 1 && rolls[0] == 10 | ||
|
||
val isComplete: Boolean | ||
get() = rolls.size == 2 || isStrike | ||
|
||
fun withRoll(pins: Int): Builder { | ||
if(isComplete) { throw IllegalStateException("Frame complete") } | ||
if(pins < 0) { throw IllegalStateException("Negative roll is invalid") } | ||
if(count + pins > 10) { throw IllegalStateException("Pin count exceeds pins on the lane") } | ||
rolls.add(pins) | ||
return this | ||
} | ||
|
||
public fun build(): Frame = when (rolls.size) { | ||
2 -> Frame(rolls[0], rolls[1]) | ||
1 -> Frame(rolls[0], 0) | ||
else -> throw AssertionError("Frame has invalid number of rolls: " + rolls.size) | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
1.0.0 |
This file contains hidden or 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,23 @@ | ||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib-jdk8")) | ||
|
||
testImplementation("junit:junit:4.13.2") | ||
testImplementation(kotlin("test-junit")) | ||
} | ||
|
||
tasks.withType<Test> { | ||
testLogging { | ||
exceptionFormat = TestExceptionFormat.FULL | ||
events("passed", "failed", "skipped") | ||
} | ||
} |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
exercises/practice/bowling/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or 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,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.