Exemplo n.º 1
0
        public void CarvePassages(int currentX, int currentY)
        {
            Direction[] directions = _randomizer.Shuffle(
                new[] { Direction.Up, Direction.Down, Direction.Left, Direction.Right });

            GridNavigator gridNavigator = GetGridNavigator(currentX, currentY);

            foreach (var direction in directions)
            {
                var nextCell = gridNavigator.GetNeighbour(2, direction);

                if (!nextCell.HasValue || isOnEdge(nextCell.Value))
                {
                    continue;
                }

                if (_cells[nextCell.Value.X, nextCell.Value.Y] != MazeCellType.Wall)
                {
                    continue;
                }

                // remove wall between cells
                var cellBetween = gridNavigator.Neighbours[direction];
                _cells[cellBetween.X, cellBetween.Y] = MazeCellType.Space;

                _cells[nextCell.Value.X, nextCell.Value.Y] = MazeCellType.Space;

                CarvePassages(nextCell.Value.X, nextCell.Value.Y);
            }

            if (!isFinishFound)
            {
                var wallOnEdge = gridNavigator.Neighbours.FirstOrDefault(c => isOnEdge(c.Value));
                if (!wallOnEdge.Equals(default(KeyValuePair <Direction, Cell>)))
                {
                    _cells[wallOnEdge.Value.X, wallOnEdge.Value.Y] = MazeCellType.Finish;
                }
                else
                {
                    _cells[currentX, currentY] = MazeCellType.Finish;
                }
                isFinishFound = true;
            }
        }