public void PlaceRandomly_GridIsEmptyAndRandomChoosesFirstPossiblePlacement_PlacedShipOnGrid(Ship ship,
                                                                                                     List <List <Cell> > possiblePlacements)
        {
            var randomIndexGeneratorStub             = new Mock <IRandomGenerator>();
            var shipPossiblePlacementsCalculatorStub = new Mock <IShipPossiblePlacementsCalculator>();
            var modifiedGrid = EmptyGridGenerator.GenerateEmptyGridOfSize(Constants.GridSize);

            shipPossiblePlacementsCalculatorStub.Setup(s => s.CalculatePossiblePlacements(ship, modifiedGrid))
            .Returns(possiblePlacements);
            randomIndexGeneratorStub.Setup(r => r.GetRandomNumberFromRange(0, possiblePlacements.Count))
            .Returns((int minValue, int maxValue) => minValue);
            const int firstIndex      = 0;
            var       chosenPlacement = possiblePlacements[firstIndex];
            var       shipPlacer      = new ShipPlacer(randomIndexGeneratorStub.Object, shipPossiblePlacementsCalculatorStub.Object);
            var       actualResult    = EmptyGridGenerator.GenerateEmptyGridOfSize(Constants.GridSize);

            foreach (var cell in chosenPlacement)
            {
                actualResult[cell.Y][cell.X] = true;
            }

            shipPlacer.PlaceRandomly(ship, modifiedGrid);

            Assert.That(modifiedGrid, Is.EqualTo(actualResult));
        }
        public void GenerateRandomGrid_Generates10ShipsAndCallPlaceRandomlyForEveryShip()
        {
            var randomShipGeneratorStub = new Mock <IShipGenerator>();
            var fourDeckShip            = new FourDeckShip(ShipShape.Line, ShipRotation._0);
            var threeDeckShip           = new ThreeDeckShip(ShipShape.Line, ShipRotation._0);
            var twoDeckShip             = new TwoDeckShip(ShipRotation._0);
            var oneDeckShip             = new OneDeckShip();

            randomShipGeneratorStub.Setup(s => s.GenerateShipOfSize(4)).Returns(fourDeckShip);
            randomShipGeneratorStub.Setup(s => s.GenerateShipOfSize(3)).Returns(threeDeckShip);
            randomShipGeneratorStub.Setup(s => s.GenerateShipOfSize(2)).Returns(twoDeckShip);
            randomShipGeneratorStub.Setup(s => s.GenerateShipOfSize(1)).Returns(oneDeckShip);
            var shipPlacerStub = new Mock <IShipPlacer>();
            var emptyGrid      = EmptyGridGenerator.GenerateEmptyGridOfSize(Constants.GridSize);
            var seaBattleGame  = new SeaBattleGame(randomShipGeneratorStub.Object, shipPlacerStub.Object);

            seaBattleGame.GenerateRandomGrid();

            Assert.That(seaBattleGame.Grid, Is.EqualTo(emptyGrid));
            randomShipGeneratorStub.Verify(generator => generator.GenerateShipOfSize(4), Times.Exactly(1));
            randomShipGeneratorStub.Verify(generator => generator.GenerateShipOfSize(3), Times.Exactly(2));
            randomShipGeneratorStub.Verify(generator => generator.GenerateShipOfSize(2), Times.Exactly(3));
            randomShipGeneratorStub.Verify(generator => generator.GenerateShipOfSize(1), Times.Exactly(4));
            shipPlacerStub.Verify(placer => placer.PlaceRandomly(fourDeckShip, It.IsAny <bool[][]>()), Times.Exactly(1));
            shipPlacerStub.Verify(placer => placer.PlaceRandomly(threeDeckShip, It.IsAny <bool[][]>()), Times.Exactly(2));
            shipPlacerStub.Verify(placer => placer.PlaceRandomly(twoDeckShip, It.IsAny <bool[][]>()), Times.Exactly(3));
            shipPlacerStub.Verify(placer => placer.PlaceRandomly(oneDeckShip, It.IsAny <bool[][]>()), Times.Exactly(4));
        }
        public void ResetGrid_GridIsNotEmpty_MakeGridEmpty()
        {
            var seaBattleGame = new SeaBattleGame(null, null);
            var emptyGrid     = EmptyGridGenerator.GenerateEmptyGridOfSize(Constants.GridSize);

            seaBattleGame.Grid[0][0] = true;

            seaBattleGame.ResetGrid();

            Assert.That(seaBattleGame.Grid, Is.EqualTo(emptyGrid));
        }
        public void CalculatePossiblePlacements_InputEmptyGrid_ReturnsAllPossiblePlacements(
            Ship ship, List <Cell> shipRelativeCells, int expectedNumberOfPossiblePlacements)
        {
            var shipBuilderStub = new Mock <IShipBuilder>();

            shipBuilderStub.Setup(s => s.Build(ship)).Returns(shipRelativeCells);
            var emptyGrid = EmptyGridGenerator.GenerateEmptyGridOfSize(Constants.GridSize);
            var shipPossiblePlacementsCalculator = new ShipPossiblePlacementsCalculator(shipBuilderStub.Object);

            var result = shipPossiblePlacementsCalculator.CalculatePossiblePlacements(ship, emptyGrid).ToList();

            shipBuilderStub.Verify(s => s.Build(ship), Times.Once);
            Assert.That(result.Count, Is.EqualTo(expectedNumberOfPossiblePlacements));
        }
示例#5
0
        public void GenerateEmptyGridOfSize_Input10_ReturnsEmptyGridOfSize10()
        {
            const int gridSize   = 10;
            var       rowOfFalse = new[] { false, false, false, false, false, false, false, false, false, false };

            var result = EmptyGridGenerator.GenerateEmptyGridOfSize(gridSize);

            Assert.That(result.Length, Is.EqualTo(gridSize));

            foreach (var row in result)
            {
                Assert.That(row, Is.EqualTo(rowOfFalse));
            }
        }
 public void ResetGrid()
 {
     Grid = EmptyGridGenerator.GenerateEmptyGridOfSize(Constants.GridSize);
 }