| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Security.Cryptography; |
| | 4 | |
|
| | 5 | | namespace Minesweeper |
| | 6 | | { |
| | 7 | | public class MineField |
| | 8 | | { |
| 4 | 9 | | public MineField(int width, int height) |
| 4 | 10 | | { |
| 4 | 11 | | Width = width; |
| 4 | 12 | | Height = height; |
| | 13 | |
|
| 4 | 14 | | Cells = (from y in Enumerable.Range(0, Height) |
| 16 | 15 | | from x in Enumerable.Range(0, Width) |
| 40 | 16 | | select new Cell((x, y), NearCellGenerator(x, y))) |
| 4 | 17 | | .ToList(); |
| 4 | 18 | | } |
| | 19 | |
|
| | 20 | | public IList<Cell> Cells { get; set; } |
| | 21 | |
|
| | 22 | | public int Width { get; } |
| | 23 | | public int Height { get; } |
| | 24 | |
|
| | 25 | | public void GenerateBombs(int countBombs) |
| 1 | 26 | | { |
| 9 | 27 | | foreach (var idx in (from idx in RandomGenerator() |
| 4 | 28 | | select idx).Distinct().Take(countBombs)) |
| 3 | 29 | | { |
| 3 | 30 | | Cells[idx].SetBomb(); |
| 3 | 31 | | } |
| 1 | 32 | | } |
| | 33 | |
|
| | 34 | | public IEnumerable<int> RandomGenerator() |
| 1 | 35 | | { |
| 3 | 36 | | while (true) |
| 3 | 37 | | { |
| 3 | 38 | | yield return RandomNumberGenerator.GetInt32(Width * Height); |
| 2 | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public void CalculatedNearBombsCount() |
| 2 | 43 | | { |
| 12 | 44 | | foreach(var cell in from cell in Cells |
| 20 | 45 | | where cell.IsBomb |
| 2 | 46 | | select cell) |
| 3 | 47 | | { |
| 27 | 48 | | foreach(var nearcell in NearCellGenerator(cell)) |
| 9 | 49 | | { |
| 9 | 50 | | nearcell.NearBombsCount++; |
| 9 | 51 | | } |
| 3 | 52 | | } |
| 2 | 53 | | } |
| | 54 | |
|
| | 55 | | private IEnumerable<Cell> NearCellGenerator(int x, int y) |
| 39 | 56 | | { |
| 103 | 57 | | return InternalNearCellCenerator().Where(x => x is not null); |
| | 58 | |
|
| | 59 | | IEnumerable<Cell> InternalNearCellCenerator() |
| 8 | 60 | | { |
| 8 | 61 | | yield return GetCell(x - 1, y - 1); |
| 8 | 62 | | yield return GetCell(x, y - 1); |
| 8 | 63 | | yield return GetCell(x + 1, y - 1); |
| 8 | 64 | | yield return GetCell(x - 1, y); |
| 8 | 65 | | yield return GetCell(x + 1, y); |
| 8 | 66 | | yield return GetCell(x - 1, y + 1); |
| 8 | 67 | | yield return GetCell(x, y + 1); |
| 8 | 68 | | yield return GetCell(x + 1, y + 1); |
| 8 | 69 | | } |
| 39 | 70 | | } |
| | 71 | |
|
| | 72 | | private IEnumerable<Cell> NearCellGenerator(Cell cell) |
| 3 | 73 | | { |
| 3 | 74 | | var (x, y) = cell.XY; |
| | 75 | |
|
| 3 | 76 | | return NearCellGenerator(x, y); |
| 3 | 77 | | } |
| | 78 | |
|
| | 79 | | public void Click(int x, int y) |
| 1 | 80 | | { |
| 1 | 81 | | GetCell(x, y).Click(); |
| 1 | 82 | | } |
| 65 | 83 | | private Cell GetCell(int x, int y) => (x, y) switch |
| 65 | 84 | | { |
| 139 | 85 | | (var a, _) when a < 0 => null, |
| 133 | 86 | | (var a, _) when a >= Width => null, |
| 117 | 87 | | (_, var b) when b < 0 => null, |
| 108 | 88 | | (_, var b) when b >= Height => null, |
| 94 | 89 | | (var a, var b) => Cells[a + b * Width], |
| 65 | 90 | | }; |
| | 91 | | } |
| | 92 | | } |