In Exercise 11, we will be constructing more classes to model some mini battles between animals.
0. Pull the skeleton code
You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:
- Be sure you are in your course workspace. Open the file explorer and you should see your work for the course. If you do not, open your course workspace through File > Open Recent.
- Open the Source Control View by clicking the 3-node (circles) graph (connected by lines) icon in your sidebar or opening the command palatte and searching for Source Control.
- Click the Ellipses in the Source Control pane and select “Pull” from the drop-down menu. This will begin the pulling process from the course repository. It should silently succeed.
- Return to the File Explorer pane and open the
exercises
directory. You should see it now contains the directory namedex11
. If you expand those directories, you will see the starter files for this exercise. You should notice theex11
directory appear.
If the above did not work, try the following:
- Click the Ellipses in the Source Control pane and select “Pull, Push” from the drop-down menu. Then select “Pull from”. Then select “upstream” and the
main
option. This will begin the pulling process from the course repository. It should silently succeed. - Return to the File Explorer pane and open the
exercises
directory. You should see it now contains another directory namedex11
. If you expand that directory, you should see the starter files
Part 1. Animal Class
Add the following import statements directly below your module level docstring. You’ll get a syntax error if you put them below your __author__
variable.
from __future__ import annotations
Write a class called Animal
with the following specifications:
- Each
Animal
object should have 3 attributes including:- a
str
attribute calledspecies
- an
int
attribute calleddanger_level
- a
str
attribute calledemoji
- a
The
Animal
class should have a constructor that takes inspecies
,danger_level
, andemoji
, in this order. This means it will have ** 4 parameters includingself
**.Your class should also have 1 methods:
fight
. The functionality of this methods should be as described below.3.1
fight
- This method takes in an
Animal
parameter namedopponent
(andself
) and returnsAnimal
. fight
should return the animal with the higherdanger_level
.- If they have the same
danger_level
then the opponent should win (be returned)
- This method takes in an
Part 2. Team Class
Write a class called Team
with the following specifications:
- Each
Team
object should have 3 attributes including:- a
str
attribute calledteam_name
- an
list[Animal]
attribute calledanimals
- an
int
attribute calledscore
- a
Your
Team
class should have a constructor that takes in two parameters in addition to self:team_name
andanimals
, in this order. The constructor should also initializescore
to 0.Your class should also have 2 methods called
battle
andwho_won
. The functionality of these methods should be as described below:3.1
battle
This method takes in a
Team
parameter namedopponent
(andself
) and returnslist[Animal]
.battle
should:- Make sure the
animals
list inself
andopponent
are of equal size and return an empty list if not. - Iterate through the animals lists in
self
andopponent
and make use of the fight method inAnimal
to match up the animals at equivalent indices. For instanceself.animals[0]
should fightopponent.animals[0]
.
- Increase the s
core
attribute of the correct team when one of their fighters wins. - Return a list that contains the winners of each fight. For instance the winner of
self.animals[0]
vsopponent.animals[0]
should be at index 0 of the return list.
3.2
who_won
This method takes in a
Team
parameter namedopponent
(andself
) and returnsstr
.who_won
should:- Return “The battle hasn’t happened yet” if both scores are 0
- Return “It was a tie!” if both scores are equal but not 0
- Return “Team <team_object.team_name> won!” using which ever team has the higher score.
- Make sure the
Part 3. Test it out!
We have provided some starter code commented out in main
for you all to play around with your classes and try some battles. Uncomment these lines and run your program to test it out. Feel free to play around!
4. Make a Backup Checkpoint “Commit”
As you make progress on this exercise, making backups is encouraged. Note that you do not have to make a backup in order to submit your work, though you are encouraged to before each submission so that you can revert back to a previous point in your project if you accidentally change something you did not intend to.
- Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
- Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
- Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
- If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
- In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Progress on Exercise 3” will suffice.
- Press the Check icon to make a Commit (a version) of your work.
- Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.
4. Submit to Gradescope for Grading
Login to Gradescope and select the assignment named “EX11 - More Classes”. You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.
If you do not see a Terminal at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.
To produce a zip file for ex11
, type the following command (all on a single line):
python -m tools.submission exercises/ex11
In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex11.zip”. The “mm”, “dd”, and so on, are timestamps with the current month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.