Beispiel #1
0
        private bool IsValidWay(MazeCoordinate point, MazeCoordinate comeFrom)
        {
            if (!this.CurrentMaze.IsPointInMaze(point))
            {
                return(false);
            }

            // Already something other than a wall?
            if (this.CurrentMaze.GetMazeTypeOnPos(point) != MazeFieldType.Wall)
            {
                return(false);
            }

            // Make sure we do not create a way through a wall.
            MazeCoordinate up    = new MazeCoordinate(point.X, point.Y - 1);
            MazeCoordinate right = new MazeCoordinate(point.X + 1, point.Y);
            MazeCoordinate down  = new MazeCoordinate(point.X, point.Y + 1);
            MazeCoordinate left  = new MazeCoordinate(point.X - 1, point.Y);

            MazeCoordinate[] pointsToCheck = { up, right, down, left };

            for (int i = 0; i < pointsToCheck.Length; i++)
            {
                if (!pointsToCheck[i].Equals(comeFrom))
                {
                    if (CurrentMaze.GetMazeTypeOnPos(pointsToCheck[i]) == MazeFieldType.Corridor)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #2
0
        internal override MazeTransformationStep TryPlaceExit()
        {
            List <MazeCoordinate> possibleExits = new List <MazeCoordinate>();

            // collect all possible exits
            for (int i = CurrentMaze.GetWidth() - 2; i >= 1; i--)
            {
                possibleExits.Add(new MazeCoordinate(i, downright.Y));
            }

            // check in random order if exit is valid
            while (possibleExits.Count > 0)
            {
                var possibleExit = possibleExits.ElementAt(Random.Next(possibleExits.Count));

                if (CurrentMaze.GetMazeTypeOnPos(possibleExit.X, possibleExit.Y + 1) == MazeFieldType.Corridor)
                {
                    return(CurrentMaze.SetMazeTypeOnPos(possibleExit, MazeFieldType.Exit));
                }
                else
                {
                    possibleExits.Remove(possibleExit);
                }
            }

            // TODO Maybe not the best way to handle this, but the maze is useless without valid exit.
            throw new Exception("Could not find a suitable exit position.");
        }
Beispiel #3
0
        private bool checkIfWallDividesTwoCorridors(MazeCoordinate target)
        {
            MazeCoordinate[] temp = MazeCoordinate.GetHorizontalVerticalAdjacentCoordinates(target);

            int TargetWallAlignsWithCorridors = 0;

            foreach (MazeCoordinate c in temp)
            {
                if (this.CurrentMaze.GetMazeTypeOnPos(c) == MazeFieldType.Corridor)
                {
                    if (target.X == c.X || target.Y == c.Y)
                    {
                        TargetWallAlignsWithCorridors++;
                    }
                }
            }

            // direct connection
            if (TargetWallAlignsWithCorridors > 2)
            {
                return(true);
            }

            temp = MazeCoordinate.GetAllAdjacentCoordinates(target);

            int CorridorFieldsInCloseProximity   = 0;
            HashSet <MazeCoordinate> inProximity = new HashSet <MazeCoordinate>();

            foreach (MazeCoordinate c in temp)
            {
                if (CurrentMaze.GetMazeTypeOnPos(c) != MazeFieldType.Wall)
                {
                    CorridorFieldsInCloseProximity++;
                    inProximity.Add(c);
                }
            }
            // This value can be reduced to create more narrow sideways, maybe make it configurable ?
            if (CorridorFieldsInCloseProximity >= 3)
            {
                return(true);
            }

            if (CorridorFieldsInCloseProximity == 2)
            {
                // check if the corridors are adjacent each other then its OK.
                foreach (MazeCoordinate c in inProximity)
                {
                    var  subset      = inProximity.Where(x => x != c).AsEnumerable();
                    bool allAdjacent = subset.All(x => x.IsAdjacentTo(c));
                    if (allAdjacent)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            if (TargetWallAlignsWithCorridors > 1 && CorridorFieldsInCloseProximity >= 2)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }