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

This commit is contained in:
Артем Харламов 2022-11-28 20:50:07 +04:00
parent 3177e452e2
commit 103a64ace1
2 changed files with 52 additions and 43 deletions

View File

@ -71,8 +71,7 @@ namespace Stormtrooper
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
public static T operator -(MapWithSetStormtroopersGeneric<T, U> map, int
position)
public static T operator -(MapWithSetStormtroopersGeneric<T, U> map, int position)
{
return map._setStormtroopers.Remove(position);
}
@ -95,13 +94,9 @@ namespace Stormtrooper
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setStormtroopers.Count; i++)
foreach (var storm in _setStormtroopers.GetStormtroopers())
{
var car = _setStormtroopers.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, storm);
}
return new(_pictureWidth, _pictureHeight);
}
@ -126,11 +121,11 @@ namespace Stormtrooper
int j = _setStormtroopers.Count - 1;
for (int i = 0; i < _setStormtroopers.Count; i++)
{
if (_setStormtroopers.Get(i) == null)
if (_setStormtroopers[i] == null)
{
for (; j > i; j--)
{
var car = _setStormtroopers.Get(j);
var car = _setStormtroopers[i];
if (car != null)
{
_setStormtroopers.Insert(car, i);
@ -174,10 +169,10 @@ namespace Stormtrooper
{
int numOfObjectsInRow = _pictureWidth / _placeSizeWidth;
int i = 0;
for (int j = 0; j < _setStormtroopers.Count; j++)
foreach (var storm in _setStormtroopers.GetStormtroopers())
{
_setStormtroopers.Get(i)?.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight + _placeSizeHeight, _pictureWidth, _pictureHeight);
_setStormtroopers.Get(i)?.DrawningObject(g);
storm.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight + _placeSizeHeight, _pictureWidth, _pictureHeight);
storm.DrawningObject(g);
i++;
}

View File

@ -14,56 +14,45 @@ namespace Stormtrooper
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;
public int TecCount = 0; // текущее количество объектов в массиве
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetStormtroopersGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="stormtrooper">Добавляемый штурмовик</param>
/// <returns></returns>
public int Insert(T stormtrooper)
{
if (_places.Count + 1 >= _maxCount) return -1;
return Insert(stormtrooper, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="stormtrooper">Добавляемый штурмовик</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T stormtrooper, int position)
{
if (position < 0 || position >= Count || TecCount == Count) return -1;
TecCount++;
while (_places[position] != null)
{
for (int i = _places.Length - 1; i > 0; --i)
{
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = stormtrooper;
if (position < 0 || position >= _maxCount) return -1;
if (_places.Count + 1 >= _maxCount) return -1;
_places.Insert(position, stormtrooper);
return position;
}
/// <summary>
@ -73,9 +62,9 @@ namespace Stormtrooper
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
if (position < 0 || position >= _maxCount) return null;
T saveStorm = _places[position];
_places[position] = null;
_places.RemoveAt(position);
return saveStorm;
}
/// <summary>
@ -83,10 +72,35 @@ namespace Stormtrooper
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= Count || _places[position] == null) return null;
return _places[position];
get
{
if (position < 0 || position >= _maxCount) return null;
return _places[position];
}
set
{
if (position < 0 || position >= _maxCount) Insert(value, position);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetStormtroopers()
{
foreach (var storm in _places)
{
if (storm != null)
{
yield return storm;
}
else
{
yield break;
}
}
}
}
}