Hi,
That was nice one. Developing a fool-proof AI for the snake can be
difficult.
Ok, here is an idea. I hope this might help you:
Draw an imaginary straight line from snake's head to the food. ( I am
assuming the smallest drawing element is one 'block' e.g. food occupies
one block, width of snake is one block, length of the snake is 'k'
blocks long etc.) Now for every block on the imaginary line, move the
block vertically upwards or downwards to nearest empty block i.e. to
the block which contains no obstacle. You will have the obstacle-less
path to the food. If the length of line (in block measure) is greater
than 'k'(the length of snake) then the snake cuddles itself until its
length is less than the line length. You can also check if there is a
return path is available by drawing another imaginary line (after
drawing the first line) from food to the current position of snake's
head and assuming the first imaginary line as an obstacle. This will
give you alternate return path.
>> Stay informed about: Worm