示例#1
0
        public Battlegrid(int width, int height)
        {
            Width = width;
            Height = height;
            ThrowIfGridSizeIsInvalid();

            _map = new BattlegridMap(width, height);
        }
        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;
        }