public void TestIsCellInRange() { Battlefield battlefield = Battlefield.Create(3); Cell cellInRange = new Cell(1, 1); Cell cellCoordinatesOverFieldSize = new Cell(4, 4); Cell cellNegativeCoordinates = new Cell(-1, -1); Assert.IsTrue(battlefield.IsCellInRange(cellInRange), "Cell should be in range"); Assert.IsFalse(battlefield.IsCellInRange(cellCoordinatesOverFieldSize), "Cell should not be in range"); Assert.IsFalse(battlefield.IsCellInRange(cellNegativeCoordinates), "Cell should not be in range"); }
/// <summary> /// Detonates a single cell on the battlefield /// </summary> /// <param name="cellToDetonate">An object containing the coordinates of the detonated mine</param> private void DetonateCell(Cell cellToDetonate) { if (this.IsCellInRange(cellToDetonate)) { this.field[cellToDetonate.Y, cellToDetonate.X] = 'X'; } }
/// <summary> /// Method that from a detonated cell returns the cells to explode after detonation /// </summary> /// <param name="cellToDetonate">A cell object representing a cell on the battlefield which should be detonated.</param> /// <returns>List of cells to explode</returns> private IEnumerable<Cell> GetCellsToExplode(Cell cellToDetonate) { IEnumerable<Cell> cellsToExplode; switch (this.field[cellToDetonate.Y, cellToDetonate.X] - '0') { case 1: cellsToExplode = PatternFactory.GenerateFirstDetonationPattern(cellToDetonate); break; case 2: cellsToExplode = PatternFactory.GenerateSecondDetonationPattern(cellToDetonate); break; case 3: cellsToExplode = PatternFactory.GenerateThirdDetonationPattern(cellToDetonate); break; case 4: cellsToExplode = PatternFactory.GenerateFourthDetonationPattern(cellToDetonate); break; case 5: cellsToExplode = PatternFactory.GenerateFifthDetonationPattern(cellToDetonate); break; default: throw new ArgumentException("Cell value is invalid."); } return cellsToExplode; }
/// <summary> /// Method that explodes cells from a given detonated cell /// </summary> /// <param name="detonatedCell">The cell that have been detonated</param> public void DetonateMine(Cell detonatedCell) { /* * We pass the detonated cell to the method cellsToExplode and get the * cells that have to explode */ IEnumerable<Cell> cellsToExplode = this.GetCellsToExplode(detonatedCell); foreach (var cell in cellsToExplode) { this.DetonateCell(cell); } this.DetonatedMines++; }
/// <summary> /// Checks if the battlefield contains a cell with certain coordinates. /// </summary> /// <param name="cell">An object containing coordinates</param> /// <returns>True or False</returns> public bool IsCellInRange(Cell cell) { bool isXCoordinateInRange = 0 <= cell.X && cell.X < this.FieldSize; bool isYCoordinateInRange = 0 <= cell.Y && cell.Y < this.FieldSize; return isXCoordinateInRange && isYCoordinateInRange; }
/// <summary> /// Checks if the Cell is a mine /// </summary> /// <param name="cell">An object containing coordinates</param> /// <returns>True or False</returns> public bool IsCellMine(Cell cell) { return '1' <= this.field[cell.Y, cell.X] && this.field[cell.Y, cell.X] <= '5'; }