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

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay