How do I make an AI that stays at a distance from the player act correctly when cornered?

How do I make an AI that stays at a distance from the player act correctly when cornered?

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?

Solutions/Answers:

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

References