How to make sure endless maze never traps player

I have a game where the player has a 9X9 grid floor around them. When they leave a tile the next row of tiles spawn in their direction of movement, and it deletes the row in the opposite direction so they tile they occupy is always the center tile of the grid. Walls randomly spawn on the 4 sides of each tile. This creates the maze like shown in the example (can’t walk through red walls)
Example

As you can see there are 4 ways to reach the edge of the grid. (now to create worst case scenario)

Now if lets say the player moved right 4 spaces. Now all 4 paths have been deleted and there is no way to access any further right squares. So the player back tracks but now those paths spawn back as dead ends. The player is now trapped. (assume that no matter how many times they walk back and forth the RNG spawns dead ends)

So mathematically or using some programming wizard trick, or logical people help me figure out a way to ensure that the program checks for a way for the player to reach the edge of the grid and to not spawn something that will block off the last exit?

If you are very interested in solving this. I can fill you in more on how the game works, and show actual program.

Can’t you rig something that caps the maximum number of walls that can surround an tile to three?

that is not a terrible idea, I will have to see if I can figure out how to do that. the way may spawning system works does not spawn in walls related to the tiles themselves, so I will need to do some math. Although you still would be able to trap someone even if a tile could only have 1 wall ex: every tile on the outside of grid has one wall spawned on the outside edge of it

You could set up some constant elements in it, like making an custom tile or two where the walls won’t spawn around it. Or just manually creating an perimeter fence with a few gaps in it if the objective is to escape an ever-changing maze.

Otherwise, you’ll be softlocked into an corner that you can’t escape from or spending longer than what it should be trying to get an exit to spawn.

Even with such a cap, you could run into a situation, where you can get trapped. A 3x3 square surrouned by a wall would still obey that 3 wall rule, since every outer grid piece would have max 2 walls, but all the 8 outside pieces would form a completely closed room.

My suggestion would be to create small patches, which are then stiched together to form your visible maze, like a 3x3 (or 5x5 patches), and the maze is then formed of f.e. 3x3 of those small patches, creating a 9x9 or 15x15 maze.

One rule could be, that every patch has to have at least two open connections, of which

  • one has to connect to another patch
  • and another one connection has to lead away from the paches into yet unused space (where a new patch would spawn, if the player comes close enough)

For the wall creation in that small patches, maybe take a look at this here, the recursive method seems pretty solid:

https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_division_method

Awesome idea. Now I need to figure out how to code that. One idea I had based on some research is that I wonder if I can do a Nav mesh around the player that will extends out of the 9X9 grid and have it check after every wall spawn if it is possible to reach the outside. I saw in a video where the person was making a chasing AI, they used a Nav mesh, and the mesh broke going up stairs, and so the AI couldn’t go. Then I believe I have heard of functions that will check if an AI can reach a point on a nav mesh

if you like that idea, then maybe this helps you, since that seems to be an even better version, AND whoever wrote that article, also already coded it for the user to test out in this blog ^.^

https://weblog.jamisbuck.org/2015/1/15/better-recursive-division-algorithm.html