diff --git a/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs index d58632c..339a883 100644 --- a/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirBombersGeneric.cs @@ -90,7 +90,7 @@ namespace AirBomber Shaking(); for (int i = 0; i < _setAirBombers.Count; i++) { - var airBomber = _setAirBombers.Get(i); + var airBomber = _setAirBombers[i]; if (airBomber != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber); @@ -119,11 +119,11 @@ namespace AirBomber int j = _setAirBombers.Count - 1; for (int i = 0; i < _setAirBombers.Count; i++) { - if (_setAirBombers.Get(i) == null) + if (_setAirBombers[i] == null) { for (; j > i; j--) { - var car = _setAirBombers.Get(j); + var car = _setAirBombers[j]; if (car != null) { _setAirBombers.Insert(car, i); @@ -165,8 +165,8 @@ namespace AirBomber int numOfObjectsInRow = _pictureWidth / _placeSizeWidth; for (int i = 0; i < _setAirBombers.Count; i++) { - _setAirBombers.Get(i)?.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setAirBombers.Get(i)?.DrawingObject(g); + _setAirBombers[i]?.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setAirBombers[i]?.DrawingObject(g); } } } diff --git a/AirBomber/AirBomber/SetAirBombersGeneric.cs b/AirBomber/AirBomber/SetAirBombersGeneric.cs index 48d9abb..e9a13ff 100644 --- a/AirBomber/AirBomber/SetAirBombersGeneric.cs +++ b/AirBomber/AirBomber/SetAirBombersGeneric.cs @@ -10,20 +10,24 @@ 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 SetAirBombersGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -32,7 +36,9 @@ namespace AirBomber /// public int Insert(T airBomber) { - return Insert(airBomber, 0); + if (_places.Count >= _maxCount) return -1; + _places.Insert(0, airBomber); + return 0; } /// /// Добавление объекта в набор на конкретную позицию @@ -42,30 +48,9 @@ namespace AirBomber /// public int Insert(T airBomber, int position) { - if (position >= _places.Length) - { - return -1; - } - if (_places[position] != null) - { - int indexNull = -1; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - indexNull = i; - break; - } - } - if (indexNull == -1) return -1; - for (int i = indexNull; i > position; i--) - { - T tmp = _places[i]; - _places[i] = _places[i - 1]; - _places[i - 1] = tmp; - } - } - _places[position] = airBomber; + if (position < 0 || position >= _maxCount) return -1; + if (_places.Count >= _maxCount) return -1; + _places.Insert(position, airBomber); return position; } /// @@ -75,12 +60,9 @@ namespace AirBomber /// public T Remove(int position) { - if (position >= _places.Length) - { - return null; - } + if (position < 0 || position >= _maxCount) return null; T removedObject = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return removedObject; } /// @@ -88,13 +70,17 @@ namespace AirBomber /// /// /// - public T Get(int position) + public T this[int position] { - if (position >= _places.Length) + get { - return null; + if (position < 0 || position >= _maxCount) return null; + return _places[position]; + } + set + { + if (position < 0 || position >= _maxCount) Insert(value, position); } - return _places[position]; } } }