Activities/Turtle in a Pond: Difference between revisions
Tonyforster (talk | contribs) |
|||
Line 23: | Line 23: | ||
=== Strategy === | === Strategy === | ||
In this strategy, the turtle moves down, regardless of whether the dot is open. | |||
<pre> | |||
def _turtle_strategy(self, turtle): | |||
turtle[1] +=1 | |||
return turtle | |||
</pre> | |||
In this strategy, the turtle moves down until it is blocked. | |||
<pre> | |||
def _turtle_strategy(self, turtle): | |||
if not self._dots[self._grid_to_dot((turtle[0], turtle[1]+1))].type: | |||
turtle[1] +=1 | |||
return turtle | |||
</pre> | |||
In this strategy, the turtle searches for an open dot, looking clockwise. | |||
<pre> | <pre> | ||
def _turtle_strategy(self, turtle): | def _turtle_strategy(self, turtle): | ||
c = turtle[1] % 2 | c = turtle[1] % 2 | ||
for i in range(6): | for i in range(6): | ||
col = turtle[0] + CIRCLE[c][ | col = turtle[0] + CIRCLE[c][i][0] | ||
row = turtle[1] + CIRCLE[c][ | row = turtle[1] + CIRCLE[c][i][1] | ||
if not self._dots[self._grid_to_dot((col, row))].type: | if not self._dots[self._grid_to_dot((col, row))].type: | ||
return [col, row] | return [col, row] | ||
Line 37: | Line 51: | ||
</pre> | </pre> | ||
In | In the '''default strategy''', the turtle choose a random direction and goes there if the dot is open. | ||
<pre> | <pre> | ||
def _turtle_strategy(self, turtle): | def _turtle_strategy(self, turtle): | ||
turtle[1] +=1 | c = turtle[1] % 2 | ||
n = int(uniform(0, 6)) | |||
for i in range(6): | |||
col = turtle[0] + CIRCLE[c][(i + n) % 6][0] | |||
row = turtle[1] + CIRCLE[c][(i + n) % 6][1] | |||
if not self._dots[self._grid_to_dot((col, row))].type: | |||
return [col, row] | |||
return turtle | return turtle | ||
</pre> | </pre> | ||
In this | In this strategy, the turtle will go off the edge if it can. | ||
<pre> | <pre> | ||
def _turtle_strategy(self, turtle): | def _turtle_strategy(self, turtle): |
Revision as of 12:07, 30 November 2011
Turtle in a Pond Activity
Turtle in a Pond is a strategy game. The goal is to surround the turtle before it runs of the screen.
How to play Turtle in a Pond
Did you know that:
- You can load your own strategy for the turtle
The Toolbars
- from left to right
- the Activity toolbar button
- the New Game button
- the Load new strategy button
- the Reload the default strategy button
- the Stop button
Strategy
In this strategy, the turtle moves down, regardless of whether the dot is open.
def _turtle_strategy(self, turtle): turtle[1] +=1 return turtle
In this strategy, the turtle moves down until it is blocked.
def _turtle_strategy(self, turtle): if not self._dots[self._grid_to_dot((turtle[0], turtle[1]+1))].type: turtle[1] +=1 return turtle
In this strategy, the turtle searches for an open dot, looking clockwise.
def _turtle_strategy(self, turtle): c = turtle[1] % 2 for i in range(6): col = turtle[0] + CIRCLE[c][i][0] row = turtle[1] + CIRCLE[c][i][1] if not self._dots[self._grid_to_dot((col, row))].type: return [col, row] return turtle
In the default strategy, the turtle choose a random direction and goes there if the dot is open.
def _turtle_strategy(self, turtle): c = turtle[1] % 2 n = int(uniform(0, 6)) for i in range(6): col = turtle[0] + CIRCLE[c][(i + n) % 6][0] row = turtle[1] + CIRCLE[c][(i + n) % 6][1] if not self._dots[self._grid_to_dot((col, row))].type: return [col, row] return turtle
In this strategy, the turtle will go off the edge if it can.
def _turtle_strategy(self, turtle): c = turtle[1] % 2 for i in range(6): col = turtle[0] + CIRCLE[c][i][0] row = turtle[1] + CIRCLE[c][i][1] if self._dots[self._grid_to_dot((col, row))].type is None: return [col, row] n = int(uniform(0, 6)) for i in range(6): col = turtle[0] + CIRCLE[c][(i + n) % 6][0] row = turtle[1] + CIRCLE[c][(i + n) % 6][1] if not self._dots[self._grid_to_dot((col, row))].type: return [col, row] return turtle
The dots are stored in a 13x13 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.
Your strategy should start with:
def _turtle_strategy(self, turtle):
The turtle argument is a tuple containing the column and row of the current turtle position. That is, turtle[0] is the horizontal position and turtle[1] is the vertical position.
Your strategy should return a tuple containing the column and row of the new turtle position, e.g.,
return [col, row]
There are some resources that you can use in your program, including:
- CIRCLE
- a 2x6x2 array of offsets that can used to find the col and row of the dots surrounding the turtle.
- self._dots
- the array of dots.
- self._pressed
- the index of the most recent dot pressed by the user
Where to get Turtle in a Pond
The Turtle in a Pond activity is available for download from the Sugar activity portal: Turtle in a Pond
The source code is available on the Sugar Labs Gitorious server.