Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've looked at some stuff online regarding this specific topic, and a lot of the info that I read involved graphs and path finding.

I really don't want to get involved in something too complex & out of my level, and also I don't need my snake to be that intelligent (it will be a large board with the snake not growing in size on every munch).

How could you structure a simpler AI for the snake that gets the job done relatively well? I would be able to get the snake to move towards the food item correctly, but my issue is that I'm not sure how to deal with the snake colliding with itself. Say the snake has a look ahead, and it finds that its tail is in the way, it could change direction, but what happens next?

Any ideas on how to tackle this? Should the snake build an instruction set from every square, or should it think on the go?

share|improve this question
    
One way to approach it is to tell the snake to head towards the area with the largest amount of space as seen here –  AbstractChaos Aug 8 '12 at 16:24
    
    
Hopefully this will be of use to you. It seems to address exactly this question: An article on gamedev.net about using genetic programming for snake games –  Bob Flanderson Aug 8 '12 at 18:20

1 Answer 1

A simple set of rules should let your snake navigate the majority of situations. If you need code, let me know and provide some of your methods/variables so I can fill in the blanks.


New piece of food shows up. Snake figures if it needs to go + or - X, and the same for Y. On the first turn snake starts heading to the food's X (by moving along the Y axis).

If the snake encounters itself while moving Y, it makes a turn in the correct X direction (it remembered whether it needs to go +X or -X), and heads straight until it reaches a square and doesn't detect its tail to the side anymore. If it detects more body ahead, it turns to the free direction, continuing until it detects an opening to the side and can return back to its course. (It "wall hugs" its tail until the tail runs out and it can get back on course.)

Once the snake reaches the correct X coordinate, it begins moving along the X to get to the correct Y coordinate.

If the snake encounters itself while moving X, it makes a turn toward the Y direction it first remembered for this piece, and checks for open space similarly to the Y movement.


Now the snake will only suicide if it runs into its tail and turns in the same direction the tail is "moving" (causing the snake to spiral until it runs out of space). This however will never happen if the snake makes sure to turn toward the direction it originally needed to, to get the current piece of food. Why? Because of where the previous piece of food was - the snake will only run into its tail if it has to double-back along the X or the Y to get the piece, so you can usually be assured the snake was previously moving in the opposite X or Y to get to the piece you just picked up.

I believe this will break down if the snake has to double back for multiple pieces in a row, and the distance between all the multiple pieces happens to be shorter than the total length of the snake. But hey, you said "simple" and "relatively" well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.