diff --git a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs
index c69531e..ff90b2e 100644
--- a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs
+++ b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs
@@ -94,13 +94,9 @@ namespace ProjectMachine
public Bitmap ShowOnMap()
{
Shaking();
- for (int i = 0; i < _setTank.Count; i++)
+ foreach (var tank in _setTank.GetTanks())
{
- var tank = _setTank.Get(i);
- if (tank != null)
- {
- return _map.CreateMap(_pictureWidth, _pictureHeight, tank);
- }
+ return _map.CreateMap(_pictureWidth, _pictureHeight, tank);
}
return new(_pictureWidth, _pictureHeight);
}
@@ -125,11 +121,11 @@ namespace ProjectMachine
int j = _setTank.Count - 1;
for (int i = 0; i < _setTank.Count; i++)
{
- if (_setTank.Get(i) == null)
+ if (_setTank[i] == null)
{
for (; j > i; j--)
{
- var car = _setTank.Get(j);
+ var car = _setTank[j];
if (car != null)
{
_setTank.Insert(car, i);
@@ -170,21 +166,13 @@ namespace ProjectMachine
///
private void DrawTanks(Graphics g)
{
- int i = 0;
- int j = 0;
- for (int k = 0; k < _setTank.Count; k++)
+ int width = _pictureWidth / _placeSizeWidth;
+ int k = 0;
+ foreach (var tank in _setTank.GetTanks())
{
- _setTank.Get(k)?.SetObject(j + 10, i + 20, _pictureWidth, _pictureHeight);
- _setTank.Get(k)?.DrawningObject(g);
- if (j >= _pictureWidth - 2*_placeSizeWidth)
- {
- j = 0;
- i += _placeSizeHeight;
- }
- else
- {
- j += _placeSizeWidth;
- }
+ tank.SetObject(k % width * _placeSizeWidth + 10, k / width * _placeSizeHeight + 20, _pictureWidth, _pictureHeight);
+ tank.DrawningObject(g);
+ k++;
}
}
}
diff --git a/ProjectMachine/ProjectMachine/SetTankGeneric.cs b/ProjectMachine/ProjectMachine/SetTankGeneric.cs
index 5c1fb15..26f9a79 100644
--- a/ProjectMachine/ProjectMachine/SetTankGeneric.cs
+++ b/ProjectMachine/ProjectMachine/SetTankGeneric.cs
@@ -14,38 +14,33 @@ namespace ProjectMachine
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 SetTankGeneric(int count)
{
- _places = new T[count];
+ _maxCount = count;
+ _places = new List();
}
///
/// Добавление объекта в набор
///
- /// Добавляемый танк
+ /// Добавляемый танк
///
public int Insert(T tank)
{
- if (tank == null)
- {
- return -1;
- }
- for (int i = Count -1; i > 0; i--)
- {
- _places[i] = _places[i - 1];
- }
- _places[0] = tank;
- return 0;
+ if (Count < _maxCount)
+ return Insert(tank, 0);
+ return -1;
}
///
/// Добавление объекта в набор на конкретную позицию
@@ -55,25 +50,10 @@ namespace ProjectMachine
///
public int Insert(T tank, int position)
{
- if (position < 0 || position > Count)
- {
+ if (position < 0 || position >= _maxCount)
return -1;
- }
- int firstNullElementIndex = position; //индекс первого нулевого элемента
- while(_places[firstNullElementIndex] != null)
- {
- if (firstNullElementIndex >= Count)
- {
- return -1;
- }
- firstNullElementIndex++;
- }
- for (int i = firstNullElementIndex; i > position; i--)
- {
- _places[i] = _places[i - 1];
- }
- _places[position] = tank;
- return 0;
+ _places.Insert(position, tank);
+ return position;
}
///
/// Удаление объекта из набора с конкретной позиции
@@ -82,12 +62,10 @@ namespace ProjectMachine
///
public T Remove(int position)
{
- if (_places[position] == null)
- {
+ if (position < 0 || position >= Count)
return null;
- }
var result = _places[position];
- _places[position] = null;
+ _places.RemoveAt(position);
return result;
}
///
@@ -95,15 +73,39 @@ namespace ProjectMachine
///
///
///
- public T Get(int position)
+ public T this[int position]
{
- if (_places[position] != null)
+ get
{
+ if (position < 0 || position >= Count)
+ return null;
return _places[position];
}
- return null;
+ set
+ {
+ if (position < 0 || position >= _maxCount)
+ return;
+ _places.Add(value);
+ }
+ }
+ ///
+ /// Проход по набору до первого пустого
+ ///
+ ///
+ public IEnumerable GetTanks()
+ {
+ foreach (var tank in _places)
+ {
+ if (tank != null)
+ {
+ yield return tank;
+ }
+ else
+ {
+ yield break;
+ }
+ }
}
}
-
}