public MazeGenerator(Grid grid, IMazeAlgorithm algorithm) { if (grid == null) { throw new ArgumentNullException(nameof(grid), $"The '{nameof(grid)}' cannot be null"); } this.maze = grid; this.algorithm = algorithm; }
public IActionResult Index(int size, string algo, MazeColor color) { // return Content($"{size}, {algo}, {color}"); int mazeSize = GetSize(size); IMazeAlgorithm algorithm = GetAlgorithm(algo); Bitmap mazeImage = Generate(mazeSize, algorithm, color); byte[] byteArray = ConvertToByteArray(mazeImage); return(File(byteArray, "image/png")); }
public Bitmap Generate(int mazeSize, IMazeAlgorithm algorithm, MazeColor color) { IMazeGenerator generator = new MazeGenerator( new ColorGrid(mazeSize, mazeSize, color), algorithm); generator.GenerateMaze(); return(generator.GetGraphicalMaze(true)); }
private Bitmap Generate(int mazeSize, IMazeAlgorithm algorithm, MazeColor color) { IMazeGenerator generator = new ConsoleLoggingDecorator( new MazeGenerator( new ColorGrid(mazeSize, mazeSize, color), algorithm)); generator.GenerateMaze(); return(generator.GetGraphicalMaze(true)); }
/// <summary> /// Uses the given algorithm to retrieve which walls should be destroyed to create the maze /// Based on this result, it will Destroy the walls that need to be destroyed, /// and set the Player and Prize object on the calculated start- and end position respectively /// </summary> /// <param name="mazeAlgorithmToUse">Implementation of the algorithm to calculate a path in the maze</param> /// <param name="allCellPositions">All available cell coordinates in between the created walls</param> MazeAlgorithmResult CreateMaze(IMazeAlgorithm mazeAlgorithmToUse, List <Vector3> allCellPositions) { MazeAlgorithmResult result = mazeAlgorithmToUse.GenerateMaze(allCellPositions); if (result.HasWallsToDestroy) { // if true, we've found some walls to destroy to create a path in the maze // Let's get the x-walls (flat ones with rotation = 0), and the y-walls (upright ones with rotation = 90) List <GameObject> xWalls = this._allWalls.Where(wall => wall.transform.rotation.eulerAngles.Equals(new Vector3(0, 0, 0))).ToList(); List <GameObject> yWalls = this._allWalls.Where(wall => wall.transform.rotation.eulerAngles.Equals(new Vector3(0, 0, 90))).ToList(); var xWallsToDestroy = result.XWallPositionsToDestroy; foreach (var xWallPosition in xWallsToDestroy) { // take off the (i.e. 0.5) offset used to center the wall Vector3 positionWithoutOffset = new Vector3(xWallPosition.x + this._wallCentrePoint, xWallPosition.y, xWallPosition.z); // try to find the X-Wall GameObject based on the position the algorithm returned var xWallToDestroy = xWalls.FirstOrDefault(wall => wall.transform.position.Equals(positionWithoutOffset)); // If it's there (as it should be), let's remove it from the scene if (xWallToDestroy != null) { Destroy(xWallToDestroy); } } var yWallsToDestroy = result.YWallPositionsToDestroy; foreach (var yWallPosition in yWallsToDestroy) { // take off the (i.e. 0.5) offset used to center the wall Vector3 positionWithoutOffset = new Vector3(yWallPosition.x, yWallPosition.y + this._wallCentrePoint, yWallPosition.z); // try to find the Y-Wall GameObject based on the position the algorithm returned var yWallToDestroy = yWalls.FirstOrDefault(wall => wall.transform.position.Equals(positionWithoutOffset)); // If it's there (as it should be), let's remove it from the scene if (yWallToDestroy != null) { Destroy(yWallToDestroy); } } } else { // something went terribly wrong throw new UnassignedReferenceException("Algorithm found no walls to destroy!"); } // return the result for further handling of positioning the Player and Prize return(result); }
private void GenerateMazeType() { if (mazeTypes == MazeTypes.Binary) { mazeAlgorithm = new BinaryMazeAlgorithm(); } else if (mazeTypes == MazeTypes.Sidewinder) { mazeAlgorithm = new SidewinderMazeAlgorithm(); } else if (mazeTypes == MazeTypes.AldousBroder) { mazeAlgorithm = new AldousBroderAlgorithm(); } }
private void SetAlgorithm() { var algo = (string)cbAlgorithm.SelectedItem; var type = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.Name == algo); if (type == null) { MessageBox.Show("No algorithm type for " + algo); } if (pbMask.Image != null && (type == typeof(Sidewinder) || type == typeof(BinaryTree))) { MessageBox.Show("Cannot use masks with Sidewinder and BinaryTree algorithms"); } _algorithm = (IMazeAlgorithm)Activator.CreateInstance(type, _grid, (int)nudRNGSeed.Value); }
public Bitmap Generate(int mazeSize, IMazeAlgorithm algorithm, MazeColor color) { IMazeGenerator generator = new MazeGenerator( new ColorGrid(mazeSize, mazeSize, color), algorithm); _logger.LogDebug($"{DateTime.Now:G} - Starting maze generation"); generator.GenerateMaze(); _logger.LogDebug($"{DateTime.Now:G} - Ending maze generation"); _logger.LogDebug($"{DateTime.Now:G} - Starting image generation"); Bitmap maze = generator.GetGraphicalMaze(true); _logger.LogDebug($"{DateTime.Now:G} - Ending image generation"); return(maze); }
private void SetAlgorithm() { var algo = (string)cbAlgorithm.SelectedItem; var type = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(t => t.Name == algo); if (type == null) { MessageBox.Show("No algorithm type for " + algo); } if (pbMask.Image != null && type == typeof(BinaryTree)) { } int rand = nudRNGSeed.Next(); _algorithm = (IMazeAlgorithm)Activator.CreateInstance(type, _grid, rand); }
public MazeGenerator(Grid grid, IMazeAlgorithm algorithm) { this.mazeGrid = grid; this.algorithm = algorithm; }
public MazeGenerator(IMazeAlgorithm algorithm) { _generationAlgorithm = algorithm; }
public Generator(IGrid gridToManipulate, IMazeAlgorithm algorithmToApply, IDistanceAlgorithm solver) { this.gridToManipulate = gridToManipulate; this.algorithmToApply = algorithmToApply; this.solver = solver; }
static void Main(string[] args) { // Prompt the user to input the type of algorithm we will be using. 1 for Binary 2 for sidewinder System.Console.WriteLine("Please tell me what type of algorithm you want to use. (1 - Binary 2 - Sidewinder)"); int algorithmType; // Create the base grid IGrid grid = new Grid.Grid(ROWS, COLUMNS); // Determine and create the type of algorithm that will be used to generate the rooms IMazeAlgorithm currentAlgorithm = null; do { // Get the type of algorithm that the user wants to use algorithmType = Convert.ToInt32(System.Console.ReadLine()); switch (algorithmType) { case 1: currentAlgorithm = new Binary(); break; case 2: currentAlgorithm = new Sidewinder(); break; default: System.Console.WriteLine("Can't find this algorithm. Please choose a valid one. (1 - Binary 2 - Sidewinder)"); break; } } while (algorithmType > 2 || algorithmType < 1); // Create the distance tracker IDistanceAlgorithm solver = new Rectangular(); // Generate the maze MazeGenerator.Generator generator = new MazeGenerator.Generator(grid, currentAlgorithm, solver); // Apply the algorithm to the generated maze generator.ApplyAlgorithm(); int maxDistance = generator.SolveMaze(new GridPosition(0, 0)); // create the .png with WIDTH_IN_PIXLES width and HEITH_IN_PIXELS height Bitmap gridPng = new Bitmap(WIDTH_IN_PIXLES, HEITH_IN_PIXLES); Graphics tool = Graphics.FromImage(gridPng); Pen blackPen = new Pen(Color.Black, PEN_THICKNESS_IN_PIXLES); // create a list of rooms from the generated grid List <Room> rooms = grid.GetRooms(); int cellWidth = WIDTH_IN_PIXLES / COLUMNS; int cellHeigth = HEITH_IN_PIXLES / ROWS; // Draw a line if there is no room in any direction. This was generated according to the roomstoconnectwith that was generated. foreach (Room room in rooms) { int xOfUpperLeft = room.column * cellWidth; int yOfUpperLeft = room.row * cellHeigth; // adding cellwidth shifts the position to the right int xOfLowerRight = (room.column * cellWidth) + cellWidth; int yOfLowerRight = (room.row * cellHeigth) + cellHeigth; if (!room.Neighbors().Contains(Direction.NORTH)) { tool.DrawLine(blackPen, xOfUpperLeft, yOfUpperLeft, xOfLowerRight, yOfUpperLeft); } if (!room.Neighbors().Contains(Direction.WEST)) { tool.DrawLine(blackPen, xOfUpperLeft, yOfUpperLeft, xOfUpperLeft, yOfLowerRight); } if (!room.Neighbors().Contains(Direction.EAST)) { tool.DrawLine(blackPen, xOfLowerRight, yOfUpperLeft, xOfLowerRight, yOfLowerRight); } if (!room.Neighbors().Contains(Direction.SOUTH)) { tool.DrawLine(blackPen, xOfUpperLeft, yOfLowerRight, xOfLowerRight, yOfLowerRight); } double intensity = ((double)maxDistance - (double)room.distance) / ((double)maxDistance); double dark = Math.Round((255d * intensity)); double bright = 128d + Math.Round(127d * intensity); Color roomColor = Color.FromArgb((int)dark, (int)bright, (int)dark); Brush roomColorBrush = new SolidBrush(roomColor); tool.FillRectangle(roomColorBrush, xOfUpperLeft, yOfUpperLeft, cellWidth, cellHeigth); } gridPng.Save("Hello.png"); blackPen.Dispose(); tool.Dispose(); gridPng.Dispose(); }