From 6550575acd24e3c9ae25d1b07b2c54519b80189e Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Wed, 2 Nov 2022 22:00:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetAirFightersGeneric.cs | 25 ++-- .../AirFighter/SetAirFightersGeneric.cs | 124 ++++++++---------- 2 files changed, 67 insertions(+), 82 deletions(-) diff --git a/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs b/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs index 3f8684e..3fc878b 100644 --- a/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs @@ -94,13 +94,9 @@ namespace AirFighter public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setAirFighters.Count; i++) + foreach (var airFighter in _setAirFighters.GetAirFighters()) { - var airFighter = _setAirFighters.Get(i); - if (airFighter != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter); } return new(_pictureWidth, _pictureHeight); } @@ -125,11 +121,11 @@ namespace AirFighter int j = _setAirFighters.Count - 1; for (int i = 0; i < _setAirFighters.Count; i++) { - if (_setAirFighters.Get(i) == null) + if (_setAirFighters[i] == null) { for (; j > i; j--) { - var airFighter = _setAirFighters.Get(j); + var airFighter = _setAirFighters[j]; if (airFighter != null) { _setAirFighters.Insert(airFighter, i); @@ -179,22 +175,23 @@ namespace AirFighter int currentWidth = _pictureWidth / _placeSizeWidth - 1; int currentHeight = _pictureHeight / _placeSizeHeight - 1; - for (int i = 0; i < _setAirFighters.Count; i++) + foreach (var airFighter in _setAirFighters.GetAirFighters()) { - _setAirFighters.Get(i)?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setAirFighters.Get(i)?.DrawningObject(g); + airFighter?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + airFighter?.DrawningObject(g); - if(currentWidth > 0) + if (currentWidth > 0) { currentWidth -= 1; } else { - if(currentHeight > 0) + if (currentHeight > 0) { currentHeight -= 1; currentWidth = _pictureWidth / _placeSizeWidth - 1; - }else return; + } + else return; } } } diff --git a/AirFighter/AirFighter/SetAirFightersGeneric.cs b/AirFighter/AirFighter/SetAirFightersGeneric.cs index 4496e04..2800138 100644 --- a/AirFighter/AirFighter/SetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/SetAirFightersGeneric.cs @@ -15,40 +15,25 @@ namespace AirFighter 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 SetAirFightersGeneric(int count) { - _places = new T[count]; - } - /// - /// Проверка на наличие пустых мест - /// - /// - /// - private bool CheckNullPosition(int firstIndex) - { - if(firstIndex >= _places.Length && firstIndex < 0) - { - return false; - } - for(int i = firstIndex; i < _places.Length; i++) - { - if(_places[i] == null) - { - return true; - } - } - return false; + _places = new List(); + _maxCount = count; } /// /// Добавление объекта в набор @@ -67,40 +52,15 @@ namespace AirFighter /// public int Insert(T airFighter, int position) { - if(airFighter == null) + if (position < 0 && position > _maxCount) { return -1; } - if (_places[position] == null) - { - _places[position] = airFighter; - } else { - if(CheckNullPosition(position + 1)) - { - T tempMain = airFighter; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - _places[i] = tempMain; - break; - } - else - { - T temp2 = _places[i]; - _places[i] = tempMain; - tempMain = temp2; - } - } - } - else - { - return -1; - } + _places.Insert(position, airFighter); + return position; } - return position; } /// /// Удаление объекта из набора с конкретной позиции @@ -109,28 +69,56 @@ namespace AirFighter /// public T Remove(int position) { - if (position >= 0 && position < _places.Length && _places[position] != null) + if (position >= 0 && position < _maxCount && _places[position] != null) { T temp = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return temp; } else return null; - } - /// - /// Получение объекта из набора по позиции - /// - /// - /// - public T Get(int position) - { - if (_places[position] != null) + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T this[int position] + { + get { - return _places[position]; + if (position >= 0 && position < _maxCount) + { + return _places[position]; + } + else + return null; } - else - return null; + set + { + if (position >= 0 && position < _maxCount) + { + _places.Insert(position, value); + } + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetAirFighters() + { + foreach (var airFighter in _places) + { + if (airFighter != null) + { + yield return airFighter; + } + else + { + yield break; + } + } + } } } -}