https://github.com/mbuffa/elixir-pathfinding-demo
An Elixir/LiveView demo of the A* pathfinding algorithm
https://github.com/mbuffa/elixir-pathfinding-demo
astar astar-search-algorithm demo elixir liveview pathfinding
Last synced: 7 months ago
JSON representation
An Elixir/LiveView demo of the A* pathfinding algorithm
- Host: GitHub
- URL: https://github.com/mbuffa/elixir-pathfinding-demo
- Owner: mbuffa
- License: mit
- Created: 2021-02-07T08:38:43.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-04T20:08:43.000Z (over 1 year ago)
- Last Synced: 2025-05-07T06:57:05.556Z (7 months ago)
- Topics: astar, astar-search-algorithm, demo, elixir, liveview, pathfinding
- Language: Elixir
- Homepage: https://path-demo.fly.dev/
- Size: 291 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PathDemo
This is a live demonstration of the A* pathfinding algorithm implemented in Elixir using LiveView. The algorithm itself is a functional step-by-step version of the famous A* algorithm, which is most commonly used in video games.
Head over to https://path-demo.fly.dev and enjoy!
## Context
I hacked together the original version of this demo literally over a weekend, ingesting a lot of content about pathing, and a bit about LiveView, in order to do an internal talk at Pandascore.
I got back to it and refactored the whole thing later to clean it up, so overall, I think this is a great demonstration of some of Elixir capabilities, and how to implement such an algorithm in a functional way, leveraging pattern matching quite extensively.
Please keep in mind that this is, however, not a perfect example of algorithmic efficiency (there's a lot of linear search through lists and it could probably be better), and that there might be mistakes or imprecisions about my implementation.
## Setup
You'll need a working version of Erlang and Elixir.
Then:
```sh
# Install dependencies
mix deps.get
# Start server
mix phx.server
```
You can then navigate to `http://localhost:4000` and start playing!
## How it works
You're welcomed by a tile map, a pair of buttons and a description box.
The demo is split into three phases:
* Waiting for a target: that's where you start. Pick a target to progress,
* Estimating the path: click on the "Find the path" button until you see a trail of foot emojis leading to your destination,
* Walking the path: click on the "Walk the path" button until you reach your destination.
Others have described the algorithm extensively, and will do it better than me. But basically, the idea of the algorithm is to expand around in every directions, calculating both the cost to enter the node (usually named `G cost`) and the distance from this node to the destination (usually named H cost`), resulting in the `F cost` of a node. On each step, we just have to pick the most interesting one (which can lead to some weird behavior when computing in non-obvious scenarios). Once we reached our destination, we can just compute a final path by going backwards and applying the same cost principle, resulting in a "smooth" final path.