Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #4
0
 public static BattleshipGame GameFromPrevState(IContainer container, BattleshipGameState prevState)
 {
     return(new BattleshipGame(
                container.Resolve <IConsole>(),
                container.Resolve <IBattleshipStateBuilder>(),
                container.Resolve <IShowGameState>(),
                prevState));
 }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
0
 public BattleshipGame(
     IConsole console,
     IBattleshipStateBuilder stateBuilder,
     IShowGameState gameShowService,
     BattleshipGameState gameState
     )
 {
     _console         = console;
     _stateBuilder    = stateBuilder;
     _gameShowService = gameShowService;
     _gameState       = gameState;
 }
Beispiel #8
0
        public void Show(BattleshipGameState state)
        {
            _console.WriteLine($"  {string.Join(' ', Enumerable.Range(1, _configuration.GridSize))}");
            int i = 0;

            foreach (var line in state.Grid)
            {
                var lineToDisplay = string.Join(' ', line.Select(l => _mapper.GetCellValueToDisplay(l)));
                _console.WriteLine($"{_charService.GetLetter(i++)} {lineToDisplay} |");
            }
            _console.WriteLine($"  {string.Join(' ', Enumerable.Range(0, _configuration.GridSize).Select(_ => "-")) } ");
        }
Beispiel #9
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);
        }
Beispiel #10
0
        public void PlayRound_ShowsGame_GivenGuess()
        {
            // arrange
            var guess     = "A1";
            var nextState = new BattleshipGameState();

            _stateBuilder.Build(initialState, guess).Returns(nextState);

            // act
            _serviceUnderTests.PlayRound(guess);

            // assert
            _gameShowService.Received().Show(nextState);
        }
Beispiel #11
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);
        }
Beispiel #13
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);
        }
        public BattleshipGameState Build(BattleshipGameState prevState, string guess)
        {
            var guessCell = _guessReader.GetCordinates(guess);

            // state shallow copy
            // that it might be a problem with multiple threads but its easier to implement and has less overhead
            var newState = new BattleshipGameState()
            {
                Grid = prevState.Grid
            };
            var newCellState = _mapper.NewCellState(newState.Grid[guessCell.Line][guessCell.Column]);

            newState.Grid[guessCell.Line][guessCell.Column] = newCellState;

            return(newState);
        }
Beispiel #15
0
        public void PlayRound(string guess)
        {
            try
            {
                _gameState = _stateBuilder.Build(_gameState, guess);

                Show(_gameState);
            }
            catch (InvalidInputException)
            {
                _gameShowService.DisplayInputWarning();
            }
            catch (CellRepetitionException)
            {
                _gameShowService.DisplayRetryWarning();
            }
        }
Beispiel #16
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);
        }
Beispiel #17
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);
        }
Beispiel #18
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);
        }
Beispiel #19
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);
        }
Beispiel #20
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);
        }
Beispiel #21
0
 private void Show(BattleshipGameState state)
 {
     _gameShowService.Show(state);
 }
Beispiel #22
0
 public void GivenIHavePreviousGameState()
 {
     _state = _stateBuilder.Build();
 }
Beispiel #23
0
 public void WhenIGenerateNewGameState()
 {
     _state = _stateBuilder.Build();
 }
Beispiel #24
0
 private bool IsFinished(BattleshipGameState gameState)
 {
     return(!gameState.Grid.Any(
                line => line.Any(
                    cell => cell == BattleshipGridCell.Ship)));
 }