Activities/Turtle in a Pond: Difference between revisions
| Line 104: | Line 104: | ||
</pre> | </pre> | ||
In | In this version, it looks for a path to the edge in the direction it was already heading. | ||
<pre> | <pre> | ||
| Line 123: | Line 123: | ||
self._orientation = (i + n) % 6 | self._orientation = (i + n) % 6 | ||
return self._dot_to_grid(dots[(i + n) % 6]) | return self._dot_to_grid(dots[(i + n) % 6]) | ||
return turtle | |||
</pre> | |||
In the '''default strategy''', a weighting function is used: preference is given to dots closer to the edges. | |||
<pre> | |||
def _turtle_strategy(self, turtle): | |||
dots = self._surrounding_dots(turtle) | |||
for i in range(6):\n\ | |||
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 | return turtle | ||
</pre> | </pre> | ||
| Line 142: | Line 166: | ||
;self._surrounding_dots((column, row)): returns an array of dots surrounding a given position in the grid | ;self._surrounding_dots((column, row)): returns an array of dots surrounding a given position in the grid | ||
;self._daylight_ahead((column, row)): returns True if there is a clear path to the edge heading in the current direction | ;self._daylight_ahead((column, row)): returns True if there is a clear path to the edge heading in the current direction | ||
:self._ordered_weights((column, row)): returns an array of surrounding dots ordered by their distance from the edge | |||
;self._dots: the array of dots from which you can test the type attribute (self._dots[i].type==None → edge; self._dots[i].type==False → open; self._dots[i].type==True → blocked) | ;self._dots: the array of dots from which you can test the type attribute (self._dots[i].type==None → edge; self._dots[i].type==False → open; self._dots[i].type==True → blocked) | ||
;self._orientation:you can set the orientation of your turtle by assigning a number from 0-5 (clockwise beginning with 30 degrees from north) | ;self._orientation:you can set the orientation of your turtle by assigning a number from 0-5 (clockwise beginning with 30 degrees from north) | ||