For Archers, shooters, hunters, and anyone using an long-range weapon or tool, practicing your aim is important.
In this assignment, you will be tasked with writing a program that prints out a target, and also prints out the locations of various “hits” on this target.
The information about where and how the target “hits” should be displayed will be provided by the use via input strings.
You should name the file target_practice.py
.
This program should accept two strings from the user. The first string specifies where on the targets the program should display the “hits”.
The target that your program prints out should be a 15-character wide by 11-character tall target, as shown below:
|
|
|
|
|
-------|-------
|
|
|
|
|
The user can “tell” the program where to put the hit markers on this target using this first input. Each hit should be represented with 4 characters, and the string could have more than one hit. Thus, the length of the string should always be a multiple of 4. Here is one example input string:
This input string represents two hits on the target (-2+3
and +4-1
).
-2+3
means that on the x axis (side to side) the hit should be at negative 2 (two characters left of center) and on the y axis the hit should be at the third level above the middle.
+4-1
means that on the x axis the hit should be four characters right of center and on the y axis the hit should be one level above the middle.
The second input should be exactly one character long. It represents that character to put onto the target to mark the hit.
For example, if the program were run with the first input as -2+3+4-1
and the second input as the string X
then it appear as follows:
Hit string:
-2+3+4-1
Marker:
X
|
|
X |
|
|
-------|-------
| X
|
|
|
|
Your code should check to ensure that the hit string has a length that is greater than 3, and a multiple of 4.
If this is not the case, it should print Please provide a valid hit string.
and then continue to ask for the input until a valid one is received.
It should also check to ensure that the hit marker has a length of exactly 1.
If not, it should print Please provide a valid marker.
and then continue to ask for the input until a valid one is received.
You are not allowed to use string multiplication for this assignment. Instead, in order to print out the target, I recommend that you use a while loop within a while loop. The “outer” while loop will be responsible for iterating through each row. For this, you can set up a while loop that begins at +5 and iterates until -5.
Within this “outer” loop, you can have an “inner” loop that will print out each character on a row, one at a time.
There is a rule that will make this easier: The hit marker input string will:
Due to this, you can loop through the hit string in-order, and you don’t have to concern yourself with printing out multiple hits on the same line.
Before we even get to setting up our while loops to print out the rows and characters of this target, we should grab the X / Y position of the first hit and save them into variables. This would look something like:
current_hit_x = int(target_string[0] + target_string[1])
current_hit_y = int(target_string[2] + target_string[3])
We need two characters for each because we need to account for the +
or -
.
Next, you should write the loops for printing this out.
Since string multiplication is not allowed, you will have to write TWO while loops, one inside of the other.
The first (outer) loop will be responsible for iterating an index variable from +5 to -5.
For example, we could call this index_row
.
EACH time this loop iterates, we want another loop inside to iterate between -7 and +7, so that we can check each location at the current row.
We could call this index_column
.
Each time the inner loop iterates, we need to decide to print one of 4 possible characters: A space, a |
, an -
, or a hit marker.
We would print a hit marker whenever index_row == current_hit_y
AND index_column == current_hit_x
.
If that is not true, then we know we don’t have to print out a hit marker.
If index_col == 0
that indicates we are at the “middle” of our target horizontally, and thus we should print out a |
.
Finally, ff index_row == 0
that indicates we are at the “middle” of our target vertically, and thus we should print out a -
.
Lets walk through part of an example.
Say that the user ran the program and gave the target string +4+4+4+2+3+1+0-2-6-4
and the character #
.
The code would grab the initial hit x/y as +4
and +4
.
Then, the outer loop would begin with index_row
as +5
, and then the inner loop would iterate from -7
to +7
, printing out one character at a time.
Since there are not hits on row +5
, no #
s would print.
After the inner loop finishes iterating, the outer loop would repeat again, with index_row
now at +4
.
The inner loop would begin iterating, and would print out a normal target up until the index_column
value reaches +4
.
At this point, index_row == current_hit_y
AND index_column == current_hit_x
, thus a #
should be printed.
Right after it prints this hashtag, the program should go ahead and change current_hit_x
and current_hit_y
to the next values in the hit string.
Then, continue looping.
Basically, this idea repeats for the rest of the target.
Rows with no hits end up printing as normal, and rows with hits must print the marker at the correct spot, and then move the current_hit_x
and current_hit_y
variables to the next values (unless the end of the string has been reached).
Hit string:
+5+5+4+4+3+3+2+2+1+1+0+0-1-1-2-2-3-3-4-4-5-5
Marker:
#
| #
| #
| #
| #
|#
-------#-------
#|
# |
# |
# |
# |
Hit string:
+7+5+0+3-4+0+4-3
Marker:
+
| +
|
+
|
|
---+---|-------
|
|
| +
|
|
You should also include a comment at the top of the code file. The comment should include your name and a short description of what the program does. Below is a template you may use:
###
### Author: Your Name Here
### Course: CSc 110
### Description: Describe your program with one
### or more sentences of text.
###
You should also make sure to follow the style principles shown in the style guidelines.
This PA is due on Tuesday, June 24th at 11:59pm. Turn in the program via Gradescope. You should make sure that all of the test cases pass before you turn it in. You can still submit without all of the cases passing, but that is not preferable.