Exemple #1
0
        public RowCol FindAdjacentEmptyCell(BattleshipBoard board)
        {
            RowCol AdjacentEmptyCell;

            bool IsAdjacentEmpty()
            {
                return(AdjacentEmptyCell.IsValid() && board.findPossibleTarget(AdjacentEmptyCell));
            }

            AdjacentEmptyCell = new RowCol(Row + 1, Col);
            if (IsAdjacentEmpty())
            {
                return(AdjacentEmptyCell);
            }

            AdjacentEmptyCell = new RowCol(Row, Col + 1);
            if (IsAdjacentEmpty())
            {
                return(AdjacentEmptyCell);
            }

            AdjacentEmptyCell = new RowCol(Row - 1, Col);
            if (IsAdjacentEmpty())
            {
                return(AdjacentEmptyCell);
            }

            AdjacentEmptyCell = new RowCol(Row, Col - 1);
            if (IsAdjacentEmpty())
            {
                return(AdjacentEmptyCell);
            }

            return(new RowCol(-1, -1));
        }
Exemple #2
0
        public void TestBattleshipBoardCreation()
        {
            List <Coordinate> battleship1Coords = new List <Coordinate> {
                new Coordinate(0, 0), new Coordinate(0, 1), new Coordinate(0, 2)
            };
            Battleship battleship1 = new Battleship(battleship1Coords);

            List <Coordinate> battleship2Coords = new List <Coordinate> {
                new Coordinate(2, 1), new Coordinate(3, 1), new Coordinate(4, 1)
            };
            Battleship battleship2 = new Battleship(battleship2Coords);

            BattleshipBoard board = new BattleshipBoard(new List <Battleship> {
                battleship1, battleship2
            });

            Assert.IsTrue(board.RemainingBattleships == 2);

            // Test overlapping ship
            List <Coordinate> overlappingBattleshipCoords = new List <Coordinate> {
                new Coordinate(3, 1), new Coordinate(3, 2), new Coordinate(3, 3)
            };

            Assert.ThrowsException <ArgumentException>(() => board.AddBattleship(new Battleship(overlappingBattleshipCoords)));

            // Test overlapping ship with illegal coordinates
            List <Coordinate> overlappingIllegalBattleshipCoords = new List <Coordinate> {
                new Coordinate(-1, 0), new Coordinate(0, 0), new Coordinate(1, 0)
            };

            Assert.ThrowsException <ArgumentException>(() => board.AddBattleship(new Battleship(overlappingIllegalBattleshipCoords)));
        }
        public void BattleShipOperation_CreateAndSink()
        {
            // Arrange
            var             expected = 1;
            BattleshipBoard btl      = new BattleshipBoard();

            btl.BuildShips(ShipType.Battleship);
            for (int i = 1; i <= 10; i++)
            {
                for (char c = 'A'; c <= 'J'; c++)
                {
                    var square = new Tuple <char, int>(c, i);
                    if (btl.GetSquareFromComputerOcean(square).ShipType is ShipType.Battleship)
                    {
                        btl.SetRedSquareComputerOcean(square);
                    }
                }
            }

            //act
            var actual = btl.GetComputerSinkShipCount(ShipType.Battleship);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #4
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
            }));
        }
Exemple #5
0
        public void AddingBattleship_ShouldReturnFalse_WhenNotPlacedOnBoard()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsFalse(board.AddBattleship( // Above
                               new Battleship(1, 5),
                               new Coord()
            {
                X = 0, Y = -1
            }));
            Assert.IsFalse(board.AddBattleship( // Left
                               new Battleship(1, 5),
                               new Coord()
            {
                X = -5, Y = 0
            }));
            Assert.IsFalse(board.AddBattleship( // Right
                               new Battleship(1, 5),
                               new Coord()
            {
                X = 10, Y = 0
            }));
            Assert.IsFalse(board.AddBattleship( // Below
                               new Battleship(1, 5),
                               new Coord()
            {
                X = 0, Y = 10
            }));
        }
        public void Initialize()
        {
            var board = new BattleshipBoard();

            board.Initialize(new RandomBoardFiller());
            Assert.Equal(5 + 4 + 3 + 3 + 2, board.Count(s => s != SquareContent.Water));
        }
        public void ReadOnlyList_Failure()
        {
            var board = new BattleshipBoard();

            Assert.Throws <ArgumentOutOfRangeException>(() => board[-1]);
            Assert.Throws <ArgumentOutOfRangeException>(() => board[10 * 10]);
        }
Exemple #8
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());
        }
        public void TestCheckSquare_CheckNeighborCellIsFreeIfCellX4Y3IsNotFree_ReturnFalse()
        {
            BattleshipBoard board = new BattleshipBoard(10);
            Ship            ship  = new Ship(4, 3, ShipTypes.CRUISER, Directions.UP);

            board.AddShip(ship);
            Assert.IsFalse(board.CheckSquare(3, 4, board));
        }
Exemple #10
0
 public void TryAttack_AttackingOutsideBoard_ShouldThrowException()
 {
     Assert.ThrowsException <Exception>(() =>
     {
         var board = new BattleshipBoard(3);
         board.TryAttack(0, 3);
     }, "Point (0, 3) is outside board.");
 }
Exemple #11
0
 public void AddBattleShip_AddBattleshipWithDiagonalPosition_ShouldThrowException()
 {
     Assert.ThrowsException <Exception>(() =>
     {
         var board = new BattleshipBoard(3);
         board.AddBattleShip(0, 0, 2, 2);
     }, "The position of the battleship needs to be at straight line position.");
 }
Exemple #12
0
 public void AddBattleShip_AddBattleshipOutsideBoard_ShouldThrowException()
 {
     Assert.ThrowsException <Exception>(() =>
     {
         var board = new BattleshipBoard(3);
         board.AddBattleShip(1, 1, 1, 5);
     }, "Point (1, 5) is outside board.");
 }
        public void ReadOnlyList()
        {
            var board = new BattleshipBoard();

            board.PlaceShip(new BoardIndex(0, 0), 1, Direction.Horizontal);
            Assert.Equal(SquareContent.Ship, board[0]);
            Assert.Equal(SquareContent.Water, board[99]);
        }
Exemple #14
0
        public void HasLostTheGame_AllShipsAreStillIntact_GameIsNotLost()
        {
            var board = new BattleshipBoard(3);

            Assert.AreEqual(1, board.AddBattleShip(0, 0, 0, 2));
            Assert.AreEqual(2, board.AddBattleShip(1, 0, 2, 0));
            Assert.AreEqual(3, board.AddBattleShip(1, 1, 2, 1));
            Assert.AreEqual(false, board.HasLostTheGame());
        }
Exemple #15
0
 public void AddBattleShip_AddBattleshipOnOccupiedSpaceLineReversed_ShouldThrowException()
 {
     Assert.ThrowsException <Exception>(() =>
     {
         var board = new BattleshipBoard(3);
         board.AddBattleShip(0, 0, 0, 2);
         board.AddBattleShip(2, 1, 0, 1);
     }, "Can not add a battleship from (2, 1) to (0, 1) because the space is occupied.");
 }
Exemple #16
0
        static void Main(string[] args)
        {
            BattleshipBoard board        = new BattleshipBoard();
            TigerWarShip    tigerWarShip = new TigerWarShip();

            board.CreateRandomBoard();
            tigerWarShip.Play(board);
            Console.ReadLine();
        }
        public void PlaceShip_Horizontal()
        {
            var board = new BattleshipBoard();

            board.PlaceShip(new BoardIndex(0, 0), 2, Direction.Horizontal);
            Assert.Equal(SquareContent.Ship, board[new BoardIndex(0, 0)]);
            Assert.Equal(SquareContent.Ship, board[new BoardIndex(1, 0)]);
            Assert.Equal(2, board.Count(s => s == SquareContent.Ship));
        }
        public async Task CreateBoard(string name, List <ShipPosition> board)
        {
            BattleshipBoard battleshipBoard = new BattleshipBoard
            {
                Id    = name,
                Board = board
            };

            await DdbContext.SaveAsync(battleshipBoard);
        }
Exemple #19
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
            }));
        }
Exemple #20
0
        public void TestIsOver_AddOneShipX4Y3DirUpThenDestroyIt_ReturnTrue()
        {
            BattleshipBoard board = new BattleshipBoard(10);
            Ship            ship  = new Ship(4, 3, ShipTypes.BOAT, Directions.UP);

            board.AddShip(ship);
            ship.CellsList[0].IsDestroyed = true;
            Game g = new Game(new NoHitState());

            Assert.IsTrue(g.IsOver(board));
        }
Exemple #21
0
        public void AddingBattleship_ShouldReturnTrue_WhenInLowerRightCorner()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsTrue(board.AddBattleship(
                              new Battleship(2, 2),
                              new Coord()
            {
                X = 8, Y = 8
            }));
        }
Exemple #22
0
        public void AddingBattleship_ShouldReturnTrue_WhenExactSizeOfBoard()
        {
            var board = new BattleshipBoard(10, 10);

            Assert.IsTrue(board.AddBattleship(
                              new Battleship(10, 10),
                              new Coord()
            {
                X = 0, Y = 0
            }));
        }
Exemple #23
0
        public void HasLostTheGame_AllShipsAreDestroyed_GameIsLost()
        {
            var board = new BattleshipBoard(3);

            Assert.AreEqual(1, board.AddBattleShip(0, 0, 0, 2));
            Assert.AreEqual(2, board.AddBattleShip(1, 0, 2, 0));
            Assert.AreEqual(3, board.AddBattleShip(1, 1, 2, 1));
            Assert.AreEqual(BattleshipBoard.AttackResult.Hit, board.TryAttack(0, 1)); // Ship 1 is down
            Assert.AreEqual(BattleshipBoard.AttackResult.Hit, board.TryAttack(1, 0)); // Ship 2 is down
            Assert.AreEqual(BattleshipBoard.AttackResult.Hit, board.TryAttack(2, 1)); // Ship 3 is down
            Assert.AreEqual(true, board.HasLostTheGame());                            // At this point, all battleships should be destroyed
        }
Exemple #24
0
        public void TryAttack_SuccessfullyAttackingAShip_ShouldThrowException()
        {
            var board = new BattleshipBoard(3);

            Assert.AreEqual(1, board.AddBattleShip(0, 0, 0, 2));
            Assert.AreEqual(2, board.AddBattleShip(1, 0, 2, 0));
            Assert.AreEqual(3, board.AddBattleShip(1, 1, 2, 1));
            Assert.AreEqual(BattleshipBoard.AttackResult.Hit, board.TryAttack(0, 1));  // Ship 1 is down
            Assert.AreEqual(BattleshipBoard.AttackResult.Miss, board.TryAttack(0, 1)); // Attacking same location is a miss
            Assert.AreEqual(BattleshipBoard.AttackResult.Miss, board.TryAttack(0, 0)); // Attacking any part of Ship 1 again will result in a miss
            Assert.AreEqual(BattleshipBoard.AttackResult.Miss, board.TryAttack(2, 2)); // Attacking at empty location will produce a miss
        }
Exemple #25
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
            }));
        }
Exemple #26
0
        public Interpreter(IBattleshipBoardService battleshipBoardService, IGamePlayBoardService gamePlayBoardService)
        {
            _battleshipBoardService = battleshipBoardService;
            _battleshipBoard        = _battleshipBoardService.GetBattleshipBoard();
            _gamePlayBoardService   = gamePlayBoardService;

            _inputValidator = new InputValidator(battleshipBoardService);
            _inputValidator.InputValidated += InputValidator_InputValidated;

            _gamePlay = new GamePlay(_gamePlayBoardService, _battleshipBoardService);
            _gamePlay.GamePlayCompleted += GamePlay_GamePlayCompleted;
        }
        /// <inheritdoc/>
        public Game Create(int player1Index, int player2Index)
        {
            var boards = new BattleshipBoard[2];

            for (var i = 0; i < 2; i++)
            {
                boards[i] = new BattleshipBoard();
                filler.Fill(BattleshipBoard.Ships, boards[i]);
            }

            return(new Game(Guid.NewGuid(), new[] { player1Index, player2Index }, boards,
                            new[] { new BoardContent(SquareContent.Unknown), new BoardContent(SquareContent.Unknown) }));
        }
Exemple #28
0
        public void HasBattleshipsRemaining_ReturnsTrue_WhenOneUndamagedBattleship()
        {
            var board = new BattleshipBoard(10, 10);

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

            Assert.IsTrue(board.HasBattleshipsRemaining());
        }
Exemple #29
0
        public void HasLostTheGame_NotAllShipsAreDestroyed_GameIsNotLost()
        {
            var board = new BattleshipBoard(3);

            Assert.AreEqual(1, board.AddBattleShip(0, 0, 0, 2));
            Assert.AreEqual(2, board.AddBattleShip(1, 0, 2, 0));
            Assert.AreEqual(3, board.AddBattleShip(1, 1, 2, 1));
            Assert.AreEqual(BattleshipBoard.AttackResult.Hit, board.TryAttack(0, 1));  // Ship 1 is down
            Assert.AreEqual(BattleshipBoard.AttackResult.Miss, board.TryAttack(0, 1)); // Attacking same location is a miss
            Assert.AreEqual(BattleshipBoard.AttackResult.Miss, board.TryAttack(0, 0)); // Attacking any part of Ship 1 again will result in a miss
            Assert.AreEqual(BattleshipBoard.AttackResult.Miss, board.TryAttack(2, 2)); // Attacking at empty location will produce a miss
            Assert.AreEqual(BattleshipBoard.AttackResult.Hit, board.TryAttack(1, 0));  // Attack ship 2
            Assert.AreEqual(false, board.HasLostTheGame());                            // At this point, battleship 3 are still alive
        }
Exemple #30
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
                    }));
                }
            }
        }