Calculating a Bowling Score part 1September 28 2008 at 9:27 AM
|MONTREALER (no login)|
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
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
Calculating a Bowling Score part 2
|October 5 2008, 9:25 AM |
Now that our program understands how one single frame of bowling is scored,
we need to make it understand a couple more things:
First, a player will bowl a minimum of 10 frames.
Second, each frame not ended with a MARK, meaning a SPARE or a STRIKE will likely be scored with the total of the 2 balls rolled for that frame.
In our previous installment, the program only had to know how to accept a correct input for a frame. Now these inputs begin amounting something. Next time, we move on to a bit more complexity.
This program is not exactly elegant, but since we're still far away from our final goal,
our only intention at this stage is to have it produce correct results and understand that a full game is a minimum of 10 frames.
vertical% = 7
frames% = 1
WHILE frames% < 11
roll% = 1
roll$(1) = "0123456789Xx"
LOCATE vertical% + frames%, 4 + (frames% >= 10): PRINT "Frame #"; frames%; ":"
WHILE roll% <= 2
k$ = ""
WHILE found% = 0 OR k$ = ""
k$ = INKEY$
found% = INSTR(roll$(roll%), k$)
IF k$ = "x" THEN k$ = "X"
LOCATE vertical% + frames%, 14 + roll% * 3: PRINT k$ ' display current roll result
IF k$ = "X" THEN LOCATE vertical% + frames%, 25: PRINT "X"
IF roll% = 1 THEN pins% = VAL(k$)
IF roll% = 2 THEN
LOCATE vertical% + frames%, 24
IF k$ = "/" THEN PRINT " "; k$ ELSE pins% = pins% + VAL(k$): PRINT pins%
IF k$ <> "X" THEN
roll% = roll% + 1
roll$(roll%) = LEFT$(roll$(1), 11 - (VAL(k$) + 1)) + "/" + " "
ELSE roll% = 3
frames% = frames% + 1
|Current Topic - Calculating a Bowling Score part 1|