COMP 141: Project 2
Darts
You are playing a strange game of darts with a square board that looks like the
image below. Pretend there is a grid on the board similar to the regular x-y Cartesian
coordinate system. The lower left corner of the board is at position (0, 0) and the
upper right corner is at (30, 30).
You earn points in this game of darts depending on what color the square is that you hit.
Red squares are worth 5 points, yellow squares are 10 points, and green squares are
15 points. If the dart lands off the board, no points are earned.
What you need to do
Write a complete Python program that asks the user where their dart landed and
prints out how many points they win for that dart, according to the dart board
diagram above. Your program
only has to handle a single dart. Your program should gracefully handle cases where
the dart lands off the board by printing a message saying that no points were won.
You may work with ints or floats. You should assume that if the dart lands on a vertical
dividing line, the points are awarded for the region to the right, and if the dart lands
on a horizontal dividing line, the points are awarded for the region above.
You should define a main() function for your program. You do not need to use
any other functions unless you so desire. (The primary point of this program is to test your
if/else skills; there will be other programs later to test your function skills.)
Test your program on lots of examples, including the sample ones below, and make sure they work correctly.
Your program should work correctly for all reasonable inputs, not just the sample
ones below.
Make sure to include comments in your code that describe what the program does as
a whole, what each separate function does, and what any complicated sections of code do.
You should feel free to use more comments if you so desire. Follow the instructions in the
comment guide.
Sample Interactions
What the computer displays (prints) is in regular text, what the user types is in bold, and what
the program is doing behind the scenes is in italics.
Test 1
(Program begins)
What is the x-coordinate where your dart landed? 5
What is the y-coordinate where your dart landed? 9
(Program determines the dart is in a red region.)
You win 5 points!
(Program ends)
Test 2
(Program begins)
What is the x-coordinate where your dart landed? 20
What is the y-coordinate where your dart landed? 10
(Program determines the dart is in a yellow region.)
You win 10 points!
(Program ends)
Test 3
(Program begins)
What is the x-coordinate where your dart landed? 25
What is the y-coordinate where your dart landed? 0
(Program determines the dart is in a green region.)
You win 15 points!
(Program ends)
Test 4
(Program begins)
What is the x-coordinate where your dart landed? -1
What is the y-coordinate where your dart landed? -1
(Program determines the dart is off the board.)
You win 0 points!
(Program ends)
Test 5
(Program begins)
What is the x-coordinate where your dart landed? 100
What is the y-coordinate where your dart landed? 150
(Program determines the dart is off the board.)
You win 0 points!
(Program ends)
Your code does not need to follow this script verbatim, but all the mentioned functionality
should work as shown.
Hints
- Work out some examples on paper first to determine how the math works in this problem.
- You should use a series of if-elif-else tests for the main logic in your program.
You can set up these tests in lots of different
ways. The most straightforward way is to test all nine regions individually with separate tests,
but your program will have a lot of lines (which is OK). Another (better) way is to use and/or to combine
multiple tests to test all the regions of the same color at once.
What to turn in
Through Moodle, turn in your code as a file called darts_yourLastName_yourFirstName.py.
Challenge Problems
From time to time, I will offer "challenge problems" on assignments. These problems
are designed to have little (but some) impact on your grade whether you do them or not.
You should think of these problems as opportunities to work on something interesting and optional,
rather than a way to raise your grade through "extra credit."
Policy on challenge problems:
-
Challenge problems will typically allow you to get 2-5% of additional credit on an assignment, yet they
will typically be much more difficult than this credit amount suggests.
-
You should not attempt a challenge problem until you have finished the rest of an assignment.
- The tutors will not provide help on challenge problems. The instructor
will provide minimal assistance only, as these problems are optional and are designed
to encourage independent thought.
- Challenge problems may be less carefully specified and less
carefully calibrated for how difficult or time-consuming they are.
Challenge problem for this assignment:
- Design a more complicated dart board than the one listed here. Some ideas include
adding additional colors and point values, enlarging the size of the board, dividing it
into more regions, using a circular board (like a traditional dart board),
or using an even more complicated shape for the board. If you do this challenge, describe your modified board
in a comment at the top of your program and how it works with respect to points. Turn in both the original dart board
project code as a .py file and a modified project .py file separately.
- Draw the dartboard using graphics commands (a new board or the one above).
- Let the user click on the dartboard to pick where their dart goes, instead of having them type in coordinates at the keyboard.