20
Many people have gone to their local bowling center to play a few games of bowling, and many people continue to struggle to calculate their scores. World Bowling has introduced a simplified scoring system in order to attract more people to the sport. This scoring system is utilized in international games.
The scoring system works like this (from Wikipedia):
The World Bowling scoring system—described as "current frame scoring"[32]—awards pins as follows:
- strike: 30 (regardless of ensuing rolls' results)
- spare: 10 plus pinfall on first roll of the current frame
- open: total pinfall for current frame
If you're not familiar with ten-pin bowling, here's a recap.
There are 10 pins at the end of a bowling lane where the goal is to knock all of them down with a bowling ball. You get 2 rolls of a ball to attempt to knock them all down, preferably knocking them all down with the first roll (known as a strike). If you do get a strike, then that frame is completed and you do not need to roll the ball a second time. A strike is worth 30.
If you don't knock down all ten, you get one more roll. If you knock down all of the remaining pins, that is known as a spare. The score is worth 10 pins + the number of pins knocked down on the first roll. For example, if I knocked down 7 pins, then managed to knock down the remaining 3, that would be worth 17.
If after your second roll you fail to knock down all ten, that is known as an open frame. The score is worth the total number of pins knocked down for that frame.
There are 10 frames in a game. If you're familiar with traditional bowling scoring, you don't get an extra roll in the 10th frame with the World Bowling Scoring. In traditional bowling scoring, it takes 12 consecutive strikes to get a perfect score of 300, whereas World Bowling scoring only requires 10 consecutive strikes.
Challenge
Your challenge is to calculate the score given values from a score sheet.
On a score sheet, a miss is indicated by a dash (-), a strike with an X, and a spare with a slash (/). If these don't apply, then the pinfall count is simply indicated with a number (1-9). Fouls and splits are also recorded onto score sheets but you do not need to worry about these.
Input
You will be given a string consisting of scores for each frame, and will have a total of ten frames. Each frame will have up to two values, or as little as 1 value if there was a strike. Your input may be string parameter to a function, read from a file, or from STDIN.
For example, if I knocked down 1 pin on my first roll, then knocked down 2, the frame would look like "12". This does not mean 12 (twelve), but means 1 and 2, for a total of 3.
If I missed every pin with both rolls (gutter balls), it would look like this "--" (score of 0).
Each frame will be separated by a space.
Sample input
-- 9- -9 X -/ 8/ 71 15 44 X
To break down this example,
- Frame 1 (--) - both rolls missed. scored 0
- Frame 2 (9-) - knocked down 9 on the first roll, missed on the second roll. Score 9
- Frame 3 (-9) - Missed all on the first, got 9 on the second. Score 9
- Frame 4 (X) - Strike, knocked down all ten. Score 30
- Frame 5 (-/) - Spare, missed all on the first, knocked down all with 2nd roll. Score 10 + 0 = 10
- Frame 6 (8/) - Spare, 8 pins on first roll, knocked down the other 2 with 2nd roll. Score 10+8 = 18
- Frame 7 (71) - open frame,7 pins on first roll, 1 pin on second roll. Score 7+1=8
- Frames 8,9,10 follow same examples as above.
Output
Output will simply be a value that has the sum of the scores from all 10 frames. Using the sample input, the output will be 128. Your output may be a string or a numeric type. It may be a function return value, or written to STDOUT.
Rules
- Assume that the input will always be valid. For example, an invalid frame would be "/8", "XX", "123", "0", etc.
- You do not need to worry about splits or fouls.
- Your code may be a full program or a function that takes in a string and returns the score.
- Your code must not throw any exceptions.
- This is code golf, the answer with the fewest number of bytes wins.
- Languages that use includes or imports must include the import statements as part of their code and count towards the byte count.
Test Cases
"-- 9- -9 X -/ 8/ 71 15 44 X" -> 128
"-- -1 2- 12 22 5- 42 61 8- 72" -> 45
"X X X 1/ 2/ 3/ 4/ 5/ -- 9/" -> 174
"X X X X X X X X X X" -> 300
"-- -- -- -- -- -- -- -- -- --" -> 0
21I'm disappointed this isn't a code-bowling challenge – Jo King – 2018-03-26T04:49:46.533
13Your first spare example says the score would be 13, but I think it is supposed to be 17. – Jo. – 2018-03-26T04:51:50.787
@Jo. Good catch. I've updated the question to fix that error. – Makotosan – 2018-03-26T12:42:21.553
@JoKing I was thinking this is a code-bowling challenge composed of 10 subchallenges when I first saw the title. – Weijun Zhou – 2018-03-27T03:36:44.847
1One of the best documented and written challenges I have seen. – Joshua – 2018-03-27T15:36:07.980