| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Runtime.CompilerServices; |
| | 4 | | using static MineSweeper.MineFieldHelper; |
| | 5 | |
|
| | 6 | | [assembly: InternalsVisibleTo("MineSweeper.Tests")] |
| | 7 | |
|
| | 8 | | namespace MineSweeper |
| | 9 | | { |
| | 10 | | public class MineField |
| | 11 | | { |
| | 12 | | public MineField(int width, int height, int bombsCount = 0) : |
| 2 | 13 | | this(width, height, RandomIndexGenerator(width, height).Distinct() |
| 2 | 14 | | .Take(bombsCount)) |
| 4 | 15 | | { } |
| | 16 | |
|
| 4 | 17 | | public MineField(int width, int height, IEnumerable<(int X, int Y)> bombPosGenerator) |
| 4 | 18 | | { |
| 4 | 19 | | Width = width; |
| 4 | 20 | | Height = height; |
| | 21 | |
|
| 4 | 22 | | Cells = (from x in Enumerable.Range(0, width) |
| 16 | 23 | | from y in Enumerable.Range(0, height) |
| 40 | 24 | | select new Cell(NearCellGenerator(x, y, GetCell))) |
| 4 | 25 | | .ToList(); |
| | 26 | |
|
| 9 | 27 | | bombPosGenerator.ForEach(x => GetCell(x)?.SetBomb()); |
| 4 | 28 | | } |
| | 29 | |
|
| | 30 | | internal IList<Cell> Cells { get; } |
| | 31 | | public int Width { get; } |
| | 32 | | public int Height { get; } |
| | 33 | |
|
| 86 | 34 | | public Cell GetCell((int X, int Y) pos) => pos switch |
| 86 | 35 | | { |
| 196 | 36 | | (var x, _) when x < 0 || x >= Width => null, |
| 168 | 37 | | (_, var y) when y < 0 || y >= Height => null, |
| 128 | 38 | | _ => Cells[(pos.Y * Width) + pos.X] |
| 86 | 39 | | }; |
| | 40 | |
|
| | 41 | | public void Click(int x, int y) => |
| 1 | 42 | | GetCell((x, y))?.Click(); |
| | 43 | | } |
| | 44 | | } |