private void Walk(Direction direction, bool escapeDeadEnd) { if (escapeDeadEnd) { // if we are in a dead end then remove the path _maze.Grid[_currentPosX, _currentPosY].IsPath = false; } else { // if we have been in this cell then remove the path // if we have not been in this cell mark the path _maze.Grid[_currentPosX, _currentPosY].IsPath = !_maze.Grid[_currentPosX, _currentPosY].HasWalked; } _maze.Grid[_currentPosX, _currentPosY].HasWalked = true; int nextX; int nextY; MazeHelper.GetNextPosition(_currentPosX, _currentPosY, direction, out nextX, out nextY); if (!_maze.Grid[nextX, nextY].HasWalked) { // if we have been in the next cell before then we were on a detour and the next cell belongs to the path _maze.Grid[_currentPosX, _currentPosY].IsPath = true; } _currentPosX = nextX; _currentPosY = nextY; }
public void CarveFromPosition(int x, int y) { _maze.Grid[x, y].HasBeenVisited = true; MazeHelper.ResetRandomDirection(); Direction tryCarveDirection = Direction.up; int tryCellX = 0; int tryCellY = 0; bool foundNextCell = false; for (int i = 0; i < 4; i++) { tryCarveDirection = MazeHelper.GetRandomDirection(); MazeHelper.GetNextPosition(x, y, tryCarveDirection, out tryCellX, out tryCellY); if (!_maze.IsInMaze(tryCellX, tryCellY)) { continue; } if (_maze.Grid[tryCellX, tryCellY].HasBeenVisited) { continue; } foundNextCell = true; break; } if (foundNextCell) { // new cell found --> remove the walls in current and in next cell _maze.Grid[x, y].SetWall(tryCarveDirection, false); _maze.Grid[tryCellX, tryCellY].SetWall(MazeHelper.OppositeDirection(tryCarveDirection), false); CarveFromPosition(tryCellX, tryCellY); // create new path CarveFromPosition(x, y); // on the way back try to expand the path on this cell also } }