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
LongThrowercan onlythrow_ataBeethat is found after following at least 5entrancetransitions. It cannot hitBees that are in the samePlaceas it or the first 4Places in front of it. If there are twoBees, one too close to theLongThrowerand the other within its range, theLongThrowershould throw past the closerBeeand target the farther one, which is within its range. - The
ShortThrowercan onlythrow_ataBeethat is found after following at most 3entrancetransitions. It cannot throw at any bees further than 3Places in front of it.
Neither of these specialized throwers can throw_at a Bee that is exactly 4 Places 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_rangeandmin_rangerather thanmaximum_rangeandminimum_rangeor 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

