DemoHut

A*

While hiking on a mountain last winter, I found this peculiar chessboard:

The pieces cannot be picked up, rather they can only be moved along horizontal or vertical lines. I wondered how dreadful it world be if someone is actually going to play chess on it.

And would it be easier on a computer? Sooner I realized this serves as a nice example to demonstrate how A*—one of the fundamental AI algorithms—works:


The objective is to find the shortest path of moving a piece to its destination. G is the actual cost so far, H is the estimated cost to the rest of the path, using a heuristic function, and F is the estimated total cost, which is the sum of the two: F = G + H. The open set consists of grids who are to be further searched and updated, and the close set includes those whose costs are determined and not to be changed. For every step during the search, the algorithm removes the grid with the smallest F value from the open set, add it to the close set, and add its adjacent grids to the open set or update their costs if possible. When the destination is added to the close set, the shortest path is found.

All demos have been tested on Microsoft Edge/macOS.