Changes

Jump to navigation Jump to search
1,000 bytes removed ,  16:23, 30 November 2011
Line 54: Line 54:  
<pre>
 
<pre>
 
def _turtle_strategy(self, turtle):
 
def _turtle_strategy(self, turtle):
     evenodd = turtle[1] % 2
+
     dots = self._surrounding_dots(turtle)
 
     for i in range(6):  # search for an opening
 
     for i in range(6):  # search for an opening
        column = turtle[0] + CIRCLE[evenodd][i][0]
+
         if not self._dots[dots[i]].type:
        row = turtle[1] + CIRCLE[evenodd][i][1]
+
             return self._dot_to_grid(dots[i])
         if not self._dots[self._grid_to_dot((column, row))].type:
  −
             return [column, row]
   
     return turtle
 
     return turtle
 
</pre>
 
</pre>
Line 66: Line 64:     
<pre>
 
<pre>
def _turtle_strategy(self, turtle):
+
    dots = self._surrounding_dots(turtle)
    evenodd = turtle[1] % 2
   
     for i in range(6):  # search for an opening
 
     for i in range(6):  # search for an opening
        column = turtle[0] + CIRCLE[evenodd][i][0]
+
         if not self._dots[dots[i]].type:
        row = turtle[1] + CIRCLE[evenodd][i][1]
  −
         if not self._dots[self._grid_to_dot((column, row))].type:
   
             self._orientation = i
 
             self._orientation = i
             return [column, row]
+
             return self._dot_to_grid(dots[i])
 
     return turtle
 
     return turtle
 
</pre>
 
</pre>
   −
In the '''default strategy''', 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.
    
<pre>
 
<pre>
 
def _turtle_strategy(self, turtle):
 
def _turtle_strategy(self, turtle):
     evenodd = turtle[1] % 2
+
     dots = self._surrounding_dots(turtle)
 
     n = int(uniform(0, 6))  # choose a random orientation
 
     n = int(uniform(0, 6))  # choose a random orientation
     for i in range(6):
+
     for i in range(6): # search for an opening
         column = turtle[0] + CIRCLE[evenodd][(i + n) % 6][0]
+
         if not self._dots[dots[(i + n) % 6]].type:
        row = turtle[1] + CIRCLE[evenodd][(i + n) % 6][1]
  −
        if not self._dots[self._grid_to_dot((column, row))].type:
   
             self._orientation = (i + n) % 6
 
             self._orientation = (i + n) % 6
             return [column, row]
+
             return self._dot_to_grid(dots[(i + n) % 6])
 
     return turtle
 
     return turtle
 
</pre>
 
</pre>
Line 96: Line 89:  
<pre>
 
<pre>
 
def _turtle_strategy(self, turtle):
 
def _turtle_strategy(self, turtle):
     evenodd = turtle[1] % 2
+
     dots = self._surrounding_dots(turtle)
     for i in range(6):  # look for an edge to escape to
+
     for i in range(6):  # search for an edge
        column = turtle[0] + CIRCLE[evenodd][i][0]
+
         if self._dots[dots[i]].type is None:
        row = turtle[1] + CIRCLE[evenodd][i][1]
  −
         if self._dots[self._grid_to_dot((column, row))].type is None:
   
             self._orientation = i
 
             self._orientation = i
             return [column, row]
+
             return self._dot_to_grid(dots[i])
    
     n = int(uniform(0, 6))  # choose a random orientation
 
     n = int(uniform(0, 6))  # choose a random orientation
     for i in range(6):
+
     for i in range(6): # search for an opening
         column = turtle[0] + CIRCLE[evenodd][(i + n) % 6][0]
+
         if not self._dots[dots[(i + n) % 6]].type:
        row = turtle[1] + CIRCLE[evenodd][(i + n) % 6][1]
  −
        if not self._dots[self._grid_to_dot((column, row))].type:
   
             self._orientation = (i + n) % 6
 
             self._orientation = (i + n) % 6
             return [column, row]
+
             return self._dot_to_grid(dots[(i + n) % 6])
 
     return turtle
 
     return turtle
 
</pre>
 
</pre>
   −
If it mostly tries to continue in the direction it was already heading, the turtle is harder to catch.
+
In the '''default strategy''', it looks for a path to the edge in the direction it was already heading.
    
<pre>
 
<pre>
 
def _turtle_strategy(self, turtle):
 
def _turtle_strategy(self, turtle):
     evenodd = turtle[1] % 2
+
     dots = self._surrounding_dots(turtle)
     if int(uniform(0, 2)) > 0:  # mostly try going straight
+
 
         column = turtle[0] + CIRCLE[evenodd][self._orientation][0]
+
     for i in range(6):  # search for an edge
        row = turtle[1] + CIRCLE[evenodd][self._orientation][1]
+
         if self._dots[dots[i]].type is None:
        if not self._dots[self._grid_to_dot((col, row))].type:
+
            self._orientation = i
            return [col, row]
+
            return self._dot_to_grid(dots[i])
 +
 
 +
    if self._daylight_ahead(turtle):
 +
        return self._dot_to_grid(dots[self._orientation])
 +
 
 
     n = int(uniform(0, 6))  # choose a random orientation
 
     n = int(uniform(0, 6))  # choose a random orientation
     for i in range(6):
+
     for i in range(6): # search for an opening
         column = turtle[0] + CIRCLE[evenodd][(i + n) % 6][0]
+
         if not self._dots[dots[(i + n) % 6]].type:
        row = turtle[1] + CIRCLE[evenodd][(i + n) % 6][1]
  −
        if not self._dots[self._grid_to_dot((column, row))].type:
   
             self._orientation = (i + n) % 6
 
             self._orientation = (i + n) % 6
             return [column, row]
+
             return self._dot_to_grid(dots[(i + n) % 6])
 
     return turtle
 
     return turtle
 
</pre>
 
</pre>
Line 148: Line 139:  
There are some resources that you can use in your program, including:
 
There are some resources that you can use in your program, including:
    +
;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._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)
Line 153: Line 146:  
;self._grid_to_dot((column, row)): returns the dot that is at a grid position (column, row)
 
;self._grid_to_dot((column, row)): returns the dot that is at a grid position (column, row)
 
;self._dot_to_grid(dot): returns an array (column, row) representing the grid position of a dot
 
;self._dot_to_grid(dot): returns an array (column, row) representing the grid position of a dot
;CIRCLE: a 2✕6✕2 array of offsets that can used to find the column and row of the dots surrounding the turtle.
  −
  −
A bit more explanation about the CIRCLE constant:
  −
  −
CIRCLE contains tuples of offsets (a column offset and a row offset) that allow you to find the grid coordinates (column and row) of the 6 dots that surround the turtle. It is complicated by the fact that the rows are staggered (in order to form hexagons) so when the turtle is on an even row (turtle[1] % 2 == 0) we use one set of offsets (CIRCLE[0]) and when it is on an odd row (turtle[1] % 2 == 1) we use a second set of offsets (CIRCLE[1]).
      
=== Where to get Turtle in a Pond ===
 
=== Where to get Turtle in a Pond ===

Navigation menu