コード例 #1
0
        public void ShouldFailToPlaceAShip_WhenIncorrectCorrectPositionsProvided(
            int boardRows, int boardColumns,
            int placementRow, int placementColumn,
            ShipType shipType)
        {
            //arrange

            //first create a board
            var boardCreator = new BoardCreator();
            var board        = boardCreator.CreateBoard(boardRows, boardColumns);

            //then create a ship
            var shipCreator = new ShipCreator();
            var ship        = shipCreator.CreateShip(shipType);

            //act
            //now place the ship on the board
            var shipPlacer = new ShipPlacer();

            IndexOutOfRangeException ex = Assert.Throws <IndexOutOfRangeException>(() =>
                                                                                   shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn));

            //assert
            Assert.Equal("Ship's placement position is out of bounds", ex.Message);
        }
コード例 #2
0
        public void ShouldPlaceAShip_WhenCorrectPositionsProvided(
            int boardRows, int boardColumns,
            int placementRow, int placementColumn,
            ShipType shipType)
        {
            //arrange

            //first create a board
            var boardCreator = new BoardCreator();
            var board        = boardCreator.CreateBoard(boardRows, boardColumns);

            //then create a ship
            var shipCreator = new ShipCreator();
            var ship        = shipCreator.CreateShip(shipType);

            //act
            //now place the ship on the board
            var shipPlacer = new ShipPlacer();

            shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn);

            //assert

            /*check that the ship has been placed on the board and that the board's
             * status is occupied for the ship*/
            Assert.True(
                board.BoardCellStatuses[placementRow, placementColumn] == BoardCellStatus.Occupied &&
                board.BoardCellStatuses[placementRow, placementColumn + ship.Size - 1] == BoardCellStatus.Occupied
                );
        }
コード例 #3
0
        public void ShouldReturnException_WhenIncorrectAttackCoordinatesProvided(
            int boardRows, int boardColumns,
            int placementRow, int placementColumn,
            int attackRow, int attackColumn,
            ShipType shipType)
        {
            //arrange

            //first create a board
            var boardCreator = new BoardCreator();
            var board        = boardCreator.CreateBoard(boardRows, boardColumns);

            //then create a ship
            var shipCreator = new ShipCreator();
            var ship        = shipCreator.CreateShip(shipType);

            //place the ship on the board
            var shipPlacer = new ShipPlacer();

            shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn);

            //act
            //now attack the ship at the given position
            var attacker = new Attacker();
            IndexOutOfRangeException ex = Assert.Throws <IndexOutOfRangeException>(() =>
                                                                                   attacker.Attack(board, attackRow, attackColumn));

            //assert
            Assert.Equal("Attack position is out of bounds", ex.Message);
        }
コード例 #4
0
        public void ShouldNotReturnLostGameStatusTrue_WhenShipsNotSunk()
        {
            //arrange

            //first create a board
            var boardCreator = new BoardCreator();
            var board        = boardCreator.CreateBoard(10, 10);

            //then create a ship
            var shipCreator = new ShipCreator();
            var ship        = shipCreator.CreateShip(ShipType.Destroyer);

            //place the ship on the board
            var shipPlacer = new ShipPlacer();

            shipPlacer.AddShipToBoard(ship, board, 0, 0);

            //act
            //now attack the ship at all given positions for the ship
            var attacker = new Attacker();

            attacker.Attack(board, 0, 1);

            //assert that the status is not lost
            Assert.False(board.HasLost);
        }
コード例 #5
0
        public void ShouldReturnCorrectAttackStatus_WhenAttackLaunched(
            int boardRows, int boardColumns,
            int placementRow, int placementColumn,
            int attackRow, int attackColumn,
            ShipType shipType, BoardCellStatus boardCellStatus)
        {
            //arrange

            //first create a board
            var boardCreator = new BoardCreator();
            var board        = boardCreator.CreateBoard(boardRows, boardColumns);

            //then create a ship
            var shipCreator = new ShipCreator();
            var ship        = shipCreator.CreateShip(shipType);

            //place the ship on the board
            var shipPlacer = new ShipPlacer();

            shipPlacer.AddShipToBoard(ship, board, placementRow, placementColumn);

            //act
            //now attack the ship at the given position
            var attacker = new Attacker();

            attacker.Attack(board, attackRow, attackColumn);

            //assert
            /*check that the status on the board is hit*/
            Assert.True(
                board.BoardCellStatuses[attackRow, attackColumn] == boardCellStatus
                );
        }