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 GivenNoShipsOnGrid()
 {
     // no ships in config
     _configuration.Ships = new List <int>();
     // re-read configuration
     _game = (BattleshipGame)_container.Resolve <IBattleshipGame>();
 }
        public void Play(BattleshipGame battleshipGame)
        {
            var position    = new (char x, int y)?();
            var lastMessage = string.Empty;

            while (!battleshipGame.IsComplete)
            {
                _console.Clear();
                _gameDrawer.Draw(battleshipGame, position);
                _console.WriteLine();
                _console.WriteAtPositionWithForegroundColor(0, _console.CursorTop + 1, lastMessage, ConsoleColor.Yellow);
                position = _positionReader.ReadPosition();

                var fireResult = battleshipGame.Fire(position.Value);
                lastMessage = _fireResultMessages[fireResult];
            }

            _console.Clear();
            _gameDrawer.Draw(battleshipGame);
            _console.WriteLine();
            _console.WriteLine("Game Completed!");
            var statistics = battleshipGame.CurrentStatistics;

            _console.WriteLine($"Hits: {statistics.Hits}");
            _console.WriteLine($"Misses: {statistics.Misses}");
            _console.WriteLine($"Accuracy: {statistics.Accuracy:P}");

            _console.WriteLine();
        }
Beispiel #4
0
        public async Task <IActionResult> ConfigurePlayer()//[FromBody]PlayerSetup setup)
        {
            var setup = new PlayerSetup();

            await TryUpdateModelAsync(setup);

            var player = _ctx.Players
                         .Include(p => p.Game)
                         .SingleOrDefault(p => p.PlayerId == setup.PlayerId);

            if (player == null)
            {
                return(NotFound());
            }

            player.Name = setup.Name;

            var game = new BattleshipGame(player.Game);

            game.PlaceShip(player, setup.IsShipHorizontal, setup.ShipX, setup.ShipY);

            _ctx.SaveChanges();

            return(RedirectToAction(nameof(Player), new { playerId = player.PlayerId }));
        }
Beispiel #5
0
        public IActionResult Shoot([FromRoute] int playerId, [FromQuery] int?x, [FromQuery] int?y)
        {
            var player = _ctx.Players
                         .Include(p => p.Game)
                         .ThenInclude(g => g.Players)
                         .SingleOrDefault(p => p.PlayerId == playerId);

            if (player == null)
            {
                return(NotFound("Spilleren findes ikke"));
            }
            if (!x.HasValue || !y.HasValue)
            {
                return(BadRequest("x og y skal angives"));
            }

            var  game     = new BattleshipGame(player.Game);
            bool isWinner = game.Shoot(player, x.Value, y.Value);

            if (isWinner)
            {
                player.Game.WinnerId = player.PlayerId;
            }

            _ctx.SaveChanges();

            if (isWinner)
            {
                return(RedirectToAction(nameof(Winner), new { playerId = player.PlayerId }));
            }

            return(RedirectToAction(nameof(Wait), new { playerId = player.PlayerId }));
        }
 public static void FireAllCells(this BattleshipGame battleship)
 {
     foreach (var cell in battleship.Cells)
     {
         battleship.Fire(cell);
     }
 }
        public BattleshipGameTests()
        {
            _fixture              = new Fixture();
            _battlefield          = new Mock <IBattlefield>();
            _shotValidator        = new Mock <IShotValidator>();
            _statisticsCalculator = new Mock <IGameStatisticsCalculator>();

            _battleshipGame = new BattleshipGame(_battlefield.Object, _shotValidator.Object, _statisticsCalculator.Object);
        }
Beispiel #8
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 #9
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);
        }
Beispiel #10
0
        public void Tester()
        {
            char[,] board = new char[10, 10]
            {
                { '.', '.', '#', '.', '.', '.', '.', '.', '.', '.' },
                { '.', '.', '#', '.', '.', '.', '.', '.', '.', '.' },
                { '.', '.', '#', '.', '.', '.', '.', '.', '.', '.' },
                { '.', '.', '#', '.', '#', '#', '#', '#', '#', '.' },
                { '.', '.', '#', '.', '.', '.', '.', '.', '#', '.' },
                { '.', '#', '#', '#', '#', '#', '.', '.', '#', '.' },
                { '.', '.', '.', '.', '.', '.', '.', '.', '#', '.' },
                { '.', '.', '.', '#', '#', '#', '#', '#', '#', '.' },
                { '.', '.', '.', '.', '.', '.', '.', '.', '#', '.' },
                { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
            };

            char[,] expected = new char[10, 10]
            {
                { 'O', 'V', 'X', 'V', 'O', 'O', 'O', 'O', 'O', 'O' },
                { 'O', 'V', 'X', 'V', 'O', 'O', 'O', 'O', 'O', 'O' },
                { 'O', 'V', 'X', 'V', 'V', 'V', 'V', 'V', 'V', 'V' },
                { 'O', 'V', 'X', 'V', 'X', 'X', 'X', 'X', 'X', 'V' },
                { 'V', 'V', 'X', 'V', 'V', 'V', 'V', 'V', 'X', 'V' },
                { 'V', 'X', 'X', 'X', 'X', 'X', 'V', 'V', 'X', 'V' },
                { 'V', 'V', 'V', 'V', 'V', 'V', 'V', 'V', 'X', 'V' },
                { 'O', 'O', 'V', 'X', 'X', 'X', 'X', 'X', 'X', 'V' },
                { 'O', 'O', 'V', 'V', 'V', 'V', 'V', 'V', 'X', 'V' },
                { 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'V', 'V', 'V' }
            };

            Tuple <int, int> coord;

            BattleshipGame.TryDecodeCoordinates("A10", out coord);
            Assert.AreEqual(Tuple.Create(9, 0), coord);

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    Assert.AreEqual(expected[row, col], BattleshipGame.Verify(board, row, col));
                }
            }
        }
Beispiel #11
0
        public void GivenNewBattleshipsGame()
        {
            // setup mocked components
            _configuration = BattleshipsConfiguration.Default;
            _console       = Substitute.For <IConsole>();
            _consoleOut    = "";
            _console
            .When(c => c.WriteLine(Arg.Any <string>()))
            .Do(callinfo =>
            {
                var line    = callinfo.ArgAt <string>(0);
                _consoleOut = $"{_consoleOut}{line}\r\n";
            });
            // use DI setup for rest
            var builder = Bootstrapper.GetContainerBuilder(_configuration);

            // register mocks
            builder.RegisterInstance <IConsole>(_console);
            builder.RegisterInstance <IBattleshipsConfiguration>(_configuration);

            _container = builder.Build();
            // get tested class instance
            _game = (BattleshipGame)_container.Resolve <IBattleshipGame>();
        }