I am working on a few different AIs for a tile-based game. I'm trying to figure out one where that tries to keep a certain distance from the player. So if distance becomes small, move away.

The whole game is in a pretty small are so it can't just forever move away. The movement is 4 directional.

How do I make the AI keep going at a distance, and once caught in a corner or close to a side of the room, to ignore the rule and just try to go around the player to the other side of the room?


1 Answer 1


There is an algorithm that adds this in automatically. Instead of merely moving away from the player, each AI will target the closest tile that is "out of range" of the player, I'll use 4 tiles. In the case that the AI is running from the player in an open area, it will choose to just run. In the case of a corridor, the AI "knows" it would be risky to slip by a player and instead chooses to keep progressing the other direction. Finally, in the case of a cornering we are left with the closest tile "out of range" to the player being behind the player.

the safe tiles (white) are behind the player

  • \$\begingroup\$ Good idea. However, this might cause the agent to move right through the threat it is supposed to avoid. The question said "ignore the rule", so this answer is correct, but I think it would be better when it would choose a path which maximizes the distance to the threat. This could be done by adding an additional weight to the cost of tiles close to the threat. \$\endgroup\$
    – Philipp
    Commented Jun 9, 2015 at 15:06
  • \$\begingroup\$ @Philipp you are correct. This algorithm, by itself, is definitely not the best algorithm to keep the AI from the player. A better algorithm would prevent the AI from entering dead ends in the first place, however, that would most likely bring the difficulty up a bit for the player. \$\endgroup\$
    – newton1212
    Commented Jun 9, 2015 at 15:15

You must log in to answer this question.

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