public void checkAdjacentTiles(Tile tile) { foreach (Tile t in tiles) { if (Vector2.Distance(new Vector2(tile.rect.X, tile.rect.Y), new Vector2(t.rect.X, t.rect.Y)) <= TileWidth * Math.Sqrt(2)) checkTile(t); } }
public void checkTile(Tile tile) { if(tile.clicked) return; if (tile.flagged) --numFlags; tile.clicked = true; if(tile.tex == noneTexture) checkAdjacentTiles(tile); }
private void CheckSurroundingTiles(Tile tile, bool checkMines) { var tilesToCheck = GetTilesToCheck(tile); foreach (var tileToCheck in tilesToCheck) { if (!tileToCheck.IsMine) tileToCheck.Covered = false; if (tileToCheck.IsMine && tileToCheck.Status != "F" && checkMines) { EndGame(false); return; } if (!tileToCheck.IsMine && tileToCheck.MineCount == 0) CheckSurroundingTiles(tileToCheck, checkMines); } CheckGameStatus(); }
private void UncoverTile(Tile tile) { if (_timer.IsEnabled == false) _timer.Start(); if (tile.Status == "") tile.Covered = false; else return; if (minesPlaced == false) PlaceMines(); if (tile.IsMine) { tile.SelectedMine = true; EndGame(false); return; } CheckSurroundingTiles(tile, false); }
private int FindMarkedTiles(Tile tile) { return GetTilesToCheck(tile).Count(tileToCheck => tileToCheck.Status == "F"); }
private void DoubleClick(Tile tile) { if (tile.MineCount == 0) return; var foundMarkedTiles = FindMarkedTiles(tile); if (foundMarkedTiles != tile.MineCount) return; CheckSurroundingTiles(tile, true); }
private void MarkTile(Tile tile) { tile.Mark(); MinesLeft = Mines - Tiles.Count(x => x.Status == "F"); }
private int CalculateMineCount(Tile tile) { return GetTilesToCheck(tile).Count(tileToCheck => tileToCheck.IsMine); }
private void AddMines(Tile[,] board) { Random random = new Random(); for (int count = 0; count < 10; ) { int row = random.Next(length); int col = random.Next(width); if (board[row, col] == null) { Mine mine = new Mine(row, col); mine.MouseRightButtonDown += Tile_RightClick; mine.Click += Tile_Click; board[row, col] = mine; count++; } } }
private int GetNumberOfMines(Tile[,] board, int row, int col) { int mineCount = 0; int rowStart = row - 1; int rowStop = row + 1; int colStart = col - 1; int colStop = col + 1; for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++) { for (int colIndex = colStart; colIndex <= colStop; colIndex++) { if (OutOfBounds(rowIndex, colIndex)) { continue; } if (board[rowIndex, colIndex] is Mine) { mineCount++; } } } return mineCount; }
private void ClickNeighborsInternal(Tile tile) { if (tile.IsViewed) { return; } if (tile.NumberOfMines > 0) { tile.LeftClick(); return; } tile.LeftClick(); int rowStart = tile.Row - 1; int rowStop = tile.Row + 1; int colStart = tile.Column - 1; int colStop = tile.Column + 1; for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++) { for (int colIndex = colStart; colIndex <= colStop; colIndex++) { if (OutOfBounds(rowIndex, colIndex)) { continue; } if (!(board[rowIndex, colIndex] is Mine)) { ClickNeighborsInternal(board[rowIndex, colIndex]); } } } }
private void ClickNeighbors(Tile tile) { int rowStart = tile.Row - 1; int rowStop = tile.Row + 1; int colStart = tile.Column - 1; int colStop = tile.Column + 1; for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++) { for (int colIndex = colStart; colIndex <= colStop; colIndex++) { if (OutOfBounds(rowIndex, colIndex)) { continue; } if (!(board[rowIndex, colIndex] is Mine) && board[rowIndex, colIndex].NumberOfMines == 0) { ClickNeighborsInternal(board[rowIndex, colIndex]); } } } }
private void AddTiles(Tile[,] board) { for (int row = 0; row <= board.GetUpperBound(0); row++) { for (int col = 0; col <= board.GetUpperBound(1); col++) { if (board[row, col] == null) { Tile tile = new Tile(GetNumberOfMines(board, row, col), row, col); tile.MouseRightButtonDown += Tile_RightClick; tile.Click += Tile_Click; board[row, col] = tile; } } } }
private IEnumerable<Tile> GetTilesToCheck(Tile tile) { var tilesToCheck = new List<Tile>(); var leftTopTile = Tiles.FirstOrDefault(x => x.Row == tile.Row - 1 && x.Col == tile.Col - 1); var aboveTile = Tiles.FirstOrDefault(x => x.Row == tile.Row - 1 && x.Col == tile.Col); var rightTopTile = Tiles.FirstOrDefault(x => x.Row == tile.Row - 1 && x.Col == tile.Col + 1); var leftTile = Tiles.FirstOrDefault(x => x.Row == tile.Row && x.Col == tile.Col - 1); var rightTile = Tiles.FirstOrDefault(x => x.Row == tile.Row && x.Col == tile.Col + 1); var belowLeftTile = Tiles.FirstOrDefault(x => x.Row == tile.Row + 1 && x.Col == tile.Col - 1); var belowTile = Tiles.FirstOrDefault(x => x.Row == tile.Row + 1 && x.Col == tile.Col); var belowRightTile = Tiles.FirstOrDefault(x => x.Row == tile.Row + 1 && x.Col == tile.Col + 1); if (leftTopTile != null && leftTopTile.Covered) tilesToCheck.Add(leftTopTile); if (aboveTile != null && aboveTile.Covered) tilesToCheck.Add(aboveTile); if (rightTopTile !=null && rightTopTile.Covered) tilesToCheck.Add(rightTopTile); if (leftTile != null && leftTile.Covered) tilesToCheck.Add(leftTile); if (rightTile != null && rightTile.Covered) tilesToCheck.Add(rightTile); if (belowLeftTile != null && belowLeftTile.Covered) tilesToCheck.Add(belowLeftTile); if (belowTile != null && belowTile.Covered) tilesToCheck.Add(belowTile); if (belowRightTile != null && belowRightTile.Covered) tilesToCheck.Add(belowRightTile); return tilesToCheck; }
public void init() { tiles = new List<Tile>(); List<Vector2> mines = new List<Vector2>(); numFlags = 0; win = false; lose = false; for (int i = 0; i < NumMines; ++i) { Random rand = new Random(); Vector2 pos = new Vector2(rand.Next() % GridWidth, rand.Next() % GridHeight); if (mines.Contains(pos)) --i; else mines.Add(pos); } for (int x = 0; x < GridWidth; ++x) { for (int y = 0; y < GridHeight; ++y) { Tile tile = new Tile(); int numMines = 0; for (int a = x - 1; a <= x + 1; ++a) { for (int b = y - 1; b <= y + 1; ++b) { if (mines.Contains(new Vector2(a, b))) { ++numMines; } } } switch (numMines) { case 0: tile.tex = noneTexture; break; case 1: tile.tex = oneTexture; break; case 2: tile.tex = twoTexture; break; case 3: tile.tex = threeTexture; break; case 4: tile.tex = fourTexture; break; case 5: tile.tex = fiveTexture; break; case 6: tile.tex = sixTexture; break; case 7: tile.tex = sevenTexture; break; case 8: tile.tex = eightTexture; break; } tile.rect = new Rectangle(x * TileWidth, MenuHeight + y * TileWidth, TileWidth, TileWidth); tile.mine = mines.Contains(new Vector2(x, y)); if (tile.mine) tile.tex = mineTexture; tiles.Add(tile); } } }
private void InitialiseGameTiles() { var tiles = new List<Tile>(); var index = 0; for (var r = 0; r < Rows; r++) { for (var c = 0; c < Columns; c++) { var tile = new Tile { Index = index, Row = r, Col = c }; tiles.Add(tile); index += 1; } } Tiles = tiles; }
static Tile[] GenerateTiles(int width, bool[] mines) { int count = mines.Length; int height = mines.Length / width; var tiles = new Tile[count]; for (int i = 0; i < tiles.Length; i++) tiles[i] = new Tile { IsMine = mines[i] }; for (int i = 0; i < tiles.Length; i++) { int x = i % width, y = i / width; if (y > 0 && mines[i - width]) tiles[i].SurroundMines++; if (y < height - 1 && mines[i + width]) tiles[i].SurroundMines++; if (x > 0 && mines[i - 1]) tiles[i].SurroundMines++; if (x < width - 1 && mines[i + 1]) tiles[i].SurroundMines++; if (y > 0 && x > 0 && mines[i - width - 1]) tiles[i].SurroundMines++; if (y > 0 && x < width - 1 && mines[i - width + 1]) tiles[i].SurroundMines++; if (y < height - 1 && x > 0 && mines[i + width - 1]) tiles[i].SurroundMines++; if (y < height - 1 && x < width - 1 && mines[i + width + 1]) tiles[i].SurroundMines++; } return tiles; }