Problem 5 (250pts): Fire Ant
Implement the FireAnt
, which does damage when it receives damage.
Specifically, if it is damaged by health
health units,
it does a damage of health
to all bees in its place (this is called reflected damage).
If it dies, it does an additional amount of damage, which is specified by its damage
attribute (by default 3).
To implement this, we have to override the FireAnt
's reduce_health
method.
Normally, Insect.reduce_health
will decrement the insect's health
by the given amount
and remove the insect from its place if health
reaches zero or lower.
However, FireAnt
also does damage to all the bees in its place when it receives damage,
with a special bonus when its health drops to 0, before being removed from its place
.
Class | Food Cost | Health | |
---|---|---|---|
FireAnt | 5 | 3 |
Hint1: To damage a
Bee
, call thereduce_health
method inherited fromInsect
.Hint2: Damaging a bee may cause it to be removed from its place. If you iterate over a list, but change the contents of that list at the same time, you may not visit all the elements. This can be prevented by making a copy of the list. You can either use a list slice, or use the built-in
list
function.>>> lst = [1,2,3,4] >>> lst[:] [1, 2, 3, 4] >>> list(lst) [1, 2, 3, 4] >>> lst[:] is not lst and list(lst) is not lst True
Once you've finished implementing the FireAnt
, give it a class attribute implemented
with the value True
.
Note: even though you are overriding the
Insect.reduce_health
function, you can still use it in your implementation by calling it directly (rather than viaself
). Note that this is not recursion (why?)
Before
Before writing any code, read the instructions and test your understanding of the problem:
python ok -q 05 -u
After
After writing code, test your implementation:
python ok -q 05
You can also test your program by playing a game or two!
A FireAnt
should destroy all co-located Bee
s when it is stung.
To start a game with ten food (for easy testing):
python gui.py --food 10