示例#1
0
        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;
        }
示例#2
0
        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
            }
        }
示例#3
0
        public void MarkPath(int startX, int startY, int targetX, int targetY)
        {
            _maze.Grid[targetX, targetY].IsPath = true;

            _currentDirection = Direction.up;
            _currentPosX      = startX;
            _currentPosY      = startY;
            while (!(_currentPosX == targetX && _currentPosY == targetY))
            {
                MazeHelper.ResetRightestDirection(_currentDirection);
                for (int countWays = 0; countWays < 4; countWays++)
                {
                    Direction tryDirection = MazeHelper.GetNextRightestDirection();
                    if (!_maze.Grid[_currentPosX, _currentPosY].HasWall(tryDirection))
                    {
                        bool escapeDeadEnd = countWays == 3;
                        _currentDirection = tryDirection;
                        Walk(_currentDirection, escapeDeadEnd);
                        break;
                    }
                }
            }
        }