Problem 2 (200pts): Build Tunnel
Complete the Place
constructor by adding code that tracks entrances.
Right now, a Place
keeps track only of its exit
.
We would like a Place
to keep track of its entrance as well.
A Place
needs to track only one entrance
.
Tracking entrances will be useful when an Ant
needs to see what Bee
s are in front of it in the tunnel.
However, simply passing an entrance to a Place
constructor will be problematic;
we would need to have both the exit and the entrance before creating a Place
! (It's a chicken or the egg problem.)
To get around this problem, we will keep track of entrances in the following way instead.
The Place
constructor should specify that:
- A newly created
Place
always starts with itsentrance
asNone
. - If the
Place
has anexit
, then theexit
'sentrance
is set to thatPlace
.
Hint1: Remember that when the
__init__
method is called, the first parameter,self
, is bound to the newly created object.Hint2: Try drawing out two
Place
s next to each other if things get confusing. In the GUI, a place'sentrance
is to its right while theexit
is to its left.Hint3: Remember that Places are not stored in a list, so you can't index into anything to access them. This means that you can't do something like
colony[index + 1]
to access an adjacent Place. How can you move from one place to another?
Before
Before writing any code, read the instructions and test your understanding of the problem:
python ok -q 02 -u
After
After writing code, test your implementation:
python ok -q 02