diff --git a/ArmoredVehicle/MapWithSetMachineGeneric.cs b/ArmoredVehicle/MapWithSetMachineGeneric.cs index 77543f0..e5fab20 100644 --- a/ArmoredVehicle/MapWithSetMachineGeneric.cs +++ b/ArmoredVehicle/MapWithSetMachineGeneric.cs @@ -49,10 +49,6 @@ _map = map; } - - - - /// /// Перегрузка оператора сложения /// @@ -92,13 +88,9 @@ public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setMachines.Count; i++) + foreach (var machine in _setMachines.GetMachine()) { - var car = _setMachines.Get(i); - if (car != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, machine); } return new(_pictureWidth, _pictureHeight); } @@ -120,14 +112,15 @@ /// private void Shaking() { + int j = _setMachines.Count - 1; for (int i = 0; i < _setMachines.Count; i++) { - if (_setMachines.Get(i) == null) + if (_setMachines[i] == null) { for (; j > i; j--) { - var car = _setMachines.Get(j); + var car = _setMachines[j]; if (car != null) { _setMachines.Insert(car, i); @@ -141,6 +134,7 @@ } } } + } /// /// Метод отрисовки фона @@ -174,18 +168,18 @@ int currentWidth = 0; int currentHeight = 0; - for (int i = 0; i < _setMachines.Count; i++) + foreach (var machine in _setMachines.GetMachine()) { int dop = 0; - if(currentWidth > 0 && currentHeight > 0) + if (currentWidth > 0 && currentHeight > 0) { dop = 30; } - _setMachines.Get(i)?.SetObject(currentWidth * _placeSizeWidth + dop, + + machine.SetObject(currentWidth * _placeSizeWidth + dop, currentHeight * _placeSizeHeight + dop, _pictureWidth, _pictureHeight); - _setMachines.Get(i)?.DrawningObject(g); - + machine?.DrawningObject(g); if (currentWidth < width - 1) { currentWidth++; @@ -196,8 +190,9 @@ currentHeight++; } if (currentHeight > height) return; + } - + } } } \ No newline at end of file diff --git a/ArmoredVehicle/SetMachineGeneric.cs b/ArmoredVehicle/SetMachineGeneric.cs index 91a8fdc..eb2a9a5 100644 --- a/ArmoredVehicle/SetMachineGeneric.cs +++ b/ArmoredVehicle/SetMachineGeneric.cs @@ -8,13 +8,14 @@ where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; + private readonly int _maxCount; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; private int BusyPlaces = 0; /// /// Конструктор @@ -22,7 +23,9 @@ /// public SetMachineGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); + } /// /// Добавление объекта в набор @@ -31,7 +34,8 @@ /// public int Insert(T machine) { - return Insert(machine, 0); + if (Count + 1 <= _maxCount) return Insert(machine, 0); + else return -1; } /// /// Добавление объекта в набор на конкретную позицию @@ -41,21 +45,13 @@ /// public int Insert(T machine, int position) { - if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return -1; - - BusyPlaces++; - while (_places[position] != null) + if (position >= _maxCount && position < 0) { - 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; - } - } + return -1; } - _places[position] = machine; + + _places.Insert(position, machine); + return position; } /// @@ -65,21 +61,68 @@ /// public T Remove(int position) { - if (position < 0 || position >= _places.Length) return null; - T deletemashine = _places[position]; - _places[position] = null; - return deletemashine; + if (position < _maxCount && position >= 0) + { + + if (_places.ElementAt(position) != null) + { + T result = _places.ElementAt(position); + + _places.RemoveAt(position); + + return result; + } + + return null; + } + + return null; } /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= _places.Length) return null; - else if (_places[position] == null) return null; - return _places[position]; + get + { + if (position < _maxCount && position >= 0) + { + return _places.ElementAt(position); + } + + return null; + } + set + { + if (position < _maxCount && position >= 0) + { + Insert(this[position], position); + + } + + } } + + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetMachine() + { + foreach (var machine in _places) + { + if (machine != null) + { + yield return machine; + } + else + { + yield break; + } + } + } + } } \ No newline at end of file