private static bool isValid(int x, int y, InnerMap map, Maze maze) { //Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); if (x > 0 && x < maze.Width - 1 && y > 0 && y < maze.Height - 1) { return !map[x, y]; } return false; }
public Maze(int width, int height, InnerMapType innerMapType) { switch (innerMapType) { case InnerMapType.BitArreintjeFast: innerMap = new BitArreintjeFastInnerMap(width, height); break; case InnerMapType.BooleanArray: innerMap = new BooleanInnerMap(width, height); break; case InnerMapType.DotNetBitArray: innerMap = new DotNetBitArrayInnerMap(width, height); break; case InnerMapType.BitArrayMappedOnHardDisk: innerMap = new BitArrayMappedOnHardDiskInnerMap(width, height); break; case InnerMapType.Hybrid: innerMap = new HybridInnerMap(width, height); break; default: break; } }
private void GoGenerate(InnerMap map, Maze maze, Random r, Action<int, int, long, long> pixelChangedCallback) { long totSteps = (((long)maze.Width - 1L) / 2L) * (((long)maze.Height - 1L) / 2L); long currentStep = 1; //needs to be optimized for (int x = 0; x < maze.Width; x++) { for (int y = 0; y < maze.Height; y++) { if (x == 0 || x == maze.Width - 1 || x == maze.Width - 2 || y == 0 || y == maze.Height - 1 || y == maze.Height - 2) { map[x, y] = false; } else { map[x, y] = true; } } } Stack<Rectangle> rectangles = new Stack<Rectangle>(); Rectangle curRect = new Rectangle(0, 0, maze.Width - 1, maze.Height - 1); rectangles.Push(curRect); while (rectangles.Count != 0) { curRect = rectangles.Pop(); if (curRect.Width > 3 && curRect.Height > 3) { Boolean horizontalSplit = true; if (curRect.Width > curRect.Height) { horizontalSplit = false; } else if (curRect.Width < curRect.Height) { horizontalSplit = true; } else { if (r.Next(2) == 0) { horizontalSplit = false; } } if (horizontalSplit) { int splitnumber = 2 + r.Next((curRect.Height - 2) / 2) * 2; int opening = 1 + r.Next((curRect.Width) / 2) * 2; Rectangle rect1 = new Rectangle(curRect.X, curRect.Y, curRect.Width, splitnumber + 1); Rectangle rect2 = new Rectangle(curRect.X, curRect.Y + splitnumber, curRect.Width, curRect.Height - splitnumber); for (int i = curRect.X; i < curRect.X + curRect.Width; i++) { if (i - curRect.X != opening) { map[i, curRect.Y + splitnumber] = false; } } //form.drawRectangle(curRect.X, curRect.Y + splitnumber, opening, 1, brushBlack); //form.drawRectangle(curRect.X + opening + 1, curRect.Y + splitnumber, curRect.Width - opening - 1, 1, brushBlack); rectangles.Push(rect1); rectangles.Push(rect2); } else { int splitnumber = 2 + r.Next((curRect.Width - 2) / 2) * 2; int opening = 1 + r.Next((curRect.Height) / 2) * 2; Rectangle rect1 = new Rectangle(curRect.X, curRect.Y, splitnumber + 1, curRect.Height); Rectangle rect2 = new Rectangle(curRect.X + splitnumber, curRect.Y, curRect.Width - splitnumber, curRect.Height); for (int i = curRect.Y; i < curRect.Y + curRect.Height; i++) { if (i - curRect.Y != opening) { map[curRect.X + splitnumber, i] = false; } } //form.drawRectangle(curRect.X + splitnumber, curRect.Y, 1, opening, brushBlack); //form.drawRectangle(curRect.X + splitnumber, curRect.Y + opening + 1, 1, curRect.Height - opening - 1, brushBlack); rectangles.Push(rect1); rectangles.Push(rect2); } } } }
public Maze(InnerMap customInnerMap) { innerMap = customInnerMap; }
private void GoGenerate(InnerMap map, Maze maze, Random r, Action<int, int, long, long> pixelChangedCallback) { long totSteps = (((long)maze.Width - 1L) / 2L) * (((long)maze.Height - 1L) / 2L) * 2; long currentStep = 1; KruskalCell[][] theMap; //Prepare theMap = new KruskalCell[map.Width][]; for (int x = 0; x < map.Width; x++) { theMap[x] = new KruskalCell[map.Height]; for (int y = 0; y < map.Height; y++) { KruskalCell c = new KruskalCell(x, y); theMap[x][y] = c; if ((x + 1) % 2 == 0 && (y + 1) % 2 == 0 && x != map.Width - 1 && y != map.Height - 1) { currentStep++; pixelChangedCallback(x, y, currentStep, totSteps); c.kruskalTileType = KruskalTileType.Passable; c.cellset.Add(c); } else { c.kruskalTileType = KruskalTileType.Solid; } } } //Find walls and add neighbouring cells List<KruskalCell> walls = new List<KruskalCell>(); for (int y = 1; y < map.Height - 2; y++) { Boolean horizontalwall = false; int startje = 1; if (y % 2 == 1) { horizontalwall = true; startje = 2; } for (int x = startje; x < map.Width - 2; x = x + 2) { KruskalCell ccc = theMap[x][y]; ccc.kruskalTileType = KruskalTileType.Solid; walls.Add(ccc); ccc.cellset.Clear(); if (horizontalwall) { //form.pixelDraw(x, y, Brushes.Blue); ccc.cellset.Add(theMap[x - 1][y]); ccc.cellset.Add(theMap[x + 1][y]); } else { //form.pixelDraw(x, y, Brushes.Yellow); ccc.cellset.Add(theMap[x][y - 1]); ccc.cellset.Add(theMap[x][y + 1]); } } } walls = walls.RandomPermutation(); int cur = 0; foreach (KruskalCell wall in walls) { cur++; KruskalCell cell1 = wall.cellset[0]; KruskalCell cell2 = wall.cellset[1]; if (!cell1.cellset.Equals(cell2.cellset)) { //Thread.Sleep(200); wall.kruskalTileType = KruskalTileType.Passable; //form.drawPixel(wall.x, wall.y, brushThisUses); currentStep++; pixelChangedCallback(wall.x, wall.y, currentStep, totSteps); List<KruskalCell> l1 = cell1.cellset; List<KruskalCell> l2 = cell2.cellset; if (l1.Count > l2.Count) { l1.AddRange(l2); foreach (KruskalCell c in l2) { c.cellset = l1; } } else { l2.AddRange(l1); foreach (KruskalCell c in l1) { c.cellset = l2; } } } } for (int y = 0; y < map.Height; y++) { for (int x = 0; x < map.Width; x++) { int ding = (int)theMap[x][y].kruskalTileType; if (ding == 0) { map[x, y] = false; } else { map[x, y] = true; } } } }
private void GoGenerate(InnerMap map, Maze maze, Random r, Action<int, int, long, long> pixelChangedCallback) { long totSteps = (((long)maze.Width - 1L) / 2L) * (((long)maze.Height - 1L) / 2L); long currentStep = 1; int width = maze.Width; int height = maze.Height; int x = 1; int y = 1; //Stack<MazePoint> stackje = new Stack<MazePoint>(); //stackje.Push(new MazePoint(x, y)); map[x, y] = true; pixelChangedCallback.Invoke(x, y, currentStep, totSteps); MazePoint[] targets = new MazePoint[4]; QuatroStack quatro = new QuatroStack(); //0 == top, 1 == right, 2 == bot, 3 == left Boolean backtracking = false; int prex = 0; int prey = 0; while (true) { //Console.WriteLine(quatro.Count + ", X: " + x + " Y: " + y); int targetCount = 0; if (x - 2 > 0 && !map[x - 2, y]) { targets[targetCount].X = x - 2; targets[targetCount].Y = y; targetCount++; } if (x + 2 < width - 1 && !map[x + 2, y]) { targets[targetCount].X = x + 2; targets[targetCount].Y = y; targetCount++; } if (y - 2 > 0 && !map[x, y - 2]) { targets[targetCount].X = x; targets[targetCount].Y = y - 2; targetCount++; } if (y + 2 < height - 1 && !map[x, y + 2]) { targets[targetCount].X = x; targets[targetCount].Y = y + 2; targetCount++; } //Thread.Sleep(1000); if (targetCount > 0) { var target = targets[r.Next(targetCount)]; if (backtracking) { backtracking = false; targetCount = 0; if (map[x - 1, y]) //Wall open at the left { targets[targetCount].X = x - 2; targets[targetCount].Y = y; targetCount++; } if (map[x + 1, y]) //Wall open at the right { targets[targetCount].X = x + 2; targets[targetCount].Y = y; targetCount++; } if (map[x, y - 1]) //Wall open at the top { targets[targetCount].X = x; targets[targetCount].Y = y - 2; targetCount++; } if (map[x, y + 1]) //Wall open at the bottom { targets[targetCount].X = x; targets[targetCount].Y = y + 2; targetCount++; } if (targetCount <= 2) //If currently only 2 exist at this tile, create junction { for (int i = 0; i < targetCount; i++) { var curMazePoint = targets[i]; if (curMazePoint.X != prex || curMazePoint.Y != prey) { if (curMazePoint.Y < y) { quatro.Push(0); //g.FillRectangle(Brushes.Green, x * 5, y * 5, 5, 5); } else if (curMazePoint.X > x) { quatro.Push(1); //g.FillRectangle(Brushes.Violet, x * 5, y * 5, 5, 5); } else if (curMazePoint.Y > y) { quatro.Push(2); //g.FillRectangle(Brushes.Blue, x * 5, y * 5, 5, 5); } else if (curMazePoint.X < x) { quatro.Push(3); //g.FillRectangle(Brushes.Brown, x * 5, y * 5, 5, 5); } break; } } } } //stackje.Push(target); map[target.X, target.Y] = true; currentStep++; if (target.X < x) { map[x - 1, y] = true; pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps); } else if (target.X > x) { map[x + 1, y] = true; pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps); } else if (target.Y < y) { map[x, y - 1] = true; pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps); } else if (target.Y > y) { map[x, y + 1] = true; pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps); } x = target.X; y = target.Y; prex = -1; prey = -1; pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps); } else { backtracking = true; if (map[x - 1, y]) //Wall open at the left { targets[targetCount].X = x - 2; targets[targetCount].Y = y; targetCount++; } if (map[x + 1, y]) //Wall open at the right { targets[targetCount].X = x + 2; targets[targetCount].Y = y; targetCount++; } if (map[x, y - 1]) //Wall open at the top { targets[targetCount].X = x; targets[targetCount].Y = y - 2; targetCount++; } if (map[x, y + 1]) //Wall open at the bottom { targets[targetCount].X = x; targets[targetCount].Y = y + 2; targetCount++; } if (targetCount > 2) //Junction { prex = x; prey = y; int whereToGo = quatro.Pop(); if (whereToGo == 0) { y -= 2; } else if (whereToGo == 1) { x += 2; } else if (whereToGo == 2) { y += 2; } else if (whereToGo == 3) { x -= 2; } } else { for (int i = 0; i < targetCount; i++) { var curMazePoint = targets[i]; if (curMazePoint.X != prex || curMazePoint.Y != prey) { prex = x; prey = y; x = curMazePoint.X; y = curMazePoint.Y; break; } } } pixelChangedCallback.Invoke(x, y, currentStep, totSteps); } if (x == 1 && y == 1) { break; } } }
private void GoGenerate(InnerMap map, Maze maze, Random r, Action<int, int, long, long> pixelChangedCallback) { int x = 1; int y = 1; Stack<MazePoint> stackje = new Stack<MazePoint>(); stackje.Push(new MazePoint(x, y)); map[x, y] = true; //pixelChangedCallback.Invoke(x, y, currentStep, totSteps); MazePoint[] targets = new MazePoint[4]; //form.drawPixel(x, y, brushThisUses); while (stackje.Count != 0) { MazePoint cur = stackje.Peek(); x = cur.X; y = cur.Y; int targetCount = 0; if (x - 2 > 0 && !map[x - 2, y]) { targets[targetCount].X = x - 2; targets[targetCount].Y = y; targetCount++; } if (x + 2 < maze.Width - 1 && !map[x + 2, y]) { targets[targetCount].X = x + 2; targets[targetCount].Y = y; targetCount++; } if (y - 2 > 0 && !map[x, y - 2]) { targets[targetCount].X = x; targets[targetCount].Y = y - 2; targetCount++; } if (y + 2 < maze.Height - 1 && !map[x, y + 2]) { targets[targetCount].X = x; targets[targetCount].Y = y + 2; targetCount++; } //Thread.Sleep(1000); if (targetCount > 0) { var target = targets[r.Next(targetCount)]; stackje.Push(target); map[target.X, target.Y] = true; if (target.X < x) { map[x - 1, y] = true; //pixelChangedCallback.Invoke(x - 1, y, currentStep, totSteps); //form.drawPixel(x - 1, y, brushThisUses); } else if (target.X > x) { map[x + 1, y] = true; //pixelChangedCallback.Invoke(x + 1, y, currentStep, totSteps); //form.drawPixel(x + 1, y, brushThisUses); } else if (target.Y < y) { map[x, y - 1] = true; //pixelChangedCallback.Invoke(x, y - 1, currentStep, totSteps); //form.drawPixel(x, y - 1, brushThisUses); } else if (target.Y > y) { map[x, y + 1] = true; //pixelChangedCallback.Invoke(x, y + 1, currentStep, totSteps); //form.drawPixel(x, y + 1, brushThisUses); } //pixelChangedCallback.Invoke(target.X, target.Y, currentStep, totSteps); //form.drawPixel(target.X, target.Y, brushThisUses); } else { stackje.Pop(); } } }
/// <summary> /// Finds the path between the start and the endpoint in a maze /// </summary> /// <param name="start">The start point</param> /// <param name="end">The end point</param> /// <param name="map">The maze.InnerMap</param> /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param> /// <returns>The shortest path in a list of points</returns> public static List<MazePointPos> GoFind(MazePoint startBefore, MazePoint endBefore, InnerMap map, Action<int, int, Boolean> callBack) { if (callBack == null) { callBack = (x, y, z) => { }; } var start = new MazePointPos(startBefore.X, startBefore.Y); var end = new MazePointPos(endBefore.X, endBefore.Y); //Callback won't work nice with this since it will find its path from back to front //Swap them so we don't have to reverse at the end ;) //MazePoint temp = start; //start = end; //end = temp; int width = map.Width; int height = map.Height; List<MazePointPos> stackje = new List<MazePointPos>(); stackje.Add(start); MazePointPos cur = new MazePointPos(); MazePointPos prev = new MazePointPos(-1, -1); var lastBackTrackDir = -1; while (stackje.Count != 0) { cur = stackje[stackje.Count - 1]; var x = cur.X; var y = cur.Y; MazePointPos target = new MazePointPos(-1, -1); //Make sure the point was not the previous point, also make sure that if we backtracked we don't go to a direction we already went to, also make sure that the point is white if ((prev.X != x + 1 || prev.Y != y) && lastBackTrackDir < 0 && x + 1 < width - 1 && map[x + 1, y]) { target = new MazePointPos(x + 1, y); } else if ((prev.X != x || prev.Y != y + 1) && lastBackTrackDir < 1 && y + 1 < height - 1 && map[x, y + 1]) { target = new MazePointPos(x, y + 1); } else if ((prev.X != x - 1 || prev.Y != y) && lastBackTrackDir < 2 && x - 1 > 0 && map[x - 1, y]) { target = new MazePointPos(x - 1, y); } else if ((prev.X != x || prev.Y != y - 1) && lastBackTrackDir < 3 && y - 1 > 0 && map[x, y - 1]) { target = new MazePointPos(x, y - 1); } else { var prepoppy = stackje[stackje.Count - 1]; stackje.RemoveAt(stackje.Count - 1); if (stackje.Count == 0) { //No path found break; } var newcur = stackje[stackje.Count - 1]; //Set the new previous point if (stackje.Count == 1) { prev = new MazePointPos(-1, -1); } else { prev = stackje.ElementAt(stackje.Count - 2); } //Console.WriteLine("Backtracking to X: " + newcur.X + " Y: " + newcur.Y); //Console.WriteLine("Setting new prev: " + prev.X + " Y: " + prev.Y); callBack.Invoke(prepoppy.X, prepoppy.Y, false); //Set the direction we backtracked from if (prepoppy.X > newcur.X) { lastBackTrackDir = 0; } else if (prepoppy.Y > newcur.Y) { lastBackTrackDir = 1; } else if (prepoppy.X < newcur.X) { lastBackTrackDir = 2; } else if (prepoppy.Y < newcur.Y) { lastBackTrackDir = 3; } //Console.WriteLine("Lastbacktrackdir: " + lastBackTrackDir); continue; } lastBackTrackDir = -1; //Console.WriteLine("Going to X: " + target.X + " Y: " + target.Y); callBack.Invoke(x, y, true); stackje.Add(target); if (target.X == end.X && target.Y == end.Y) { //Path found break; } prev = cur; } for (int i = 0; i < stackje.Count; i++) { byte formulathing = (byte)((double)i / (double)stackje.Count * 255.0); var currentStackjeThing = stackje[i]; stackje[i] = new MazePointPos(currentStackjeThing.X, currentStackjeThing.Y, formulathing); } return stackje; }
/// <summary> /// Finds the path between the start and the endpoint in a maze /// </summary> /// <param name="map">The maze.InnerMap</param> /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param> /// <returns>The shortest path in a list of points</returns> public static List<MazePointPos> GoFind(InnerMap map, Action<int, int, Boolean> callBack) { return GoFind(new MazePoint(1, 1), new MazePoint(map.Width - 3, map.Height - 3), map, callBack); }
/// <summary> /// Finds the path between the start and the endpoint in a maze /// </summary> /// <param name="start">The start point</param> /// <param name="end">The end point</param> /// <param name="map">The maze.InnerMap</param> /// <param name="callBack">The callback that can be used to see what the pathfinder is doing (or null), the boolean true = a new path find thingy or false when it determined that path is not correct</param> /// <returns>The shortest path in a list of points</returns> public static List<MazePoint> GoFind(MazePoint start, MazePoint end, InnerMap map, Action<int, int, Boolean> callBack) { if (callBack == null) { callBack = (x, y, z) => { }; } //Callback won't work nice with this since it will find its path from back to front //Swap them so we don't have to reverse at the end ;) //MazePoint temp = start; //start = end; //end = temp; int width = map.Width; int height = map.Height; InnerMap visitedMap = new BitArreintjeFastInnerMap(width, height); List<MazePoint> pointlist = new List<MazePoint>(); //@todo Controleer dit InnerMap visited = new BitArreintjeFastInnerMap(width, height); for (int x = 0; x < width; x++) { //visited[x] = new BitArreintjeFast(height); for (int y = 0; y < height; y++) { if (x == 0 || y == 0 || x == width || y == height) { visited[x, y] = true; } //else //{ // visited[x][y] = false; //} } } //Hier begint het gedoe Stack<MazePoint> stackje = new Stack<MazePoint>(); stackje.Push(start); visited[start.X, start.Y] = true; callBack.Invoke(start.X, start.Y, true); //form.pixelDraw(x, y, Brushes.White); while (stackje.Count != 0) { MazePoint cur = stackje.Peek(); int x = cur.X; int y = cur.Y; if (end.X == x && end.Y == y) { callBack.Invoke(x, y, true); break; } MazePoint target = new MazePoint(-1, -1); if (isValid(x + 1, y, map, visited, width, height)) { target = new MazePoint(x + 1, y); } else if (isValid(x, y + 1, map, visited, width, height)) { target = new MazePoint(x, y + 1); } else if (isValid(x - 1, y, map, visited, width, height)) { target = new MazePoint(x - 1, y); } else if (isValid(x, y - 1, map, visited, width, height)) { target = new MazePoint(x, y - 1); } //Thread.Sleep(1000); if (target.X != -1) { callBack.Invoke(x, y, true); //var target = targets[r.Next(targets.Count)]; stackje.Push(target); visited[target.X, target.Y] = true; //form.pixelDraw(target.X, target.Y, Brushes.Blue); //Thread.Sleep(200); //if (target.X < x) //{ // visited[x - 1][y] = true; // //form.pixelDraw(x - 1, y, Brushes.White); //} //else if (target.X > x) //{ // visited[x + 1][y] = true; // //form.pixelDraw(x + 1, y, Brushes.White); //} //else if (target.Y < y) //{ // visited[x][y - 1] = true; // //form.pixelDraw(x, y - 1, Brushes.White); //} //else if (target.Y > y) //{ // visited[x][y + 1] = true; // //form.pixelDraw(x, y + 1, Brushes.White); //} } else { callBack.Invoke(x, y, false); stackje.Pop(); } } pointlist.AddRange(stackje); pointlist.Reverse(); return pointlist; }
private static Boolean isValid(int x, int y, InnerMap map, InnerMap visitedmap, int width, int height) { if (x > 0 && x < width - 1 && y > 0 && y < height - 1) { if (visitedmap[x, y]) { return false; } else { return map[x, y]; } } else { return false; } }