I am making a 2D tile-based maze game, and I'm trying to program an AI player that could find its path through the maze. Different from ordinary path finding, I'd like to limit the vision of each player (including the AI player) to 2x2 around them. That is, the AI should only know the surrounding 5x5 grid around it and the exact coordinates in the maze, like:
Tile mapRecord[MAP_SIZE][MAP_SIZE];
Direction FindPathAI(int row, int column, Tile surroundings[5][5]) {
int i, j;
int r = row - 3, c = column - 3;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
r = (r + MAP_SIZE + 1) % MAP_SIZE; //wrap around
c = (c + MAP_SIZE + 1) % MAP_SIZE; //wrap around
mapRecord[r][c] = surroundings[i][j];
}
}
//FindPath
//return the direction to go
}
What could be a possible way to solve this? I'm thinking that I could declare an array of the size of the whole map, and record the the vision of the AI player to the map. But then I get stuck on what should I do next...any idea? Thanks.
If the entrance and exit (start/finish) of the maze are both connected to the outside wall by other walls (i.e., one is not on an "island" in the middle), you have a simply-connected maze.
+----S----+
| |
| +-F-+ | <-- OK: "Finish" is connected
| +---+-| Simply-connected maze
| |
+---------+
+----S----+
| |
| +-F-+ | <-- Bad: "Finish" is not connected
| +---+ | Maze with Start or Finish on an island
| |
+---------+
If you don't have an "island" situation like the above, you can use the simple "wall follower" method, which you can do blindfolded (i.e., no vision at all!). Simply keep your left hand (or right hand) on a wall, and walk. If you reach an obstacle, turn in whichever direction allows you to keep your hand on the wall. You will eventually reach the exit, if there is one.
If you do have to deal with islands, you can use something like Trémaux's algorithm. If you think of each empty tile in your 2D tile-based maze, each tile will be either empty, marked once, or twice. (0, 1, or 2 marks.)
Obviously when a tile is visited, you increment the marks, so assume that, here:
When you reach your exit, tiles marked exactly once will take you right back to the start.
See the Wikipedia page on maze solving for more information.