COS 100: Introduction to Programming
Interim 2022
HW Project 04: Darts game
Due: 01/13 Thu 10pm
Now a certain man drew a bow at random, and struck the king of Israel between
the joints of his armor. So he said to the driver of his chariot, "Turn around
and take me out of the battle, for I am wounded."
I Kings 22:34
Project goal
Create a simulation of a 301 darts game.Project specification
-
Name your project source code
darts.py
. - A standard dart board has the integer values 1–20. It's also possible to miss the board and get zero points. For this project, we're going to ignore bull's eyes and multipliers, so the only possible dart values are 0–20, and we'll assume they're all equally likely.
- In a game of 301, you start with 301 points, and the goal is to reach 0 points. Each time you throw a dart, if the dart value is less than or equal to your remaining score, you subtract the dart value from your total. If subtracting the dart value from your score would take you below zero, the dart doesn't count. (In a real game, a player throws three times per turn. For simplicity, we will simply make one throw per turn for this project.)
- For each dart throw, tell the user the throw number, whether the dart missed, the value of the dart if it didn't miss, whether the dart counted, and the updated score if the dart did count.
- This program requires no user interaction, and since we're using a random number generator, each run should be different. Some sample runs are below, but it is unlikely that your program will generate the exact same sequence. However, note how the program responds to each throw.
Notes and hints
-
Generating random numbers:
Write
import random
at the top of your program. Then writerandom.randint(low, high)
to get a random number betweenlow
andhigh
, inclusive. For instance, if you saidrandom.randint(3, 7)
, you would get back one of 3, 4, 5, 6, or 7. - Even though the program is generating quite a bit of output, you will likely find that the amount of code you have to write is less than the previous project. However, it is still important to plan your code out before you start writing and develop incrementally, as always.
Suggested order of development
- Generate a random number and print it out.
- Loop 10 times and generate different random numbers for each loop iteration.
- Start with a score of 301, generate a random number each iteration and subtract that from your running total.
- Extend the loop from 10 times to as many times as needed.
- Add in the rest of the requirements above.
Grading rubric
- 3 pt: generating random throw values
- 5 pt: calculating and printing out score after each throw
- 4 pt: handling end of game
- 6 pt: looping logic
- 2 pt: style (good variable names, commenting, etc.)
Sample runs
-
Welcome. Initial score is 301 Throw 1 hit the 17, score is now 284 Throw 2 hit the 9, score is now 275 Throw 3 hit the 16, score is now 259 Throw 4 hit the 18, score is now 241 Throw 5 missed. =( Throw 6 hit the 20, score is now 221 Throw 7 hit the 2, score is now 219 Throw 8 missed. =( Throw 9 hit the 14, score is now 205 Throw 10 hit the 11, score is now 194 Throw 11 hit the 9, score is now 185 Throw 12 hit the 6, score is now 179 Throw 13 hit the 1, score is now 178 Throw 14 hit the 5, score is now 173 Throw 15 hit the 11, score is now 162 Throw 16 hit the 16, score is now 146 Throw 17 hit the 15, score is now 131 Throw 18 hit the 6, score is now 125 Throw 19 hit the 7, score is now 118 Throw 20 missed. =( Throw 21 hit the 17, score is now 101 Throw 22 hit the 17, score is now 84 Throw 23 hit the 3, score is now 81 Throw 24 hit the 16, score is now 65 Throw 25 hit the 11, score is now 54 Throw 26 hit the 12, score is now 42 Throw 27 hit the 16, score is now 26 Throw 28 hit the 20, score is now 6 Throw 29 hit the 9, but score is only 6, so it doesn't count. Throw 30 hit the 1, score is now 5 Throw 31 hit the 15, but score is only 5, so it doesn't count. Throw 32 hit the 5, score is now 0 The game is over after 32 throws.
-
Welcome. Initial score is 301 Throw 1 missed. =( Throw 2 hit the 10, score is now 291 Throw 3 hit the 16, score is now 275 Throw 4 hit the 18, score is now 257 Throw 5 hit the 13, score is now 244 Throw 6 hit the 6, score is now 238 Throw 7 hit the 10, score is now 228 Throw 8 hit the 4, score is now 224 Throw 9 missed. =( Throw 10 hit the 18, score is now 206 Throw 11 hit the 8, score is now 198 Throw 12 hit the 5, score is now 193 Throw 13 hit the 1, score is now 192 Throw 14 hit the 5, score is now 187 Throw 15 hit the 11, score is now 176 Throw 16 hit the 4, score is now 172 Throw 17 hit the 9, score is now 163 Throw 18 hit the 17, score is now 146 Throw 19 hit the 19, score is now 127 Throw 20 hit the 8, score is now 119 Throw 21 hit the 20, score is now 99 Throw 22 hit the 18, score is now 81 Throw 23 hit the 7, score is now 74 Throw 24 hit the 1, score is now 73 Throw 25 hit the 19, score is now 54 Throw 26 missed. =( Throw 27 hit the 4, score is now 50 Throw 28 hit the 8, score is now 42 Throw 29 hit the 7, score is now 35 Throw 30 hit the 19, score is now 16 Throw 31 hit the 10, score is now 6 Throw 32 hit the 7, but score is only 6, so it doesn't count. Throw 33 missed. =( Throw 34 hit the 19, but score is only 6, so it doesn't count. Throw 35 hit the 5, score is now 1 Throw 36 hit the 14, but score is only 1, so it doesn't count. Throw 37 hit the 18, but score is only 1, so it doesn't count. Throw 38 hit the 2, but score is only 1, so it doesn't count. Throw 39 hit the 17, but score is only 1, so it doesn't count. Throw 40 hit the 8, but score is only 1, so it doesn't count. Throw 41 hit the 4, but score is only 1, so it doesn't count. Throw 42 hit the 17, but score is only 1, so it doesn't count. Throw 43 hit the 13, but score is only 1, so it doesn't count. Throw 44 hit the 12, but score is only 1, so it doesn't count. Throw 45 hit the 11, but score is only 1, so it doesn't count. Throw 46 hit the 15, but score is only 1, so it doesn't count. Throw 47 hit the 16, but score is only 1, so it doesn't count. Throw 48 hit the 9, but score is only 1, so it doesn't count. Throw 49 hit the 13, but score is only 1, so it doesn't count. Throw 50 hit the 12, but score is only 1, so it doesn't count. Throw 51 hit the 15, but score is only 1, so it doesn't count. Throw 52 hit the 7, but score is only 1, so it doesn't count. Throw 53 hit the 7, but score is only 1, so it doesn't count. Throw 54 hit the 19, but score is only 1, so it doesn't count. Throw 55 hit the 3, but score is only 1, so it doesn't count. Throw 56 hit the 2, but score is only 1, so it doesn't count. Throw 57 hit the 2, but score is only 1, so it doesn't count. Throw 58 hit the 6, but score is only 1, so it doesn't count. Throw 59 hit the 9, but score is only 1, so it doesn't count. Throw 60 hit the 2, but score is only 1, so it doesn't count. Throw 61 missed. =( Throw 62 hit the 13, but score is only 1, so it doesn't count. Throw 63 hit the 3, but score is only 1, so it doesn't count. Throw 64 hit the 13, but score is only 1, so it doesn't count. Throw 65 hit the 19, but score is only 1, so it doesn't count. Throw 66 hit the 14, but score is only 1, so it doesn't count. Throw 67 hit the 4, but score is only 1, so it doesn't count. Throw 68 hit the 13, but score is only 1, so it doesn't count. Throw 69 hit the 12, but score is only 1, so it doesn't count. Throw 70 hit the 2, but score is only 1, so it doesn't count. Throw 71 missed. =( Throw 72 hit the 2, but score is only 1, so it doesn't count. Throw 73 hit the 16, but score is only 1, so it doesn't count. Throw 74 missed. =( Throw 75 hit the 12, but score is only 1, so it doesn't count. Throw 76 hit the 3, but score is only 1, so it doesn't count. Throw 77 hit the 1, score is now 0 The game is over after 77 throws.
Before moving on
As an optional challenge, think about possible extensions that you can add to your program to make it more exciting. Be creative. If you would like to code up these extensions:- First, for the sake of the ease of grading, please submit the original, vanilla version of your project to Moodle as
darts.py
. - Make a copy called
extension.py
and go wild. - If you are especially proud of your extension, feel free to submit it to Moodle as a second file.
- Make sure
darts.py
is still there: that's the only file that will be graded, and should not contain extensions. - I might look at extensions and possibly show truly exciting ones to the class. (If you don't want your code shown, don't submit it.)
Start early, have fun, and discuss questions on Moodle.