Activities/Turtle in a Pond: Difference between revisions
| Line 73: | Line 73: | ||
</pre> | </pre> | ||
The turtle choose a random direction and goes there if the dot is open. | The turtle choose a random direction and goes there if the dot is open. (This is the beginner strategy.) | ||
<pre> | <pre> | ||
| Line 86: | Line 86: | ||
</pre> | </pre> | ||
In this strategy, the turtle will go off the edge if it can. | In this strategy, the turtle will go off the edge if it can. (This is the intermediate strategy.) | ||
<pre> | <pre> | ||
| Line 126: | Line 126: | ||
</pre> | </pre> | ||
A weighing function is used: preference is given to dots closer to the edges. | |||
<pre> | <pre> | ||
def _turtle_strategy(self, turtle): | def _turtle_strategy(self, turtle): | ||
| Line 137: | Line 137: | ||
return turtle | return turtle | ||
</pre> | </pre> | ||
This is the expert strategy. | |||
<pre> | |||
def _turtle_strategy(self, turtle): | |||
dots = self._surrounding_dots(turtle) | |||
for i in range(6): | |||
if self._dots[dots[i]].type is None: | |||
self._orientation = i | |||
return self._dot_to_grid(dots[i]) | |||
dots_ordered_by_weight = self._ordered_weights(turtle) | |||
for i in range(6): | |||
self._orientation = dots.index(dots_ordered_by_weight[i]) | |||
if self._daylight_ahead(turtle): | |||
return self._dot_to_grid(dots[self._orientation]) | |||
n = int(uniform(0, 6)) | |||
for i in range(6): | |||
if not self._dots[dots[(i + n) % 6]].type: | |||
self._orientation = (i + n) % 6 | |||
return self._dot_to_grid(dots[(i + n) % 6]) | |||
self._orientation = (i + n) % 6 | |||
return turtle | |||
</pre> | |||
The dots are stored in a 13✕13 array. Each dot has an attribute, 'type', that determines it status. The edges have a type=None. Occupied dots have a type=True. Unoccupied dots have a type=False. | The dots are stored in a 13✕13 array. Each dot has an attribute, 'type', that determines it status. The edges have a type=None. Occupied dots have a type=True. Unoccupied dots have a type=False. | ||