| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | |
|
| | 4 | | namespace MineSweeper |
| | 5 | | { |
| | 6 | | public class Cell |
| | 7 | | { |
| 3 | 8 | | public Cell() : this(Enumerable.Empty<Cell>()) |
| 3 | 9 | | { |
| 3 | 10 | | } |
| | 11 | |
|
| 78 | 12 | | public Cell(IEnumerable<Cell> nearCellGenerator) => NearCellGenerator = nearCellGenerator; |
| | 13 | |
|
| | 14 | | public bool IsBomb { get; private set; } |
| | 15 | | public int NearBombsCount { get; set; } |
| 39 | 16 | | public bool IsCovered { get; private set; } = true; |
| | 17 | | public IEnumerable<Cell> NearCellGenerator { get; } |
| | 18 | |
|
| | 19 | | public void SetBomb() |
| 6 | 20 | | { |
| 6 | 21 | | IsBomb = true; |
| 23 | 22 | | NearCellGenerator.ForEach(x => x.NearBombsCount++); |
| 6 | 23 | | } |
| | 24 | |
|
| | 25 | | public void Click() |
| 22 | 26 | | { |
| 34 | 27 | | if (IsCovered is false) return; |
| 10 | 28 | | else IsCovered = false; |
| | 29 | |
|
| 14 | 30 | | if (NearBombsCount is not 0) return; |
| | 31 | |
|
| 25 | 32 | | NearCellGenerator.ForEach(x => x.Click()); |
| 22 | 33 | | } |
| | 34 | |
|
| 12 | 35 | | public override string ToString() => this switch |
| 12 | 36 | | { |
| 14 | 37 | | { IsCovered: true } => ".", |
| 13 | 38 | | { IsBomb: true } => "*", |
| 21 | 39 | | _ => NearBombsCount.ToString(), |
| 12 | 40 | | }; |
| | 41 | | } |
| | 42 | | } |