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;
+ }
+ }
+ }
+
}
}