From 20616550bc843ede097203a7f51e90ede87e8ffb Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Sun, 9 Oct 2022 18:27:29 +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,=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8?= =?UTF-8?q?=D0=B1=D0=BE=D0=BA=20=D0=B2=D0=BE=20=D0=B2=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BE=D0=BC=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80?= =?UTF-8?q?=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D0=BE?= =?UTF-8?q?=D0=BC=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetArmoredCarsGeneric.cs | 27 +++--- .../ArmoredCar/SetArmoredCarsGeneric.cs | 89 +++++++++++-------- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs index 24ac509..8b95e84 100644 --- a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs @@ -90,13 +90,9 @@ namespace ArmoredCar public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setCars.Count; i++) + foreach (var armoredCar in _setCars.GetCars()) { - var car = _setCars.Get(i); - if (car != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar); } return new(_pictureWidth, _pictureHeight); } @@ -121,11 +117,11 @@ namespace ArmoredCar int j = _setCars.Count - 1; for (int i = 0; i < _setCars.Count; i++) { - if (_setCars.Get(i) == null) + if (_setCars[i] == null) { for (; j > i; j--) { - var car = _setCars.Get(j); + var car = _setCars[i]; if (car != null) { _setCars.Insert(car, i); @@ -177,14 +173,13 @@ namespace ArmoredCar private void DrawArmoredCars(Graphics g) { int width = _pictureWidth / _placeSizeWidth; - - for (int i = 0; i < _setCars.Count; i++) - { - if (_setCars.Get(i) != null) { - _setCars.Get(i)?.SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setCars.Get(i)?.DrawningObject(g); - } - } + int k = 0; + foreach (var armoredCar in _setCars.GetCars()) + { + armoredCar.SetObject(k % width * _placeSizeWidth + 5, k / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + armoredCar.DrawningObject(g); + k++; + } } } } diff --git a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs index 550c07e..a4057ec 100644 --- a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs @@ -10,20 +10,23 @@ namespace ArmoredCar 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 SetArmoredCarsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -31,8 +34,10 @@ namespace ArmoredCar /// Добавляемый автомобиль /// public int Insert(T armoredCar) - { - return Insert(armoredCar, 0); + { + if (Count < _maxCount) + return Insert(armoredCar, 0); + return -1; } /// /// Добавление объекта в набор на конкретную позицию @@ -42,31 +47,10 @@ namespace ArmoredCar /// public int Insert(T armoredCar, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) return -1; - - if (!(_places[position] == null)) - { - int index_empty = -1; - // поиск первого пустого элемента - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - index_empty = i; - } - } - if (index_empty == -1) - return -1; - else - { - for (int i = index_empty; i > position; i--) - { - _places[i] = _places[i - 1]; - } - } - } - _places[position] = armoredCar; + + _places.Insert(position, armoredCar); return position; } /// @@ -76,22 +60,55 @@ namespace ArmoredCar /// public T Remove(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) return null; T armoredCar = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return armoredCar; } + + /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) - return null; - return _places[position]; + get + { + if (position < 0 || position >= _maxCount) + return null; + return _places[position]; + } + set + { + + if (position < 0 || position >= _maxCount) + return; + + _places.Add(value); + } } + + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetCars() + { + foreach (var car in _places) + { + if (car != null) + { + yield return car; + } + else + { + yield break; + } + } + } + } }