Exemple #1
0
 private static void WriteCheatInfo(Battlegrid battlegrid)
 {
     foreach (var ship in battlegrid.Ships)
         Console.WriteLine(
             "{0} x {1} : {2}",
             (char)(ship.Position.X + 65),
             ship.Position.Y,
             ship.Orientation);
 }
        public void Given_a_10_by_10_grid_when_a_ship_is_added_and_pebbles_thrown_at_it_then_expect_hits_and_eventually_a_sinking()
        {
            var battlegrid =
                new Battlegrid(10, 10)
                    .Add(new Destroyer(), new Position(4, 4), Orientation.Horizontal);

            Assert.That(battlegrid.LaunchPebble(new Position(4, 4)) == StrikeResult.Hit);
            Assert.That(battlegrid.LaunchPebble(new Position(5, 4)) == StrikeResult.Hit);
            Assert.That(battlegrid.LaunchPebble(new Position(6, 4)) == StrikeResult.Hit);
            Assert.That(battlegrid.LaunchPebble(new Position(7, 4)) == StrikeResult.FullySunk);
        }
        public Battlegrid Build(int width, int height, IEnumerable<IShip> ships)
        {
            var battlegrid = new Battlegrid(width, height);
            var map = new BattlegridMap(battlegrid.Width, battlegrid.Height);

            foreach (var ship in ships) {
                var attempts = 0;
                do {
                    Relocate(ship, battlegrid.Width, battlegrid.Height);

                    if (attempts++ > MaxAttempts)
                        throw new BattlegridException(
                            "Gave up whilst trying to randomly place the ship on the battlegrid. " +
                            "There is probably no viable positions left.");

                } while (map.TryAdd(ship) != HitType.None);

                battlegrid.Add(ship);
            }

            return battlegrid;
        }