4
\$\begingroup\$

I'm making a game that is a bit of a mix of snake and pacman.

Basically, two players go through the maze and each time they pick up a piece of candy, a cube is added to their tail. If one player touches the other's tail, he steals that piece for himself and the game is over when one player has all the pieces.

Right now they both move randomly unless there's a piece of candy one space away that is not their tail.

I'm trying to find a method/algorithm (preferably a different method for each player) for both players to target/avoid the other player. Any advice?

snake

\$\endgroup\$
  • \$\begingroup\$ I have an idea: first have 3 possible modes for each snake. In mode 1) the snake will try to reach the food. In mode 2), the snake will try to reach the other snake's tail. In mode 3) move towards random point. You can use A* to find the path. \$\endgroup\$ – user55564 Dec 7 '14 at 6:23
1
\$\begingroup\$

For an algorithm that finds paths for snakes eating fruit, see this related question: How to find a safe path for an AI snake?

As for the adversarial part - snakes eating each other's tails - unfortunately you've created a game where either: both players can force a draw, or the game will never end. This is because:

  • The game only ends if one player loses their whole tail (i.e. left with just a head)
  • The snakes move one grid at a time at the same speed, and they can either:
    • Both move towards each other (reduce distance by 2)
    • One moves towards the other, the other moves away (distance unchanged)
    • Both move away from each other (increase distance by 2)

So the only way a game could end is if the snakes are an odd distance away, because that's the only way one snake could eat the other's tail completely, and this happens:

..ht    .ht.    hH..     Snake 1: ht (lose)
....    .H..    .T..     Snake 2: HT (win)
TH..    .T..    ....

But notice that snake 1 could always force a draw if it also moves towards the other snake, making their heads run into each other. Also note that if the snakes are an even distance away, the game could never end.

So you'll need to change your game design, like add other winning conditions, such as:

  • Player that eats the most things (candy or tail) wins
  • Normal snakes: players can't each other's tails, or only conditionally (after eating a powerup?)
  • Snakes can move at variable speeds
  • Eliminate the draw condition (e.g. only the larger snake wins if both heads collide)
\$\endgroup\$
-1
\$\begingroup\$

It depends on how complex you want your AI behaviors to be. If you want the most realistic, do some research on the A* algorithm. The other easier option would most likely be depth first search. Definitely look into AI pathfinding and you should find your solution.

\$\endgroup\$
  • \$\begingroup\$ Right now I'm trying Lee's algorithm \$\endgroup\$ – jking14 Dec 8 '14 at 21:15
  • 3
    \$\begingroup\$ @Kamakazi By providing a few vague hints about where to do research and what to look into you aren't really answering the question. \$\endgroup\$ – Philipp Sep 4 '15 at 16:29
  • \$\begingroup\$ A year ago, I stated that AI pathfinding was the answer. I'm not going to write a pathfinding algorithm on here when you can easily RTFW if you don't understand how to write one. Best of luck. \$\endgroup\$ – Kamakazi Sep 6 '15 at 1:55
  • 1
    \$\begingroup\$ @Kamakazi you should have submitted it as a comment than. \$\endgroup\$ – Savlon Jan 3 '16 at 9:14
  • 1
    \$\begingroup\$ Answer = Use A* or other pathfinding algorithm -> A* Explained \$\endgroup\$ – Kamakazi Jan 22 '16 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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