Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a demo for snake game

Tags:

java

I've written a snake game in Java. What I also want to do is to create a demo for that (so snake would play by itself). I've written a simple demo, but snake dies pretty fast. So, is there any algorithms or something for that kind of problem? I believe it is a little bit similar to chess game problem? I want that snake would be alive as long as possible. Thank you.

like image 576
good_evening Avatar asked Feb 07 '12 06:02

good_evening


1 Answers

The Google-sponsored AI Challenge ran a "Tron" game in 2010. You might get some good ideas from searching for solutions to that challenge.

If you just want a very simple strategy that makes a reasonable demo then you might try something like the following:

  • Never make a move that causes you to crash unless you have no other option
  • If your next move forces you to choose between two or more distinct (unconnected) spaces, always move into the larger of the two spaces. This will stop your snake from getting trapped too easily.
  • If you are moving along a wall, keep moving along the wall 98% of the time, following it around to the left or right as needed. This will help your snake look reasonably intelligent, and also conserve space in the playfield.
  • Otherwise move ahead 90% of the time, turn left and right randomly 5% of the time each (this will make your demo less boring).

Apart from that, I don't think a Chess-style AI approach (with a move search tree) would work very well. You wouldn't be able to easily search enough moves in advance.

like image 94
mikera Avatar answered Sep 28 '22 04:09

mikera