private void ConstructPortals() { PortalCells = new Dictionary <GridPoint, string>(); Portals = new Dictionary <string, IList <GridPoint> >(); var emptyCells = MazeCells .Where(kvp => DonutMazeCellType.Empty.Equals(kvp.Value.Type)); foreach (var emptyCell in emptyCells) { var point = emptyCell.Key; var movementDirections = new List <MovementDirection>() { MovementDirection.Down, MovementDirection.Left, MovementDirection.Right, MovementDirection.Up }; foreach (var direction in movementDirections) { // Assumption: One point can only be associated with at // most one portal. var neighbor1 = point.Move(direction, 1); if (!MazeCells.ContainsKey(neighbor1) || !DonutMazeCellType.Portal.Equals(MazeCells[neighbor1].Type)) { continue; } var neighbor2 = point.Move(direction, 2); if (!MazeCells.ContainsKey(neighbor2) || !DonutMazeCellType.Portal.Equals(MazeCells[neighbor2].Type)) { throw new Exception("Only one neighboring portal cell found"); } var portalId = string.Empty; if (MovementDirection.Right.Equals(direction) || MovementDirection.Up.Equals(direction)) { portalId = MazeCells[neighbor1].PortalLetter + MazeCells[neighbor2].PortalLetter; } else { portalId = MazeCells[neighbor2].PortalLetter + MazeCells[neighbor1].PortalLetter; } PortalCells.Add(point, portalId); if (!Portals.ContainsKey(portalId)) { Portals.Add(portalId, new List <GridPoint>()); } Portals[portalId].Add(point); } } EntrancePoint = new GridPoint3D(Portals[EntrancePortalId][0], 0); ExitPoint = new GridPoint3D(Portals[ExitPortalId][0], 0); }
private void CalculateMazeBoundaries() { OuterWallLeft = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type)) .Min(kvp => kvp.Key.X); OuterWallRight = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type)) .Max(kvp => kvp.Key.X); OuterWallTop = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type)) .Min(kvp => kvp.Key.Y); OuterWallBottom = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type)) .Max(kvp => kvp.Key.Y); int midX = (OuterWallRight - OuterWallLeft) / 2; int midY = (OuterWallBottom - OuterWallTop) / 2; InnerWallLeft = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type) && kvp.Key.X <= midX && kvp.Key.Y == midY) .Max(kvp => kvp.Key.X); InnerWallRight = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type) && kvp.Key.X >= midX && kvp.Key.Y == midY) .Min(kvp => kvp.Key.X); InnerWallTop = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type) && kvp.Key.Y <= midY && kvp.Key.X == midX) .Max(kvp => kvp.Key.Y); InnerWallBottom = MazeCells .Where(kvp => !DonutMazeCellType.Portal.Equals(kvp.Value.Type) && kvp.Key.Y >= midY && kvp.Key.X == midX) .Min(kvp => kvp.Key.Y); }