React-sim
GitHub
Code for this example

Snake

This model tries to win the game of Snake.

A snake moves on a 2d grid. It can go straight, turn left or right. If it hits the borders of the grid or a part of its body, the game is over. If the snake eats a fruit, it grows in size. The goal of the game is to eat all the fruits, until the snake covers the entirety of the grid.

If the snake can stay on a circuit that covers the entirety of the grid, it can just loop that circuit endlessly, eat all the fruits and never collide with itself or the walls.

But it could be very slow to do that.

So instead, the snake tries to find better circuits.

Whenever it eats a fruit, and a new fruit appears, the snake finds the quickest route from its head to the new fruit. Then, it tries to find a route that goes from the fruit to its tail, and covers all the free tiles (in other words, the longest route from its fruit to the tail.)

If the snake can find such a route, then there is another safe circuit it can take - only this one will take it to the fruit sooner.

So, it updates its route.

Finding a quickest path between two points on a grid is easy, but how to find the longest path?

We start by finding the shortest path between these two points. Then, we take this path segment by segment and see each time if we can extend it. When we run out of segments where we can extend the path, we have a long path between the two points. This other model shows how this works:

This method is not guaranteed to find the longest path between 2 points - sometimes such a route will leave a couple of cells uncovered. This is why the snake waits to find a circuit that covers every cell in the grid before it updates its circuit.

Edit this page on GitHub
Previous:
Segregation
React-SimGitHub