Skip to content

Commit

Permalink
[New Exercise]: Bowling (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
glaxxie authored Jan 2, 2024
1 parent 4f2a6d2 commit 4587071
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "bowling",
"name": "Bowling",
"uuid": "f374b4a0-d2a2-40c2-bd41-215b0fd64316",
"practices": ["classes"],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "book-store",
"name": "Book Store",
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.
127 changes: 127 additions & 0 deletions exercises/practice/bowling/.meta/Bowling.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<#
.SYNOPSIS
Implement a class to score a bowling game.
.DESCRIPTION
For the detailed rules of the game, check instructions.
Write code to keep track of the score of a game of bowling.
It should support two operations:
- roll(pins): is called each time the player rolls a ball.
The argument is an integer represent the number of pins got knocked down (0 - 10)
- score(): is called only at the very end of the game.
It returns an integer represent the total score for that game.
The class also should handle various cases of errors based on invalid or illegal inputs.
You can decide what error message you want to throw.
.EXAMPLE
$rolls = @(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
$game = [BowlingGame]::new()
foreach ($roll in $rolls) {
$game.Roll($roll)
}
$game.Score()
Returns: 300
#>
Class Frame {
[int] $Number
[int[]] $Throws
[int] $Score
[Frame] $Next

Frame([int]$number) {
$this.Number = $number
$this.Throws = @()
$this.Score = 0
$this.Next = $null
}

[void] Update([int]$pins) {
<#
.DESCRIPTION
Function to update the frame with new pins, and handle error if the pin invalid the frame
#>
$this.Throws += $pins
$this.Score += $pins

if ($this.Throws.Count -eq 3) {
$sum = $this.Throws[1] + $this.Throws[2]
if ($this.Throws[0] -eq 10 -and $this.Throws[1] -lt 10 -and $sum -gt 10) {
Throw "Two rolls in bonus spare can't be more than 10 points"
}
}else {
if ($this.Score -gt 10 -and $this.Throws[0] -lt 10) {
Throw "Two rolls in a spare can't be more than 10 points"
}
}
}

#boolean logic to handle frame
[bool] IsStrike() {
return $this.Score -eq 10 -and $this.Throws.Count -eq 1
}

[bool] IsSpare() {
return $this.Score -eq 10 -and $this.Throws.Count -eq 2
}

[bool] IsComplete() {
if ($this.Number -eq 10) {
return ($this.Score -lt 10 -and $this.Throws.Count -eq 2) -or ($this.Throws.Count -eq 3)
}
return $this.IsStrike() -or $this.Throws.Count -eq 2
}
}

Class BowlingGame {
[Frame] hidden $Head
[Frame] hidden $CurrentFrame

BowlingGame() {
$this.CurrentFrame = $null
$this.Head = $null
}

[void] Roll([int]$pins) {
if ($pins -lt 0 -or $pins -gt 10) {Throw "Invalid number of pin"}

if ($null -eq $this.Head) {
$this.CurrentFrame = [Frame]::new(1)
$this.Head = $this.CurrentFrame
}

if ($this.CurrentFrame.IsComplete()) {
if ($this.CurrentFrame.Number -eq 10) {Throw "Game's finished. Can't make any more roll"}
$this.CurrentFrame.Next = [Frame]::new($this.CurrentFrame.Number + 1)
$this.CurrentFrame = $this.CurrentFrame.Next
}
$this.CurrentFrame.Update($pins)
}

[int] Score() {
if ($null -eq $this.Head) {
Throw "Can't score game that hasn't started"
}

if ($this.CurrentFrame.Number -lt 10 -or (-not $this.CurrentFrame.IsComplete())) {
Throw "Can't score incomplete game"
}

$total = 0
while ($this.Head) {
$total += $this.Head.Score
$nextThrows = $this.Head.Next.Throws + $this.Head.Next.Next.Throws
if ($this.Head.IsSpare()) {
$total += $nextThrows[0]
}elseif ($this.Head.IsStrike()) {
$total += $nextThrows[0] + $nextThrows[1]
}
$this.Head = $this.Head.Next
}
return $total
}
}
19 changes: 19 additions & 0 deletions exercises/practice/bowling/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glaxxie"
],
"files": {
"solution": [
"Bowling.ps1"
],
"test": [
"Bowling.tests.ps1"
],
"example": [
".meta/Bowling.example.ps1"
]
},
"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"
}
103 changes: 103 additions & 0 deletions exercises/practice/bowling/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[656ae006-25c2-438c-a549-f338e7ec7441]
description = "should be able to score a game with all zeros"

[f85dcc56-cd6b-4875-81b3-e50921e3597b]
description = "should be able to score a game with no strikes or spares"

[d1f56305-3ac2-4fe0-8645-0b37e3073e20]
description = "a spare followed by zeros is worth ten points"

[0b8c8bb7-764a-4287-801a-f9e9012f8be4]
description = "points scored in the roll after a spare are counted twice"

[4d54d502-1565-4691-84cd-f29a09c65bea]
description = "consecutive spares each get a one roll bonus"

[e5c9cf3d-abbe-4b74-ad48-34051b2b08c0]
description = "a spare in the last frame gets a one roll bonus that is counted once"

[75269642-2b34-4b72-95a4-9be28ab16902]
description = "a strike earns ten points in a frame with a single roll"

[037f978c-5d01-4e49-bdeb-9e20a2e6f9a6]
description = "points scored in the two rolls after a strike are counted twice as a bonus"

[1635e82b-14ec-4cd1-bce4-4ea14bd13a49]
description = "consecutive strikes each get the two roll bonus"

[e483e8b6-cb4b-4959-b310-e3982030d766]
description = "a strike in the last frame gets a two roll bonus that is counted once"

[9d5c87db-84bc-4e01-8e95-53350c8af1f8]
description = "rolling a spare with the two roll bonus does not get a bonus roll"

[576faac1-7cff-4029-ad72-c16bcada79b5]
description = "strikes with the two roll bonus do not get bonus rolls"

[efb426ec-7e15-42e6-9b96-b4fca3ec2359]
description = "last two strikes followed by only last bonus with non strike points"

[72e24404-b6c6-46af-b188-875514c0377b]
description = "a strike with the one roll bonus after a spare in the last frame does not get a bonus"

[62ee4c72-8ee8-4250-b794-234f1fec17b1]
description = "all strikes is a perfect game"

[1245216b-19c6-422c-b34b-6e4012d7459f]
description = "rolls cannot score negative points"

[5fcbd206-782c-4faa-8f3a-be5c538ba841]
description = "a roll cannot score more than 10 points"

[fb023c31-d842-422d-ad7e-79ce1db23c21]
description = "two rolls in a frame cannot score more than 10 points"

[6082d689-d677-4214-80d7-99940189381b]
description = "bonus roll after a strike in the last frame cannot score more than 10 points"

[e9565fe6-510a-4675-ba6b-733a56767a45]
description = "two bonus rolls after a strike in the last frame cannot score more than 10 points"

[2f6acf99-448e-4282-8103-0b9c7df99c3d]
description = "two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike"

[6380495a-8bc4-4cdb-a59f-5f0212dbed01]
description = "the second bonus rolls after a strike in the last frame cannot be a strike if the first one is not a strike"

[2b2976ea-446c-47a3-9817-42777f09fe7e]
description = "second bonus roll after a strike in the last frame cannot score more than 10 points"

[29220245-ac8d-463d-bc19-98a94cfada8a]
description = "an unstarted game cannot be scored"

[4473dc5d-1f86-486f-bf79-426a52ddc955]
description = "an incomplete game cannot be scored"

[2ccb8980-1b37-4988-b7d1-e5701c317df3]
description = "cannot roll if game already has ten frames"

[4864f09b-9df3-4b65-9924-c595ed236f1b]
description = "bonus rolls for a strike in the last frame must be rolled before score can be calculated"

[537f4e37-4b51-4d1c-97e2-986eb37b2ac1]
description = "both bonus rolls for a strike in the last frame must be rolled before score can be calculated"

[8134e8c1-4201-4197-bf9f-1431afcde4b9]
description = "bonus roll for a spare in the last frame must be rolled before score can be calculated"

[9d4a9a55-134a-4bad-bae8-3babf84bd570]
description = "cannot roll after bonus roll for spare"

[d3e02652-a799-4ae3-b53b-68582cc604be]
description = "cannot roll after bonus rolls for strike"
42 changes: 42 additions & 0 deletions exercises/practice/bowling/Bowling.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<#
.SYNOPSIS
Implement a class to score a bowling game.
.DESCRIPTION
For the detailed rules of the game, check instructions.
Write code to keep track of the score of a game of bowling.
It should support two operations:
- roll(pins): is called each time the player rolls a ball.
The argument is an integer represent the number of pins got knocked down (0 - 10)
- score(): is called only at the very end of the game.
It returns an integer represent the total score for that game.
The class also should handle various cases of errors based on invalid or illegal inputs.
You can decide what error message you want to throw.
.EXAMPLE
$rolls = @(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10)
$game = [BowlingGame]::new()
foreach ($roll in $rolls) {
$game.Roll($roll)
}
$game.Score()
Returns: 300
#>
Class BowlingGame {
BowlingGame() {
Throw "Please implement this class"
}

Roll($pins) {
Throw "Please implement this fucntion"
}

Score() {
Throw "Please implement this fucntion"
}
}
Loading

0 comments on commit 4587071

Please sign in to comment.