示例#1
0
        public void WhenShipAlreadySunk_ShouldNotRecordShot()
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var ships = new []
            {
                CreateShip("a1", cb),
                CreateShip("g1", cb)
            };

            var fleet = new Fleet(ships);

            fleet.AcceptShot(cb.CoordinateFromInput("a1"));
            fleet.AcceptShot(cb.CoordinateFromInput("b1"));
            fleet.AcceptShot(cb.CoordinateFromInput("c1"));
            fleet.AcceptShot(cb.CoordinateFromInput("d1"));

            Assert.True(ships[0].IsSunk);
            Assert.True(fleet.AnyShipOperational);
            Assert.True(fleet.ShotsRecorded.Any());
            Assert.True(fleet.ShotsRecorded.All(x => x.Value == ShotResult.SINK));

            fleet.AcceptShot(cb.CoordinateFromInput("a1"));
            Assert.True(fleet.ShotsRecorded.Last().Value == ShotResult.SINK);
            Assert.True(fleet.ShotsRecorded.Last().Key.ToString(cb) == "d1");
        }
        public void WhenCreatedFleet_ShouldNotBeDefeated()
        {
            var cb       = new TestCoordinateBoundary(4, 4);
            var random   = new TestRandomProvider(0);
            var strategy = new TestShotStrategy(cb);
            var builder  = new FleetBuilder(cb, random);
            var admiral  = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));

            Assert.False(admiral.IsDefeated);
        }
        public void ShouldCreateInvalidHorizontalPosition(string value, ShipSize size)
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var coord = cb.CoordinateFromInput(value);

            var result = ShipPosition.Horizontal(coord, size);

            Assert.False(result.IsValid);
            Assert.Null(result.Coordinates);
        }
        public void ShouldCreateHorizontalCoordinates(string value, ShipSize size)
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var coord = cb.CoordinateFromInput(value);

            var result = ShipPosition.Horizontal(coord, size);

            Assert.True(result.IsValid);
            Assert.NotNull(result.Coordinates);
            Assert.True(result.Coordinates.Count() == (int)size);
        }
示例#5
0
        public void WhenUnableToFitAllShips_ShouldThrow()
        {
            var cb      = new TestCoordinateBoundary(3, 3);
            var builder = new FleetBuilder(cb, random);

            var ships = new[]
            {
                ShipSize.Battleship
            };

            Assert.Throws <FleetAllocationException>(() => builder.BuildFleet(ships));
        }
        public void WhenGivenIncorrectCoordinates_ShouldNotRecordShot()
        {
            var cb       = new TestCoordinateBoundary(4, 4);
            var random   = new TestRandomProvider(0);
            var strategy = new TestShotStrategy(cb);
            var builder  = new FleetBuilder(cb, random);
            var admiral1 = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));
            var admiral2 = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));

            strategy.SetCoordinate("xx");
            Assert.Throws <BattleCoordinateException>(() => admiral1.ShootAt(admiral2));
            Assert.False(admiral2.Fleet.ShotsRecorded.Any());
        }
        public void WhenRandomlyShot_ShouldRecordRandomShot()
        {
            var cb       = new TestCoordinateBoundary(4, 4);
            var random   = new TestRandomProvider(0);
            var strategy = new RandomShotStrategy(cb, random);
            var builder  = new FleetBuilder(cb, random);
            var admiral1 = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));
            var admiral2 = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));

            admiral1.ShootAt(admiral2);
            Assert.True(admiral2.Fleet.ShotsRecorded.Any());
            Assert.True(admiral2.Fleet.ShotsRecorded.First().Key.ToString(cb) == "a1");
        }
示例#8
0
        public void When4BulkheadShipHitOnce_ShouldNotSink()
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var coord = cb.CoordinateFromInput("a1");
            var pos   = ShipPosition.Horizontal(coord, ShipSize.Destroyer);

            var ship = new Ship(pos);

            Assert.False(ship.IsSunk);

            ship.Hit(coord);

            Assert.False(ship.IsSunk);
        }
示例#9
0
        public void WhenAbleToFitAllShips_NoShipShouldOverlap(int colums, int rows, ShipSize[] ships)
        {
            var cb      = new TestCoordinateBoundary(colums, rows);
            var builder = new FleetBuilder(cb, random);

            var fleet = builder.BuildFleet(ships);

            Assert.NotNull(fleet);
            Assert.True(fleet.Count() == ships.Length);

            var allAllocatedCoordinates      = fleet.SelectMany(f => f.Coordinates).ToList();
            var distinctAllocatedCoordinates = allAllocatedCoordinates.Distinct(new BattleCoordinateEqualityComparer()).ToList();

            Assert.True(allAllocatedCoordinates.Count == distinctAllocatedCoordinates.Count);
        }
示例#10
0
        public void WhenShipHitTwiceAtSameCoords_ShouldNotCountAsHit()
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var coord = cb.CoordinateFromInput("a1");
            var pos   = ShipPosition.Horizontal(coord, ShipSize.Destroyer);

            var ship = new Ship(pos);

            Assert.False(ship.IsSunk);

            ship.Hit(coord);
            ship.Hit(new BattleCoordinate(coord.ColumnNumber + 1, coord.RowNumber, coord));
            ship.Hit(new BattleCoordinate(coord.ColumnNumber + 2, coord.RowNumber, coord));
            ship.Hit(coord);

            Assert.False(ship.IsSunk);
        }
示例#11
0
        public void WhenAllShipBulkheadsHit_ShouldSink()
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var coord = cb.CoordinateFromInput("a1");
            var pos   = ShipPosition.Horizontal(coord, ShipSize.Destroyer);

            var ship = new Ship(pos);

            Assert.False(ship.IsSunk);

            ship.Hit(coord);
            ship.Hit(new BattleCoordinate(coord.ColumnNumber + 1, coord.RowNumber, coord));
            ship.Hit(new BattleCoordinate(coord.ColumnNumber + 2, coord.RowNumber, coord));
            ship.Hit(new BattleCoordinate(coord.ColumnNumber + 3, coord.RowNumber, coord));

            Assert.True(ship.IsSunk);
        }
示例#12
0
        public void WhenShotAtShip_ShouldRecordShotAsHit()
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var ships = new []
            {
                CreateShip("a1", cb)
            };

            var fleet = new Fleet(ships);

            fleet.AcceptShot(cb.CoordinateFromInput("b1"));

            Assert.False(ships[0].IsSunk);
            Assert.True(fleet.ShotsRecorded.Any());
            Assert.True(fleet.ShotsRecorded.Last().Value == ShotResult.HIT);
            Assert.True(fleet.ShotsRecorded.Last().Key.ToString(cb) == "b1");
        }
示例#13
0
        public void WhenAbleToFitAllShips_ShouldNotThrow()
        {
            var cb      = new TestCoordinateBoundary(4, 4);
            var builder = new FleetBuilder(cb, random);

            var ships = new[]
            {
                ShipSize.Destroyer
            };

            var fleet = builder.BuildFleet(ships);

            Assert.NotNull(fleet);
            Assert.True(fleet.Any());
            Assert.True(fleet.First().Coordinates.Any());
            Assert.True(fleet.First().Coordinates.First().ToString(cb) == "a1");
            Assert.True(fleet.First().Coordinates.Last().ToString(cb) == "a4");
        }
示例#14
0
        public void WhenAllShipsSunk_ShouldReportNoShipsOperational()
        {
            var cb    = new TestCoordinateBoundary(10, 10);
            var ships = new []
            {
                CreateShip("a1", cb)
            };

            var fleet = new Fleet(ships);

            fleet.AcceptShot(cb.CoordinateFromInput("a1"));
            fleet.AcceptShot(cb.CoordinateFromInput("b1"));
            fleet.AcceptShot(cb.CoordinateFromInput("c1"));
            fleet.AcceptShot(cb.CoordinateFromInput("d1"));

            Assert.True(ships[0].IsSunk);
            Assert.False(fleet.AnyShipOperational);
        }
示例#15
0
        public void WhenAllShipsSunk_ShouldBeDefeated()
        {
            var cb       = new TestCoordinateBoundary(4, 1);
            var random   = new TestRandomProvider(0);
            var strategy = new TestShotStrategy(cb);
            var builder  = new FleetBuilder(cb, random);
            var admiral1 = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));
            var admiral2 = new FleetAdmiral(strategy, builder.BuildFleet(new [] { ShipSize.Destroyer }));

            strategy.SetCoordinate("a1");
            admiral1.ShootAt(admiral2);
            strategy.SetCoordinate("b1");
            admiral1.ShootAt(admiral2);
            strategy.SetCoordinate("c1");
            admiral1.ShootAt(admiral2);
            strategy.SetCoordinate("d1");
            admiral1.ShootAt(admiral2);

            Assert.True(admiral2.IsDefeated);
        }