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();
            }
        }
        private void RegisterSunkenShip(ShipKind sunkenShipKind)
        {
            var shipBuilder = new ShipBuilder(sunkenShipKind).WithSquares(GridSquareStatus.Hit);
            var ship        = shipBuilder.Build();

            for (var index = 0; index < ship.Squares.Length - 1; index++)
            {
                IGridSquare shipSquare = ship.Squares[index];
                _strategy.RegisterShotResult(shipSquare.Coordinate, ShotResult.CreateHit(ship, true));
            }

            shipBuilder.WithHasSunk(true);
            _strategy.RegisterShotResult(ship.Squares[ship.Kind.Size - 1].Coordinate, ShotResult.CreateHit(ship, true));
        }
        public void DetermineTargetCoordinate_ShouldDetectTheShipWith2HitsWhenANeighborShipAlsoHasAHit()
        {
            for (int numberOfChecks = 0; numberOfChecks < 10; numberOfChecks++)
            {
                //Arrange
                GridCoordinate hit1  = new GridCoordinate(RandomGenerator.Next(2, _grid.Size - 2), RandomGenerator.Next(2, _grid.Size - 2));
                GridCoordinate miss1 = new GridCoordinate(hit1.Row - 1, hit1.Column);
                GridCoordinate hit2  = new GridCoordinate(hit1.Row + 1, hit1.Column);
                GridCoordinate miss2 = new GridCoordinate(hit2.Row + 1, hit2.Column);
                GridCoordinate hit3  = new GridCoordinate(hit2.Row, hit2.Column + 1);
                var            ship1 = new ShipBuilder(ShipKind.Carrier).Build();
                var            ship2 = new ShipBuilder(ShipKind.Battleship).Build();

                _gridBuilder.WithSquareStatus(miss1, GridSquareStatus.Miss);

                _gridBuilder.WithSquareStatus(hit1, GridSquareStatus.Hit);
                _strategy.RegisterShotResult(hit1, ShotResult.CreateHit(ship1, true));

                _gridBuilder.WithSquareStatus(hit2, GridSquareStatus.Hit);
                _strategy.RegisterShotResult(hit2, ShotResult.CreateHit(ship2, true));

                _gridBuilder.WithSquareStatus(miss2, GridSquareStatus.Miss);

                _gridBuilder.WithSquareStatus(hit3, GridSquareStatus.Hit);
                _strategy.RegisterShotResult(hit3, ShotResult.CreateHit(ship2, true));

                IList <GridCoordinate> expectedCoordinates = new List <GridCoordinate>
                {
                    new GridCoordinate(hit3.Row, hit3.Column + 1),
                    new GridCoordinate(hit2.Row, hit2.Column - 1)
                };

                //Act
                GridCoordinate result = _strategy.DetermineTargetCoordinate();

                Assert.That(expectedCoordinates.Contains(result), Is.True,
                            $"Bij een grid met volgende situatie: missers -> {miss1} en {miss2}, rake schoten -> {hit1}, {hit2} en {hit3} " +
                            $"moet er geschoten worden op ייn van de volgende coordinaten: {expectedCoordinates.ToArray().Print()}. " +
                            $"De strategie kiest soms echter {result}");

                Setup();
            }
        }
Esempio n. 4
0
        private void RegisterSunkenShip(ShipKind sunkenShipKind, GridCoordinate[] coordinates)
        {
            // Get ship squares
            IGridSquare[] squares = coordinates.Select(c =>
            {
                GridSquare square = new GridSquare(c);
                square.Status     = GridSquareStatus.Hit;
                return(square);
            }).ToArray();
            var shipBuilder = new ShipBuilder(sunkenShipKind).WithSquares(squares);
            var ship        = shipBuilder.Build();

            for (var index = 0; index < ship.Squares.Length - 1; index++)
            {
                IGridSquare shipSquare = ship.Squares[index];
                _strategy.RegisterShotResult(shipSquare.Coordinate, ShotResult.CreateHit(ship, true));
            }

            shipBuilder.WithHasSunk(true);
            _strategy.RegisterShotResult(ship.Squares[ship.Kind.Size - 1].Coordinate, ShotResult.CreateHit(ship, true));
        }
        private void DetectSunkenShipsVertically()
        {
            for (int j = 0; j < _grid.Size; j++)
            {
                for (int i = 0; i < _grid.Size; i++)
                {
                    if (_grid.Squares[i, j].Status == GridSquareStatus.Hit)
                    {
                        int topIndex = i;
                        while (i < _grid.Size && _grid.Squares[i, j].Status == GridSquareStatus.Hit)
                        {
                            i++;
                        }

                        int bottomIndex = i - 1;
                        if (bottomIndex > topIndex)
                        {
                            var kind        = ShipKind.All.First(kind => kind.Size == bottomIndex - topIndex + 1);
                            var shipBuilder = new ShipBuilder(kind);
                            var ship        = shipBuilder.Build();

                            IList <IGridSquare> shipSquares = new List <IGridSquare>();
                            for (var index = topIndex; index < bottomIndex; index++)
                            {
                                shipSquares.Add(_grid.Squares[index, j]);
                                _strategy.RegisterShotResult(_grid.Squares[index, j].Coordinate,
                                                             ShotResult.CreateHit(ship, true));
                            }

                            shipSquares.Add(_grid.Squares[bottomIndex, j]);

                            shipBuilder.WithSquares(shipSquares.ToArray());
                            shipBuilder.WithHasSunk(true);
                            _strategy.RegisterShotResult(_grid.Squares[bottomIndex, j].Coordinate,
                                                         ShotResult.CreateHit(ship, true));
                        }
                    }
                }
            }
        }
        private void DetectSunkenShipsHorizontally()
        {
            for (int i = 0; i < _grid.Size; i++)
            {
                for (int j = 0; j < _grid.Size; j++)
                {
                    if (_grid.Squares[i, j].Status == GridSquareStatus.Hit)
                    {
                        int leftIndex = j;
                        while (j < _grid.Size && _grid.Squares[i, j].Status == GridSquareStatus.Hit)
                        {
                            j++;
                        }

                        int rightIndex = j - 1;
                        if (rightIndex > leftIndex)
                        {
                            var kind        = ShipKind.All.First(kind => kind.Size == rightIndex - leftIndex + 1);
                            var shipBuilder = new ShipBuilder(kind);
                            var ship        = shipBuilder.Build();
                            IList <IGridSquare> shipSquares = new List <IGridSquare>();
                            for (var index = leftIndex; index < rightIndex; index++)
                            {
                                shipSquares.Add(_grid.Squares[i, index]);
                                _strategy.RegisterShotResult(_grid.Squares[i, index].Coordinate,
                                                             ShotResult.CreateHit(ship, true));
                            }

                            shipSquares.Add(_grid.Squares[i, rightIndex]);
                            shipBuilder.WithSquares(shipSquares.ToArray());
                            shipBuilder.WithHasSunk(true);
                            _strategy.RegisterShotResult(_grid.Squares[i, rightIndex].Coordinate,
                                                         ShotResult.CreateHit(ship, true));
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public ShotResult ShootAt(IPlayer opponent, GridCoordinate coordinate)
        {
            if (HasBombsLoaded == true)
            {
                this._HasBombsLoaded = false;
                IGridSquare square = opponent.Grid.Shoot(coordinate);
                opponent.ReloadBombs();

                if (square.Status == GridSquareStatus.Hit)
                {
                    return(ShotResult.CreateHit(opponent.Fleet.FindShipAtCoordinate(coordinate), this.settings.MustReportSunkenShip));
                }
                if (square.Status == GridSquareStatus.Miss)
                {
                    return(ShotResult.CreateMissed());
                }
                return(ShotResult.CreateMissed());
            }
            else
            {
                return(ShotResult.CreateMisfire("You have no bombs loaded"));
            }
        }
        public ShotResult ShootAt(IPlayer opponent, GridCoordinate coordinate)
        {
            /* Get the grid of the opponent & shoot */
            IGridSquare square = opponent.Grid.Shoot(coordinate);

            /* Bomb has been shot */
            this.nrOfBombsLoaded = 0;

            /* The grid square informas about the result of the shot */
            if (square.Status == GridSquareStatus.Hit)
            {
                /* Ship hit */
                return(ShotResult.CreateHit(opponent.Fleet.FindShipAtCoordinate(coordinate), mustReportSunkenShip));
            }
            else if (square.Status == GridSquareStatus.Miss)
            {
                /* Missed */
                return(ShotResult.CreateMissed());
            }
            else
            {
                return(ShotResult.CreateMisfire("out of bound"));
            }
        }
        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();
            }
        }