One day when wandering around saw a pair of motion diagram:
Really is very greedy, all kinds of turning all kinds of eating, feel very interesting.
Use Perl to achieve automatic eating full, premeditated, before the character of the snake, depth-first algorithm, A * algorithm, are to pave the way for this article.
So, how to let the snakes do not eat themselves?
1, let the snake follow our designed route, in a n*m (N, M are even, odd not discussed) The game area, the design of a good route is as follows:
When the flowers thanks, ripe fruit, spring and autumn a reincarnation, the snake finally eat full ...
2, if the snake is always chasing their own tail run, then never die; however, there is no bird to use, we need a "greedy" snake.
3, in 2 slightly improved, after eating food and then chasing tail, well, already very close. But here's the problem: How to eat food? What happens when you eat food and die?
In fact, my idea is to solve the problem in 3:
1, using a * algorithm to calculate the shortest path to food
2. Simulate the scene after eating food, can return to snake tail, can-eat food, can't-continue chasing tail, repeat 1, 2
3, if unable to return to the tail, keep the current direction of movement (resigned, to hang the rhythm)
The pseudo code is as follows:
# While (live && not full) {# calculate the snake head to the path of the food # if (with the snake head to the food path) {# simulate the scene after eating the food # calculate if there is a path to the snake tail # if ( There is a path) { # execute the snake head to the path of the food # }# else{ # No path to the tail of the snake # next# }# }# Find the current workable direction chasing the tail of the snake # }# if (eat full) {# display: Congratulations # }# else{# show: WTF? # }
Simulate the scene after eating food: use a A * to calculate the path and create a "shadow snake" to simulate the condition after eating food
Calculate if there is a path to the tail of the snake: similar to the "Chase tail" method, you can also use a *
Find the current possible direction to chase the tail of the snake:
Because the tail changes while moving, a * is not attempted, and the algorithm is first searched using the specified direction depth.
The pseudo code is as follows:
Subtrace_tail{@moves=grep{feasible}; foreach(@moves){ Next if(Next step out of bounds | |to eat themselves)Unshift @feasible,$_ if(away from food) other directionsPush @feasible } foreach(@feasible) {Next_move=cur_move+$_Specify direction first explore the next_move of the reverse step set to not pass each step, starting from the tail of the snake has not been passed; return each of the original path, the current position of the snake body is not allowed to go throughif(with Path)return 1the execution is complete without a path,returnCur_move}
Run as follows:
In fact, it will be trapped, although chasing the tail to avoid food priority, but the only path may be food, not eat and eat.
The code is messy, ready to add a good comment after the paste up.
After reading this, Perl can't say that he's going to play with the snake.