예제 #1
0
        public void GivenMapWithShipPlaced_WhenPlacingShipOverlappingOtherShip_DoesNotAddShipToCells()
        {
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(width / 2, height / 2);
            var       ship       = ShipStub.FullStub(this.player);

            map.Place(ship, coordinate, Direction.East);

            var overLappingShip = ShipStub.FullStub(this.player);

            try
            {
                map.Place(overLappingShip, coordinate, Direction.East);
            }
            catch (Exception)
            {
                //No-Op
            }

            var cellsWithOccupants = map.Cells.Where(x => x.Occupied);

            foreach (var cellsWithOccupant in cellsWithOccupants)
            {
                Assert.That(!overLappingShip.Cells.Contains(cellsWithOccupant));
            }
        }
        public void GivenShip_WhenConstructing_AddsCorrectAmountOfShipSegments()
        {
            const int segmentCount = 4;

            Ship ship = new ShipStub(player, segmentCount);

            Assert.AreEqual(segmentCount, ship.Cells.Count());
        }
        public void GivenShip_WhenConstructing_AssociatesThePlayerToShip()
        {
            const int segmentCount = 4;

            Ship ship = new ShipStub(player, segmentCount);

            Assert.AreEqual(player, ship.Owner);
        }
예제 #4
0
        public void GivenShip_WhenConstructing_AssociatesThePlayerToShip()
        {
            const int segmentCount = 4;

            Ship ship = new ShipStub(player, segmentCount, new WeaponStub(player, 1, WeaponType.SingleShot));

            Assert.AreEqual(player, ship.Owner);
        }
예제 #5
0
        public void GivenMap_WhenPlacingShipWithInvalidDirection_ThrowsException(Direction direction)
        {
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(width / 2, height / 2);
            var       ship       = ShipStub.FullStub(this.player);

            Assert.Throws <InvalidOperationException>(() => map.Place(ship, coordinate, direction));
        }
예제 #6
0
        public void GivenMap_WhenPlacingShipWithDirectionLeadingToOutOfBounds_ThrowsException(Direction direction)
        {
            const int width      = 1;
            const int height     = 1;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(width / 2, height / 2);
            var       ship       = ShipStub.FullStub(this.player, new WeaponStub(player, 1, WeaponType.SingleShot));

            Assert.Throws <InvalidOperationException>(() => map.Place(ship, coordinate, direction));
        }
예제 #7
0
        public void GivenMap_WhenPlacingShipOfOtherPlayer_ThrowsException()
        {
            const int width       = 5;
            const int height      = 5;
            var       map         = new PlayerMap(width, height, this.player);
            var       coordinate  = new Point(width / 2, height / 2);
            var       otherPlayer = new BattleshipPlayer("OtherPlayer", 'A', PlayerType.One);
            var       ship        = ShipStub.FullStub(otherPlayer);

            Assert.Throws <InvalidOperationException>(() => map.Place(ship, coordinate, Direction.East));
        }
예제 #8
0
        public void GivenMapWithShipPlaced_WhenPlacingShipOverlappingOtherShip_ThrowsException()
        {
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(width / 2, height / 2);
            var       ship       = ShipStub.FullStub(this.player);

            map.Place(ship, coordinate, Direction.West);

            var overLappingShip = ShipStub.FullStub(this.player);

            Assert.Throws <InvalidOperationException>(() => map.Place(overLappingShip, coordinate, Direction.East));
        }
        public void GivenShip_WhenConstruting_SetsDestroyedToFalse()
        {
            const int segmentCount = 4;

            Ship      ship       = new ShipStub(player, segmentCount);
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(0, 0);

            ship.Place(coordinate, Direction.East, map);

            Assert.False(ship.Destroyed);
        }
예제 #10
0
        public void GivenShipWithAllSegmentsUnDamaged_WhenDamagingSegment_DoesNotMarkShipAsDestroyed()
        {
            var       ship       = new ShipStub(player, 2);
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(0, 0);

            ship.Place(coordinate, Direction.East, map);
            var segmentToDamage = ship.Cells.First();

            segmentToDamage.LandShot();

            Assert.False(ship.Destroyed);
        }
예제 #11
0
        public void GivenShipWithOneSegment_WhenDamagingSegment_MarksCellsAsDestroyed()
        {
            var       ship       = new ShipStub(player, 1, new WeaponStub(player, 1, WeaponType.SingleShot));
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(0, 0);

            ship.Place(coordinate, Direction.East, map);
            var segmentToDamage = ship.Cells.First();

            segmentToDamage.LandShot();

            Assert.True(ship.Cells.All(x => x != null && x.Hit));
        }
예제 #12
0
        public void GivenMap_WhenPlacingShip_PlacesShip(Direction direction)
        {
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(width / 2, height / 2);
            var       ship       = ShipStub.FullStub(this.player);

            map.Place(ship, coordinate, direction);

            var cellOccupants = map.Cells.Where(x => x.Occupied).Select(x => x.OccupiedBy).ToList();

            foreach (var segment in ship.Cells)
            {
                Assert.Contains(segment, map.Cells.ToList());
            }
        }
예제 #13
0
        public void GivenShipWithAllButOneSegmentDamaged_WhenDamagingSegment_MarksShipAsDestroyed()
        {
            var       ship       = new ShipStub(player, 4);
            const int width      = 5;
            const int height     = 5;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(0, 0);

            ship.Place(coordinate, Direction.East, map);
            var segmentToDamage = ship.Cells.First();

            foreach (var segments in ship.Cells.Where(x => x != segmentToDamage))
            {
                segments.LandShot();
            }

            segmentToDamage.LandShot();

            Assert.True(ship.Destroyed);
        }
예제 #14
0
        public void GivenMap_WhenPlacingShipWithDirectionLeadingToOutOfBounds_DoesAddShipToCells(Direction direction)
        {
            const int width      = 1;
            const int height     = 1;
            var       map        = new PlayerMap(width, height, this.player);
            var       coordinate = new Point(width / 2, height / 2);
            var       ship       = ShipStub.FullStub(this.player);

            try
            {
                map.Place(ship, coordinate, direction);
            }
            catch (Exception)
            {
                //No-Op
            }

            var cellsWithOccupants = map.Cells.Where(x => x.Occupied);

            Assert.IsEmpty(cellsWithOccupants);
        }