From 8a247dcc9a839579b98cc33c24b1149f200b3283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 1 Oct 2022 01:01:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=BD=D0=B0=20=D1=81?= =?UTF-8?q?=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 --- .../AirBomber/MapWithSetAirplanesGeneric.cs | 8 +-- AirBomber/AirBomber/SetAirplanesGeneric.cs | 60 ++++++++++++------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index f1b5c96..26a65ef 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -90,7 +90,7 @@ namespace AirBomber Shaking(); for (int i = 0; i < _setAirplanes.Count; i++) { - var airplane = _setAirplanes.Get(i); + var airplane = _setAirplanes[i]; if (airplane != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, airplane); @@ -119,11 +119,11 @@ namespace AirBomber int j = _setAirplanes.Count - 1; for (int i = 0; i < _setAirplanes.Count; i++) { - if (_setAirplanes.Get(i) == null) + if (_setAirplanes[i] == null) { for (; j > i; j--) { - var airplane = _setAirplanes.Get(j); + var airplane = _setAirplanes[j]; if (airplane != null) { _setAirplanes.Insert(airplane, i); @@ -175,7 +175,7 @@ namespace AirBomber int maxLeft = (countInLine - 1) * _placeSizeWidth; for (int i = 0; i < _setAirplanes.Count; i++) { - var airplane = _setAirplanes.Get(i); + var airplane = _setAirplanes[i]; airplane?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); airplane?.DrawningObject(g); } diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index 178d798..7acf838 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -14,20 +14,23 @@ namespace AirBomber 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 SetAirplanesGeneric(int count) { - _places = new T[count]; + _maxcount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -41,7 +44,7 @@ namespace AirBomber private bool isCorrectPosition(int position) { - return 0 <= position && position < Count; + return 0 <= position && position < _maxcount; } /// /// Добавление объекта в набор на конкретную позицию @@ -51,22 +54,11 @@ namespace AirBomber /// public bool Insert(T airplane, int position) { - int positionNullElement = position; - while (Get(positionNullElement) != null) - { - positionNullElement++; - } - // Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false - if (!isCorrectPosition(positionNullElement)) + if (!isCorrectPosition(position)) { return false; } - while (positionNullElement != position) // Смещение вправо - { - _places[positionNullElement] = _places[positionNullElement - 1]; - positionNullElement--; - } - _places[position] = airplane; + _places.Insert(position, airplane); return true; } /// @@ -78,7 +70,7 @@ namespace AirBomber { if (!isCorrectPosition(position)) return false; - _places[position] = null; + _places.RemoveAt(position); return true; } /// @@ -86,9 +78,35 @@ namespace AirBomber /// /// /// - public T Get(int position) + public T this[int position] { - return isCorrectPosition(position) ? _places[position] : null; + get + { + return isCorrectPosition(position) && position < Count ? _places[position] : null; + } + set + { + Insert(value, position); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetCars() + { + foreach (var car in _places) + { + if (car != null) + { + yield return car; + } + else + { + yield break; + } + } + } } }