public void OnCellClick(CellViewModel cell) { int row = cell.Row; int column = cell.Column; bool gameOver = false; if (!_cellViewModels[row, column].IsMine) { CalculateNeighbourMines(row, column); _numberOfMoves++; } else { //GameOver Display Board. gameOver = true; } if (isDone() || gameOver) { for (int i = 0; i < _numberOfRows; ++i) { for (int j = 0; j < _numberOfColumns; j++) { if (_cellViewModels[i, j].IsMine) { _cellViewModels[i, j].IsItRevelaed = true; } _cellViewModels[i, j].OnClickEvent -= OnCellClick; } } GameOver?.Invoke(new GameStatusEventArgs(_numberOfMoves, !gameOver)); IsGameOver = true; } }
void Initialize() { _cellViewModels = new CellViewModel[_numberOfRows, _numberOfColumns]; for (int i = 0; i < _numberOfRows; ++i) { for (int j = 0; j < _numberOfColumns; j++) { _cellViewModels[i, j] = new CellViewModel(i, j, false); _cellViewModels[i, j].OnClickEvent += OnCellClick; } } Random random = new Random(); for (var mines = 0; mines < _numberOfMines;) { var x1 = random.Next(_numberOfRows); var y1 = random.Next(_numberOfColumns); if (!_cellViewModels[x1, y1].IsMine) { _cellViewModels[x1, y1].IsMine = true; Console.WriteLine("Mine activated at {0},{1}", x1, y1); ++mines; } } }