Let’s Save the Turtle

The goal is to help the turtle escape the bag you saw earlier here. The easiest way is to make him move in a straight line. He might then march through the sides of the bag. You can constrain him to only escape through the top, but let him go where he wants for now. When he’s out, you need to get him to stop. But how do you know when he’s out? The left edge of the bag is at -35, and the right is at +35. The bottom and top are also at -35 and +35, respectively. This makes checking his escape attempts easy:

 def​ ​escaped​(position):
  x = int(position[0])
  y = int(position[1])
 return​ x < -35 ​or​ x > 35 ​or​ y < -35 ​or​ y > 35

Now all you need to do is set him off and keep him going until he’s out: ...

Get Genetic Algorithms and Machine Learning for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.