Bowling is played in ten frames, each of which allows one or two deliveries. If all ten pins are bowled over in the first delivery, there is no second delivery.
Each frame has a score based on the delivery in that frame, as well as the next one or two deliveries. This means that the score for a frame may not necessarily be posted at the end of the frame. It also means that the tenth frame may require a total of three deliveries to resolve the scoring.
A game can be as few as twelve deliveries: ten frames of strikes require two additional deliveries in the tenth frame to resolve the rule B scoring. A game can be as many as twenty-one deliveries: nine open frames of less than 10 pins bowled over during the frame, and a spare in the tenth frame requiring one extra delivery to resolve the rule C scoring.
There is a relatively straight-forward annotation for play. Each frame has two characters to describe the pins bowled during the delivery. The final frame has three characters for a total of 21 characters.
Rule A: If the frame is open, the two characters are the two deliveries; the total will be less than 10. If a delivery fails to bowl over any pins, a "-" is used instead of a number.
Rule B: If the frame is strike, the two characters are "X ". No second delivery was made.
Rule C: If the frame is a spare, the first character is the number of pins on the first delivery. The second character is a "/".
"8/9-X X 6/4/X 8-X XXX"
This can be analyzed into ten frames as follows:
|Frame||First delivery||Second delivery||Scoring rule||Frame Score||Total|
|1||8||/, must have been 2||C- spare = 10 + next delivery||19||19|
|2||9||-, 0||A- open = 9||9||28|
|3||10||(not taken)||B- strike = 10 + next 2 deliveries||26||54|
|4||10||(not taken)||B- strike = 10 + next 2 deliveries||20||74|
|5||6||/, must have been 4||C- spare = 10 + next delivery||14||88|
|6||4||/, must have been 6||C- spare = 10 + next delivery||20||108|
|7||10||(not taken)||B- strike = 10 + next 2 deliveries||18||126|
|8||8||-, 0||A- open = 8||8||134|
|9||10||(not taken)||B- strike = 10 + next 2 deliveries||30||164|
|10||10||10 and 10||B- strike = 10 + next 2 deliveries, two extra deliveries are taken during this 10th frame.||30||194|
Each of the first nine frames has a two-character code for each delivery. There are three forms:
The tenth frame has a three-character code for each of the deliveries. There are three forms:
Write a valid(game)() function that will validate a 21-character string as describing a legal game.
Write a scoring method, scores(game)(), that will accept the 21-character scoring string and produce a sequence of frame-by-frame totals.
Write a reporting method, scoreCard(game)(), that will use the validation and scoring functions to produce a scorecard. The scorecard shows three lines of output with 5 character positions for each frame.
The game shown above would have the following output.
1 2 3 4 5 6 7 8 9 10 8/ 9- X X 6/ 4/ X 8- X XXX 19 28 54 74 88 108 126 134 164 194