/// <summary> /// Metoda otwierająca wybrane pole. /// </summary> /// <param name="x">Współrzędna pozioma otwieranego pola</param> /// <param name="y">Współrzędna pionowa otwieranego pola</param> public void OpenField(int x, int y) { if (!IsStarted) { Fill(x, y); IsStarted = true; } if (!Fields[y][x].IsMarked) { Fields[y][x].IsOpened = true; FieldOpened?.Invoke(x, y, Fields[y][x].Value); if (Fields[y][x].IsABomb) { IsFinished = true; GameFinished?.Invoke(false); } else { if (Fields[y][x].Value == 0) { OpenBlankAreas(); } bool victory = true; for (int yi = 1; yi <= Height; yi++) { for (int xi = 1; xi <= Width; xi++) { if (!Fields[yi][xi].IsOpened && !Fields[yi][xi].IsABomb) { victory = false; break; } } } if (victory) { IsFinished = true; GameFinished?.Invoke(true); } } } }
/// <summary> /// Pomocnicza metoda odsłaniająca puste fragmenty planszy. /// </summary> private void OpenBlankAreas() { int zerosNow = 0; int zerosBefore = 0; do { zerosBefore = zerosNow; zerosNow = 0; for (int y = 1; y <= Height; y++) { for (int x = 1; x <= Width; x++) { if (Fields[y][x].IsOpened && Fields[y][x].Value == 0) { Fields[y - 1][x - 1].IsOpened = true; FieldOpened?.Invoke(x - 1, y - 1, Fields[y - 1][x - 1].Value); Fields[y - 1][x].IsOpened = true; FieldOpened?.Invoke(x, y - 1, Fields[y - 1][x].Value); Fields[y - 1][x + 1].IsOpened = true; FieldOpened?.Invoke(x + 1, y - 1, Fields[y - 1][x + 1].Value); Fields[y][x - 1].IsOpened = true; FieldOpened?.Invoke(x - 1, y, Fields[y][x - 1].Value); Fields[y][x + 1].IsOpened = true; FieldOpened?.Invoke(x + 1, y, Fields[y][x + 1].Value); Fields[y + 1][x - 1].IsOpened = true; FieldOpened?.Invoke(x - 1, y + 1, Fields[y + 1][x - 1].Value); Fields[y + 1][x].IsOpened = true; FieldOpened?.Invoke(x, y + 1, Fields[y + 1][x].Value); Fields[y + 1][x + 1].IsOpened = true; FieldOpened?.Invoke(x + 1, y + 1, Fields[y + 1][x + 1].Value); zerosNow++; } } } } while (zerosNow != zerosBefore); }