I'm building a snake game that plays on the surface of a cube. Currently it uses Dijkstra's algorithm for pathfinding. Despite optimizations with set and priority queue data structures, it is still a bit too slow. You notice the delay when the snake eats a food item and begins searching for a new one.
I'm trying to get it to use A* instead but I can't find a good heuristic. On a flat grid with 4 directions of movement, I would use Manhattan distance. I've tried using 3D Manhattan distance abs(dx) + abs(dy) + abs(dz)
which didn't work for good reason: to the snake, the game world is really 6 grids (corresponding to the faces of the cube) with unusual wrap-around properties.
In the code, each square is stored in a grid[15][15]
2D array. There are 6 such arrays to store each face. So each square has an (arrayX, arrayY, d)
triple to describe the offset in the 2D array and specify which array. Also, each square has an (x, y, z)
triple describing spatial position.
Here's the area of game code where the pathfinding happens:
https://github.com/mhluska/Snakeception/blob/master/src/js/game.coffee#L105
Here's the library code for A*:
https://github.com/mhluska/Stimpack/blob/master/src/js/graph.coffee#L60
What is a suitable, concise heuristic for this game world?