public void UncoverCell(MineCell cell) { if (cell.HasFlag || cell.IsUncovered) { return; } cell.IsUncovered = true; if (cell.HasMine) { GameState = GameState.Defeat; _chrono.Stop(); } else { if (cell.NeighbourhoodMineCount == 0) { UncoverNeighbours(cell); } if (Cells.All(c => c.HasMine ^ c.IsUncovered)) { GameState = GameState.Victory; _chrono.Stop(); } } }
private bool PlayEasyMoves(MineCell cell, bool returnOnFirstAction) { var result = false; var neighbours = _mineField.GetNeighbours(cell).ToArray(); var flaggedNeighbours = neighbours.FilterByVisibleState(MineState.Flagged).ToArray(); var coveredNeighbours = neighbours.FilterByVisibleState(MineState.Covered).ToArray(); if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == (flaggedNeighbours.Length + coveredNeighbours.Length)) { for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++) { coveredNeighbours[i].HasFlag = true; result = true; } } else if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == flaggedNeighbours.Length) { for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++) { _mineField.UncoverCell(coveredNeighbours[i]); result = true; } } return result; }
private bool PlayEasyMoves(MineCell cell, bool returnOnFirstAction) { var result = false; var neighbours = _mineField.GetNeighbours(cell).ToArray(); var flaggedNeighbours = neighbours.FilterByVisibleState(MineState.Flagged).ToArray(); var coveredNeighbours = neighbours.FilterByVisibleState(MineState.Covered).ToArray(); if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == (flaggedNeighbours.Length + coveredNeighbours.Length)) { for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++) { coveredNeighbours[i].HasFlag = true; result = true; } } else if (coveredNeighbours.Length > 0 && cell.NeighbourhoodMineCount == flaggedNeighbours.Length) { for (var i = 0; i < coveredNeighbours.Length && !(result && returnOnFirstAction); i++) { _mineField.UncoverCell(coveredNeighbours[i]); result = true; } } return(result); }
public MineFieldCell(int row, int col, MineCell mineCell, Func<MineFieldCell, Task> leftClick, Func<MineFieldCell, Task> rightClick) : this() { Row = row; Col = col; MineCell = mineCell; _previousState = MineState.Covered; _leftClick = leftClick; _rightClick = rightClick; }
public MineFieldCell(int row, int col, MineCell mineCell, Func <MineFieldCell, Task> leftClick, Func <MineFieldCell, Task> rightClick) : this() { Row = row; Col = col; MineCell = mineCell; _previousState = MineState.Covered; _leftClick = leftClick; _rightClick = rightClick; }
public void UncoverNeighbours(MineCell cell) { var neighbours = GetNeighbours(cell); if (!cell.HasFlag && neighbours.Count(c => c.HasFlag) == cell.NeighbourhoodMineCount) { foreach (var neighbour in GetNeighbours(cell)) { UncoverCell(neighbour); } } }
private bool PlayHardMoves(MineCell[] uncoveredCells, bool returnOnFirstAction) { var minedZones = GetMinedZones(uncoveredCells); var hardMoves = from zone in minedZones from other in minedZones where zone.Intersect(other) && PlayHardMoves(zone, other, returnOnFirstAction) select 1; return returnOnFirstAction ? hardMoves.Any() : hardMoves.Count() > 0; }
public MineField(int rows, int cols, int mineCount) { Rows = rows; Cols = cols; _mineCount = mineCount; GameState = GameState.NotStarted; Cells = new MineCell[rows * cols]; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { Cells[GetCellIndex(row, col)] = new MineCell(row, col); } } }
private ICollection<MinedZone> GetMinedZones(MineCell[] uncoveredCells) { var minedZonesById = new Dictionary<string, MinedZone>(); foreach (var cell in uncoveredCells) { var neighbours = _mineField.GetNeighbours(cell).ToArray(); var flaggedNeighbours = neighbours.FilterByVisibleState(MineState.Flagged).ToArray(); var coveredNeighbours = neighbours.FilterByVisibleState(MineState.Covered).ToArray(); var remainingCount = cell.NeighbourhoodMineCount - flaggedNeighbours.Length; if (remainingCount > 0 && remainingCount < coveredNeighbours.Length) { var zone = new MinedZone(remainingCount, coveredNeighbours); minedZonesById[zone.Id] = zone; } } return minedZonesById.Values; }
public IEnumerable<MineCell> GetNeighbours(MineCell cell) => GetNeighbours(cell.Row, cell.Col);
public MinedZone(int mineCount, MineCell[] cells) { MineCount = mineCount; _cells = cells; Id = string.Join("|", _cells.Select(c => $"{c.Row},{c.Col}").OrderBy(c => c)); }
public IEnumerable <MineCell> GetNeighbours(MineCell cell) => GetNeighbours(cell.Row, cell.Col);