private void CreateMaze(Difficulty diff) { LevelParameters prefs; switch (diff) { case Difficulty.Easy: prefs = GameConstants.Easy; break; case Difficulty.Normal: prefs = GameConstants.Normal; break; case Difficulty.Hard: prefs = GameConstants.Hard; break; case Difficulty.Impossible: prefs = GameConstants.Impossible; break; default: prefs = GameConstants.Normal; break; } Layer.ClearAllLayers(); MouseHandler.ClearMouseEvents(); GameGrid = MazeGrid.Generate(prefs.SideLength); GameGrid.GridVisible = true; Layer.AddToDraw(GameGrid); Layer.AddToUpdate(GameGrid); rotationsEnabled = prefs.RotationsEnabled; inspectionTime = prefs.InspectionTime * 1000; // timerille millisekunteina solveTime = prefs.SolveTime * 1000; currentDifficulty = diff; timer = new Timer(inspectionTime); StartInspection(); }
public static List<Point> GetAllConnectedEmptyCells(MazeGrid grid, Point p, List<Point> connected) { connected.Add(p); Point[] nBs = GetEmptyNeighbours(grid, p); for (int i = 0; i < nBs.Length; i++) { if (!connected.Contains(nBs[i])) { GetAllConnectedEmptyCells(grid, nBs[i], connected); } //connected.AddRange(GetAllConnectedEmptyCells(grid, nBs[i], connected)); } return connected; }
private static bool IsEmpty(MazeGrid grid, Point p) { return grid.Grid[p.X, p.Y].Type == CellType.Empty; }
/// <summary> /// Onko ruutu ruudukon sisäpuolella. /// </summary> /// <param name="grid"></param> /// <param name="p"></param> /// <returns></returns> private static bool IsValidCell(MazeGrid grid, Point p) { return p.X >= 0 && p.X < grid.SideLength && p.Y >= 0 && p.Y < grid.SideLength; }
private static Point[] GetEmptyNeighbours(MazeGrid grid, Point p) { List<Point> points = new List<Point>(); Point top = new Point(p.X, p.Y + 1); Point bottom = new Point(p.X, p.Y - 1); Point left = new Point(p.X - 1, p.Y); Point right = new Point(p.X + 1, p.Y); // dem epic design if (IsValidCell(grid, top)) if (IsEmpty(grid, top)) points.Add(top); if (IsValidCell(grid, bottom)) if (IsEmpty(grid, bottom)) points.Add(bottom); if (IsValidCell(grid, left)) if (IsEmpty(grid, left)) points.Add(left); if (IsValidCell(grid, right)) if (IsEmpty(grid, right)) points.Add(right); return points.ToArray(); }
public static MazeGrid Generate(int sideLength) { MazeGrid grid = new MazeGrid(sideLength); List<Point> walls = new List<Point>(); int[,] labels = new int[sideLength, sideLength]; int currentLabel = 0; for (int i = 0; i < sideLength; i++) { for (int j = 0; j < sideLength; j++) { labels[i, j] = -1; if (i % 2 != 0 || j % 2 != 0) { grid.Grid[i, j] = new GridCell(CellType.Wall); walls.Add(new Point(i, j)); } else { grid.Grid[i, j] = new GridCell(CellType.Empty); labels[i, j] = currentLabel; currentLabel++; } } } while (walls.Count > 0) { int cWall = RandomGen.NextInt(0, walls.Count); Point cPoint = walls[cWall]; walls.RemoveAt(cWall); Point[] nBs = GetEmptyNeighbours(grid, cPoint); int lowestLabel = int.MaxValue; // gg int lowestIndex = -1; bool diffLabels = false; for (int i = 0; i < nBs.Length; i++) { int cLabel = labels[nBs[i].X, nBs[i].Y]; if (cLabel != lowestLabel && lowestLabel != int.MaxValue) diffLabels = true; if (cLabel < lowestLabel) { lowestLabel = cLabel; lowestIndex = i; } } if (!diffLabels) continue; // ei poisteta seinää // grid.Grid[nBs[lowestIndex].X, nBs[lowestIndex].Y].Type = CellType.Empty; grid.Grid[cPoint.X, cPoint.Y].Type = CellType.Empty; // labels[nBs[lowestIndex].X, nBs[lowestIndex].Y] = lowestLabel; labels[cPoint.X, cPoint.Y] = lowestLabel; List<Point> connectedNodes = new List<Point>(); connectedNodes = GetAllConnectedEmptyCells(grid, cPoint, connectedNodes); for (int i = 0; i < connectedNodes.Count; i++) { labels[connectedNodes[i].X, connectedNodes[i].Y] = lowestLabel; } } Point start, end; do start = new Point(RandomGen.NextInt(0, (int)Math.Ceiling(sideLength / 3.0f)), RandomGen.NextInt(0, (int)Math.Ceiling(sideLength / 3.0f))); while (grid.Grid[start.X, start.Y].Type != CellType.Empty); grid.Grid[start.X, start.Y].Type = CellType.Start; do end = new Point(RandomGen.NextInt(2 * (sideLength / 3), sideLength), RandomGen.NextInt(2 * (sideLength / 3), sideLength)); while (grid.Grid[end.X, end.Y].Type != CellType.Empty); grid.Grid[end.X, end.Y].Type = CellType.End; return grid; }