trackler 2.2.1.134 → 2.2.1.135
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trackler/version.rb +1 -1
- data/problem-specifications/exercises/series/canonical-data.json +105 -0
- data/problem-specifications/exercises/series/description.md +6 -6
- data/problem-specifications/exercises/sieve/description.md +5 -3
- data/tracks/ceylon/exercises/sieve/README.md +5 -3
- data/tracks/fsharp/.gitignore +1 -0
- data/tracks/fsharp/docs/GENERATORS.md +314 -0
- data/tracks/fsharp/generators/Exercise.fs +39 -80
- data/tracks/fsharp/generators/Generators.fs +42 -40
- data/tracks/fsharp/generators/Generators.fsproj.user +6 -0
- data/tracks/haskell/exercises/crypto-square/README.md +4 -5
- data/tracks/haskell/exercises/isbn-verifier/package.yaml +1 -1
- data/tracks/haskell/exercises/isbn-verifier/test/Tests.hs +4 -0
- data/tracks/haskell/exercises/kindergarten-garden/package.yaml +1 -1
- data/tracks/haskell/exercises/series/README.md +6 -6
- data/tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs +6 -8
- data/tracks/haskell/exercises/series/examples/success-standard/src/Series.hs +0 -1
- data/tracks/haskell/exercises/series/package.yaml +1 -1
- data/tracks/haskell/exercises/series/test/Tests.hs +17 -2
- data/tracks/haskell/exercises/sieve/README.md +5 -3
- data/tracks/java/CONTRIBUTING.md +5 -6
- data/tracks/java/exercises/book-store/.meta/version +1 -1
- data/tracks/java/exercises/change/.meta/version +1 -1
- data/tracks/java/exercises/circular-buffer/.meta/version +1 -1
- data/tracks/java/exercises/custom-set/.meta/version +1 -1
- data/tracks/java/exercises/forth/.meta/version +1 -1
- data/tracks/java/exercises/list-ops/.meta/version +1 -1
- data/tracks/java/exercises/minesweeper/.meta/version +1 -1
- data/tracks/java/exercises/ocr-numbers/.meta/version +1 -1
- data/tracks/java/exercises/wordy/.meta/version +1 -1
- data/tracks/nim/config.json +57 -44
- data/tracks/nim/exercises/pangram/README.md +26 -0
- data/tracks/nim/exercises/pangram/example.nim +9 -0
- data/tracks/nim/exercises/pangram/pangram_test.nim +35 -0
- data/tracks/nim/exercises/roman-numerals/README.md +61 -0
- data/tracks/nim/exercises/roman-numerals/example.nim +30 -0
- data/tracks/nim/exercises/roman-numerals/roman_numerals_test.nim +28 -0
- data/tracks/nim/exercises/scrabble-score/README.md +57 -0
- data/tracks/nim/exercises/scrabble-score/example.nim +13 -0
- data/tracks/nim/exercises/scrabble-score/scrabble_score_test.nim +37 -0
- data/tracks/perl5/README.md +2 -1
- data/tracks/perl5/cpanfile +2 -1
- data/tracks/perl5/exercises/grains/.meta/exercise-data.yaml +55 -0
- data/tracks/perl5/exercises/grains/.meta/solutions/Grains.pm +15 -23
- data/tracks/perl5/exercises/grains/Grains.pm +13 -0
- data/tracks/perl5/exercises/grains/cpanfile +1 -0
- data/tracks/perl5/exercises/grains/grains.t +136 -75
- data/tracks/ruby/README.md +0 -6
- data/tracks/rust/.travis.yml +1 -1
- data/tracks/rust/_test/check-stubs.sh +0 -0
- data/tracks/rust/_test/count-ignores.sh +0 -0
- data/tracks/rust/_test/ensure-lib-src-rs-exist.sh +0 -0
- data/tracks/rust/_test/ensure-readmes-are-updated.sh +0 -0
- data/tracks/rust/_test/ensure-stubs-compile.sh +0 -0
- data/tracks/rust/_test/verify-exercise-difficulties.sh +52 -0
- data/tracks/rust/config.json +4 -4
- data/tracks/swift/exercises/space-age/Tests/SpaceAgeTests/SpaceAgeTests.swift +15 -15
- data/tracks/typescript/config.json +46 -1
- data/tracks/typescript/exercises/bowling/README.md +93 -0
- data/tracks/typescript/exercises/bowling/bowling.example.ts +77 -0
- data/tracks/typescript/exercises/bowling/bowling.test.ts +137 -0
- data/tracks/typescript/exercises/bowling/bowling.ts +0 -0
- data/tracks/typescript/exercises/bowling/package.json +36 -0
- data/tracks/typescript/exercises/bowling/tsconfig.json +22 -0
- data/tracks/typescript/exercises/bowling/tslint.json +127 -0
- data/tracks/typescript/exercises/bowling/yarn.lock +2624 -0
- data/tracks/typescript/exercises/connect/README.md +59 -0
- data/tracks/typescript/exercises/connect/connect.example.ts +77 -0
- data/tracks/typescript/exercises/connect/connect.test.ts +108 -0
- data/tracks/typescript/exercises/connect/connect.ts +0 -0
- data/tracks/typescript/exercises/connect/package.json +36 -0
- data/tracks/typescript/exercises/connect/tsconfig.json +22 -0
- data/tracks/typescript/exercises/connect/tslint.json +127 -0
- data/tracks/typescript/exercises/connect/yarn.lock +2624 -0
- data/tracks/typescript/exercises/rectangles/README.md +92 -0
- data/tracks/typescript/exercises/rectangles/package.json +36 -0
- data/tracks/typescript/exercises/rectangles/rectangles.example.ts +59 -0
- data/tracks/typescript/exercises/rectangles/rectangles.test.ts +134 -0
- data/tracks/typescript/exercises/rectangles/rectangles.ts +0 -0
- data/tracks/typescript/exercises/rectangles/tsconfig.json +22 -0
- data/tracks/typescript/exercises/rectangles/tslint.json +127 -0
- data/tracks/typescript/exercises/rectangles/yarn.lock +2624 -0
- metadata +42 -2
data/tracks/ruby/README.md
CHANGED
@@ -324,14 +324,8 @@ converting string indices to integer indices.
|
|
324
324
|
|
325
325
|
## READMEs
|
326
326
|
|
327
|
-
<<<<<<< HEAD
|
328
327
|
All exercises must have a README.md file, but should not be created manually. The READMEs
|
329
328
|
are constructed using shared metadata, which lives in the [problem-specifications][] repo.
|
330
|
-
=======
|
331
|
-
Do not add a README or README.md file to the exercise's directory. The READMEs
|
332
|
-
are constructed using shared metadata, which lives in the
|
333
|
-
[problem-specifications][] repo.
|
334
|
-
>>>>>>> Whitespace and wrapping, not integral to this PR
|
335
329
|
|
336
330
|
Use the `configlet` tool to generate a README from shared metadata:
|
337
331
|
|
data/tracks/rust/.travis.yml
CHANGED
@@ -8,11 +8,11 @@ script:
|
|
8
8
|
- "./bin/fetch-configlet"
|
9
9
|
- "sh ./_test/ensure-readmes-are-updated.sh"
|
10
10
|
- "./bin/configlet lint ."
|
11
|
+
- "./_test/verify-exercise-difficulties.sh"
|
11
12
|
sudo: false
|
12
13
|
rust:
|
13
14
|
- stable
|
14
15
|
- beta
|
15
|
-
- nightly
|
16
16
|
env:
|
17
17
|
- DENYWARNINGS=
|
18
18
|
- DENYWARNINGS=1
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
repo=$(cd "$(dirname "$0")/.." && pwd)
|
4
|
+
config=$repo/config.json
|
5
|
+
|
6
|
+
es=0
|
7
|
+
|
8
|
+
# ensure every exercise has a difficulty
|
9
|
+
no_difficulty=$(
|
10
|
+
jq --raw-output '
|
11
|
+
.exercises[] |
|
12
|
+
select((.deprecated | not) and (has("difficulty") | not)) |
|
13
|
+
.slug
|
14
|
+
' $config
|
15
|
+
)
|
16
|
+
if [ -n "$no_difficulty" ]; then
|
17
|
+
echo "Exercises without a difficulty in config.json:"
|
18
|
+
echo "$no_difficulty"
|
19
|
+
es=1
|
20
|
+
fi
|
21
|
+
|
22
|
+
# ensure that all difficulties are one of 1, 4, 7, 10
|
23
|
+
invalid_difficulty=$(
|
24
|
+
jq --raw-output '
|
25
|
+
.exercises[] |
|
26
|
+
select(
|
27
|
+
(.deprecated | not) and
|
28
|
+
has("difficulty") and
|
29
|
+
(
|
30
|
+
.difficulty | tostring |
|
31
|
+
in({"1":null,"4":null,"7":null,"10":null}) |
|
32
|
+
not
|
33
|
+
)
|
34
|
+
) |
|
35
|
+
"\(.slug) (\(.difficulty))"
|
36
|
+
' $config
|
37
|
+
)
|
38
|
+
if [ -n "$invalid_difficulty" ]; then
|
39
|
+
echo "Exercises with invalid difficulty (must be in {1, 4, 7, 10})"
|
40
|
+
echo "$invalid_difficulty"
|
41
|
+
es=1
|
42
|
+
fi
|
43
|
+
|
44
|
+
# ensure difficulties are sorted
|
45
|
+
exercise_order=$(jq --raw-output '.exercises[] | select(.deprecated | not) | .slug' $config)
|
46
|
+
sorted_order=$(jq --raw-output '.exercises | sort_by(.difficulty) | .[] | select(.deprecated | not) | .slug' $config)
|
47
|
+
if [ "$exercise_order" != "$sorted_order" ]; then
|
48
|
+
echo "Exercises are not in sorted order in config.json"
|
49
|
+
es=1
|
50
|
+
fi
|
51
|
+
|
52
|
+
exit $es
|
data/tracks/rust/config.json
CHANGED
@@ -200,7 +200,7 @@
|
|
200
200
|
"slug": "saddle-points",
|
201
201
|
"core": false,
|
202
202
|
"unlocked_by": null,
|
203
|
-
"difficulty":
|
203
|
+
"difficulty": 4,
|
204
204
|
"topics": [
|
205
205
|
"vectors",
|
206
206
|
"iterators"
|
@@ -211,7 +211,7 @@
|
|
211
211
|
"slug": "isogram",
|
212
212
|
"core": false,
|
213
213
|
"unlocked_by": null,
|
214
|
-
"difficulty":
|
214
|
+
"difficulty": 4,
|
215
215
|
"topics": [
|
216
216
|
"chars",
|
217
217
|
"iterators",
|
@@ -772,7 +772,7 @@
|
|
772
772
|
"slug": "poker",
|
773
773
|
"core": false,
|
774
774
|
"unlocked_by": null,
|
775
|
-
"difficulty":
|
775
|
+
"difficulty": 7,
|
776
776
|
"topics": [
|
777
777
|
"lifetimes",
|
778
778
|
"struct",
|
@@ -786,7 +786,7 @@
|
|
786
786
|
"slug": "decimal",
|
787
787
|
"core": false,
|
788
788
|
"unlocked_by": null,
|
789
|
-
"difficulty":
|
789
|
+
"difficulty": 7,
|
790
790
|
"topics": [
|
791
791
|
"struct",
|
792
792
|
"traits",
|
@@ -9,49 +9,49 @@ class SpaceAgeTests: XCTestCase {
|
|
9
9
|
|
10
10
|
func testAgeInEarthYears() {
|
11
11
|
let age = SpaceAge(1_000_000_000)
|
12
|
-
XCTAssertEqual(31.69, age.onEarth)
|
12
|
+
XCTAssertEqual(31.69, age.onEarth, accuracy: 0.01)
|
13
13
|
}
|
14
14
|
|
15
15
|
func testAgeInMercuryYears() {
|
16
16
|
let age = SpaceAge(2_134_835_688)
|
17
|
-
XCTAssertEqual(67.65, age.onEarth)
|
18
|
-
XCTAssertEqual(280.88, age.onMercury)
|
17
|
+
XCTAssertEqual(67.65, age.onEarth, accuracy: 0.01)
|
18
|
+
XCTAssertEqual(280.88, age.onMercury, accuracy: 0.01)
|
19
19
|
}
|
20
20
|
|
21
21
|
func testAgeInVenusYears() {
|
22
22
|
let age = SpaceAge(189_839_836)
|
23
|
-
XCTAssertEqual(6.02, age.onEarth)
|
24
|
-
XCTAssertEqual(9.78, age.onVenus)
|
23
|
+
XCTAssertEqual(6.02, age.onEarth, accuracy: 0.01)
|
24
|
+
XCTAssertEqual(9.78, age.onVenus, accuracy: 0.01)
|
25
25
|
}
|
26
26
|
|
27
27
|
func testAgeOnMars() {
|
28
28
|
let age = SpaceAge(2_329_871_239)
|
29
|
-
XCTAssertEqual(73.83, age.onEarth)
|
30
|
-
XCTAssertEqual(39.25, age.onMars)
|
29
|
+
XCTAssertEqual(73.83, age.onEarth, accuracy: 0.01)
|
30
|
+
XCTAssertEqual(39.25, age.onMars, accuracy: 0.01)
|
31
31
|
}
|
32
32
|
|
33
33
|
func testAgeOnJupiter() {
|
34
34
|
let age = SpaceAge(901_876_382)
|
35
|
-
XCTAssertEqual(28.58, age.onEarth)
|
36
|
-
XCTAssertEqual(2.41, age.onJupiter)
|
35
|
+
XCTAssertEqual(28.58, age.onEarth, accuracy: 0.01)
|
36
|
+
XCTAssertEqual(2.41, age.onJupiter, accuracy: 0.01)
|
37
37
|
}
|
38
38
|
|
39
39
|
func testAgeOnSaturn() {
|
40
40
|
let age = SpaceAge(3_000_000_000)
|
41
|
-
XCTAssertEqual(95.06, age.onEarth)
|
42
|
-
XCTAssertEqual(3.23, age.onSaturn)
|
41
|
+
XCTAssertEqual(95.06, age.onEarth, accuracy: 0.01)
|
42
|
+
XCTAssertEqual(3.23, age.onSaturn, accuracy: 0.01)
|
43
43
|
}
|
44
44
|
|
45
45
|
func testAgeOnUranus() {
|
46
46
|
let age = SpaceAge(3_210_123_456)
|
47
|
-
XCTAssertEqual(101.72, age.onEarth)
|
48
|
-
XCTAssertEqual(1.21, age.onUranus)
|
47
|
+
XCTAssertEqual(101.72, age.onEarth, accuracy: 0.01)
|
48
|
+
XCTAssertEqual(1.21, age.onUranus, accuracy: 0.01)
|
49
49
|
}
|
50
50
|
|
51
51
|
func testAgeOnNeptune() {
|
52
52
|
let age = SpaceAge(8_210_123_456)
|
53
|
-
XCTAssertEqual(260.16, age.onEarth)
|
54
|
-
XCTAssertEqual(1.58, age.onNeptune)
|
53
|
+
XCTAssertEqual(260.16, age.onEarth, accuracy: 0.01)
|
54
|
+
XCTAssertEqual(1.58, age.onNeptune, accuracy: 0.01)
|
55
55
|
}
|
56
56
|
|
57
57
|
static var allTests: [(String, (SpaceAgeTests) -> () throws -> Void)] {
|
@@ -400,6 +400,20 @@
|
|
400
400
|
"unlocked_by": "bob",
|
401
401
|
"uuid": "9850a74a-1842-4db0-b3ed-5a53b4f25050"
|
402
402
|
},
|
403
|
+
{
|
404
|
+
"core": false,
|
405
|
+
"difficulty": 4,
|
406
|
+
"slug": "rectangles",
|
407
|
+
"topics": [
|
408
|
+
"control_flow_conditionals",
|
409
|
+
"control_flow_loops",
|
410
|
+
"arrays",
|
411
|
+
"matrices",
|
412
|
+
"strings"
|
413
|
+
],
|
414
|
+
"unlocked_by": "matrix",
|
415
|
+
"uuid": "6d81d98f-afde-4aea-85ff-c7baef98fb7e"
|
416
|
+
},
|
403
417
|
{
|
404
418
|
"core": false,
|
405
419
|
"difficulty": 5,
|
@@ -645,6 +659,21 @@
|
|
645
659
|
"unlocked_by": null,
|
646
660
|
"uuid": "f5b00e76-3015-444d-8012-9862990db08c"
|
647
661
|
},
|
662
|
+
{
|
663
|
+
"core": false,
|
664
|
+
"difficulty": 7,
|
665
|
+
"slug": "connect",
|
666
|
+
"topics": [
|
667
|
+
"control_flow_conditionals",
|
668
|
+
"control_flow_loops",
|
669
|
+
"games",
|
670
|
+
"parsing",
|
671
|
+
"arrays",
|
672
|
+
"maps"
|
673
|
+
],
|
674
|
+
"unlocked_by": "grade-school",
|
675
|
+
"uuid": "69cbdc2a-50f4-4e14-b9f4-c1ddee021018"
|
676
|
+
},
|
648
677
|
{
|
649
678
|
"core": false,
|
650
679
|
"difficulty": 8,
|
@@ -671,6 +700,22 @@
|
|
671
700
|
],
|
672
701
|
"uuid": "26787ded-0aa2-46f6-88dd-13a00cce9236"
|
673
702
|
},
|
703
|
+
{
|
704
|
+
"core": false,
|
705
|
+
"difficulty": 8,
|
706
|
+
"slug": "bowling",
|
707
|
+
"topics": [
|
708
|
+
"arrays",
|
709
|
+
"control_flow_conditionals",
|
710
|
+
"control_flow_loops",
|
711
|
+
"exception_handling",
|
712
|
+
"games",
|
713
|
+
"parsing",
|
714
|
+
"text_formatting"
|
715
|
+
],
|
716
|
+
"unlocked_by": "grade-school",
|
717
|
+
"uuid": "18376dda-8e8d-4cf7-a44a-db71da438e87"
|
718
|
+
},
|
674
719
|
{
|
675
720
|
"core": false,
|
676
721
|
"difficulty": 9,
|
@@ -1092,4 +1137,4 @@
|
|
1092
1137
|
"foregone": [],
|
1093
1138
|
"language": "TypeScript",
|
1094
1139
|
"test_pattern": ".*[.]test[.]ts$"
|
1095
|
-
}
|
1140
|
+
}
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# Bowling
|
2
|
+
|
3
|
+
Score a bowling game.
|
4
|
+
|
5
|
+
Bowling is a game where players roll a heavy ball to knock down pins
|
6
|
+
arranged in a triangle. Write code to keep track of the score
|
7
|
+
of a game of bowling.
|
8
|
+
|
9
|
+
## Scoring Bowling
|
10
|
+
|
11
|
+
The game consists of 10 frames. A frame is composed of one or two ball
|
12
|
+
throws with 10 pins standing at frame initialization. There are three
|
13
|
+
cases for the tabulation of a frame.
|
14
|
+
|
15
|
+
* An open frame is where a score of less than 10 is recorded for the
|
16
|
+
frame. In this case the score for the frame is the number of pins
|
17
|
+
knocked down.
|
18
|
+
|
19
|
+
* A spare is where all ten pins are knocked down by the second
|
20
|
+
throw. The total value of a spare is 10 plus the number of pins
|
21
|
+
knocked down in their next throw.
|
22
|
+
|
23
|
+
* A strike is where all ten pins are knocked down by the first
|
24
|
+
throw. The total value of a strike is 10 plus the number of pins
|
25
|
+
knocked down in the next two throws. If a strike is immediately
|
26
|
+
followed by a second strike, then the value of the first strike
|
27
|
+
cannot be determined until the ball is thrown one more time.
|
28
|
+
|
29
|
+
Here is a three frame example:
|
30
|
+
|
31
|
+
| Frame 1 | Frame 2 | Frame 3 |
|
32
|
+
| :-------------: |:-------------:| :---------------------:|
|
33
|
+
| X (strike) | 5/ (spare) | 9 0 (open frame) |
|
34
|
+
|
35
|
+
Frame 1 is (10 + 5 + 5) = 20
|
36
|
+
|
37
|
+
Frame 2 is (5 + 5 + 9) = 19
|
38
|
+
|
39
|
+
Frame 3 is (9 + 0) = 9
|
40
|
+
|
41
|
+
This means the current running total is 48.
|
42
|
+
|
43
|
+
The tenth frame in the game is a special case. If someone throws a
|
44
|
+
strike or a spare then they get a fill ball. Fill balls exist to
|
45
|
+
calculate the total of the 10th frame. Scoring a strike or spare on
|
46
|
+
the fill ball does not give the player more fill balls. The total
|
47
|
+
value of the 10th frame is the total number of pins knocked down.
|
48
|
+
|
49
|
+
For a tenth frame of X1/ (strike and a spare), the total value is 20.
|
50
|
+
|
51
|
+
For a tenth frame of XXX (three strikes), the total value is 30.
|
52
|
+
|
53
|
+
## Requirements
|
54
|
+
|
55
|
+
Write code to keep track of the score of a game of bowling. It should
|
56
|
+
support two operations:
|
57
|
+
|
58
|
+
* `roll(pins : int)` is called each time the player rolls a ball. The
|
59
|
+
argument is the number of pins knocked down.
|
60
|
+
* `score() : int` is called only at the very end of the game. It
|
61
|
+
returns the total score for that game.
|
62
|
+
|
63
|
+
## Setup
|
64
|
+
|
65
|
+
Go through the setup instructions for TypeScript to
|
66
|
+
install the necessary dependencies:
|
67
|
+
|
68
|
+
http://exercism.io/languages/typescript
|
69
|
+
|
70
|
+
## Requirements
|
71
|
+
|
72
|
+
Install assignment dependencies:
|
73
|
+
|
74
|
+
```bash
|
75
|
+
$ yarn install
|
76
|
+
```
|
77
|
+
|
78
|
+
## Making the test suite pass
|
79
|
+
|
80
|
+
Execute the tests with:
|
81
|
+
|
82
|
+
```bash
|
83
|
+
$ yarn test
|
84
|
+
```
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
## Source
|
89
|
+
|
90
|
+
The Bowling Game Kata at but UncleBob [http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata](http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata)
|
91
|
+
|
92
|
+
## Submitting Incomplete Solutions
|
93
|
+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
@@ -0,0 +1,77 @@
|
|
1
|
+
export default class Bowling {
|
2
|
+
rolls: number[]
|
3
|
+
|
4
|
+
constructor(rolls: number[]) {
|
5
|
+
this.rolls = rolls
|
6
|
+
}
|
7
|
+
|
8
|
+
score() {
|
9
|
+
const initialState = {
|
10
|
+
frameNumber: 1,
|
11
|
+
rollNumber: 1,
|
12
|
+
pinsRemaining: 10,
|
13
|
+
spareLastFrame: false,
|
14
|
+
strikeLastFrame: false,
|
15
|
+
twoStrikesInARow: false,
|
16
|
+
fillBall: false,
|
17
|
+
score: 0,
|
18
|
+
}
|
19
|
+
|
20
|
+
const finalState = this.rolls.reduce((state, roll) => {
|
21
|
+
if (roll < 0 || roll > 10) {
|
22
|
+
throw new Error('Pins must have a value from 0 to 10')
|
23
|
+
}
|
24
|
+
|
25
|
+
if (roll > state.pinsRemaining) {
|
26
|
+
throw new Error('Pin count exceeds pins on the lane')
|
27
|
+
}
|
28
|
+
|
29
|
+
if (state.frameNumber > 10) {
|
30
|
+
throw new Error('Should not be able to roll after game is over')
|
31
|
+
}
|
32
|
+
|
33
|
+
const finalFrame = state.frameNumber === 10
|
34
|
+
const strike = state.rollNumber === 1 && roll === 10
|
35
|
+
const spare = state.rollNumber === 2 && roll === state.pinsRemaining
|
36
|
+
const frameOver = finalFrame
|
37
|
+
? (!state.fillBall && !spare && state.rollNumber === 2) || state.rollNumber === 3
|
38
|
+
: strike || spare || state.rollNumber === 2
|
39
|
+
|
40
|
+
let score = state.score + roll
|
41
|
+
|
42
|
+
if (state.strikeLastFrame && state.rollNumber < 3) { score += roll }
|
43
|
+
if (state.spareLastFrame && state.rollNumber === 1) { score += roll }
|
44
|
+
if (state.twoStrikesInARow && state.rollNumber === 1) { score += roll }
|
45
|
+
|
46
|
+
const next = {
|
47
|
+
frameNumber: 0,
|
48
|
+
rollNumber: 0,
|
49
|
+
pinsRemaining: 0,
|
50
|
+
spareLastFrame: false,
|
51
|
+
strikeLastFrame: false,
|
52
|
+
twoStrikesInARow: false,
|
53
|
+
fillBall: false,
|
54
|
+
score: 0
|
55
|
+
}
|
56
|
+
|
57
|
+
next.frameNumber = frameOver ? state.frameNumber + 1 : state.frameNumber
|
58
|
+
next.rollNumber = frameOver ? 1 : state.rollNumber + 1
|
59
|
+
next.pinsRemaining = finalFrame
|
60
|
+
? ((strike || spare) ? 10 : state.pinsRemaining - roll)
|
61
|
+
: (frameOver ? 10 : state.pinsRemaining - roll)
|
62
|
+
next.spareLastFrame = frameOver ? spare : state.spareLastFrame
|
63
|
+
next.strikeLastFrame = frameOver ? strike : state.strikeLastFrame
|
64
|
+
next.twoStrikesInARow = frameOver ? strike && state.strikeLastFrame : state.twoStrikesInARow
|
65
|
+
next.fillBall = next.fillBall || (finalFrame && (strike || spare))
|
66
|
+
next.score = score
|
67
|
+
|
68
|
+
return next
|
69
|
+
}, initialState)
|
70
|
+
|
71
|
+
if (finalState.frameNumber !== 11) {
|
72
|
+
throw new Error('Score cannot be taken until the end of the game')
|
73
|
+
}
|
74
|
+
|
75
|
+
return finalState.score
|
76
|
+
}
|
77
|
+
}
|
@@ -0,0 +1,137 @@
|
|
1
|
+
import Bowling from './bowling'
|
2
|
+
|
3
|
+
describe('Bowling', () => {
|
4
|
+
describe('Check game can be scored correctly.', () => {
|
5
|
+
it('should be able to score a game with all gutterballs', () => {
|
6
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
7
|
+
expect(new Bowling(rolls).score()).toEqual(0)
|
8
|
+
})
|
9
|
+
|
10
|
+
xit('should be able to score a game with all open frames', () => {
|
11
|
+
const rolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6]
|
12
|
+
expect(new Bowling(rolls).score()).toEqual(90)
|
13
|
+
})
|
14
|
+
|
15
|
+
xit('a spare followed by zeros is worth ten points', () => {
|
16
|
+
const rolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
17
|
+
expect(new Bowling(rolls).score()).toEqual(10)
|
18
|
+
})
|
19
|
+
|
20
|
+
xit('points scored in the roll after a spare are counted twice', () => {
|
21
|
+
const rolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
22
|
+
expect(new Bowling(rolls).score()).toEqual(16)
|
23
|
+
})
|
24
|
+
|
25
|
+
xit('consecutive spares each get a one roll bonus', () => {
|
26
|
+
const rolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
27
|
+
expect(new Bowling(rolls).score()).toEqual(31)
|
28
|
+
})
|
29
|
+
|
30
|
+
xit('should allow fill ball when the last frame is a spare', () => {
|
31
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7]
|
32
|
+
expect(new Bowling(rolls).score()).toEqual(17)
|
33
|
+
})
|
34
|
+
|
35
|
+
xit('a strike earns ten points in a frame with a single roll', () => {
|
36
|
+
const rolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
37
|
+
expect(new Bowling(rolls).score()).toEqual(10)
|
38
|
+
})
|
39
|
+
|
40
|
+
xit('points scored in the two rolls after a strike are counted twice as a bonus', () => {
|
41
|
+
const rolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
42
|
+
expect(new Bowling(rolls).score()).toEqual(26)
|
43
|
+
})
|
44
|
+
|
45
|
+
xit('should be able to score multiple strikes in a row', () => {
|
46
|
+
const rolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
47
|
+
expect(new Bowling(rolls).score()).toEqual(81)
|
48
|
+
})
|
49
|
+
|
50
|
+
xit('should allow fill balls when the last frame is a strike', () => {
|
51
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1]
|
52
|
+
expect(new Bowling(rolls).score()).toEqual(18)
|
53
|
+
})
|
54
|
+
|
55
|
+
xit('rolling a spare with the two roll bonus does not get a bonus roll', () => {
|
56
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3]
|
57
|
+
expect(new Bowling(rolls).score()).toEqual(20)
|
58
|
+
})
|
59
|
+
|
60
|
+
xit('strikes with the two roll bonus do not get bonus rolls', () => {
|
61
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10]
|
62
|
+
expect(new Bowling(rolls).score()).toEqual(30)
|
63
|
+
})
|
64
|
+
|
65
|
+
xit('a strike with the one roll bonus after a spare in the last frame does not get a bonus', () => {
|
66
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10]
|
67
|
+
expect(new Bowling(rolls).score()).toEqual(20)
|
68
|
+
})
|
69
|
+
|
70
|
+
xit('should be able to score a perfect game', () => {
|
71
|
+
const rolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
|
72
|
+
expect(new Bowling(rolls).score()).toEqual(300)
|
73
|
+
})
|
74
|
+
})
|
75
|
+
|
76
|
+
describe('Check game rules.', () => {
|
77
|
+
xit('rolls can not score negative points', () => {
|
78
|
+
const rolls = [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
79
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Pins must have a value from 0 to 10')
|
80
|
+
})
|
81
|
+
|
82
|
+
xit('a roll can not score more than 10 points', () => {
|
83
|
+
const rolls = [11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
84
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Pins must have a value from 0 to 10')
|
85
|
+
})
|
86
|
+
|
87
|
+
xit('two rolls in a frame can not score more than 10 points', () => {
|
88
|
+
const rolls = [5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
89
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Pin count exceeds pins on the lane')
|
90
|
+
})
|
91
|
+
|
92
|
+
xit('two bonus rolls after a strike in the last frame can not score more than 10 points', () => {
|
93
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 6]
|
94
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Pin count exceeds pins on the lane')
|
95
|
+
})
|
96
|
+
|
97
|
+
xit('two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike', () => {
|
98
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6]
|
99
|
+
expect(new Bowling(rolls).score()).toEqual(26)
|
100
|
+
})
|
101
|
+
|
102
|
+
xit('the second bonus rolls after a strike in the last frame can not be a strike if the first one is not a strike', () => {
|
103
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6, 10]
|
104
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Pin count exceeds pins on the lane')
|
105
|
+
})
|
106
|
+
|
107
|
+
xit('an unstarted game can not be scored', () => {
|
108
|
+
const rolls: number[] = []
|
109
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Score cannot be taken until the end of the game')
|
110
|
+
})
|
111
|
+
|
112
|
+
xit('an incomplete game can not be scored', () => {
|
113
|
+
const rolls = [0, 0]
|
114
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Score cannot be taken until the end of the game')
|
115
|
+
})
|
116
|
+
|
117
|
+
xit('a game with more than ten frames and no last frame spare or strike can not be scored', () => {
|
118
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
119
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Should not be able to roll after game is over')
|
120
|
+
})
|
121
|
+
|
122
|
+
xit('bonus rolls for a strike in the last frame must be rolled before score can be calculated', () => {
|
123
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]
|
124
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Score cannot be taken until the end of the game')
|
125
|
+
})
|
126
|
+
|
127
|
+
xit('both bonus rolls for a strike in the last frame must be rolled before score can be calculated', () => {
|
128
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]
|
129
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Score cannot be taken until the end of the game')
|
130
|
+
})
|
131
|
+
|
132
|
+
xit('bonus roll for a spare in the last frame must be rolled before score can be calculated', () => {
|
133
|
+
const rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3]
|
134
|
+
expect(() => { new Bowling(rolls).score() }).toThrowError('Score cannot be taken until the end of the game')
|
135
|
+
})
|
136
|
+
})
|
137
|
+
})
|