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

This commit is contained in:
Дарья Антонова 2022-12-16 22:21:59 +04:00
parent a0aee749e1
commit e1b456f4a8
2 changed files with 64 additions and 41 deletions

View File

@ -94,13 +94,9 @@ namespace AirBomber
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setJets.Count; i++)
foreach (var car in _setJets.GetJets())
{
var car = _setJets.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return new(_pictureWidth, _pictureHeight);
}
@ -125,11 +121,11 @@ namespace AirBomber
int j = _setJets.Count - 1;
for (int i = 0; i < _setJets.Count; i++)
{
if (_setJets.Get(i) == null)
if (_setJets[i] == null)
{
for (; j > i; j--)
{
var car = _setJets.Get(j);
var car = _setJets[j];
if (car != null)
{
_setJets.Insert(car, i);
@ -189,9 +185,9 @@ namespace AirBomber
{
// установка позиции
//_setJets.Get(i)?.SetObject(ccol * _placeSizeWidth, crow * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setJets.Get(i)?.SetObject(rows - i % cols * _placeSizeWidth, i / cols * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
_setJets.Get(i)?.DrawningObject(g);
// (сначала передвигаемся влево)
_setJets[i]?.SetObject(rows - i % cols * _placeSizeWidth, i / cols * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
_setJets[i]?.DrawningObject(g);
//(сначала передвигаемся влево)
ccol++;
if (ccol >= cols)
{

View File

@ -9,23 +9,26 @@ namespace AirBomber
internal class SetJetGeneric<T> where T : class
{
/// <summary>
/// Массив объектов, которые храним (самолетов)
/// Список объектов, которые храним (самолетов)
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// Количество объектов в списке
/// </summary>
public int Count => _places.Length;
private int JetPlaces = 0;
public int Count => _places.Count;
/// <summary>
/// Ограничение максимального количества самолетов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count">Количество самолетов</param>
public SetJetGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
@ -34,29 +37,20 @@ namespace AirBomber
/// <param name="jet">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T jet)
{
return Insert(jet, 0);
}
//public int Insert(T jet)
//{
// return Insert(jet, 0);
//}
public int Insert(T jet, int position = 0)
{
if (position < 0 || position >= _places.Length || JetPlaces == _places.Length)
if (position < 0 || position >= _maxCount || _maxCount <= _places.Count)
{
return -1;
}
JetPlaces++;
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] = jet;
_places.Insert(position, jet);
_places.Remove(null);
return position;
}
/// <summary>
@ -66,9 +60,13 @@ namespace AirBomber
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
if (position < 0 || position >= _maxCount) return null;
T savedJet = _places[position];
_places[position] = null;
return savedJet;
}
@ -77,11 +75,40 @@ namespace AirBomber
/// </summary>
/// <param name="position">Позиция получаемого самолета</param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
// проверка позиции
if (position < 0 || position >= _places.Length) return null;
return _places[position];
get
{
// проверка позиции
if (position < 0 || position >= _maxCount) return null;
return _places[position];
}
set
{
// проверка позиции
if (position < 0 || position >= _maxCount)
{
_places[position] = value;
}
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetJets()
{
foreach (var jet in _places)
{
if (jet != null)
{
yield return jet;
}
else
{
yield break;
}
}
}
}
}