Problem 4 (250pts): Short and Long Thrower
The ThrowerAnt
is a great offensive unit, but it'd be nice to have a cheaper unit that can throw.
Implement two subclasses of ThrowerAnt
that are less costly but have constraints on the distance they can throw:
- The
LongThrower
can onlythrow_at
aBee
that is found after following at least 5entrance
transitions. It cannot hitBee
s that are in the samePlace
as it or the first 4Place
s in front of it. If there are twoBee
s, one too close to theLongThrower
and the other within its range, theLongThrower
should throw past the closerBee
and target the farther one, which is within its range. - The
ShortThrower
can onlythrow_at
aBee
that is found after following at most 3entrance
transitions. It cannot throw at any bees further than 3Place
s in front of it.
Neither of these specialized throwers can throw_at
a Bee
that is exactly 4 Place
s away.
Class | Food Cost | Health | |
---|---|---|---|
ShortThrower | 2 | 1 | |
LongThrower | 2 | 1 |
A good way to approach the implementation to ShortThrower
and LongThrower
is to have it inherit the nearest_bee
method from the base ThrowerAnt
class. The logic of choosing which bee a thrower ant will attack is essentially the same, except the ShortThrower
and LongThrower
ants have maximum and minimum ranges, respectively.
To implement these behaviors, you will need to modify the nearest_bee
method to reference min_range
and max_range
attributes, and only return a bee that is in range.
Make sure to give these min_range
and max_range
sensible defaults in ThrowerAnt
that do not change its behavior. Then, implement the subclasses LongThrower
and ShortThrower
with appropriately constrained ranges and correct food costs.
Hint:
float('inf')
returns an infinite positive value represented as a float that can be compared with other numbers.
Don't forget to set the implemented
class attribute of LongThrower
and ShortThrower
to True
.
Note: Please make sure your attributes are called
max_range
andmin_range
rather thanmaximum_range
andminimum_range
or something. The tests directly reference the variable via this name.
Before
Before writing any code, read the instructions and test your understanding of the problem:
python ok -q 04 -u
After
After writing code, test your implementation (rerun the tests for 03 to make sure they still work):
python ok -q 03
python ok -q 04