Problem 3 (200pts): Thrower Throws
In order for a ThrowerAnt
to attack, it must know which bee it should hit.
The provided implementation of the nearest_bee
method in the ThrowerAnt
class only allows them to hit bees in the same Place
.
Your job is to fix it so that a ThrowerAnt
will throw_at
the nearest bee in front of it that is not still in the Hive
.
This includes bees that are in the same Place
as a ThrowerAnt
.
Hint: All
Place
s have anis_hive
attribute which isTrue
when that place is theHive
.
Change nearest_bee
so that it returns a random Bee
from the nearest place that contains bees.
Your implementation should follow this logic:
- Start from the current
Place
of theThrowerAnt
. - For each place, return a random bee if there is any, or consider the next place that is stored as the current place's
entrance
. - If there is no bee to attack, return
None
.
Hint 1: The
random_bee
function provided inants.py
returns a random bee from a list of bees orNone
if the list is empty.Hint 2: As a reminder, if there are no bees present at a
Place
, then thebees
attribute of thatPlace
instance will be an empty list.Hint 3: Having trouble visualizing the test cases? Try drawing them out on paper! The example diagram provided in Game Layout shows the first test case for this problem.
Before writing any code, unlock the tests to verify your understanding of the question:
$ python ok -q 03 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 03
After implementing nearest_bee
, a ThrowerAnt
should be able to throw_at
a Bee
in front of it that is not still in the Hive
.
Make sure that your ants do the right thing!
To start a game with ten food (for easy testing):
$ python gui.py --food 10
Congratulations! Now you have finished Phase 1 of this project!