Bowling Score Spiked

Bob Martin told a story about pair-programming the code to compute bowling scores. I wrote this alternate implementation mostly to see if I could and then wrote it up as an example of spike-programming. website

The input is the sequence of throws recorded along the top of each frame. The output is the running total reported in the bottom. I chose to represent the input as a string of digits with commas at frame boundaries. Strikes and spares are denoted by x and / as is the custom with hand scoring. My program translates the input to output in place with a series of substitution commands.

s/\/, (\d)/\/ $1, $1/g; s/\/, (x)/\/ 10, x/g; s/(\d )\//$1.(10-$1)/ge; s/x, (\d \d)/x $1, $1/g; s/x/10/g; s/(((,?)( \d+)+){10}).*/$1/; s/(\d+) (\d+)/$1+$2/ge; s/(\d+) (\d+)/$1+$2/ge; s/(\d+)/$total+=$1/ge;

This program was innovative in that it propagated the bonus for strikes and spares by duplicating digits without adding them up until the end.

> That one is really pretty amazing. Amazingly unreadable is what I think when I see this solution. Yuck. That's perl for ya.

I can't go bowling without recalling my discovery years earlier when I coded a scoring algorithm with a simple loop by looking ahead at the next frame rather than carrying incomplete bonuses forward and then back-patching results as is done when scoring in realtime.