As a part of the primary school program design training, but also one of their own before thinking of a problem, and finally the use of primary school completed the snake AI an attempt, under a summary.
Background introduction:
First of all, I aimed at the snake ai this keyword in Baidu and Google on the search, roughly obtained a bit of information
1, A * * pathfinding algorithm is a classical algorithm in artificial intelligence, many AI use this algorithm to improve performance.
2, in the Alphago fame, artificial intelligence, after a household name, there is a greedy snake ai full-screen GIF image has been read in the microblog crazy turn.
3, this GIF figure already appeared in 2013 (actually than Alphago earlier).
4, domestic too greedy snake ai (that is, Baidu to get the information) of the article has a reference value of only two: how to write a snake ai with Python and the implementation of the snake Ai snake ai. But I feel that these two algorithms are still very rudimentary, and I don't think I can guarantee to eat full screen every time.
5, the full-screen GIF is a Russian realized (the last paragraph also appeared in Russian), Lenovo Tetris, probably Russian programmers like to write these things to entertain it.
6, careful observation of the GIF, you can conclude that it is definitely not a simple blind search algorithm, definitely using a more advanced AI algorithm (from the snake eat food path can be spied one or two).
7, there is no direct reference to the code, although there is a Python code, but has been basically do not understand the ....
My design:
First of all, my starting point is to try to use the knowledge of the data structure class to solve this problem, so I do not use a * (after running the efficiency is very substantial), but also useless other advanced AI algorithm. Second, I wanted to focus on algorithmic design as much as possible (and later to prove it was wise), so I gave up the idea of using Java's swing and java2d as a graphical interface, direct console programming, and writing code in C + + (which later proved foolish).
Here directly to write my ideas, if you have read the above two linked articles understand the following content is certainly easier. If you do not see, remember the two words, the snake chasing their own tail will certainly not die, because the tail is always set aside space. Snakes move only one step at a time, because the situation is changing.
First of all, the analysis of snakes in the state of the following three kinds:
1, can eat food, after eating food can also find tail, this time directly to eat food.
2, snakes can not eat food, or eat food after the tail (can not find the tail to eat is not allowed, this time to eat after eating no way to go), this time with the snake Tail walk, go to eat food so far (why can go to the following explanation).
3, in the State 2 premise, the tail can not find, at this time can only take a step (wander, in fact, wander strategy when this kind of game is very important idea, I also read some books only understand).
Above, but also I retrieved the core of the AI algorithm, but, in practice, I found a lot of details of these articles are not clear, practice is not allowed to have a little mistake, so, combined with their own practice, to do a more detailed analysis of these situations:
1, can eat food, and after eating can find the tail. This happens only when the snake is short, and when it is finished, it can find its tail, which means eating along the shortest path. Perhaps, the shortest way to eat food can not find the tail, and walk a longer path to find, but still think the latter is not a state one, otherwise, writing code will become very complex. But sometimes, there may be more than one direction to the shortest path to food, which can be randomly selected with a similar Monte Carlo approach, which can improve the likelihood of finding a tail after eating food, as evidenced by practice.
2, chasing the tail to go, but also pay attention to, first of all, to ensure that after this step can continue to find the tail, this is the basic premise, second, if the two direction can find the tail, should choose from the food farther direction (note is not close to food or close to the tail), should be the only way, To make sure there is enough room for the snake to eat food.
3, in my review of the article wander strategy is relatively simple random, but, I think with Dfs to find a relatively deep road, according to this road is better, but attention can not stick to the wall completely according to the deepest road, otherwise it will cause snakes can not turn back, like this.
In this way, the algorithm can be written as pseudo-code:
1 If you can eat food2 If virtual snakes follow the rules shortest way to find a tail after eating food3 Real Snake move one step4 re-judge5 else if virtual snakes go through the irregular shortest way to eat food can find the tail6 Real Snake move one step7 re-judge8 else if you know to reach your own tail and move one step has let you reach your tail9 choose to move from the farthest point of the foodTen re-judge One Else ADFS moves one step to the deepest path
On paper, the best way to understand this algorithm is to practice! Practice only to find that full screen is not so easy to imagine, from the point of view of the game, the snake's information is always incomplete, so, there must be a better algorithm to solve this problem, unless to a certain length start along a path can traverse all the paths of the loop repeatedly around, but this is too boring. Remember that there seems to be a game about AI snake, but did not retrieve too much information. Hopefully, we'll have a chance to work out better algorithms later.
Demo
Source Code Acquisition:
Point here
Elementary analysis on AI algorithm of primary snake