DEV Community

Cover image for AI snake game with a* algorithm 🐍

Posted on • Updated on

AI snake game with a* algorithm 🐍


In this post, i want to share my old project (again) haha. It's about implementing a* (a star) algorithm on simple snake game web based using JS.

For you guys who don't know about what exactly a* algorithm used for, so here it is.

A Star is shortest route finder algorithm. Yup and of course, used for finding path from one point to other point.

🙍‍♂️: "Hey, we already have a masterpice pytaghoras to deal with that, isn't it?"

Ha, here's the thing. A* is more complex than that. It will gives you the shortest and the fast way considering all blocker and obstacle.

We can see a* is used by maps app to find route from your current place to your destination. We have the road as rule. We don't wanna break through the road, or climb neighbor house, or just walk straight no matter what. That's what we called obstacle.

Let's take a look on this video. This is how a* works.

In snake game that i made, the obstacle will be the edge area, and snake body itself. And the destination is, FOOD 🍎.

I wanna know, how accurate a* is. So i made comparison of me fight with my bot, playing snake. You know the result, i never can beat it 😣. Damn you my creation.

Source Code 🎉

GitHub logo mtegarsantosa / snake-astar

🐍 Snake with A* Algorithm

Top comments (0)