/// <summary> /// Method which handles a shot being fired within a Game /// </summary> /// <param name="game"></param> /// <param name="shot"></param> /// <returns></returns> public Game FireShot(Game game, GridCoords shot) { var indices = ArrayMapper.GetArrayIndicesForCoords(shot); var target = game.GameGrid.Cells[indices.X, indices.Y]; switch (target.Status) { case GridCellStatus.OpenSea: game = ShotMissed(game, indices); break; case GridCellStatus.ShipIntact: game = ShotLanded(game, indices); break; case GridCellStatus.ShotLanded: AlreadyAttempted(game); break; case GridCellStatus.ShotMissed: AlreadyAttempted(game); break; } ; return(game); }
/// <summary> /// Calculates the max size boat that can be started vertically from this Grid Cell /// </summary> public void CalculateVerticalBoatCapacity(Grid grid) { // Get the array indices for the current Grid Cell var gridRef = new GridCoords(XRef, YRef); var indices = ArrayMapper.GetArrayIndicesForCoords(gridRef); var x = indices.X; var y = indices.Y; // Get the height of the grid var height = grid.Cells.GetLength(0); // Reset VerticalBoatCapacity = 0; // Calculate for (int i = x; i < height; i++) { if (grid.Cells[x, i].Status == GridCellStatus.OpenSea) { VerticalBoatCapacity++; } else { VerticalBoatCapacity = 0; break; } } }
public void GetArrayIndicesForCoords_WhenArgumentsAreValid_ExpectSuccessfulMapping() { // Arrange _gridCoordsArgument = new GridCoords(5, "D"); _arrayIndicesResponse = new Indices(4, 3); // Act var result = ArrayMapper.GetArrayIndicesForCoords(_gridCoordsArgument); // Assert Assert.AreEqual(this._arrayIndicesResponse.X, result.X); Assert.AreEqual(this._arrayIndicesResponse.Y, result.Y); }
public void GetArrayIndicesForCoords_WhenCoordsArgumentIsNull_ExpectArgumentNullException() { // Arrange _gridCoordsArgument = null; // Act try { var result = ArrayMapper.GetArrayIndicesForCoords(_gridCoordsArgument); } catch (Exception ex) { // Assert Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); throw; } }
public void GetArrayIndicesForCoords_WhenTheXCoordIsInvalid_ExpectArgumentException() { // Arrange _gridCoordsArgument = new GridCoords(-1, "E"); // Act try { var result = ArrayMapper.GetArrayIndicesForCoords(_gridCoordsArgument); } catch (Exception ex) { // Assert Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); throw; } }