yifili09 / Snake-AI

A snake game AI written in c/c++.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snake-AI

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.

What can this program do?

  • Play classic snake game.

  • Watch how an AI snake eat food.

  • Used as a console/terminal graphics library.

Getting Started

Compile and run

$ make
$ make run

For usage, see function main()

Keyboard Control

Key Feature
W move up
A move left
S move down
D move right
Space pause/resume game
Esc terminate game

Demo

  • Snake AI

Graph algorithms (green area is scanned when searching and red area is the path)

  • Dijkstra

  • Dijkstra + A*

Other algorithms

  • Maze generate

How does the AI algorithm work?

Function Snake.decideNext() will compute the next move direction(D) of the snake.

Procedure:

  1. Compute the shortest path(P1) from the origin snake(S1)'s head to food.

  2. Move a virtual snake S2(the same as S1) to eat the food along P1.

  3. 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.

  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.

  5. Let D be the direction that makes the snake the farthest from the food.

For more in Chinese, please see 中文博客

Todos

  • Optimize AI algorithm

    AI algorithm is imperfect since the snake sometimes moves to an insoluable situation(just run the program and you will see).

License

See the LICENSE file for license rights and limitations.

About

A snake game AI written in c/c++.

License:MIT License


Languages

Language:C++ 99.1%Language:Makefile 0.9%