Optional 2: Dangerous Laser
We've been developing this ant for a long time in secret. It's so dangerous that we had to lock it in the super hidden CS61A underground vault, but we finally think it is ready to go out on the field. In this problem, you'll be implementing the final ant -- LaserAnt
, a ThrowerAnt
with a twist.
Note: There are no unlocking tests for this question.
Class | Food Cost | Health | |
---|---|---|---|
Laser | 10 | 1 |
The LaserAnt
shoots out a powerful laser, damaging all that dare to stand in its path. Both Bee
s and Ant
s, of all types, are at risk of being damaged by LaserAnt
. When a LaserAnt
takes its action, it will damage all Insect
s in its place (excluding itself, but including its container if it has one) and the Place
s in front of it, excluding the Hive
.
If that were it, LaserAnt
would be too powerful for us to contain. The LaserAnt
has a base damage of 2
. But, LaserAnt
's laser comes with some quirks. The laser is weakened by 0.25
each place it travels away from LaserAnt
's place. Additionally, LaserAnt
has limited battery. Each time LaserAnt
actually damages an Insect
its laser's total damage goes down by 0.0625 (1/16). If LaserAnt
's damage becomes negative due to these restrictions, it simply does 0 damage instead.
The exact order in which things are damaged within a turn is unspecified.
In order to complete the implementation of this ultimate ant, read through the LaserAnt class, set the class attributes appropriately, and implement the following two functions:
insects_in_front
is an instance method, called by theaction
method, that returns a dictionary where each key is anInsect
and each corresponding value is the distance (in places) that thatInsect
is away fromLaserAnt
. The dictionary should include allInsects
on the same place or in front of theLaserAnt
, excludingLaserAnt
itself.calculate_damage
is an instance method that takes indistance
, the distance that an insect is away from theLaserAnt
instance. It returns the damage that theLaserAnt
instance should afflict based on:- The
distance
away from theLaserAnt
instance that anInsect
is. - The number of
Insect
s that thisLaserAnt
has damaged, stored in theinsects_shot
instance attribute.
In addition to implementing the methods above, you may need to modify, add, or use class or instance attributes in the LaserAnt
class as needed.
Before
There are no locked test for this problem.
After
You can run the provided test, but it is not exhaustive:
python ok -q optional2
Make sure to test your code!