A snake js implementation of AI, the basic idea: use to feed a snake impersonation, if after eating food (shortest path breadth-first search) snake in a safe state, let the real snakes to eat if not safe, then go with the Snake's tail (longest path breadth-first search), no step needs to be carried out once the above judgment. Note: If you can find the snake path of the head to the tail of a serpent, the snake secure.
The above algorithm may still cause the snake to enter an infinite loop, there is no safe path can eat food, this happens at a time when most food supplies had now didn't find a solution, if interested or have a better solution, please contact me.