public TileView(TileController contrllr) { controller = contrllr; InitializeComponent(); }
protected void GenerateTiles(Size size, int amountOfMines) { tiles = new TileController[size.Width, size.Height]; int amountOfTilesToDo = size.Width * size.Height; Random random = new Random(); int maxPathLength = Math.Min(size.Width + size.Height, amountOfTilesToDo - amountOfMines); int minPathLength = Math.Max(maxPathLength / 2, 2); int pathLength = random.Next(minPathLength, maxPathLength + 1); List<Point> path = new List<Point>(); path.Add(new Point(random.Next(size.Width), random.Next(size.Height))); bool corner = true; int stuckCounter = 500; while (path.Count < pathLength) { stuckCounter--; if (stuckCounter < 0) { break; } Point p; if (corner) { p = GetRandomNeighbour(path[path.Count - 1], random); } else { Point diff = path[path.Count - 1] - new Size(path[path.Count - 2]); p = diff + new Size(path[path.Count - 1]); } if (path.Contains(p) || !FieldSize.Contains(p)) continue; corner = !corner; path.Add(p); } Player = new PlayerController(path[0], this); tiles[path[path.Count - 1].X, path[path.Count - 1].Y] = new ExitController(path[path.Count - 1],this ); for (int i = 0; i < path.Count -1; i++) { tiles[path[i].X, path[i].Y] = new TileController(path[i], this); amountOfTilesToDo--; } for (int x = 0; x < size.Width; x++) { for (int y = 0; y < size.Height; y++) { if (tiles[x, y] != null) { continue; } bool isMine = random.Next(amountOfTilesToDo) < amountOfMines; if (isMine) { tiles[x, y] = new MineController(new Point(x, y), this); } else { tiles[x, y] = new TileController(new Point(x, y), this); } amountOfTilesToDo--; if (isMine) { amountOfMines--; } } } }