private bool isPossibleShip(GridCoordinate coordinate) { int numberUntouched = 0; foreach (Direction direction in this.possibleDirections) { int numberUntouchedDirection = 1; GridCoordinate nextCoordinate = coordinate.GetNeighbor(direction); while (!nextCoordinate.IsOutOfBounds(this.opponentGrid.Size)) { if (opponentGrid.GetSquareAt(nextCoordinate).Status == GridSquareStatus.Untouched) { numberUntouchedDirection++; nextCoordinate = nextCoordinate.GetNeighbor(direction); } else { break; } } nextCoordinate = coordinate.GetNeighbor(direction.Opposite); while (!nextCoordinate.IsOutOfBounds(this.opponentGrid.Size)) { if (opponentGrid.GetSquareAt(nextCoordinate).Status == GridSquareStatus.Untouched) { numberUntouchedDirection++; nextCoordinate = nextCoordinate.GetNeighbor(direction.Opposite); } else { break; } } if (numberUntouchedDirection > numberUntouched) { numberUntouched = numberUntouchedDirection; } } if (numberUntouched < this.GetSmallestShipSize()) { return(false); } return(true); }
private void AssertGettingNeighbor(GridCoordinate coordinate, Direction direction) { //Act GridCoordinate neighbor = coordinate.GetNeighbor(direction); //Assert int expectedRow = coordinate.Row + direction.YStep; int expectedColumn = coordinate.Column + direction.XStep; Assert.That(neighbor.Row, Is.EqualTo(expectedRow), $"The row of the neighbor of {coordinate} in direction '{direction}' should be {expectedRow}"); Assert.That(neighbor.Column, Is.EqualTo(expectedColumn), $"The column of the neighbor of {coordinate} in direction '{direction}' should be {expectedColumn}"); }
private List <GridCoordinate> GetNeighbours(GridCoordinate coordinate, Direction[] possibleDirections, GridSquareStatus withStatus) { List <GridCoordinate> result = new List <GridCoordinate>(); foreach (Direction direction in possibleDirections) { GridCoordinate otherCoordinate = coordinate.GetNeighbor(direction); if (!otherCoordinate.IsOutOfBounds(this.opponentGrid.Size)) { IGridSquare otherSquare = this.opponentGrid.GetSquareAt(otherCoordinate); if (otherSquare.Status == withStatus) { result.Add(otherCoordinate); } } } return(result); }
public void DetermineTargetCoordinate_ShouldShootInTheRightDirectionIfTwoSquaresAreHit() { for (int numberOfChecks = 0; numberOfChecks < 10; numberOfChecks++) { //Arrange GridCoordinate hit1 = new GridCoordinate(RandomGenerator.Next(2, _grid.Size - 1), RandomGenerator.Next(2, _grid.Size - 1)); int horizontalStep = RandomGenerator.Next(0, 2); int verticalStep = horizontalStep == 0 ? 1 : 0; GridCoordinate hit2 = new GridCoordinate(hit1.Row + verticalStep, hit1.Column + horizontalStep); var ship = new ShipBuilder(ShipKind.Carrier).Build(); _gridBuilder.WithSquareStatus(hit1, GridSquareStatus.Hit); _strategy.RegisterShotResult(hit1, ShotResult.CreateHit(ship, true)); _gridBuilder.WithSquareStatus(hit2, GridSquareStatus.Hit); _strategy.RegisterShotResult(hit2, ShotResult.CreateHit(ship, true)); IList <GridCoordinate> expectedCoordinates = new List <GridCoordinate>(); Direction hit2To1Direction = Direction.FromCoordinates(hit2, hit1); expectedCoordinates.Add(hit1.GetNeighbor(hit2To1Direction)); Direction hit1To2Direction = hit2To1Direction.Opposite; expectedCoordinates.Add(hit2.GetNeighbor(hit1To2Direction)); //Act GridCoordinate result = _strategy.DetermineTargetCoordinate(); Assert.That(expectedCoordinates.Contains(result), Is.True, $"Bij een grid met 2 rake schoten shot op {hit1} en {hit2} " + $"moet er geschoten worden op ייn van de volgende coordinaten: {expectedCoordinates.ToArray().Print()}. " + $"De strategie kiest soms echter {result}"); Setup(); } }