Skip to content

Commit

Permalink
bowling: Add exercise (#611)
Browse files Browse the repository at this point in the history
* bowling: Add exercise

* bowling: Correct PR review
  • Loading branch information
vmichalak authored Jan 24, 2024
1 parent a7a6d58 commit 51eafd6
Show file tree
Hide file tree
Showing 15 changed files with 823 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,17 @@
"difficulty": 5,
"topics": ["arrays", "generics", "recursion", "searching"]
},
{
"slug": "bowling",
"name": "Bowling",
"uuid": "ae084405-48d7-43ea-9bc5-6c4ebae4c1ef",
"practices": [],
"prerequisites": [
"for-loops",
"arrays"
],
"difficulty": 6
},
{
"slug": "etl",
"name": "ETL",
Expand Down
56 changes: 56 additions & 0 deletions exercises/practice/bowling/.docs/instructions.md
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.
20 changes: 20 additions & 0 deletions exercises/practice/bowling/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"vmichalak"
],
"contributors": [],
"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"
}
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 exercises/practice/bowling/.meta/src/reference/kotlin/Frame.kt
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)
}
}
}
1 change: 1 addition & 0 deletions exercises/practice/bowling/.meta/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.0
23 changes: 23 additions & 0 deletions exercises/practice/bowling/build.gradle.kts
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.
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
Loading

0 comments on commit 51eafd6

Please sign in to comment.