/// <summary> /// Recursively opens all adjacent cells of a cell which has no neighbors with mines. /// </summary> /// <param name="cellPos">The current cell.</param> private void OpenEmptyCellsRecursive(ICellPosition cellPos) { // All neighbors must not have mines. Debug.Assert(this.AllNeighborMines[cellPos.Row, cellPos.Col] == 0, "All neighbors must not have mines!"); for (int row = -1; row < 2; row++) { for (int col = -1; col < 2; col++) { if (col == 0 && row == 0) { continue; } if (this.IsInsideMatrix(cellPos.Row + row, cellPos.Col + col)) { CellPos neighborCellPos = new CellPos(cellPos.Row + row, cellPos.Col + col); int currentIndex = this.GetIndex(neighborCellPos); if (this.Cells[currentIndex].IsOpened) { continue; } this.Cells[currentIndex].OpenCell(); this.OpenedCellsCount += 1; if (this.AllNeighborMines[neighborCellPos.Row, neighborCellPos.Col] == 0) { this.OpenEmptyCellsRecursive(neighborCellPos); } } } } }
/// <summary> /// Handles cell opening (recursively). Reveals cell's content and returns it's state. /// </summary> /// <param name="cellPosition">Cell's position in the minefield matrix.</param> /// <returns>State of the minefield.</returns> protected override bool OpenCell(ICellPosition cellPosition) { bool steppedOnAMine = base.OpenCell(cellPosition); if (!steppedOnAMine) { if (this.AllNeighborMines[cellPosition.Row, cellPosition.Col] == 0) { this.OpenEmptyCellsRecursive(cellPosition); } } return steppedOnAMine; }
/// <summary> /// Converts (row,col) coordinates to index in the cell list. /// </summary> /// <param name="cell">The cell position.</param> /// <returns>The index in the cell list.</returns> protected int GetIndex(ICellPosition cell) { int index = (cell.Row * this.ColumnsCount) + cell.Col; return(index); }
/// <summary> /// Flags the cell on the given coordinates. /// </summary> /// <param name="cell">The position of the cell.</param> public void FlagCell(ICellPosition cell) { var result = this.minefield.FlagCellHandler(cell); this.HandleCellInteractionResult(result); }
/// <summary> /// Handles cell flagging. /// </summary> /// <param name="cellPosition">Cell's position in the minefield matrix.</param> /// <returns>State of the minefield.</returns> public CellActionResult FlagCellHandler(ICellPosition cellPosition) { return(this.CellInteractionHandler(cellPosition, this.FlagCell)); }
public FakeCell(ICellPosition position) { CellPosition = position; }
public Live(ICell[,] grid, ICellPosition position) { this.Grid = grid; this.Position = position; }
public IObjectSize Subtract(ICellPosition other) { return(new ObjectSize(ColumnIndex - other.ColumnIndex, RowIndex - other.RowIndex)); }
public Queue <IInstruction> CreateInstructions(string instructionString, ICell[,] grid, ICellPosition position) { Queue <IInstruction> result = new Queue <IInstruction>(); IInstruction tempInstruction = null; var set = instructionString.Split(' '); foreach (string instruction in set) { switch (instruction) { case "x": tempInstruction = new Live(grid, position); break; case "up": tempInstruction = new Up(grid, position); break; case "down": tempInstruction = new Down(grid, position); break; default: case "left": tempInstruction = new Left(grid, position); break; case "right": tempInstruction = new Right(grid, position); break; } result.Enqueue(tempInstruction); } return(result); }
/// <summary> /// Draws game minefield on screen via given IRenderer. /// </summary> /// <param name="minefield">Image of the minefield represented by two dimensional array of CellImage enumeration.</param> /// <param name="neighborMines">Two dimensional array of numbers representing neighbor mines for each cell.</param> /// <param name="topLeft">Top left coordinates of the board.</param> public void DrawGameField(CellImage[,] minefield, int[,] neighborMines, ICellPosition topLeft) { for (int row = 0; row < minefield.GetLength(0); row++) { for (int col = 0; col < minefield.GetLength(1); col++) { int rowOnScreen = topLeft.Row + BoardOffsetByRow + row; int colOnScreen = topLeft.Col + BoardOffsetByColumn + (col * CellSpaceOnScreen); string symbol; var symbolType = minefield[row, col]; if (symbolType == CellImage.Num) { int num = neighborMines[row, col]; symbol = (num == 0) ? EmptyCell : num.ToString(); } else { symbol = this.symbols[symbolType]; } this.DrawCell(rowOnScreen, colOnScreen, symbol); } } }
public ICell InsertCell(ICellPosition position) { return(new ExcelCell(internalTable.InsertCell(new ExcelCellIndex(position.CellReference)))); }
public ICell GetCell(ICellPosition position) { var internalCell = internalTable.GetCell(new ExcelCellIndex(position.CellReference)); return(internalCell == null ? null : new ExcelCell(internalCell)); }