int CountAdjacentMarkedCells(Point position) { int result = 0; EachAdjacentCells(position, positionAdjacent => { CellView cell = GetCell(positionAdjacent); if (cell.IsMark) { result++; } }); return result; }
void OpenAdjacentCells(Point position, bool checkMine = false) { EachAdjacentCells(position, positionAdjacent => { CellView cell = GetCell(positionAdjacent); if (!cell.IsMark) { if (checkMine && cell.IsMine) { cell.IsExplode = true; } cell.IsOpen = true; } }); }
void CellPropertyChanged(Object sender, PropertyChangedEventArgs e) { CellView cell = (CellView)sender; switch (e.PropertyName) { case nameof(CellView.IsExplode): game.GameOver(); break; case nameof(CellView.IsOpen): if (cell.AdjacentMines == 0) { OpenAdjacentCells(cell.Position); } game.CheckVictory(); break; } }
public MyList<CellView> GenerateGrid() { Cells.Clear(); List<CellView> cells = new List<CellView>(); for (int widthIterator = 0; widthIterator < settings.WidthArea; widthIterator++) { for (int heightIterator = 0; heightIterator < settings.HeightArea; heightIterator++) { Point currentCellPosition = new Point(widthIterator, heightIterator); CellView cell = new CellView() { IsMine = game.CheckMine(currentCellPosition), AdjacentMines = GetBombsCount(currentCellPosition), Position = currentCellPosition }; cell.PropertyChanged += CellPropertyChanged; cells.Add(cell); } } Cells.AddRange(cells); return Cells; }