diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs index 379f1c9..b2e97c1 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs @@ -90,7 +90,7 @@ namespace AircraftCarrier Shaking(); for (int i = 0; i < _setAircraftCarriers.Count; i++) { - var aircraftcarrier = _setAircraftCarriers.Get(i); + var aircraftcarrier = _setAircraftCarriers[i]; if (aircraftcarrier != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, aircraftcarrier); @@ -119,11 +119,11 @@ namespace AircraftCarrier int j = _setAircraftCarriers.Count - 1; for (int i = 0; i < _setAircraftCarriers.Count; i++) { - if (_setAircraftCarriers.Get(i) == null) + if (_setAircraftCarriers[i] == null) { for (; j > i; j--) { - var aircraftcarrier = _setAircraftCarriers.Get(j); + var aircraftcarrier = _setAircraftCarriers[j]; if (aircraftcarrier != null) { _setAircraftCarriers.Insert(aircraftcarrier, i); @@ -148,7 +148,7 @@ namespace AircraftCarrier for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - {//линия рамзетки места + {//линия разметки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } @@ -166,14 +166,13 @@ namespace AircraftCarrier int xNumOfPlaces = _pictureWidth / _placeSizeWidth; int rowNum = yNumOfPlaces - 1; int columnNum = 0; - for (int i = 0; i < _setAircraftCarriers.Count; i++) { - if (_setAircraftCarriers.Get(i) != null) + if (_setAircraftCarriers[i] != null) { - (float Left, float Top, float Right, float Bottom) = _setAircraftCarriers.Get(i).GetCurrentPosition(); - _setAircraftCarriers.Get(i).SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight); - _setAircraftCarriers.Get(i).DrawningObject(g); + (float Left, float Top, float Right, float Bottom) = _setAircraftCarriers[i].GetCurrentPosition(); + _setAircraftCarriers[i].SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight); + _setAircraftCarriers[i].DrawningObject(g); } if (columnNum == xNumOfPlaces - 1) { diff --git a/AircraftCarrier/AircraftCarrier/MapsCollection.cs b/AircraftCarrier/AircraftCarrier/MapsCollection.cs new file mode 100644 index 0000000..3050853 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MapsCollection.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class MapsCollection + { + /// + /// Словарь (хранилище) с картами + /// + readonly Dictionary> _mapStorages; + /// + /// Возвращение списка названий карт + /// + public List Keys => _mapStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public void AddMap(string name, AbstractMap map) + { + // TODO Прописать логику для добавления + if (Keys.Contains(name)) + return; + _mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map)); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // TODO Прописать логику для удаления + _mapStorages.Remove(name); + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetAircraftCarriersGeneric this[string + ind] + { + get + { + _mapStorages.TryGetValue(ind, out var mapWithSetAircraftCarriersGeneric); + return mapWithSetAircraftCarriersGeneric; + } + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs index 017e06b..6521afa 100644 --- a/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs @@ -9,18 +9,20 @@ namespace AircraftCarrier internal class SetAircraftCarriersGeneric where T : class { - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + private readonly int _maxCount; /// /// Конструктор /// /// public SetAircraftCarriersGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -29,8 +31,13 @@ namespace AircraftCarrier /// public int Insert(T AircraftCarrier) { + // TODO вставка в начало набора return Insert(AircraftCarrier, 0); } + private bool CorrectPos(int pos) + { + return 0 <= pos && pos < _maxCount; + } /// /// Добавление объекта в набор на конкретную позицию /// @@ -39,37 +46,15 @@ namespace AircraftCarrier /// public int Insert(T AircraftCarrier, int position) { - // TODO проверка позиции - if (position >= _places.Length || position < 0) + // проверка позиции + if (!CorrectPos(position)) + { return -1; - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] == null) - { - _places[position] = AircraftCarrier; - return position; } - // проверка, что после вставляемого элемента в массиве есть пустой элемент - int findEmptyPos = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - findEmptyPos = i; - break; - } - } - if (findEmptyPos < 0) return -1; - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - for (int i = findEmptyPos; i > position; i--) - { - _places[i] = _places[i - 1]; - } - // TODO вставка по позиции - _places[position] = AircraftCarrier; + // вставка по позиции + _places.Insert(position, AircraftCarrier); return position; } - /// /// Удаление объекта из набора с конкретной позиции /// @@ -77,11 +62,12 @@ namespace AircraftCarrier /// public T Remove(int position) { - // TODO проверка позиции - if (position >= _places.Length || position < 0) return null; - // TODO удаление объекта из массива, присовив элементу массива значение null + // проверка позиции + if (!CorrectPos(position)) + return null; + // удаление объекта из массива, присовив элементу массива значение null T temp = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return temp; } /// @@ -89,11 +75,31 @@ namespace AircraftCarrier /// /// /// - public T Get(int position) + public T this[int position] { - if (position >= _places.Length || position < 0) - return null; - return _places[position]; + get + { + return CorrectPos(position) && position < Count ? _places[position] : null; + } + set + { + Insert(value, position); + } + + } + public IEnumerable GetAircraftCarriers() + { + foreach (var aircraftcarrier in _places) + { + if (aircraftcarrier != null) + { + yield return aircraftcarrier; + } + else + { + yield break; + } + } } } } \ No newline at end of file