Этап 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()
{
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
/// <param name="g"></param>
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++;
}
}
}

View File

@ -14,38 +14,33 @@ namespace ProjectMachine
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetTankGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый танк</param>
/// <param name="tank">Добавляемый танк</param>
/// <returns></returns>
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;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -55,25 +50,10 @@ namespace ProjectMachine
/// <returns></returns>
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;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -82,12 +62,10 @@ namespace ProjectMachine
/// <returns></returns>
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;
}
/// <summary>
@ -95,15 +73,39 @@ namespace ProjectMachine
/// </summary>
/// <param name="position"></param>
/// <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 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;
}
}
}
}
}