public override void PlaceVertex(MazePoint p, Direction d) { MazePoint finalPoint; if (_movementHelper.CanMove(p, d, Size, out finalPoint)) { MazeCell cell; switch (d) { case Direction.Right: case Direction.Up: case Direction.Forward: cell = Maze[p.X, p.Y, p.Z]; cell.Directions = _flagParser.AddDirectionsToFlag(cell.Directions, d); break; case Direction.Left: case Direction.Down: case Direction.Back: cell = Maze[finalPoint.X, finalPoint.Y, finalPoint.Z]; cell.Directions = _flagParser.AddDirectionsToFlag(cell.Directions, _flagParser.OppositeDirection(d)); break; default: throw new ArgumentException(String.Format("Flag not supported: {0}", d)); } } }
public override void PlaceVertex(MazePoint p, Direction d) { var startCell = Maze[p.X, p.Y, p.Z]; MazePoint finalPoint; if (_movementHelper.CanMove(p, d, Size, out finalPoint)) { var finalCell = Maze[finalPoint.X, finalPoint.Y, finalPoint.Z]; startCell.Directions = _flagParser.AddDirectionsToFlag(startCell.Directions, d); finalCell.Directions = _flagParser.AddDirectionsToFlag(finalCell.Directions, _flagParser.OppositeDirection(d)); } }
public override void PlaceVertex(MazePoint p, Direction d) { MazeCell startCell; MazePoint final; MazeCell finalCell; if (Maze.TryGetValue(p, out startCell) && _movementHelper.CanMove(p, d, Size, out final) && Maze.TryGetValue(final, out finalCell)) { startCell.Directions = _flagParser.AddDirectionsToFlag(startCell.Directions, d); finalCell.Directions = _flagParser.AddDirectionsToFlag(finalCell.Directions, _flagParser.OppositeDirection(d)); } }
public AlgorithmRunResults RecursiveBackTracker(IMazeCarver carver) { var pointsAndDirections = new List <DirectionAndPoint>(); var directions = carver.CarvableDirections().ToList(); _arrayHelper.Shuffle(directions); var currentPoint = carver.CurrentPoint; foreach (var direction in directions) { carver.JumpInDirection(direction); var carvedDirections = carver.AlreadyCarvedDirections(); if (!carvedDirections.Any()) { pointsAndDirections.Add(new DirectionAndPoint { Direction = direction, MazePoint = currentPoint }); var oppositeDirection = _directionsFlagParser.OppositeDirection(direction); carver.CarveInDirection(oppositeDirection); RecursiveBackTracker(carver); } carver.JumpToPoint(currentPoint); } return(new AlgorithmRunResults { Carver = carver, DirectionsCarvedIn = pointsAndDirections }); }
private IEnumerable <GraphEdge> GetGraphEdges(IMazeCarver carver, PointAndDirections junction) { foreach (var direction in junction.Directions) { var result = new GraphEdge { DirectionsToPoint = new List <Direction>() { direction } }; carver.JumpToPoint(junction.Point); carver.JumpInDirection(direction); Direction inputDirection = direction; bool endFound = false; do { var directions = carver.GetsDirectionsFromPoint().ToList(); if (IsStartOrEndPointOrJunction(carver, directions)) { result.Point = carver.CurrentPoint; endFound = true; } else { var oppositeDirection = _directionsFlagParser.OppositeDirection(inputDirection); directions = directions.Where(x => x != oppositeDirection).ToList(); inputDirection = directions.Single(); result.DirectionsToPoint.Add(inputDirection); carver.JumpInDirection(inputDirection); } } while (!endFound); yield return(result); } }
public void LeftFlag_OppositeDirection_RightFlagReturned() { //Arrange var left = Direction.Left; //Act var final = _flagParser.OppositeDirection(left); //Assert Assert.That(final, Is.EqualTo(Direction.Right)); }
private List <DirectionAndPoint> GetPathToLastPoint(List <DirectionAndPoint> previousPoints, IMaze maze) { //If we are at the end return a list with the final Point added with No direction if (maze.CurrentPoint.Equals(maze.EndPoint)) { previousPoints.Add(new DirectionAndPoint { Direction = Direction.None, MazePoint = maze.CurrentPoint }); return(previousPoints); } //If the cell has been visited immediately return the current List of previous points if (previousPoints.Any(x => x.MazePoint.Equals(maze.CurrentPoint))) { return(previousPoints); } var directions = maze.GetsDirectionsFromPoint().ToList(); _arrayHelper.Shuffle(directions); var currentPoint = maze.CurrentPoint; //check each direction for path to end foreach (var direction in directions) { maze.MoveInDirection(direction); var newPreviousPoints = previousPoints.Concat(new[] { new DirectionAndPoint { Direction = direction, MazePoint = currentPoint } }).ToList(); var path = GetPathToLastPoint(newPreviousPoints, maze); var last = path.Last(); if (last.MazePoint.Equals(maze.EndPoint)) { return(path); } //Move back to start point if no path is found. maze.MoveInDirection(_directionsFlagParser.OppositeDirection(direction)); } //If no path in any direction reaches end then return previous points. return(previousPoints); }
public AlgorithmRunResults GenerateMaze(IMazeCarver maze, MazeGenerationSettings settings) { var pointAndDirection = new List <DirectionAndPoint>(); var randomPoint = _randomPointGenerator.RandomPoint(maze.Size); maze.JumpToPoint(randomPoint); var activeCells = new LinkedList <MazePoint>(); activeCells.AddLast(randomPoint); while (activeCells.Any()) { var currentPoint = activeCells.Last.Value; maze.JumpToPoint(currentPoint); var carvableDirections = maze.CarvableDirections().ToList(); _arrayHelper.Shuffle(carvableDirections); var carved = false; foreach (var direction in carvableDirections) { maze.JumpInDirection(direction); var carvedDirections = maze.AlreadyCarvedDirections(); if (!carvedDirections.Any()) { pointAndDirection.Add(new DirectionAndPoint { Direction = direction, MazePoint = currentPoint }); var oppositeDirection = _directionsFlagParser.OppositeDirection(direction); maze.CarveInDirection(oppositeDirection); activeCells.AddLast(maze.CurrentPoint); carved = true; break; } maze.JumpToPoint(currentPoint); } if (!carved) { activeCells.RemoveLast(); } } return(new AlgorithmRunResults { Carver = maze, DirectionsCarvedIn = pointAndDirection }); }
private AlgorithmRunResults GenerateMaze(IMazeCarver maze, List <GrowingTreeStrategy> strategies) { var pointsAndDirections = new List <DirectionAndPoint>(); var randomPoint = _randomPointGenerator.RandomPoint(maze.Size); var activeCells = new List <MazePoint> { randomPoint }; while (activeCells.Any()) { var currentPoint = GetNextPoint(activeCells, strategies); maze.JumpToPoint(currentPoint); var carvableDirections = maze.CarvableDirections().ToList(); _arrayHelper.Shuffle(carvableDirections); var carved = false; foreach (var direction in carvableDirections) { maze.JumpInDirection(direction); var carvedDirections = maze.AlreadyCarvedDirections(); if (!carvedDirections.Any()) { pointsAndDirections.Add(new DirectionAndPoint { Direction = direction, MazePoint = currentPoint }); var oppositeDirection = _directionsFlagParser.OppositeDirection(direction); maze.CarveInDirection(oppositeDirection); activeCells.Add(maze.CurrentPoint); carved = true; break; } maze.JumpToPoint(currentPoint); } if (!carved) { activeCells.Remove(currentPoint); } } return(new AlgorithmRunResults { Carver = maze, DirectionsCarvedIn = pointsAndDirections }); }
private void DeadEndCarver(IMazeCarver carver, int numberOfWalls, bool hasPreferredDirection) { var pointsAndDirections = _pointsAndDirectionsRetriever.GetDeadEnds(carver).ToList(); _arrayHelper.Shuffle(pointsAndDirections); foreach (var pointAndDirections in pointsAndDirections) { if (numberOfWalls > 0) { Direction preferredDirection = Direction.None; if (hasPreferredDirection) { preferredDirection = _directionsFlagParser .OppositeDirection(pointAndDirections.Directions.First()); } numberOfWalls = CheckPoint(pointAndDirections.Point, carver, numberOfWalls, preferredDirection); } else { break; } } RandomCarveWalls(carver, numberOfWalls); }
public override AgentResults RunAgentBase(IMaze maze) { var pointAndDirectionsList = new LinkedList <DirectionAndPoint>(); if (!maze.CurrentPoint.Equals(maze.EndPoint)) { var firstDirections = maze.GetsDirectionsFromPoint().ToList(); _arrayHelper.Shuffle(firstDirections); var first = firstDirections.First(); var currentPoint = maze.CurrentPoint; maze.MoveInDirection(first); var lastDirectionMoved = first; pointAndDirectionsList.AddLast(new DirectionAndPoint { MazePoint = currentPoint, Direction = first }); while (!maze.CurrentPoint.Equals(maze.EndPoint)) { var directions = maze.GetsDirectionsFromPoint().ToList(); var reverseDirection = _directionsFlagParser.OppositeDirection(lastDirectionMoved); var filteredDirections = directions.Where(x => x != reverseDirection).ToList(); _arrayHelper.Shuffle(filteredDirections); if (_pointsAndDirectionsRetriever.IsJunction(directions)) { var direction = filteredDirections.First(); pointAndDirectionsList.AddLast(new DirectionAndPoint { Direction = direction, MazePoint = maze.CurrentPoint }); maze.MoveInDirection(direction); lastDirectionMoved = direction; } else { if (filteredDirections.Any()) { var direction = filteredDirections.First(); pointAndDirectionsList.AddLast(new DirectionAndPoint { Direction = direction, MazePoint = maze.CurrentPoint }); maze.MoveInDirection(direction); lastDirectionMoved = direction; } else { var direction = reverseDirection; pointAndDirectionsList.AddLast(new DirectionAndPoint { Direction = direction, MazePoint = maze.CurrentPoint }); maze.MoveInDirection(direction); lastDirectionMoved = direction; } } } } var list = pointAndDirectionsList.ToList(); return(new AgentResults { Movements = list.Distinct(DirectionAndPoint.DirectionMazePointComparer).ToList() }); }