From 3318cf7623f9de4f3d026f26ff7aaaf9682d0e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Sun, 29 Sep 2024 18:45:55 +1300 Subject: [PATCH] Add bowling exercise (#115) Co-authored-by: Isaac Van Doren <69181572+isaacvando@users.noreply.github.com> --- config.json | 8 + .../practice/bowling/.docs/instructions.md | 56 +++ exercises/practice/bowling/.meta/Example.roc | 118 +++++++ exercises/practice/bowling/.meta/config.json | 19 + exercises/practice/bowling/.meta/template.j2 | 47 +++ exercises/practice/bowling/.meta/tests.toml | 104 ++++++ exercises/practice/bowling/Bowling.roc | 21 ++ exercises/practice/bowling/bowling-test.roc | 328 ++++++++++++++++++ 8 files changed, 701 insertions(+) create mode 100644 exercises/practice/bowling/.docs/instructions.md create mode 100644 exercises/practice/bowling/.meta/Example.roc create mode 100644 exercises/practice/bowling/.meta/config.json create mode 100644 exercises/practice/bowling/.meta/template.j2 create mode 100644 exercises/practice/bowling/.meta/tests.toml create mode 100644 exercises/practice/bowling/Bowling.roc create mode 100644 exercises/practice/bowling/bowling-test.roc diff --git a/config.json b/config.json index 05a5120..93c6f62 100644 --- a/config.json +++ b/config.json @@ -551,6 +551,14 @@ "prerequisites": [], "difficulty": 6 }, + { + "slug": "bowling", + "name": "Bowling", + "uuid": "308493bb-182f-4a48-bafb-56c5b2f214e5", + "practices": [], + "prerequisites": [], + "difficulty": 7 + }, { "slug": "pig-latin", "name": "Pig Latin", diff --git a/exercises/practice/bowling/.docs/instructions.md b/exercises/practice/bowling/.docs/instructions.md new file mode 100644 index 0000000..60ccad1 --- /dev/null +++ b/exercises/practice/bowling/.docs/instructions.md @@ -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. diff --git a/exercises/practice/bowling/.meta/Example.roc b/exercises/practice/bowling/.meta/Example.roc new file mode 100644 index 0000000..fab998f --- /dev/null +++ b/exercises/practice/bowling/.meta/Example.roc @@ -0,0 +1,118 @@ +module [Game, create, roll, score] + +Frame : [ + Ball1 U64, # unfinished frame + Ball2 U64 U64, + Spare U64 U64, + Strike, + SpareFill U64, + StrikeFill1 U64, # unfinished frame + StrikeFill2 U64 U64, +] + +Game := { frames : List Frame } + +create : { previousRolls ? List U64 } -> Result Game [MoreThan10Pins, GameOver] +create = \{ previousRolls ? [] } -> + List.walkTry previousRolls (@Game { frames: [] }) \game, pins -> + roll game pins + +checkMax10Pins : Frame, U64 -> Result {} [MoreThan10Pins, GameOver] +checkMax10Pins = \lastFrame, pins -> + when lastFrame is + Ball1 pins1 -> if pins1 + pins > 10 then Err MoreThan10Pins else Ok {} + StrikeFill1 pins1 -> + if pins > 10 || (pins1 < 10 && pins1 + pins > 10) then Err MoreThan10Pins else Ok {} + + Ball2 _ _ | Spare _ _ | Strike -> if pins > 10 then Err MoreThan10Pins else Ok {} + SpareFill _ | StrikeFill2 _ _ -> Err GameOver + +isOver : Game -> Bool +isOver = \@Game { frames } -> + when frames is + _ if List.len frames < 10 -> Bool.false + [.., Ball1 _] | [.., Spare _ _] | [.., Strike] | [.., StrikeFill1 _] -> Bool.false + [.., Ball2 _ _] | [.., SpareFill _] | [.., StrikeFill2 _ _] -> Bool.true + _ -> Bool.false + +roll : Game, U64 -> Result Game [MoreThan10Pins, GameOver] +roll = \@Game { frames }, pins -> + if @Game { frames } |> isOver then + Err GameOver + else + + lastFrame = frames |> List.last |> Result.withDefault (Ball2 0 0) + checkMax10Pins? lastFrame pins + updatedFrames = + when lastFrame is + Ball1 pins1 -> + frames + |> List.dropLast 1 + |> List.append + ( + if pins1 + pins == 10 then + Spare pins1 pins + else + Ball2 pins1 pins + ) + + StrikeFill1 pins1 -> + frames |> List.dropLast 1 |> List.append (StrikeFill2 pins1 pins) + + Ball2 _ _ | Spare _ _ | Strike if List.len frames < 10 -> + if pins == 10 then + frames |> List.append Strike + else + frames |> List.append (Ball1 pins) + + Spare _ _ -> + frames |> List.append (SpareFill pins) + + Strike -> + frames |> List.append (StrikeFill1 pins) + + Ball2 _ _ | SpareFill _ | StrikeFill2 _ _ -> + crash "Impossible, an unfinished game cannot have these in the last frame after the 10th frame" + @Game { frames: updatedFrames } |> Ok + +mapTriplets : List Frame, (Frame, Frame, Frame -> U64) -> List U64 +mapTriplets = \list, scoreFunc -> + getOr0 = \index -> list |> List.get index |> Result.withDefault (Ball2 0 0) + List.range { start: At 0, end: Before (List.len list) } + |> List.map \index -> + scoreFunc (getOr0 index) (getOr0 (index + 1)) (getOr0 (index + 2)) + +firstPins : Frame -> U64 +firstPins = \frame -> + when frame is + Ball1 pins | Ball2 pins _ | Spare pins _ | SpareFill pins | StrikeFill1 pins | StrikeFill2 pins _ -> pins + Strike -> 10 + +totalPins : Frame -> U64 +totalPins = \frame -> + when frame is + Ball2 pins1 pins2 | Spare pins1 pins2 | StrikeFill2 pins1 pins2 -> pins1 + pins2 + Ball1 pins | SpareFill pins | StrikeFill1 pins -> pins + Strike -> 10 + +score : Game -> Result U64 [GameIsNotOver] +score = \@Game { frames } -> + if @Game { frames } |> isOver then + frames + |> mapTriplets \frame1, frame2, frame3 -> + when frame1 is + Ball2 pins1 pins2 -> pins1 + pins2 + Spare pins1 pins2 -> pins1 + pins2 + firstPins frame2 + Strike -> + when frame2 is + Strike -> 10 + 10 + firstPins frame3 + _ -> 10 + totalPins frame2 + + SpareFill _ -> 0 # already counted in the Spare + StrikeFill2 _ _ -> 0 # already counter in the Strike + Ball1 _ | StrikeFill1 _ -> + crash "Impossible, unfinished frames should not exist in a finished game" + |> List.sum + |> Ok + else + Err GameIsNotOver diff --git a/exercises/practice/bowling/.meta/config.json b/exercises/practice/bowling/.meta/config.json new file mode 100644 index 0000000..bf54587 --- /dev/null +++ b/exercises/practice/bowling/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "ageron" + ], + "files": { + "solution": [ + "Bowling.roc" + ], + "test": [ + "bowling-test.roc" + ], + "example": [ + ".meta/Example.roc" + ] + }, + "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" +} diff --git a/exercises/practice/bowling/.meta/template.j2 b/exercises/practice/bowling/.meta/template.j2 new file mode 100644 index 0000000..682105d --- /dev/null +++ b/exercises/practice/bowling/.meta/template.j2 @@ -0,0 +1,47 @@ +{%- import "generator_macros.j2" as macros with context -%} +{{ macros.canonical_ref() }} +{{ macros.header() }} + +import {{ exercise | to_pascal }} exposing [Game, create, roll, score] + +replayGame : List U64 -> Result Game _ +replayGame = \rolls -> + newGame = create? {} + rolls + |> List.walkUntil (Ok newGame) \state, pins -> + when state is + Ok game -> + when game |> roll pins is + Ok updatedGame -> Continue (Ok updatedGame) + Err err -> Break (Err err) + + Err _ -> crash "Impossible, we don't start or Continue with an Err" + + +{% for case in cases -%} +# {{ case["description"] }} +expect + maybeGame = create { previousRolls : {{ case["input"]["previousRolls"] | to_roc }} } + {%- if case["property"] == "score" %} + result = maybeGame |> Result.try \game -> score game + {%- else %} + result = maybeGame |> Result.try \game -> + game |> {{ case["property"] | to_camel }} {{ case["input"]["roll"] }} + {%- endif %} + {%- if case["expected"]["error"] %} + result |> Result.isErr + {%- else %} + result == Ok {{ case["expected"] | to_roc }} + {%- endif %} + {%- if case["property"] == "score" and not case["expected"]["error"] %} + +# should be able to replay this finished game from the start +expect + rolls = {{ case["input"]["previousRolls"] | to_roc }} + result = replayGame rolls + result |> Result.isOk + + {%- endif %} + + +{% endfor %} diff --git a/exercises/practice/bowling/.meta/tests.toml b/exercises/practice/bowling/.meta/tests.toml new file mode 100644 index 0000000..9b1bb4b --- /dev/null +++ b/exercises/practice/bowling/.meta/tests.toml @@ -0,0 +1,104 @@ +# 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" +include = false + +[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" diff --git a/exercises/practice/bowling/Bowling.roc b/exercises/practice/bowling/Bowling.roc new file mode 100644 index 0000000..f85d41c --- /dev/null +++ b/exercises/practice/bowling/Bowling.roc @@ -0,0 +1,21 @@ +module [Game, create, roll, score] + +Game := { + # TODO: change this opaque type however you need + todo : U64, + todo2 : U64, + todo3 : U64, + # etc. +} + +create : { previousRolls ? List U64 } -> Result Game _ +create = \{ previousRolls ? [] } -> + crash "Please implement the 'create' function" + +roll : Game, U64 -> Result Game _ +roll = \game, pins -> + crash "Please implement the 'roll' function" + +score : Game -> Result U64 _ +score = \finishedGame -> + crash "Please implement the 'score' function" diff --git a/exercises/practice/bowling/bowling-test.roc b/exercises/practice/bowling/bowling-test.roc new file mode 100644 index 0000000..896d285 --- /dev/null +++ b/exercises/practice/bowling/bowling-test.roc @@ -0,0 +1,328 @@ +# These tests are auto-generated with test data from: +# https://github.com/exercism/problem-specifications/tree/main/exercises/bowling/canonical-data.json +# File last updated on 2024-09-23 +app [main] { + pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br", +} + +main = + Task.ok {} + +import Bowling exposing [Game, create, roll, score] + +replayGame : List U64 -> Result Game _ +replayGame = \rolls -> + newGame = create? {} + rolls + |> List.walkUntil (Ok newGame) \state, pins -> + when state is + Ok game -> + when game |> roll pins is + Ok updatedGame -> Continue (Ok updatedGame) + Err err -> Break (Err err) + + Err _ -> crash "Impossible, we don't start or Continue with an Err" + +# should be able to score a game with all zeros +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 0 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# should be able to score a game with no strikes or spares +expect + maybeGame = create { previousRolls: [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6] } + result = maybeGame |> Result.try \game -> score game + result == Ok 90 + +# should be able to replay this finished game from the start +expect + rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6] + result = replayGame rolls + result |> Result.isOk + +# a spare followed by zeros is worth ten points +expect + maybeGame = create { previousRolls: [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 10 + +# should be able to replay this finished game from the start +expect + rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# points scored in the roll after a spare are counted twice +expect + maybeGame = create { previousRolls: [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 16 + +# should be able to replay this finished game from the start +expect + rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# consecutive spares each get a one roll bonus +expect + maybeGame = create { previousRolls: [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 31 + +# should be able to replay this finished game from the start +expect + rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# a spare in the last frame gets a one roll bonus that is counted once +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7] } + result = maybeGame |> Result.try \game -> score game + result == Ok 17 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7] + result = replayGame rolls + result |> Result.isOk + +# a strike earns ten points in a frame with a single roll +expect + maybeGame = create { previousRolls: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 10 + +# should be able to replay this finished game from the start +expect + rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# points scored in the two rolls after a strike are counted twice as a bonus +expect + maybeGame = create { previousRolls: [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 26 + +# should be able to replay this finished game from the start +expect + rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# consecutive strikes each get the two roll bonus +expect + maybeGame = create { previousRolls: [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = maybeGame |> Result.try \game -> score game + result == Ok 81 + +# should be able to replay this finished game from the start +expect + rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + result = replayGame rolls + result |> Result.isOk + +# a strike in the last frame gets a two roll bonus that is counted once +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1] } + result = maybeGame |> Result.try \game -> score game + result == Ok 18 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1] + result = replayGame rolls + result |> Result.isOk + +# rolling a spare with the two roll bonus does not get a bonus roll +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3] } + result = maybeGame |> Result.try \game -> score game + result == Ok 20 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3] + result = replayGame rolls + result |> Result.isOk + +# strikes with the two roll bonus do not get bonus rolls +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10] } + result = maybeGame |> Result.try \game -> score game + result == Ok 30 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10] + result = replayGame rolls + result |> Result.isOk + +# last two strikes followed by only last bonus with non strike points +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 1] } + result = maybeGame |> Result.try \game -> score game + result == Ok 31 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 1] + result = replayGame rolls + result |> Result.isOk + +# a strike with the one roll bonus after a spare in the last frame does not get a bonus +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10] } + result = maybeGame |> Result.try \game -> score game + result == Ok 20 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10] + result = replayGame rolls + result |> Result.isOk + +# all strikes is a perfect game +expect + maybeGame = create { previousRolls: [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] } + result = maybeGame |> Result.try \game -> score game + result == Ok 300 + +# should be able to replay this finished game from the start +expect + rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] + result = replayGame rolls + result |> Result.isOk + +# a roll cannot score more than 10 points +expect + maybeGame = create { previousRolls: [] } + result = + maybeGame + |> Result.try \game -> + game |> roll 11 + result |> Result.isErr + +# two rolls in a frame cannot score more than 10 points +expect + maybeGame = create { previousRolls: [5] } + result = + maybeGame + |> Result.try \game -> + game |> roll 6 + result |> Result.isErr + +# bonus roll after a strike in the last frame cannot score more than 10 points +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] } + result = + maybeGame + |> Result.try \game -> + game |> roll 11 + result |> Result.isErr + +# two bonus rolls after a strike in the last frame cannot score more than 10 points +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5] } + result = + maybeGame + |> Result.try \game -> + game |> roll 6 + result |> Result.isErr + +# two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6] } + result = maybeGame |> Result.try \game -> score game + result == Ok 26 + +# should be able to replay this finished game from the start +expect + rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6] + result = replayGame rolls + result |> Result.isOk + +# the second bonus rolls after a strike in the last frame cannot be a strike if the first one is not a strike +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6] } + result = + maybeGame + |> Result.try \game -> + game |> roll 10 + result |> Result.isErr + +# second bonus roll after a strike in the last frame cannot score more than 10 points +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10] } + result = + maybeGame + |> Result.try \game -> + game |> roll 11 + result |> Result.isErr + +# an unstarted game cannot be scored +expect + maybeGame = create { previousRolls: [] } + result = maybeGame |> Result.try \game -> score game + result |> Result.isErr + +# an incomplete game cannot be scored +expect + maybeGame = create { previousRolls: [0, 0] } + result = maybeGame |> Result.try \game -> score game + result |> Result.isErr + +# cannot roll if game already has ten frames +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } + result = + maybeGame + |> Result.try \game -> + game |> roll 0 + result |> Result.isErr + +# bonus rolls for a strike in the last frame must be rolled before score can be calculated +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] } + result = maybeGame |> Result.try \game -> score game + result |> Result.isErr + +# both bonus rolls for a strike in the last frame must be rolled before score can be calculated +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10] } + result = maybeGame |> Result.try \game -> score game + result |> Result.isErr + +# bonus roll for a spare in the last frame must be rolled before score can be calculated +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3] } + result = maybeGame |> Result.try \game -> score game + result |> Result.isErr + +# cannot roll after bonus roll for spare +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2] } + result = + maybeGame + |> Result.try \game -> + game |> roll 2 + result |> Result.isErr + +# cannot roll after bonus rolls for strike +expect + maybeGame = create { previousRolls: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2] } + result = + maybeGame + |> Result.try \game -> + game |> roll 2 + result |> Result.isErr +