Esempio n. 1
0
        public void SetUp()
        {
            initialState            = BattleshipGameState.Empty(gridSize);
            initialState.Grid[0][0] = BattleshipGridCell.Ship;
            initialState.Grid[0][1] = BattleshipGridCell.Ship;
            initialState.Grid[0][2] = BattleshipGridCell.Ship;

            state1            = BattleshipGameState.Empty(gridSize);
            state1.Grid[0][0] = BattleshipGridCell.Hit;
            state1.Grid[0][1] = BattleshipGridCell.Ship;
            state1.Grid[0][2] = BattleshipGridCell.Ship;

            state2            = BattleshipGameState.Empty(gridSize);
            state2.Grid[0][0] = BattleshipGridCell.Hit;
            state2.Grid[0][1] = BattleshipGridCell.Hit;
            state2.Grid[0][2] = BattleshipGridCell.Ship;

            state3            = BattleshipGameState.Empty(gridSize);
            state3.Grid[0][0] = BattleshipGridCell.Hit;
            state3.Grid[0][1] = BattleshipGridCell.Hit;
            state3.Grid[0][2] = BattleshipGridCell.Hit;

            _console      = Substitute.For <IConsole>();
            _stateBuilder = Substitute.For <IBattleshipStateBuilder>();
            _stateBuilder.Build().Returns(initialState);
            _gameShowService = Substitute.For <IShowGameState>();

            _serviceUnderTests = new BattleshipGame(_console,
                                                    _stateBuilder,
                                                    _gameShowService);
        }
Esempio n. 2
0
        public void Show_ShouldShowHitMissMarkers_GivenUsedGrid()
        {
            // arrange
            var expectedFirstScreen =
                "  1 2 3 4 5 6 7 8 9 10\r\n" +
                "A *                 x |\r\n" +
                "B                     |\r\n" +
                "C                     |\r\n" +
                "D                     |\r\n" +
                "E                     |\r\n" +
                "F                     |\r\n" +
                "G                     |\r\n" +
                "H                     |\r\n" +
                "I                     |\r\n" +
                "J                     |\r\n" +
                "  - - - - - - - - - - \r\n";
            var grid = BattleshipGameState.Empty(10).Grid;

            grid[0][0] = BattleshipGridCell.Hit;
            grid[0][9] = BattleshipGridCell.Miss;
            grid[0][8] = BattleshipGridCell.Ship;
            var state = new BattleshipGameState {
                Grid = grid
            };

            // act
            _serviceUnderTests.Show(state);

            // assert
            Assert.AreEqual(expectedFirstScreen, _consoleOut);
        }
Esempio n. 3
0
        public void Show_ShouldShowEmptyGrid_GivenEmptyGrid()
        {
            // arrange
            var expectedFirstScreen =
                "  1 2 3 4 5 6 7 8 9 10\r\n" +
                "A                     |\r\n" +
                "B                     |\r\n" +
                "C                     |\r\n" +
                "D                     |\r\n" +
                "E                     |\r\n" +
                "F                     |\r\n" +
                "G                     |\r\n" +
                "H                     |\r\n" +
                "I                     |\r\n" +
                "J                     |\r\n" +
                "  - - - - - - - - - - \r\n";
            var emptyGrid = BattleshipGameState.Empty(10).Grid;
            var empty     = new BattleshipGameState {
                Grid = emptyGrid
            };

            // act
            _serviceUnderTests.Show(empty);

            // assert
            Assert.AreEqual(expectedFirstScreen, _consoleOut);
        }
Esempio n. 4
0
        public void GivenCellStateIn(string cellState, int line, int column)
        {
            // prepare state for test case
            var state = BattleshipGameState.Empty(_configuration.GridSize);

            state.Grid[line][column] = GetState(cellState);

            // get tested class instance
            _game = GameFromPrevState(_container, state);
        }
Esempio n. 5
0
        public void IsGuessColliding_ReturnsTrue_WhenShipWouldExtendOverBorder(int x, int y, int length, bool isVertical, bool expected)
        {
            var grid = BattleshipGameState.Empty(gridSize).Grid;
            var ship = new BattleShip(length, isVertical);

            // act
            var result = _servceUnderTest.IsNextShipColliding(grid, ship, new GridCoordinate(x, y));

            // assert
            Assert.AreEqual(expected, result);
        }
Esempio n. 6
0
        public void Build_ShouldReturnEmptyBoard_WhenNoShipsConfigured()
        {
            // arrange
            _config.Ships.Returns(new List <int>());
            var expected = BattleshipGameState.Empty(gridSize);

            // act
            var result = _servceUnderTest.Build();

            // assert
            Assert.AreEqual(expected.Grid, result.Grid);
        }
Esempio n. 7
0
        public void GivenShipsInFolowingGridPoints(Table table)
        {
            // prepare state for test case
            var state = BattleshipGameState.Empty(_configuration.GridSize);

            foreach (var row in table.Rows)
            {
                var line   = int.Parse(row["line"]) - 1;
                var column = int.Parse(row["column"]) - 1;
                state.Grid[line][column] = BattleshipGridCell.Ship;
            }
            // get tested class instance
            _game = GameFromPrevState(_container, state);
        }
        public BattleshipGameState Build()
        {
            var result = BattleshipGameState.Empty(_configuration.GridSize);

            foreach (var shipLength in _configuration.Ships.OrderByDescending(s => s))
            {
                var isVertical = _random.IsNextVertical();
                var ship       = new BattleShip(shipLength, isVertical);
                var firstCell  = GetShipStart(result.Grid, ship);

                PlaceShipOnGrid(result.Grid, ship, firstCell);
            }

            return(result);
        }
Esempio n. 9
0
        public void IsGuessColliding_ReturnsTrue_ForShipsCollision(int x, int y, bool isVertical, bool expected)
        {
            // arrange
            var grid = BattleshipGameState.Empty(gridSize).Grid;

            grid[1][2] = BattleshipGridCell.Ship;    // place ship on grid
            grid[2][2] = BattleshipGridCell.Ship;
            var ship = new BattleShip(2, isVertical);

            // act
            var result = _servceUnderTest.IsNextShipColliding(grid, ship, new GridCoordinate(x, y));

            // assert
            Assert.AreEqual(expected, result);
        }
Esempio n. 10
0
        public void Build_ShouldReturnMissMark_WhenShotMissed()
        {
            // arrange
            var prev  = BattleshipGameState.Empty(gridSize);
            var guess = "B2";
            var coord = new GridCoordinate(1, 1);

            _guessService.GetCordinates(guess).Returns(coord);
            var expected = BattleshipGameState.Empty(gridSize);

            expected.Grid[1][1] = BattleshipGridCell.Miss;

            // act
            var result = _servceUnderTest.Build(prev, guess);

            // assert
            Assert.AreEqual(expected.Grid, result.Grid);
        }
Esempio n. 11
0
        public void Build_ShouldReturnBoardWithSingleShip_WhenSingleShipInConfiguration(int x, int y)
        {
            // arrange
            _config.Ships.Returns(new List <int> {
                1
            });
            var coord = new GridCoordinate(x, y);

            _randomService.NextCell().Returns(coord);
            var expected = BattleshipGameState.Empty(gridSize).Grid;

            expected[x][y] = BattleshipGridCell.Ship;

            // act
            var result = _servceUnderTest.Build();

            // assert
            Assert.AreEqual(expected, result.Grid);
        }
Esempio n. 12
0
        public void Build_ShouldRotateShip_WhenItsSelectedRandomly(bool isVertical, int shipLength)
        {
            // arrange
            int x = 1;
            int y = 1;

            _config.Ships.Returns(new List <int> {
                shipLength
            });
            var coord = new GridCoordinate(x, y);

            _randomService.NextCell().Returns(coord);
            _randomService.IsNextVertical().Returns(isVertical);
            var expected = BattleshipGameState.Empty(gridSize).Grid;

            expected[x][y] = BattleshipGridCell.Ship;
            expected[isVertical ? x + 1 : x][isVertical ? y : y + 1] = BattleshipGridCell.Ship;

            // act
            var result = _servceUnderTest.Build();

            // assert
            Assert.AreEqual(expected, result.Grid);
        }
Esempio n. 13
0
        public void Build_ShouldPlaceAllShips_FromLongestToSmallestInConfiguration()
        {
            var grid           = BattleshipGameState.Empty(gridSize).Grid;
            var firstShipStart = new GridCoordinate(8, 4);

            grid[firstShipStart.Line][firstShipStart.Column]     = BattleshipGridCell.Ship; // 1st ship: 4 mast horizontal
            grid[firstShipStart.Line][firstShipStart.Column + 1] = BattleshipGridCell.Ship;
            grid[firstShipStart.Line][firstShipStart.Column + 2] = BattleshipGridCell.Ship;
            grid[firstShipStart.Line][firstShipStart.Column + 3] = BattleshipGridCell.Ship;
            var secondShipStart = new GridCoordinate(2, 3);

            grid[secondShipStart.Line][secondShipStart.Column]     = BattleshipGridCell.Ship; // 2nd ship: 3 mast vertical
            grid[secondShipStart.Line + 1][secondShipStart.Column] = BattleshipGridCell.Ship;
            grid[secondShipStart.Line + 2][secondShipStart.Column] = BattleshipGridCell.Ship;
            var thirdShipStart = new GridCoordinate(1, 2);

            grid[thirdShipStart.Line][thirdShipStart.Column]     = BattleshipGridCell.Ship; // 1st ship: 2 mast vertical
            grid[thirdShipStart.Line + 1][thirdShipStart.Column] = BattleshipGridCell.Ship;
            var ships = new[] { 2, 3, 4 };

            _config.Ships.Returns(ships);
            var starts = new[] { firstShipStart, secondShipStart, thirdShipStart };

            _randomService.NextCell().Returns(firstShipStart, secondShipStart, thirdShipStart);
            _randomService.IsNextVertical().Returns(false, true, true);
            _detectCollisionService.IsNextShipColliding(
                Arg.Any <List <List <BattleshipGridCell> > >(),
                Arg.Any <BattleShip>(),
                Arg.Is <GridCoordinate>(i => !starts.Contains(i))
                ).Returns(true);
            // act
            var result = _servceUnderTest.Build();

            // assert
            Assert.AreEqual(grid, result.Grid);
        }
Esempio n. 14
0
        public void GetShipStart_ShouldPreventCollisionsWithOtherShips_WhenPlacingNextShip()
        {
            // arrange
            var grid           = BattleshipGameState.Empty(gridSize).Grid;
            var firstShipStart = new GridCoordinate(1, 2);

            grid[firstShipStart.Line][firstShipStart.Column]     = BattleshipGridCell.Ship; // place ship on grid
            grid[firstShipStart.Line + 1][firstShipStart.Column] = BattleshipGridCell.Ship;
            var nextShip = new BattleShip(2, true);
            var expected = new GridCoordinate(2, 3);

            _randomService.NextCell()
            .Returns(firstShipStart,    // we randomly got space that is
                     firstShipStart,    // already used by other ships
                     expected);         // until we got proper one
            _detectCollisionService.IsNextShipColliding(grid, nextShip, firstShipStart).Returns(true);
            _detectCollisionService.IsNextShipColliding(grid, nextShip, expected).Returns(false);

            // act
            var result = _servceUnderTest.GetShipStart(grid, nextShip);

            // assert
            Assert.AreEqual(expected, result);
        }