Beispiel #1
0
        public void ReceivingAttack_OnOccupiedCell_ResultsInHit()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsTrue(board.AddBattleship(
                              new Battleship(2, 2),
                              new Coord()
            {
                X = 3, Y = 3
            }));

            Assert.IsTrue(board.ReceiveAttackAt( // 1st battleship cell
                              new Coord()
            {
                X = 3, Y = 3
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 2nd battleship cell
                              new Coord()
            {
                X = 4, Y = 3
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 3rd battleship cell
                              new Coord()
            {
                X = 3, Y = 4
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 4th battleship cell
                              new Coord()
            {
                X = 4, Y = 4
            }));
        }
Beispiel #2
0
        public void HasBattleshipsRemaining_ReturnsTrue_WhenOneOfTwoBattleshipsSunk()
        {
            var board = new BattleshipBoard(10, 10);

            // Add two battleships
            Assert.IsTrue(board.AddBattleship( // Battleship A
                              new Battleship(4, 1),
                              new Coord()
            {
                X = 0, Y = 0
            }));
            Assert.IsTrue(board.AddBattleship( // Battleship B
                              new Battleship(1, 4),
                              new Coord()
            {
                X = 1, Y = 1
            }));

            // Attack all cells of battleship A and sink it
            for (var i = 0; i < 4; i++)
            {
                Assert.IsTrue(board.HasBattleshipsRemaining());
                board.ReceiveAttackAt(new Coord()
                {
                    X = i, Y = 0
                });
            }

            // Battleship B still survives
            Assert.IsTrue(board.HasBattleshipsRemaining());
        }
Beispiel #3
0
        public void ReceivingAttack_OnOccupiedDamagedCell_ResultsInHit()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsTrue(board.AddBattleship(
                              new Battleship(2, 2),
                              new Coord()
            {
                X = 3, Y = 3
            }));

            // First round: Cells previously undamaged
            Assert.IsTrue(board.ReceiveAttackAt( // 1st battleship cell
                              new Coord()
            {
                X = 3, Y = 3
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 2nd battleship cell
                              new Coord()
            {
                X = 4, Y = 3
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 3rd battleship cell
                              new Coord()
            {
                X = 3, Y = 4
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 4th battleship cell
                              new Coord()
            {
                X = 4, Y = 4
            }));

            // Second round: Cells previously damaged
            Assert.IsTrue(board.ReceiveAttackAt( // 1st battleship cell
                              new Coord()
            {
                X = 3, Y = 3
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 2nd battleship cell
                              new Coord()
            {
                X = 4, Y = 3
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 3rd battleship cell
                              new Coord()
            {
                X = 3, Y = 4
            }));
            Assert.IsTrue(board.ReceiveAttackAt( // 4th battleship cell
                              new Coord()
            {
                X = 4, Y = 4
            }));
        }
Beispiel #4
0
        public void ReceivingAttack_OnEmptyCell_ResultsInMiss()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsTrue(board.AddBattleship( // Add battleship
                              new Battleship(2, 2),
                              new Coord()
            {
                X = 3, Y = 3
            }));

            Assert.IsFalse(board.ReceiveAttackAt( // Top-left of board
                               new Coord()
            {
                X = 0, Y = 0
            }));
            Assert.IsFalse(board.ReceiveAttackAt( // Top-right of board
                               new Coord()
            {
                X = 9, Y = 0
            }));
            Assert.IsFalse(board.ReceiveAttackAt( // Bottom-left of board
                               new Coord()
            {
                X = 0, Y = 9
            }));
            Assert.IsFalse(board.ReceiveAttackAt( // Bottom-right of board
                               new Coord()
            {
                X = 9, Y = 9
            }));

            Assert.IsFalse(board.ReceiveAttackAt( // Above battleship
                               new Coord()
            {
                X = 4, Y = 2
            }));
            Assert.IsFalse(board.ReceiveAttackAt( // Below battleship
                               new Coord()
            {
                X = 4, Y = 5
            }));
            Assert.IsFalse(board.ReceiveAttackAt( // Left of battleship
                               new Coord()
            {
                X = 2, Y = 3
            }));
            Assert.IsFalse(board.ReceiveAttackAt( // Right of battleship
                               new Coord()
            {
                X = 7, Y = 3
            }));
        }
Beispiel #5
0
        public void ReceivingAttack_WhenBoardEmpty_ResultsInMiss()
        {
            var board = new BattleshipBoard(3, 3);

            for (var x = 0; x < board.Width; x++)
            {
                for (var y = 0; y < board.Height; y++)
                {
                    Assert.IsFalse(board.ReceiveAttackAt(new Coord()
                    {
                        X = x, Y = y
                    }));
                }
            }
        }
Beispiel #6
0
        public void HasBattleshipsRemaining_ReturnsTrue_UntilLoneBattleshipSunk()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsTrue(board.AddBattleship(
                              new Battleship(4, 1),
                              new Coord()
            {
                X = 0, Y = 0
            }));

            // Attack all cells of the lone battleship and sink it
            for (var i = 0; i < 4; i++)
            {
                Assert.IsTrue(board.HasBattleshipsRemaining());
                board.ReceiveAttackAt(new Coord()
                {
                    X = i, Y = 0
                });
            }
            Assert.IsFalse(board.HasBattleshipsRemaining());
        }