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

View File

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