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\$
1
  • \$\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
    Commented Dec 7, 2014 at 6:23

2 Answers 2

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\$
5
  • \$\begingroup\$ Right now I'm trying Lee's algorithm \$\endgroup\$
    – jking14
    Commented Dec 8, 2014 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
    Commented Sep 4, 2015 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
    Commented Sep 6, 2015 at 1:55
  • 1
    \$\begingroup\$ @Kamakazi you should have submitted it as a comment than. \$\endgroup\$
    – Savlon
    Commented Jan 3, 2016 at 9:14
  • 1
    \$\begingroup\$ Answer = Use A* or other pathfinding algorithm -> A* Explained \$\endgroup\$
    – Kamakazi
    Commented Jan 22, 2016 at 22:11

You must log in to answer this question.

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