# Calculating a Bowling Score part 1

September 28 2008 at 9:27 AM

This is the first part of how to program a routine that will calculate a bowling score. If you bowl, your bowling center is probably equipped with a computerized system to keep track of each player's score. The calculation of a bowling score is unusual, since bowlers get bonuses for SPAREs and STRIKEs, which can really add up when rolled consecutively.

For example, there are 10 pins to knock down per frame, and 10 frames to be played. This implies a maximum of 100 pins, but a performance of all consecutive strikes gives a bowler a perfect game of 300!

Yes, after 10 consecutive STRIKEs a bowler gets 2 more rolls, but even with 2 more STRIKEs, the total would still be only 120, not 300. The 300 score is a perfect game, and it's mostly the result all those bonus pins added on to the actual pins knocked down.

In this first part, we're only creating an example of how one frame can be played: A player is allowed a maximum of 2 rolls, in an attempt to knock down 10 pins. If the player knocks down all 10 pins on the first roll, he has a STRIKE, which is marked as an 'X', and he doesn't get a second roll.

If the player only knocks down nine 9 pins or less, another roll is allowed to try to get the pins that are still standing. If all remaining pins are knocked down, the player scores a SPARE, marked with an '/'.

If the player doesn't score a SPARE or a STRIKE the total number of pins knocked down is 9 or less. Both rolls will be marked with a number, which added together cannot be greater than 9.

This program is less than 1k with plenty of spacing, a couple of optional lines and comments removed -
Its structure is a pair of nested WHILE-WEND loops, contains only 2 IF-THENs, and allows only correct keystrokes. Its design is that it will allow only acceptable keystrokes, so it never displays error messages.

This is the code that we begin with, our next installment will have more ambitious goals:

=============================================================================

SCREEN 9 ' OPTIONAL - specified as 9 because of LOCATEs

roll% = 1 ' establish first roll and
roll\$(1) = "0123456789X" ' all knockdown possiblities in a string all 10 pins are a STRIKE, represented as 'X'

WHILE roll% <= 2 ' we get only 2 rolls per frame

LOCATE 1 + roll%, 30: PRINT roll\$(roll%) ' OPTIONAL: show all possibilities for the current roll

k\$ = "" ' set keystroke string variable to null

WHILE found% = 0 OR k\$ = "" ' loop until a possiblity has been entered

k\$ = INKEY\$
found% = INSTR(roll\$(roll%), k\$) ' the loop will run until the keystroke is found in the set of possibilities

WEND

IF k\$ = "x" THEN k\$ = "X" ' this will turn 'x' into 'X'
LOCATE 7, 3 + roll%: PRINT k\$; ' display current roll result

IF k\$ <> "X" THEN ' if it wasn't a strike

roll% = roll% + 1 ' we get a 2nd roll

roll\$(roll%) = LEFT\$(roll\$(1), 11 - (VAL(k\$) + 1)) + "/" ' and an adjusted set of possibilities

ELSE roll% = 3 ' if it was a strike, we're done for this frame

END IF

WEND

 Respond to this message