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)