diff --git a/AirBomber/AirBomber/MapWithSetJetsGeneric.cs b/AirBomber/AirBomber/MapWithSetJetsGeneric.cs index 05be60f..ee86681 100644 --- a/AirBomber/AirBomber/MapWithSetJetsGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetJetsGeneric.cs @@ -94,13 +94,9 @@ namespace AirBomber public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setJets.Count; i++) + foreach (var car in _setJets.GetJets()) { - var car = _setJets.Get(i); - if (car != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, car); } return new(_pictureWidth, _pictureHeight); } @@ -125,11 +121,11 @@ namespace AirBomber int j = _setJets.Count - 1; for (int i = 0; i < _setJets.Count; i++) { - if (_setJets.Get(i) == null) + if (_setJets[i] == null) { for (; j > i; j--) { - var car = _setJets.Get(j); + var car = _setJets[j]; if (car != null) { _setJets.Insert(car, i); @@ -189,9 +185,9 @@ namespace AirBomber { // установка позиции //_setJets.Get(i)?.SetObject(ccol * _placeSizeWidth, crow * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setJets.Get(i)?.SetObject(rows - i % cols * _placeSizeWidth, i / cols * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); - _setJets.Get(i)?.DrawningObject(g); - // (сначала передвигаемся влево) + _setJets[i]?.SetObject(rows - i % cols * _placeSizeWidth, i / cols * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); + _setJets[i]?.DrawningObject(g); + //(сначала передвигаемся влево) ccol++; if (ccol >= cols) { diff --git a/AirBomber/AirBomber/SetJetGeneric.cs b/AirBomber/AirBomber/SetJetGeneric.cs index 4abaf03..a3edbba 100644 --- a/AirBomber/AirBomber/SetJetGeneric.cs +++ b/AirBomber/AirBomber/SetJetGeneric.cs @@ -9,23 +9,26 @@ namespace AirBomber internal class SetJetGeneric where T : class { /// - /// Массив объектов, которые храним (самолетов) + /// Список объектов, которые храним (самолетов) /// - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; - private int JetPlaces = 0; - + public int Count => _places.Count; + /// + /// Ограничение максимального количества самолетов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// Количество самолетов public SetJetGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// @@ -34,29 +37,20 @@ namespace AirBomber /// Добавляемый самолет /// Позиция /// - public int Insert(T jet) - { - return Insert(jet, 0); - } + //public int Insert(T jet) + //{ + // return Insert(jet, 0); + //} public int Insert(T jet, int position = 0) { - if (position < 0 || position >= _places.Length || JetPlaces == _places.Length) + if (position < 0 || position >= _maxCount || _maxCount <= _places.Count) { return -1; } - JetPlaces++; - while (_places[position] != null) - { - for (int i = _places.Length - 1; i > 0; --i) - { - if (_places[i] == null && _places[i - 1] != null) - { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = jet; + _places.Insert(position, jet); + + _places.Remove(null); + return position; } /// @@ -66,9 +60,13 @@ namespace AirBomber /// public T Remove(int position) { - if (position < 0 || position >= _places.Length) return null; + if (position < 0 || position >= _maxCount) return null; + T savedJet = _places[position]; + _places[position] = null; + + return savedJet; } @@ -77,11 +75,40 @@ namespace AirBomber /// /// Позиция получаемого самолета /// - public T Get(int position) + public T this[int position] { - // проверка позиции - if (position < 0 || position >= _places.Length) return null; - return _places[position]; + get + { + // проверка позиции + if (position < 0 || position >= _maxCount) return null; + return _places[position]; + } + set + { + // проверка позиции + if (position < 0 || position >= _maxCount) + { + _places[position] = value; + } + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetJets() + { + foreach (var jet in _places) + { + if (jet != null) + { + yield return jet; + } + else + { + yield break; + } + } } } }