コード例 #1
0
        public void RecordsLastShotCorrectly(int shipSize, ShotResult expectedShotResult)
        {
            var player1 = new Mock <IBattleShipShooter>();
            var player2 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship('A', 1, 'A', 1),
                new Ship('A', 1, 'A', 2),
            };
            var coordinates = new Coordinates('A', 1);

            player1.Setup(x => x.Shoot()).Returns(coordinates);
            player2.Setup(x => x.Shoot()).Returns(coordinates);
            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships.Where(s => s.Size == shipSize).ToArray);
            player2.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships.Where(s => s.Size == shipSize).ToArray);

            var game = new BattleShipGame(player1.Object, player2.Object);

            game.StarNewGame();
            game.Shoot();

            if (game.Attacker.Shooter == player1.Object)
            {
                player1.Verify(x => x.ReportLastShotResult(coordinates, expectedShotResult));
                player2.Verify(x => x.ReportOponentsLastShotResult(coordinates, expectedShotResult));
            }
            else
            {
                player2.Verify(x => x.ReportLastShotResult(coordinates, expectedShotResult));
                player1.Verify(x => x.ReportOponentsLastShotResult(coordinates, expectedShotResult));
            }
        }
コード例 #2
0
        public void InitiateNewGame()
        {
            var player1 = new Mock <IBattleShipShooter>();
            var game    = new BattleShipGame(player1.Object, player1.Object);

            game.StarNewGame();

            Assert.IsNotNull(game);
        }
コード例 #3
0
        public void ThrowsErrorWhenShotIsOutOfRange()
        {
            var player1 = new Mock <IBattleShipShooter>();

            player1.Setup(x => x.Shoot()).Returns(() => new Coordinates('W', 3));

            var game = new BattleShipGame(player1.Object, player1.Object);

            game.StarNewGame();

            Assert.Throws <ArgumentException>(() => game.Shoot());
        }
コード例 #4
0
        public void ThrowsAnExceptionWhenShipsAreTooCloseToEachOther(int startX1, int startY1, int endX1, int endY1, int startX2, int startY2, int endX2, int endY2)
        {
            var player1 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship(startX1, startY1, endX1, endY1),
                new Ship(startX2, startY2, endX2, endY2)
            };

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);

            var game = new BattleShipGame(player1.Object, player1.Object);

            Assert.That(() => game.StarNewGame(),
                        Throws.TypeOf <ArgumentException>().With.Message.EqualTo(", ships are too close to each other!"));
        }
コード例 #5
0
        public void ThrowsErrorWhenTooMuchShipsBySize(int count, int size)
        {
            var player1 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship('A', 1, 'A', size),
                new Ship('B', 1, 'B', size),
                new Ship('C', 1, 'C', size),
                new Ship('D', 1, 'D', size),
                new Ship('F', 1, 'F', size)
            };

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships.Take(count).ToArray);
            var game = new BattleShipGame(player1.Object, player1.Object);

            Assert.Throws <Exception>(() => game.StarNewGame());
        }
コード例 #6
0
        public void KeepsAttackerAfterHit()
        {
            var player1     = new Mock <IBattleShipShooter>();
            var player2     = new Mock <IBattleShipShooter>();
            var ships       = new[] { new Ship('A', 1, 'A', 1) };
            var coordinates = new Coordinates('A', 1);

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);
            player2.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);
            player1.Setup(x => x.Shoot()).Returns(coordinates);
            player2.Setup(x => x.Shoot()).Returns(coordinates);

            var game = new BattleShipGame(player1.Object, player2.Object);

            game.StarNewGame();

            var attackerBefore = game.Attacker.Shooter;

            game.Shoot();
            Assert.AreEqual(attackerBefore, game.Attacker.Shooter);
        }
コード例 #7
0
        public void DoesntThrowAnExceptionWhenShipsPlacementIsValid()
        {
            var player1 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship('A', 1, 'C', 1),
                new Ship('G', 2, 'H', 2),
                new Ship('J', 3, 'J', 3),
                new Ship('B', 4, 'D', 4),
                new Ship('F', 6, 'F', 6),
                new Ship('H', 6, 'I', 6),
                new Ship('D', 7, 'D', 10),
                new Ship('A', 8, 'A', 8),
                new Ship('F', 10, 'G', 10),
                new Ship('J', 9, 'J', 9)
            };

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);

            var game = new BattleShipGame(player1.Object, player1.Object);

            Assert.That(() => game.StarNewGame(), Throws.Nothing);
        }