public RecursiveBacktracker(int w, int h) { width = w; height = h; totalNumber = w * h; maze = new MazeCell[width][]; for (int i = 0; i < width; i++) { maze[i] = new MazeCell[height]; for (int j = 0; j < height; j++) { maze[i][j] = new MazeCell(); maze[i][j].X = i; maze[i][j].Y = j; } } Random rand = new Random(); startPosition = new Point(rand.Next(0, width), rand.Next(0, height)); stack = new Stack<MazeCell>(); }
public MazeCell() { east = west = north = south = null; X = Y = -1; Visited = 0; }
public void Generate(MazeCell current, Point position) { if (current == null) return; Random rand = new Random(); while (totalNumber > 0) { current.Visited = 2; position.X = current.X; position.Y = current.Y; // Randomize int east = rand.Next(0, 99); int west = rand.Next(0, 99); int north = rand.Next(0, 99); int south = rand.Next(0, 99); List<KeyValuePair<Sides, int>> paths = new List<KeyValuePair<Sides, int>>(); paths.Add(new KeyValuePair<Sides, int>(Sides.East, east)); paths.Add(new KeyValuePair<Sides, int>(Sides.West, west)); paths.Add(new KeyValuePair<Sides, int>(Sides.North, north)); paths.Add(new KeyValuePair<Sides, int>(Sides.South, south)); paths.Sort(Compare1); // to be separated... List<KeyValuePair<Sides, int>> filteredPaths = findUnvisited(paths, position); if (filteredPaths.Count == 0) { current = stack.Pop(); position.X = current.X; position.Y = current.Y; } var path = filteredPaths[0]; if (path.Key == Sides.East) { current.East = maze[position.X + 1][position.Y]; maze[position.X + 1][position.Y].West = current; stack.Push(current); current = maze[position.X + 1][position.Y]; totalNumber--; } if (path.Key == Sides.West) { current.West = maze[position.X - 1][position.Y]; maze[position.X - 1][position.Y].East = current; stack.Push(current); current = maze[position.X - 1][position.Y]; current.X = position.X - 1; current.Y = position.Y - 1; totalNumber--; } if (path.Key == Sides.North) { current.North = maze[position.X][position.Y - 1]; maze[position.X][position.Y - 1].South = current; stack.Push(current); current = maze[position.X][position.Y - 1]; totalNumber--; } if (path.Key == Sides.South) { current.South = maze[position.X][position.Y + 1]; maze[position.X][position.Y + 1].North = current; stack.Push(current); current = maze[position.X][position.Y + 1]; totalNumber--; } } }
public void Generate(MazeCell current) { current.Visited = 2; Random rand = new Random(); while (totalNumber > 0) { //current = stack.Pop(); int east = rand.Next(0, 99999); int west = rand.Next(0, 99999); int north = rand.Next(0, 99999); int south = rand.Next(0, 99999); List<KeyValuePair<Sides, int>> paths = new List<KeyValuePair<Sides, int>>(); paths.Add(new KeyValuePair<Sides, int>(Sides.East, east)); paths.Add(new KeyValuePair<Sides, int>(Sides.West, west)); paths.Add(new KeyValuePair<Sides, int>(Sides.North, north)); paths.Add(new KeyValuePair<Sides, int>(Sides.South, south)); paths.Sort(Compare1); List<KeyValuePair<Sides, int>> filteredPaths = findUnvisited(paths, new Point(current.X,current.Y)); if (filteredPaths.Count == 0) { if (stack.Count == 0) break; current = stack.Pop(); } else { var path = filteredPaths[0]; if (path.Key == Sides.East) { stack.Push(current); current.East = maze[current.X + 1][current.Y]; maze[current.X + 1][current.Y].West = current; current = maze[current.X + 1][current.Y]; current.Visited = 2; totalNumber--; } if (path.Key == Sides.West) { stack.Push(current); current.West = maze[current.X - 1][current.Y]; maze[current.X - 1][current.Y].East = current; current = maze[current.X - 1][current.Y]; current.Visited = 2; totalNumber--; } if (path.Key == Sides.North) { stack.Push(current); current.North = maze[current.X][current.Y - 1]; maze[current.X][current.Y - 1].South = current; current = maze[current.X][current.Y - 1]; current.Visited = 2; totalNumber--; } if (path.Key == Sides.South) { stack.Push(current); current.South = maze[current.X][current.Y + 1]; maze[current.X][current.Y + 1].North = current; current = maze[current.X][current.Y + 1]; current.Visited = 2; totalNumber--; } } } }
public RandomMouse(MazeCell[][] mc, Point sp, Point ep) { maze = mc; startPoint = sp; endPoint = ep; }