9
\$\begingroup\$

I'm new to this gamedev stackechange but have used the math and cs sites before.

So, I'm in a competition to create AI for a snake that will compete with four other snakes in 5 minute rounds where the rules are much like the traditional Nokia snake game except that there are five snakes, the board is 30x30 and there are a number of small obstacles on the field.

Like the Nokia game, your snake grows when you get to the fruit and if you crash into yourself, another snake or the wall you die. The game runs with a 30ms delay between moves and the server sends the new game state every 50ms which the code must analyze and what not and output the next move.

The winner is the snake who had the longest length at any point in the game. Tie breakers are decided by kills.

So far what I have done is implemented an A* graph search from each snake to determine if my snake is the closest to the apple and if it is, it goes for the apple. Otherwise, I made a neat little algorithm to determine the emptiest area of the board, which my snake goes for, to anticipate the next apple.

Other than this I have some small survivability checks to ensure my snake isn't walking into a trap that it can't get out and if it does get stuck, I have something to give it a better chance of getting out.

...

Anyway, I've tested my snake on a test server and it does quite well. Generally, my strategy of only going for the apple when its a sure thing and finding space when its not makes it grow faster than any other snakes (some snakes do a similar thing but often just go to the middle or a corner) sometimes it wins these trial games but is more often than not beaten by the same snake who seems to have the edge on survivability (my snake grows quicker but then dies somehow and this other snake just plods slowly along and wins on consistency.

So I was wondering about any ideas anyone has to try and improve my snake. Or maybe ideas at a new approach to take. My functions and classes are good so changes that might seem drastic shouldn't be too bad. I encourage all ideas.

Any thoughts?

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

A good defensive strategy could be to do a perfect shape (so your head and backside are always 1 space away from each other, and you make a full square or whatever is possible to form at your given length), this would make you pretty much immortal since no snake could ever get into your enclosed area and you will never run into yourself (unless an apple somehow spawned in your mouth, but I'd imagine that's impossible if there's no space).

This would make it so if the apple actually appear in your enclosed area you could be programmed not to go for it and the other snakes will probably die trying to get it, then after some time elapsed you could get it in such a way to transition back into your shape as best as possible (could be a bit of a tricky algorithm but seems doable)

When do go for apples would just depend on how far away they are, maybe if you are the closest by a good margin you could go for it, but otherwise stay conservative, hard to say would probably need to tweak it based on trial and error, but I think it would be a good strategy.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ So basically, his "snake" should "turtle". xD +1 good idea. \$\endgroup\$
    – Cypher
    Sep 20, 2012 at 1:34
  • \$\begingroup\$ Good one. But there are obstacles on the board. So, other snake programmers must have made their snakes to check if there is an obstruction (whether wall or other snake enclosing the apple). They may not come and just die. One thing can be done to avoid this problem is to have some considerable spacing between head and tail. \$\endgroup\$
    – Shashwat
    Sep 20, 2012 at 5:17
  • \$\begingroup\$ hmm interesting idea, i think i could implement something like this for when my snake is just hanging around waiting for the next apple to spawn.. haha, I'm slightly worried about the apple spawning in my mouth tho, that would result in certain death and these games are oft decided by one untimely death \$\endgroup\$
    – Rawb
    Sep 20, 2012 at 11:37
  • \$\begingroup\$ @Ronald Actually if you detect in apple spawning in your mouth you could just turn inward and back outward and continue, since AI has perfect reflex it actually should be easy to remedy (and might even fix itself) based on how you implement it. \$\endgroup\$ Sep 20, 2012 at 13:16
0
\$\begingroup\$

The snake should adapt its strategy to its current length.

A very long snake can use its body to fence in opponents. This means that it should make a seal with a wall.

If the snake is traveling east, with an apple north-east of it, it should travel all the way to the wall before turning north, so the possible routes for other snakes is severely limited.

  0123456789
a ··········
b ··········
c ········A·
d ··········
e ooooooC···
f o·········
g o·········
h oooooooo··
i ··········
j ··········

This snake with head C, should turn north at e9, not at e8, so that any opponent below it, will be trapped, and unable to eat any apples that get spawn above the 'e' line.

The larger the area you can cut off and the longer you can hold it, the better.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .