DEV Community

Cover image for AI snake game with a* algorithm 🐍
arsannnnn
arsannnnn

Posted on • Edited on

5

AI snake game with a* algorithm 🐍

Hallo!

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

Sentry blog image

Identify what makes your TTFB high so you can fix it

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay