public void MarkMovementMazeTestWhiteSpaceOk() { var randomGeneratorService = new RandomGeneratorService(); var mazeSetupService = new MazeSetupService(); var moveMazeCellService = new MoveMazeCellService(randomGeneratorService); var generatedMaze = mazeSetupService.InitializeMaze(10, 10); MazeCellValueEnum expectec = MazeCellValueEnum.Empty; MazeCell nextCell = new MazeCell() { PositionX = 1, PositionY = 1, LastDirectionMovement = (int)Direction.S }; var result = moveMazeCellService.ValidateAndCalculateCellMovementType(nextCell, 10, 10, 9, 9, Direction.S, MazeCellValueEnum.Empty, generatedMaze); Assert.IsTrue(expectec == result); }
public void CreateMazeFakeWays(ref IMazeCell[,] generatedMaze) { var exitWayCells = (from MazeCell cell in generatedMaze where cell.Value == (int)MazeCellValueEnum.ExitWay select cell); if (exitWayCells.Any()) { int newFakeWaysCount = exitWayCells.Count() / 4; var randomExitWayCells = exitWayCells.OrderBy(x => Guid.NewGuid()).Take(newFakeWaysCount); foreach (var randomCell in randomExitWayCells) { randomCell.LastDirectionMovement = (int)_moveMazeCellService.NextRandomDirectionWall(randomCell, generatedMaze, _moveMazeCellService.CalculateOppositeDirection((Direction)randomCell.LastDirectionMovement), false); var nextCell = _moveMazeCellService.MoveToNextCell(randomCell, (Direction)randomCell.LastDirectionMovement); MazeCellValueEnum validateNextCell = _moveMazeCellService.ValidateAndCalculateCellMovementType(nextCell, generatedMaze.GetLength(0), generatedMaze.GetLength(1), randomCell.PositionX, randomCell.PositionY, _moveMazeCellService.CalculateOppositeDirection((Direction)randomCell.LastDirectionMovement), MazeCellValueEnum.Empty, generatedMaze); if (validateNextCell != MazeCellValueEnum.InvalidMark) { MazeCell currentCell = new MazeCell(); currentCell.PositionX = nextCell.PositionX; currentCell.PositionY = nextCell.PositionY; currentCell.LastDirectionMovement = randomCell.LastDirectionMovement; generatedMaze[nextCell.PositionX, nextCell.PositionY].Value = (int)validateNextCell; int wayLenght = _randomGeneratorService.randomGenerator.Next(5, 30); for (int i = 0; i < wayLenght; i++) { nextCell = _moveMazeCellService.MoveToNextCell(currentCell, _moveMazeCellService.NextRandomDirectionWall(currentCell, generatedMaze, _moveMazeCellService.CalculateOppositeDirection((Direction)currentCell.LastDirectionMovement), false)); validateNextCell = _moveMazeCellService.ValidateAndCalculateCellMovementType(nextCell, generatedMaze.GetLength(0), generatedMaze.GetLength(1), randomCell.PositionX, randomCell.PositionY, _moveMazeCellService.CalculateOppositeDirection((Direction)randomCell.LastDirectionMovement), MazeCellValueEnum.Empty, generatedMaze); if (validateNextCell != MazeCellValueEnum.InvalidMark) { currentCell.PositionX = nextCell.PositionX; currentCell.PositionY = nextCell.PositionY; currentCell.LastDirectionMovement = randomCell.LastDirectionMovement; generatedMaze[nextCell.PositionX, nextCell.PositionY].Value = (int)validateNextCell; } } } } } }
public MazeCellValueEnum ValidateAndCalculateCellMovementType(MazeCell nextCell, int HorizontalSize, int VerticalSize, int startPositionX, int startPositionY, Direction entrySide, MazeCellValueEnum markType, IMazeCell[,] generatedMaze) { MazeCellValueEnum markToMake = MazeCellValueEnum.InvalidMark; if (nextCell.PositionX < HorizontalSize && nextCell.PositionY < VerticalSize && nextCell.PositionX >= 0 && nextCell.PositionY >= 0) { switch (entrySide) { case Direction.N: case Direction.S: if (nextCell.PositionY != startPositionY) { markToMake = markType; } break; case Direction.E: case Direction.W: if (nextCell.PositionX != startPositionX) { markToMake = markType; } break; } if (markToMake != MazeCellValueEnum.InvalidMark) { if (generatedMaze[nextCell.PositionX, nextCell.PositionY].Value == (int)MazeCellValueEnum.WallBorder) { if (markType == MazeCellValueEnum.ExitWay) { markToMake = MazeCellValueEnum.Exit; } else { markToMake = MazeCellValueEnum.InvalidMark; } } else { if (markType == MazeCellValueEnum.Empty) { if (generatedMaze[nextCell.PositionX, nextCell.PositionY].Value == (int)MazeCellValueEnum.ExitWay) { markToMake = MazeCellValueEnum.InvalidMark; } else { int NewX = nextCell.PositionX; int NewY = nextCell.PositionY; switch ((Direction)nextCell.LastDirectionMovement) { case Direction.N: NewY--; break; case Direction.S: NewY++; break; case Direction.E: NewX++; break; case Direction.W: NewX--; break; } if (NewX < 0 || NewY < 0 || NewX >= generatedMaze.GetLength(0) || NewY >= generatedMaze.GetLength(1)) { markToMake = MazeCellValueEnum.InvalidMark; } else if (generatedMaze[NewX, NewY].Value != (int)MazeCellValueEnum.Wall) { markToMake = MazeCellValueEnum.InvalidMark; } } } } } } return(markToMake); }
public void CreateEntryAndExitWayPath(int HorizontalSize, int VerticalSize, ref IMazeCell[,] generatedMaze) { Direction entrySide = (Direction)_randomGeneratorService.randomGenerator.Next(0, 3); int entrySideStartPosition = 1; if (entrySide == Direction.N || entrySide == Direction.S) { entrySideStartPosition = _randomGeneratorService.randomGenerator.Next(HorizontalSize / 3, HorizontalSize - (HorizontalSize / 3)); } else { entrySideStartPosition = _randomGeneratorService.randomGenerator.Next(VerticalSize / 3, VerticalSize - (VerticalSize / 3)); } MazeCell startPosition = new MazeCell(); switch (entrySide) { case Direction.N: startPosition.PositionX = entrySideStartPosition; startPosition.PositionY = 0; break; case Direction.E: startPosition.PositionX = HorizontalSize - 1; startPosition.PositionY = entrySideStartPosition; break; case Direction.S: startPosition.PositionX = entrySideStartPosition; startPosition.PositionY = VerticalSize - 1; break; case Direction.W: startPosition.PositionX = 0; startPosition.PositionY = entrySideStartPosition; break; } generatedMaze[startPosition.PositionX, startPosition.PositionY].Value = (int)MazeCellValueEnum.Entry; MazeCell currentCell = new MazeCell { PositionX = startPosition.PositionX, PositionY = startPosition.PositionY }; currentCell.LastDirectionMovement = (int)_moveMazeCellService.CalculateOppositeDirection(entrySide); MazeCell nextCell = _moveMazeCellService.MoveToNextCell(currentCell, (Direction)currentCell.LastDirectionMovement); currentCell.PositionX = nextCell.PositionX; currentCell.PositionY = nextCell.PositionY; generatedMaze[nextCell.PositionX, nextCell.PositionY].Value = (int)MazeCellValueEnum.ExitWay; while (generatedMaze[currentCell.PositionX, currentCell.PositionY].Value != (int)MazeCellValueEnum.Exit) { nextCell = _moveMazeCellService.MoveToNextCell(currentCell, _moveMazeCellService.NextRandomDirectionWall(currentCell, generatedMaze, _moveMazeCellService.CalculateOppositeDirection((Direction)currentCell.LastDirectionMovement), false)); MazeCellValueEnum validateMovement = _moveMazeCellService.ValidateAndCalculateCellMovementType(nextCell, HorizontalSize, VerticalSize, startPosition.PositionX, startPosition.PositionY, entrySide, MazeCellValueEnum.ExitWay, generatedMaze); if (validateMovement != MazeCellValueEnum.InvalidMark) { currentCell.PositionX = nextCell.PositionX; currentCell.PositionY = nextCell.PositionY; currentCell.LastDirectionMovement = nextCell.LastDirectionMovement; generatedMaze[nextCell.PositionX, nextCell.PositionY].Value = (int)validateMovement; if (generatedMaze[currentCell.PositionX, currentCell.PositionY].Value != (int)MazeCellValueEnum.Exit) { nextCell = _moveMazeCellService.MoveToNextCell(nextCell, (Direction)currentCell.LastDirectionMovement); MazeCellValueEnum validateNextMovement = _moveMazeCellService.ValidateAndCalculateCellMovementType(nextCell, HorizontalSize, VerticalSize, startPosition.PositionX, startPosition.PositionY, entrySide, MazeCellValueEnum.ExitWay, generatedMaze); if (validateNextMovement != MazeCellValueEnum.InvalidMark) { currentCell.PositionX = nextCell.PositionX; currentCell.PositionY = nextCell.PositionY; currentCell.LastDirectionMovement = nextCell.LastDirectionMovement; generatedMaze[nextCell.PositionX, nextCell.PositionY].Value = (int)validateNextMovement; } } } } }