This lab is simple on the surface, but definitely annoying to implement. It’ll be good for refining your logic & reasoning skills. That and you’ll learn how to score bowling!
Here’s how bowling is scored at a high level:
Barring strikes & spares, a game’s total score is the sum of the number of pins knocked down in each frame. The highest possible score is 300, consisting of 12 strikes.
Strikes & spares are a bit more complicated. A strike’s value is 10 plus the number of pins knocked down in the next 2 rolls. A spare’s value is 10 plus the number of pins knocked down in the next roll. If it’s the 10th frame, then you will have 2 rolls to start. If you get a strike or spare, then you will have 1 more roll to finish the frame, making 3 rolls total. Otherwise it’s just 2 rolls for the last frame.
Here’s a rough outline of the program flow:
done
when prompted for their nameAlso note that the player can enter done
at the beginning of the game, in which case no scores will be evaluated, and the print statement will be different.
Algorithmically speaking, we can break this down into 2 parts:
More specifically, scoring a frame is as follows:
If it was a strike, then get the current frame’s score plus the next 2 rolls If it was a spare, then get the current frame’s score plus the next roll Otherwise, just get the current frame’s score
Also if you get 2 strikes in a row, then the score (for that frame alone) is STRIKE + STRIKE + next_roll
.
The general formula for scoring a frame is:
1
2
3
4
5
6
7
8
score += current_frame_score
if current_frame.is_strike:
if next_frame.is_strike:
score += 10 + next_next_frame.first_roll
else:
score += next_frame.first_roll + next_frame.second_roll
elif current_frame.is_spare:
score += next_frame.first_roll
The caveats to consider are
There are no special formatting requirements for this lab. Just output the words & lines as shown in the sample output.
As for the input requirements, just make sure your data types are what they should be. No error checking is required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Enter player's name (done for no more players): John
Enter score for frame 1, roll 1: 10
Enter score for frame 2, roll 1: 10
Enter score for frame 3, roll 1: 10
Enter score for frame 4, roll 1: 10
Enter score for frame 5, roll 1: 10
Enter score for frame 6, roll 1: 10
Enter score for frame 7, roll 1: 10
Enter score for frame 8, roll 1: 10
Enter score for frame 9, roll 1: 10
Enter score for frame 10, roll 1: 10
Enter score for frame 10, roll 2: 10
Enter score for frame 10, roll 3: 10
Enter player's name (done for no more players): Cheryl
Enter score for frame 1, roll 1: 8
Enter score for frame 1, roll 2: 1
Enter score for frame 2, roll 1: 0
Enter score for frame 2, roll 2: 9
Enter score for frame 3, roll 1: 2
Enter score for frame 3, roll 2: 8
Enter score for frame 4, roll 1: 10
Enter score for frame 5, roll 1: 6
Enter score for frame 5, roll 2: 3
Enter score for frame 6, roll 1: 7
Enter score for frame 6, roll 2: 0
Enter score for frame 7, roll 1: 5
Enter score for frame 7, roll 2: 2
Enter score for frame 8, roll 1: 10
Enter score for frame 9, roll 1: 0
Enter score for frame 9, roll 2: 6
Enter score for frame 10, roll 1: 2
Enter score for frame 10, roll 2: 8
Enter score for frame 10, roll 3: 10
Enter player's name (done for no more players): done
John scored 300.
Cheryl scored 122.
Cheryl did the worst by scoring 122.
John won the game by scoring 300.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Enter player's name (done for no more players): John
Enter score for frame 1, roll 1: 8
Enter score for frame 1, roll 2: 1
Enter score for frame 2, roll 1: 0
Enter score for frame 2, roll 2: 9
Enter score for frame 3, roll 1: 2
Enter score for frame 3, roll 2: 8
Enter score for frame 4, roll 1: 10
Enter score for frame 5, roll 1: 6
Enter score for frame 5, roll 2: 3
Enter score for frame 6, roll 1: 7
Enter score for frame 6, roll 2: 0
Enter score for frame 7, roll 1: 5
Enter score for frame 7, roll 2: 2
Enter score for frame 8, roll 1: 10
Enter score for frame 9, roll 1: 0
Enter score for frame 9, roll 2: 6
Enter score for frame 10, roll 1: 2
Enter score for frame 10, roll 2: 8
Enter score for frame 10, roll 3: 10
Enter player's name (done for no more players): done
John scored 122.
John did the worst by scoring 122.
John won the game by scoring 122.
As per the lab’s instructions, you’ll need to implement the following:
vector
of string
s to store the name(s) of the player(s)vector
of 21 int
s to store 2 rolls per frame, plus 1 roll for the 10th frame (if applicable)vector
of int
s to store the score(s) of the player(s)
done
when prompted for their name), then the game should be scored and the results should be printedAs usual, the “automated” testing for this lab is assuming you’re running on a Unix-like system. If you’re on Windows, the test commands below likely won’t work.
I will say though, if you are running Ubuntu or some Debian distro on Windows, then the following tests should work. Just make sure you have curl
and make
installed.
1
sudo apt install curl make
If you wish to test your program without inputting the values yourself, I’ve provided a Makefile
that will both compile and test the program using the inputs from the outputs here and here based on the commands below.
The Makefile assumes your source file is named
bowling.cpp
. If it’s not then change it to that if you wish to test with the Makefile. Also make sure to run the following commands in the same directory as yourbowling.cpp
.
1
curl https://raw.githubusercontent.com/Ethan0429/cs102-writeups/main/lab7/Makefile --output Makefile
or
1
curl https://utknotes.pisaucer.com/CS102/EthanNotes/Lab07/Makefile.txt --output Makefile
compiles your program
1
make
Make sure you’ve at least completed the gameplay loop of the program before running the tests. Basically, don’t test until you have everything and just want to make sure the scoring works. Also, the following tests omit the input from the output. So the Canvas output will show the inputs printed along with the outputs, whereas the test outputs will not show those inputs (the pins knocked in each roll). This is just a result of the test(s), and does not reflect your program’s full output in that sense.
tests the 1st input from output 1
1
make test1
tests the 2nd input from output 2
1
make test2
You’ll want to test for a bunch of input types as well, since the two provided on Canvas won’t cover every case. The following command will test 5 player inputs, each of which will have random rolls each frame. You can view the inputs from the test.txt
file that is generated once the command is run. I can’t provide a solution output, so you’ll have to check your scores yourself. You can do it manually, or I recommend using this bowling score calculator
tests for random inputs (assumes you have python3 installed)
1
make random
You don’t need to run
make
to compile before testing.make test1
,make test2
, andmake random
will compile the program for you as well as run the tests.
Lastly, I’ll mention that if either of the make
tests don’t print anything out, it’s likely because you’re handling the incorrect amount of rolls/frames or your output doesn’t match the format from the example shown earlier.
Good luck! Open a ticket on the Discord or come to office hours if you have any questions, as always.