![endless maze generator algorithm endless maze generator algorithm](https://thisisyohan.com/sites/default/files/project/maze-7.png)
Both of these names should give you clues as to how the technique works.Ī maze is a complex structure of interconnected passageways. It is also given the name of a recursive backtracking algorithm. The technique I am going to describe uses a depth first search strategy.
Endless maze generator algorithm code#
It's a lot of code to review.In this article I’m going to take a look at one of the many algorithms that can be used to generate mazes. Up and Down are equivalents of Left and Right in the other dimension. My initial remark is the same as I read in the comments from Rick Davin. IMainUserInterface userInterface = new MainUserInterface() If stack is empty then labyrinth is generated successfully Disabling corresponding wall for each cellĬell top = (cell.X - 2 > 0) ? Cells.Find(c => c.X = cell.X - 2 & c.Y = cell.Y) : null Ĭell right = (cell.Y + 2 c.Y = cell.Y + 2 & c.X = cell.X) : null Ĭell bottom = (cell.X + 2 c.X = cell.X + 2 & c.Y = cell.Y) : null Ĭell left = (cell.Y - 2 > 0) ? Cells.Find(c => c.Y = cell.Y - 2 & c.X = cell.X) : null Assigning coordinates of a wall between a and b Algorithm is done when current cell is back at the beginning Width = width % 2 = 0 ? width - 1 : width ĬurrentCell.Display(ConsoleColor.Green, ConsoleColor.Green) Height = height % 2 = 0 ? height - 1 : height Setting an odd number even if it's even Public Labyrinth(int height, int width, ConsoleColor fieldColor, ConsoleColor wallsColor) Stack for recursive generation algorithm
![endless maze generator algorithm endless maze generator algorithm](https://catlikecoding.com/unity/tutorials/maze/02-maze-delay.png)
Protected void ColorDisplay(string str, ConsoleColor fgColor, ConsoleColor bgColor)ĬonsoleColor defaultFg = Console.ForegroundColor ĬonsoleColor defaultBg = Console.BackgroundColor Any hereditary class can use this function to display string in color Return walls.Any(c => c.IsCollidingWith(this)) Check if walls list contains player coordinates Public void HandleKey(ConsoleKeyInfo cki) Public Player(char value, List walls) : base(value, 1, 1) Walls initialization, value and coordinates go to base constructor
![endless maze generator algorithm endless maze generator algorithm](https://i.ytimg.com/vi/qMTT7sUOd6s/hqdefault.jpg)
![endless maze generator algorithm endless maze generator algorithm](https://www.astrolog.org/labyrnth/sample/follow.gif)
Displaying available walls around each cell Not displaying cell that hasn't been visitedĭisplay(Console.ForegroundColor, bgColor) ĭisplay(Console.ForegroundColor, Console.BackgroundColor) Public void Display(ConsoleColor bgColor) Public void StartGame(IGameLoop gameLoop, IGame game) This interface should be implemented by game objectĬlass MainUserInterface : IMainUserInterface Void StartGame(IGameLoop gameLoop, IGame game) Is there a workaround? Direction: namespace Labyrinth Is there a way to properly restart game when it quits to menu? Obviously, I can't use constructor on IGame, and because of that I've made Initialize() method, but is that ok? Because menu is in a loop Game object keeps all values from previous play. For maze generation I've followed this algorithm and aside from that I've made a little game, to go from A to B.Įverything works fine but I'm curious whether it's possible to improve/simplify my code or not? If you have any thoughts on readability or efficiency - please share. So for over a weekend I decided to write something rather simple.