Этап 1. Замена массива на список

This commit is contained in:
evasina2312@gmail.com 2022-11-10 07:54:26 +04:00
parent 000c848d81
commit ec5c587297
2 changed files with 54 additions and 64 deletions

View File

@ -94,13 +94,9 @@ namespace ProjectMachine
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setTank.Count; i++) foreach (var tank in _setTank.GetTanks())
{ {
var tank = _setTank.Get(i); return _map.CreateMap(_pictureWidth, _pictureHeight, tank);
if (tank != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, tank);
}
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
@ -125,11 +121,11 @@ namespace ProjectMachine
int j = _setTank.Count - 1; int j = _setTank.Count - 1;
for (int i = 0; i < _setTank.Count; i++) for (int i = 0; i < _setTank.Count; i++)
{ {
if (_setTank.Get(i) == null) if (_setTank[i] == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var car = _setTank.Get(j); var car = _setTank[j];
if (car != null) if (car != null)
{ {
_setTank.Insert(car, i); _setTank.Insert(car, i);
@ -170,21 +166,13 @@ namespace ProjectMachine
/// <param name="g"></param> /// <param name="g"></param>
private void DrawTanks(Graphics g) private void DrawTanks(Graphics g)
{ {
int i = 0; int width = _pictureWidth / _placeSizeWidth;
int j = 0; int k = 0;
for (int k = 0; k < _setTank.Count; k++) foreach (var tank in _setTank.GetTanks())
{ {
_setTank.Get(k)?.SetObject(j + 10, i + 20, _pictureWidth, _pictureHeight); tank.SetObject(k % width * _placeSizeWidth + 10, k / width * _placeSizeHeight + 20, _pictureWidth, _pictureHeight);
_setTank.Get(k)?.DrawningObject(g); tank.DrawningObject(g);
if (j >= _pictureWidth - 2*_placeSizeWidth) k++;
{
j = 0;
i += _placeSizeHeight;
}
else
{
j += _placeSizeWidth;
}
} }
} }
} }

View File

@ -14,38 +14,33 @@ namespace ProjectMachine
where T : class where T : class
{ {
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly T[] _places; private readonly List<T> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в списке
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetTankGeneric(int count) public SetTankGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
_places = new List<T>();
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="car">Добавляемый танк</param> /// <param name="tank">Добавляемый танк</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T tank) public int Insert(T tank)
{ {
if (tank == null) if (Count < _maxCount)
{ return Insert(tank, 0);
return -1; return -1;
}
for (int i = Count -1; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = tank;
return 0;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -55,25 +50,10 @@ namespace ProjectMachine
/// <returns></returns> /// <returns></returns>
public int Insert(T tank, int position) public int Insert(T tank, int position)
{ {
if (position < 0 || position > Count) if (position < 0 || position >= _maxCount)
{
return -1; return -1;
} _places.Insert(position, tank);
int firstNullElementIndex = position; //индекс первого нулевого элемента return 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;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -82,12 +62,10 @@ namespace ProjectMachine
/// <returns></returns> /// <returns></returns>
public T Remove(int position) public T Remove(int position)
{ {
if (_places[position] == null) if (position < 0 || position >= Count)
{
return null; return null;
}
var result = _places[position]; var result = _places[position];
_places[position] = null; _places.RemoveAt(position);
return result; return result;
} }
/// <summary> /// <summary>
@ -95,15 +73,39 @@ namespace ProjectMachine
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
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 _places[position];
} }
return null; set
{
if (position < 0 || position >= _maxCount)
return;
_places.Add(value);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetTanks()
{
foreach (var tank in _places)
{
if (tank != null)
{
yield return tank;
}
else
{
yield break;
}
}
} }
} }
} }