Inspiration
I was inspired by Code Bullet the youtuber.
What it does
The snake can find the apple itself. It doesn't rely on a user to direct it with arrow keys.
How we built it
I built a snake class with an array strong the head and all body parts of the snake. When snake collides with the apple, it appends to that array one more square body (which is the tail).
The AI was built with the A star search algorithm.
Challenges we ran into
- The snake head wouldn't be able to find the apple when its trapped with the snake's body.
- The snake AI would not take the optimal path because the body was blocking it at that certain point in time.
- The snake body would all follow the direction of the head, but a real snake doesn't move like that. The snake's body has a trailing effect which I was having trouble replicating.
Accomplishments that we're proud of
- I was able to make the snake AI see through its body to always find the optimal path. The way this works is I know the path cost of the snake at any point in time and the length of the snake body respective to the tail (so the length of tail is 0). So, when the AI is checking for neighbours, if a neighbour is a snake body, but the path cost is greater than the length of snake body from the tail, then the snake would consider that path.
- I was able to replicate real snake movement by changing direction of snake body starting from the tail and not the head.
What we learned
I learned graph data structures and algorithms as well as implementation of a linked list for the snake body (even though I used an array but python basically treats the array as linked list because you can dynamically add and delete elements from anywhere).
What's next for Snake AI
- I still wasn't able to figure out how to make the snake find the apple even when its body traps the apple. I can do it in two ways:
- I find a way to make snake avoid positions where the snake body doesn't trap the apple
- I find a way to make the snake head find a different path such that the body doesn't trap the apple anymore.
Log in or sign up for Devpost to join the conversation.