Beispiel #1
0
        public Maze GenerateMaze(int width, int height, Point startPosition)
        {
            _maze = new Maze(width, height);
            _maze.FillMaze();
            CurrentPosition = startPosition;
            _pointsToTry = new Stack<Point>();
            _pointsToTry.Push(startPosition);

            while (_pointsToTry.Count > 0)
            {
                _maze.ReleasePoint(CurrentPosition);
                List<Point> freePoints = GetFreeNeighbors(CurrentPosition);

                if (freePoints.Count > 0)
                {
                    _pointsToTry.Push(CurrentPosition);
                    CurrentPosition = freePoints[_random.Next(freePoints.Count)];
                }
                else
                {
                    UpdateFurthestPoint();
                    CurrentPosition = _pointsToTry.Pop();
                }
            }

            return _maze;
        }
Beispiel #2
0
        public void SaveMazeToFile(Maze mazeToSave, string filePath)
        {
            var stringToSave = new StringBuilder();

            for (int y = 0; y < mazeToSave.Height; y++)
            {
                for (int x = 0; x < mazeToSave.Width; x++)
                {
                    if (mazeToSave.IsOccupied(x, y))
                    {
                        stringToSave.Append("X");
                    }
                    else
                    {
                        stringToSave.Append(".");
                    }
                }

                stringToSave.AppendLine();
            }

            File.WriteAllText(filePath, stringToSave.ToString());
        }