| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | namespace Minesweeper |
| | | 6 | | { |
| | | 7 | | public class Cell |
| | | 8 | | { |
| | 3 | 9 | | public Cell() : this((0,0), Enumerable.Empty<Cell>()) |
| | 3 | 10 | | { |
| | | 11 | | |
| | 3 | 12 | | } |
| | | 13 | | |
| | 39 | 14 | | public Cell((int x, int y) p, IEnumerable<Cell> enumerable) |
| | 39 | 15 | | { |
| | 39 | 16 | | XY = p; |
| | 39 | 17 | | NearCellGenerator = enumerable; |
| | 39 | 18 | | } |
| | | 19 | | |
| | | 20 | | public bool IsBomb { get; set; } |
| | | 21 | | public int NearBombsCount { get; set; } |
| | 39 | 22 | | public bool IsCover { get; private set; } = true; |
| | | 23 | | public (int x, int y) XY { get; } |
| | | 24 | | public IEnumerable<Cell> NearCellGenerator { get; } |
| | | 25 | | |
| | | 26 | | public void SetBomb() |
| | 7 | 27 | | { |
| | 7 | 28 | | IsBomb = true; |
| | 7 | 29 | | } |
| | | 30 | | |
| | | 31 | | public void Click() |
| | 22 | 32 | | { |
| | 34 | 33 | | if (IsCover is not true) return; |
| | | 34 | | |
| | 10 | 35 | | IsCover = false; |
| | | 36 | | |
| | 10 | 37 | | if (NearBombsCount == 0) |
| | 7 | 38 | | { |
| | 59 | 39 | | foreach (var nearcell in NearCellGenerator) |
| | 19 | 40 | | { |
| | 19 | 41 | | nearcell.Click(); |
| | 19 | 42 | | } |
| | 7 | 43 | | } |
| | 22 | 44 | | } |
| | | 45 | | |
| | 12 | 46 | | public override string ToString() => (IsCover, IsBomb, NearBombsCount) switch |
| | 12 | 47 | | { |
| | 14 | 48 | | (true, _, _) => ".", |
| | 13 | 49 | | (false, true, _) => "*", |
| | 21 | 50 | | (false, false, var m) => m.ToString() |
| | 12 | 51 | | }; |
| | | 52 | | } |
| | | 53 | | } |