public void ShootAutomatically_ShouldUseTheShootingStrategyToShoot() { //Arrange _computerPlayer.ReloadBombs(); var playerBuilder = new PlayerBuilder(); IPlayer opponent = playerBuilder.Build(); GridCoordinate determinedCoordinate = new GridCoordinateBuilder().Build(); _shootingStrategyMock.Setup(ss => ss.DetermineTargetCoordinate()).Returns(determinedCoordinate); //Act _computerPlayer.ShootAutomatically(opponent); //Assert _shootingStrategyMock.Verify(ss => ss.DetermineTargetCoordinate(), "Use the DetermineTargetCoordinate method of the shooting strategy."); var opponentGridMock = playerBuilder.GridMock; opponentGridMock.Verify(g => g.Shoot(determinedCoordinate), Times.AtLeastOnce, "Use the ShootAt method to shoot at the opponent on the coordinate determined by the strategy."); _shootingStrategyMock.Verify(ss => ss.RegisterShotResult(It.IsAny <GridCoordinate>(), It.IsAny <ShotResult>()), "After shooting, the result should be registered with the shooting strategy."); }
public void ShootAt_ShouldReturnHitWhenAShipIsHit() { //Arrange var playerBuilder = new PlayerBuilder(); playerBuilder.GridMock.Setup(g => g.Shoot(It.IsAny <GridCoordinate>())).Returns((GridCoordinate c) => new GridSquare(c) { Status = GridSquareStatus.Hit }); var kind = ShipKind.All.NextRandomElement(); IShip hitShip = new ShipBuilder(kind).Build(); playerBuilder.FleetMock.Setup(f => f.FindShipAtCoordinate(It.IsAny <GridCoordinate>())).Returns(hitShip); IPlayer opponent = playerBuilder.Build(); GridCoordinate coordinate = new GridCoordinateBuilder().Build(); _humanPlayer.ReloadBombs(); //Act var result = _humanPlayer.ShootAt(opponent, coordinate); //Assert playerBuilder.GridMock.Verify(g => g.Shoot(coordinate), Times.Once, "The Shoot method of the opponent grid should have been called."); Assert.That(result.ShotFired, Is.True, "The result should indicate that the shot was fired."); Assert.That(result.Hit, Is.True, "The result should indicate that it was a hit."); playerBuilder.FleetMock.Verify(f => f.FindShipAtCoordinate(coordinate), Times.Once, "Use the FindShipAtCoordinate of the opponent fleet to determine which kind of ship was hit."); }
public void DetermineTargetCoordinate_ShouldShootANeighborOfAHitSquare() { for (int numberOfChecks = 0; numberOfChecks < 5; numberOfChecks++) { //Arrange GridCoordinate hitCoordinate = new GridCoordinateBuilder(_grid.Size).Build(); _gridBuilder.WithSquareStatus(hitCoordinate, GridSquareStatus.Hit); var ship = new ShipBuilder(ShipKind.Carrier).Build(); _strategy.RegisterShotResult(hitCoordinate, ShotResult.CreateHit(ship, true)); IList <GridCoordinate> expectedCoordinates = new List <GridCoordinate>(); foreach (Direction direction in Direction.BasicDirections) { GridCoordinate neighbor = hitCoordinate.GetNeighbor(direction); if (!neighbor.IsOutOfBounds(_grid.Size)) { expectedCoordinates.Add(neighbor); } } //Act GridCoordinate result = _strategy.DetermineTargetCoordinate(); Assert.That(expectedCoordinates.Contains(result), Is.True, $"Bij een leeg grid met een raak shot op {hitCoordinate} " + $"moet er geschoten worden op ייn van de volgende coordinaten: {expectedCoordinates.ToArray().Print()}. " + $"De strategie kiest soms echter {result}"); Setup(); } }
public void TryMoveShipTo_ShouldFailWhenASegmentCoordinateIsOutsideTheGrid() { AssertContains5Ships(); //Arrange IShip shipToMove = _internalDictionary.Values.NextRandomElement(); GridCoordinate[] targetCoordinates = new GridCoordinate[shipToMove.Kind.Size]; for (int i = 0; i < shipToMove.Kind.Size; i++) { targetCoordinates[i] = new GridCoordinateBuilder().Build(); } int invalidCoordinateIndex = RandomGenerator.Next(0, shipToMove.Kind.Size); int gridSize = _gridMock.Object.Size; int invalidRow = RandomGenerator.Next(gridSize, gridSize + 1); int invalidColumn = RandomGenerator.Next(-2, 0); targetCoordinates[invalidCoordinateIndex] = new GridCoordinate(invalidRow, invalidColumn); //Act Result result = _fleet.TryMoveShipTo(shipToMove.Kind, targetCoordinates, _gridMock.Object); //Assert Assert.That(result.IsFailure, Is.True); }
public void GetNeighbor_ShouldReturnAdjacentCoordinateInTheGivenDirection() { GridCoordinate coordinate = new GridCoordinateBuilder().Build(); foreach (Direction direction in Direction.AllDirections) { AssertGettingNeighbor(coordinate, direction); } }
public void GetOtherEnd_ShouldReturnTheCoordinateSomeDistanceInAGivenDirection() { GridCoordinate coordinate = new GridCoordinateBuilder().Build(); foreach (Direction direction in Direction.AllDirections) { int distance = RandomGenerator.Next(2, 6); AssertGettingOtherEnd(coordinate, direction, distance); } }
public void CanBeFoundAtCoordinate_ShouldReturnFalseWhenNoSquaresAreSet() { //Arrange GridCoordinate someCoordinate = new GridCoordinateBuilder().Build(); //Act bool result = _ship.CanBeFoundAtCoordinate(someCoordinate); //Assert Assert.That(result, Is.False); }
public void Constructor_ShouldSetDefaults() { //Arrange GridCoordinate coordinate = new GridCoordinateBuilder().Build(); //Act GridSquare square = new GridSquare(coordinate); //Assert Assert.That(square.Coordinate, Is.SameAs(coordinate)); Assert.That(square.NumberOfBombs, Is.Zero); Assert.That(square.Status, Is.EqualTo(GridSquareStatus.Untouched)); }
public void GetSquareAt_ShouldReturnCorrectSquare() { //Arrange int gridSize = RandomGenerator.Next(10, 16); Grid grid = new Grid(gridSize); GridCoordinate coordinate = new GridCoordinateBuilder(gridSize).Build(); //Act IGridSquare square = grid.GetSquareAt(coordinate); //Assert Assert.That(square, Is.Not.Null); Assert.That(square.Coordinate, Is.EqualTo(coordinate)); }
public void ShootAtOpponent_ShouldReturnAMisfireWhenTheGameIsNotStartedYet() { //Arrange GridCoordinate targetCoordinate = new GridCoordinateBuilder().Build(); _player1Builder.WithBombsLoaded(true); Mock <IPlayer> player1Mock = _player1Builder.BuildMock(); //Act ShotResult result = _game.ShootAtOpponent(_player2.Id, targetCoordinate); player1Mock.Verify(p => p.ShootAt(It.IsAny <IPlayer>(), It.IsAny <GridCoordinate>()), Times.Never, "The ShootAt method of the player that shoots should not be called when the game has not started yet."); Assert.That(result.ShotFired, Is.False, "The ShotResult should indicate that no shot is fired."); Assert.That(result.MisfireReason, Is.Not.Empty, "The ShotResult should contain a misfire reason."); }
public void DetermineTargetCoordinate_ShouldOnlyPickCoordinatesThatAreNotHitByABomb() { //Arrange _gridBuilder.WithAllSquaresWithStatus(GridSquareStatus.Miss); //Set some squares to Hit for (int i = 0; i < 10; i++) { var coordinate = new GridCoordinateBuilder(_grid.Size).Build(); _gridBuilder.WithSquareStatus(coordinate, GridSquareStatus.Hit); } //Set some squares to Untouched var untouchedCoordinates = new List <GridCoordinate>(); for (int i = 0; i < RandomGenerator.Next(2, _grid.Size + 1); i++) { int column = RandomGenerator.Next(0, _grid.Size - 1); var coordinate1 = new GridCoordinate(i, column); untouchedCoordinates.Add(coordinate1); _gridBuilder.WithSquareStatus(coordinate1, GridSquareStatus.Untouched); var coordinate2 = new GridCoordinate(i, column + 1); untouchedCoordinates.Add(coordinate2); _gridBuilder.WithSquareStatus(coordinate2, GridSquareStatus.Untouched); } //Act var determinedCoordinates = new List <GridCoordinate>(); int numberOfDeterminations = untouchedCoordinates.Count / 2; for (int i = 0; i < numberOfDeterminations; i++) { var coordinate = _strategy.DetermineTargetCoordinate(); determinedCoordinates.Add(coordinate); _gridBuilder.WithSquareStatus(coordinate, GridSquareStatus.Miss); } //Assert string invalidDeterminationMessage = $"When a grid has only {untouchedCoordinates.Count} untouched squares " + $"and DetermineTargetCoordinate is called {numberOfDeterminations} times, " + $"then {numberOfDeterminations} of the untouched squares should have been picked " + "(this test marks a square as Missed after it is picked)."; Assert.That(determinedCoordinates.All(c => untouchedCoordinates.Contains(c)), Is.True, invalidDeterminationMessage); }
public void ShootAt_ShouldResultInHasBombsLoadedBeingFalse() { //Arrange var playerBuilder = new PlayerBuilder(); IPlayer opponent = playerBuilder.Build(); GridCoordinate coordinate = new GridCoordinateBuilder().Build(); _humanPlayer.ReloadBombs(); //Act _humanPlayer.ShootAt(opponent, coordinate); //Assert playerBuilder.GridMock.Verify(g => g.Shoot(coordinate), Times.Once, "The Shoot method of the opponent grid should have been called."); Assert.That(_humanPlayer.HasBombsLoaded, Is.False); }
public void Shoot_ShouldHitTheMatchingSquareWithABombAndReturnIt() { //Arrange int gridSize = RandomGenerator.Next(10, 16); Grid grid = new Grid(gridSize); GridCoordinate coordinate = new GridCoordinateBuilder(gridSize).Build(); //Act IGridSquare square = grid.Shoot(coordinate); //Assert Assert.That(square, Is.Not.Null); Assert.That(square.Coordinate, Is.EqualTo(coordinate), "The wrong square is shot."); Assert.That(square.Status, Is.Not.EqualTo(GridSquareStatus.Untouched), "After hitting a square, its status should not be Untouched. " + "Use the HitByBomb method of the square to achieve this."); }
public void EXTRA_HitByBomb_ShouldIncreaseNumberOfBombsOnEachHit() { //Arrange GridCoordinate coordinate = new GridCoordinateBuilder().Build(); GridSquare square = new GridSquare(coordinate); //Act int numberOfHits = RandomGenerator.Next(4, 11); for (int i = 0; i < numberOfHits; i++) { square.HitByBomb(); } //Assert Assert.That(square.NumberOfBombs, Is.EqualTo(numberOfHits), $"The number of bombs after one hit should be {numberOfHits} after calling HitByBomb {numberOfHits} times."); }
public void ShootAt_ShouldReturnMissedWhenTheShotIsAMiss() { //Arrange var playerBuilder = new PlayerBuilder(); IPlayer opponent = playerBuilder.Build(); GridCoordinate coordinate = new GridCoordinateBuilder().Build(); _humanPlayer.ReloadBombs(); //Act var result = _humanPlayer.ShootAt(opponent, coordinate); //Assert playerBuilder.GridMock.Verify(g => g.Shoot(coordinate), Times.Once, "The Shoot method of the opponent grid should have been called."); Assert.That(result.ShotFired, Is.True, "The result should indicate that the shot was fired."); Assert.That(result.Hit, Is.False, "The result should indicate that it was not a hit."); }
public void GetSquareAt_ShouldReturnCorrectSquare() { //Arrange int gridSize = RandomGenerator.Next(10, 16); Grid grid = new Grid(gridSize); AssertSquaresAreInitialized(grid, gridSize); GridCoordinate coordinate = new GridCoordinateBuilder(gridSize).Build(); //Act IGridSquare square = grid.GetSquareAt(coordinate); //Assert Assert.That(square, Is.Not.Null); Assert.That(square.Coordinate, Is.EqualTo(coordinate)); Assert.That(grid.Squares, Has.One.SameAs(square), "The IGridSquare returned is not one of the squares of the grid. Did you perhaps returned a new instance of GridSquare?"); }
public void ShootAtOpponent_ShouldLetTheComputerShootWhenAllBombsAreShot() { AssertThatGameIsConstructed(); //Arrange var shootingStrategyMock = new Mock <IShootingStrategy>(); IPlayer computerPlayer = new ComputerPlayer(_settings, shootingStrategyMock.Object); _player2Builder.WithBombsLoaded(true); IPlayer humanPlayer = _player2; _game = new Game(_settings, computerPlayer, humanPlayer); _game.Start(); GridCoordinate targetCoordinate = new GridCoordinateBuilder().Build(); Mock <IPlayer> humanPlayerMock = _player2Builder.BuildMock(); ShotResult expectedShotResult = ShotResult.CreateMissed(); humanPlayerMock.Setup(p => p.ShootAt(It.IsAny <IPlayer>(), It.IsAny <GridCoordinate>())) .Returns(() => { _player2Builder.WithBombsLoaded(false); return(expectedShotResult); }); //Act ShotResult result = _game.ShootAtOpponent(_player2.Id, targetCoordinate); humanPlayerMock.Verify(p => p.ShootAt(computerPlayer, targetCoordinate), Times.Once, "The ShootAt method of the human player is not called correctly."); shootingStrategyMock.Verify(s => s.DetermineTargetCoordinate(), Times.AtLeast(1), "The computer player did not shoot. You must call the ShootAutomatically method of the computer player. " + "The ShootAutomatically method should in turn call the DetermineTargetCoordinate method of the shooting strategy of the computer. " + "This test fails because no call to the DetermineTargetCoordinate method is detected."); humanPlayerMock.Verify(p => p.ReloadBombs(), Times.Once, "The ReloadBombs method of the human player should be called after the computer is done with shooting."); Assert.That(result, Is.SameAs(expectedShotResult), "The ShotResult returned by the ShootAt method (of the human player) should be returned."); }
public void HitByBomb_ShouldRegisterTheHitAsAMissAndInvokeTheOnHitByBombEvent() { //Arrange GridCoordinate coordinate = new GridCoordinateBuilder().Build(); GridSquare square = new GridSquare(coordinate); square.OnHitByBomb += Square_OnHitByBomb; //Act square.HitByBomb(); //Assert Assert.That(square.NumberOfBombs, Is.EqualTo(1), "The number of bombs after one hit should be 1."); Assert.That(square.Status, Is.EqualTo(GridSquareStatus.Miss), "When a square is hit by a bomb it gets the Status Miss " + "(This Status can change after invoking the OnHitByBomb event)."); Assert.That(_onHitByBombEventTriggered, Is.True, "The OnHitByBomb event is not invoked. " + "You can invoke the event with this statement: 'OnHitByBomb?.Invoke(this);'."); }
public void TryMoveShipTo_ShouldFailWhenSegmentCoordinatesDoNotMatchLengthOfTheShip() { AssertContains5Ships(); //Arrange IShip shipToMove = _internalDictionary.Values.NextRandomElement(); int invalidSegmentSize = Math.Max(2, (shipToMove.Kind.Size + 1) % 6); GridCoordinate[] targetCoordinates = new GridCoordinate[invalidSegmentSize]; for (int i = 0; i < invalidSegmentSize; i++) { targetCoordinates[i] = new GridCoordinateBuilder().Build(); } //Act Result result = _fleet.TryMoveShipTo(shipToMove.Kind, targetCoordinates, _gridMock.Object); //Assert Assert.That(result.IsFailure, Is.True); }
public void FindShipAtCoordinate_ShouldReturnTheShipThatOccupiesTheCoordinate() { AssertContains5Ships(); //Arrange Mock <IShip>[] shipMocks = ReplaceShipsWithMocks(); Mock <IShip> matchingShipMock = shipMocks.NextRandomElement(); matchingShipMock.Setup(s => s.CanBeFoundAtCoordinate(It.IsAny <GridCoordinate>())).Returns(true); IShip matchingShip = matchingShipMock.Object; GridCoordinate coordinate = new GridCoordinateBuilder().Build(); //Act IShip result = _fleet.FindShipAtCoordinate(coordinate); //Act + Assert matchingShipMock.Verify(s => s.CanBeFoundAtCoordinate(coordinate), Times.AtLeastOnce, "Use the CanBeFoundAtCoordinate of the Ship class to determine if a ship occupies the coordinate"); Assert.That(result, Is.SameAs(matchingShip)); }
public void ShootAtOpponent_ShouldUseTheShootAtMethodOfThePlayer() { //Arrange GridCoordinate targetCoordinate = new GridCoordinateBuilder().Build(); _player1Builder.WithBombsLoaded(true); ShotResult expectedShotResult = ShotResult.CreateMissed(); Mock <IPlayer> player1Mock = _player1Builder.BuildMock(); player1Mock.Setup(p => p.ShootAt(It.IsAny <IPlayer>(), It.IsAny <GridCoordinate>())) .Returns(expectedShotResult); _game.Start(); //Act ShotResult result = _game.ShootAtOpponent(_player1.Id, targetCoordinate); player1Mock.Verify(p => p.ShootAt(_player2, targetCoordinate), Times.Once, "The ShootAt method of the player that shoots is not called correctly."); Assert.That(result, Is.SameAs(expectedShotResult), "The ShotResult returned by the ShootAt method should be returned."); }
public void FindShipAtCoordinate_ShouldReturnNullWhenNoShipOccupiesTheCoordinate() { AssertContains5Ships(); //Arrange Mock <IShip>[] shipMocks = ReplaceShipsWithMocks(); GridCoordinate coordinate = new GridCoordinateBuilder().Build(); //Act IShip result = _fleet.FindShipAtCoordinate(coordinate); //Act + Assert Assert.That(result, Is.Null); foreach (Mock <IShip> shipMock in shipMocks) { IShip ship = shipMock.Object; shipMock.Verify(s => s.CanBeFoundAtCoordinate(coordinate), Times.Once, $"The CanBeFoundAtCoordinate of the Ship is not called for the '{ship.Kind.Name}'. " + $"Each ship must be checked."); } }
public void Shoot_ShouldHitTheMatchingSquareWithABombAndReturnIt() { //Arrange int gridSize = RandomGenerator.Next(10, 16); Grid grid = new Grid(gridSize); AssertSquaresAreInitialized(grid, gridSize); GridCoordinate coordinate = new GridCoordinateBuilder(gridSize).Build(); //Act IGridSquare square = grid.Shoot(coordinate); //Assert Assert.That(square, Is.Not.Null); Assert.That(square.Coordinate, Is.EqualTo(coordinate), "The wrong square is shot."); Assert.That(square.Status, Is.Not.EqualTo(GridSquareStatus.Untouched), "After hitting a square, its status should not be Untouched. " + "Use the HitByBomb method of the square to achieve this."); Assert.That(grid.Squares, Has.One.SameAs(square), "The IGridSquare returned is not one of the squares of the grid. Did you perhaps returned a new instance of GridSquare?"); }
public void CanBeFoundAtCoordinate_ShouldReturnFalseWhenNoneOfTheSegmentSquaresMatch() { //Arrange IGridSquare[] squares = new GridSquareArrayBuilder(_kind).BuildArray(); _ship.PositionOnGrid(squares); GridCoordinate noneShipCoordinate = new GridCoordinateBuilder().Build(); while (squares.Any(s => s.Coordinate == noneShipCoordinate)) { noneShipCoordinate = new GridCoordinateBuilder().Build(); } //Act bool result = _ship.CanBeFoundAtCoordinate(noneShipCoordinate); //Assert Assert.That(result, Is.False, () => { string segmentCoordinates = squares.Select(s => s.Coordinate).ToArray().Print(); return($"Expected ship with segment coordinates {segmentCoordinates} not to be found at {noneShipCoordinate}"); }); }