Pure JavaScript A* maze solving – with a bit of magic thanks to Phaser and EasyStar.js

In my previous post I showed you how to create a pure JavaScript perfect tile maze generation, with a visual step by step process.

There isn’t any maze without an algorithm to solve it, and one of the most famous algorithms used in maze solving is the so-called A*.

From Wikipedia: In computer science, A* (pronounced as “A star”) is a computer algorithm that is widely used in pathfinding and graph traversal, the process of plotting an efficiently traversable path between multiple points, called nodes. Noted for its performance and accuracy, it enjoys widespread use.

Here is the maze of the original post, solved by A* algorithm, going from the top left cell to the bottom right cell:

Phaser was used to draw the maze and animate the red dot as it travels through the maze.

Since there’s no need to reinvent the wheel, I used the amazing EasyStar.js by Bryce Neal. There is also a Phaser plugin but I used the original library.

Here is the source code, with only new lines highlighted:

index.html:

game.js:

Now that we can generate a maze and solve it, next time I will show you a game built upon these features. Meanwhile, you can download the source code.