A snake game AI written in c/c++.
The goal is to eat all the food and make the map fill with the snake's bodies.
-
Play classic snake game.
-
Watch how an AI snake eat food.
-
Used as a console/terminal graphics library.
Compile and run
$ make
$ make run
For usage, see function main()
Key | Feature |
---|---|
W | move up |
A | move left |
S | move down |
D | move right |
Space | pause/resume game |
Esc | terminate game |
Graph algorithms (green area is scanned when searching and red area is the path)
Other algorithms
Function Snake.decideNext() will compute the next move direction(D) of the snake.
Procedure:
-
Compute the shortest path(P1) from the origin snake(S1)'s head to food.
-
Move a virtual snake S2(the same as S1) to eat the food along P1.
-
Compute the longest path(P2) from the S2's head to its tail. If P2 exists, let D be the first direction in P1. Otherwise go to step 4.
-
Compute the longest path(P3) from the S1's head to its tail. If P3 exists, let D be the first direction in P3. Otherwise go to step 5.
-
Let D be the direction that makes the snake the farthest from the food.
For more in Chinese, please see 中文博客
-
Optimize AI algorithm
AI algorithm is imperfect since the snake sometimes moves to an insoluable situation(just run the program and you will see).
See the LICENSE file for license rights and limitations.