public void IsOutOfBounds_ShouldReturnTrueWhenCoordinateIsNotWithinGrid(int row, int column, int gridSize, bool expected)
        {
            GridCoordinate coordinate = new GridCoordinate(row, column);

            Assert.That(coordinate.IsOutOfBounds(gridSize), Is.EqualTo(expected),
                        $"Coordinate ({row},{column}) should result in '{expected}'");
        }
        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();
            }
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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_ShouldPickRandomCoordinatesWithinTheGrid()
        {
            int            numberOfDeterminations       = 20;
            int            numberOfDifferentCoordinates = 0;
            GridCoordinate previousCoordinate           = _strategy.DetermineTargetCoordinate();

            for (int i = 1; i < numberOfDeterminations; i++)
            {
                GridCoordinate currentCoordinate = _strategy.DetermineTargetCoordinate();
                Assert.That(currentCoordinate.IsOutOfBounds(_grid.Size), Is.False,
                            $"A coordinate {currentCoordinate} was determined. This coordinate is out of bounds.");
                if (currentCoordinate != previousCoordinate)
                {
                    numberOfDifferentCoordinates++;
                }

                previousCoordinate = currentCoordinate;
            }

            Assert.That(numberOfDifferentCoordinates, Is.GreaterThan(14),
                        "The strategy is not random enough. " +
                        $"Out of {numberOfDeterminations} determined coordinates, only {numberOfDifferentCoordinates} are different.");
        }