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

This commit is contained in:
Danil Kargin 2022-11-02 22:00:08 +04:00
parent 3290b58955
commit 6550575acd
2 changed files with 67 additions and 82 deletions

View File

@ -94,14 +94,10 @@ namespace AirFighter
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setAirFighters.Count; i++) foreach (var airFighter in _setAirFighters.GetAirFighters())
{
var airFighter = _setAirFighters.Get(i);
if (airFighter != null)
{ {
return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter); return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter);
} }
}
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
/// <summary> /// <summary>
@ -125,11 +121,11 @@ namespace AirFighter
int j = _setAirFighters.Count - 1; int j = _setAirFighters.Count - 1;
for (int i = 0; i < _setAirFighters.Count; i++) for (int i = 0; i < _setAirFighters.Count; i++)
{ {
if (_setAirFighters.Get(i) == null) if (_setAirFighters[i] == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var airFighter = _setAirFighters.Get(j); var airFighter = _setAirFighters[j];
if (airFighter != null) if (airFighter != null)
{ {
_setAirFighters.Insert(airFighter, i); _setAirFighters.Insert(airFighter, i);
@ -179,22 +175,23 @@ namespace AirFighter
int currentWidth = _pictureWidth / _placeSizeWidth - 1; int currentWidth = _pictureWidth / _placeSizeWidth - 1;
int currentHeight = _pictureHeight / _placeSizeHeight - 1; int currentHeight = _pictureHeight / _placeSizeHeight - 1;
for (int i = 0; i < _setAirFighters.Count; i++) foreach (var airFighter in _setAirFighters.GetAirFighters())
{ {
_setAirFighters.Get(i)?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); airFighter?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setAirFighters.Get(i)?.DrawningObject(g); airFighter?.DrawningObject(g);
if(currentWidth > 0) if (currentWidth > 0)
{ {
currentWidth -= 1; currentWidth -= 1;
} }
else else
{ {
if(currentHeight > 0) if (currentHeight > 0)
{ {
currentHeight -= 1; currentHeight -= 1;
currentWidth = _pictureWidth / _placeSizeWidth - 1; currentWidth = _pictureWidth / _placeSizeWidth - 1;
}else return; }
else return;
} }
} }
} }

View File

@ -15,40 +15,25 @@ namespace AirFighter
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;
/// <summary>
/// Максимальная длина списка
/// </summary>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetAirFightersGeneric(int count) public SetAirFightersGeneric(int count)
{ {
_places = new T[count]; _places = new List<T>();
} _maxCount = count;
/// <summary>
/// Проверка на наличие пустых мест
/// </summary>
/// <param name="firstIndex"></param>
/// <returns></returns>
private bool CheckNullPosition(int firstIndex)
{
if(firstIndex >= _places.Length && firstIndex < 0)
{
return false;
}
for(int i = firstIndex; i < _places.Length; i++)
{
if(_places[i] == null)
{
return true;
}
}
return false;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@ -67,41 +52,16 @@ namespace AirFighter
/// <returns></returns> /// <returns></returns>
public int Insert(T airFighter, int position) public int Insert(T airFighter, int position)
{ {
if(airFighter == null) if (position < 0 && position > _maxCount)
{ {
return -1; return -1;
} }
if (_places[position] == null)
{
_places[position] = airFighter;
}
else else
{ {
if(CheckNullPosition(position + 1)) _places.Insert(position, airFighter);
{
T tempMain = airFighter;
for (int i = position; i < _places.Length; i++)
{
if (_places[i] == null)
{
_places[i] = tempMain;
break;
}
else
{
T temp2 = _places[i];
_places[i] = tempMain;
tempMain = temp2;
}
}
}
else
{
return -1;
}
}
return position; return position;
} }
}
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
/// </summary> /// </summary>
@ -109,10 +69,10 @@ namespace AirFighter
/// <returns></returns> /// <returns></returns>
public T Remove(int position) public T Remove(int position)
{ {
if (position >= 0 && position < _places.Length && _places[position] != null) if (position >= 0 && position < _maxCount && _places[position] != null)
{ {
T temp = _places[position]; T temp = _places[position];
_places[position] = null; _places.RemoveAt(position);
return temp; return temp;
} }
else else
@ -123,14 +83,42 @@ namespace AirFighter
/// </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 < _maxCount)
{ {
return _places[position]; return _places[position];
} }
else else
return null; return null;
} }
} set
{
if (position >= 0 && position < _maxCount)
{
_places.Insert(position, value);
}
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetAirFighters()
{
foreach (var airFighter in _places)
{
if (airFighter != null)
{
yield return airFighter;
}
else
{
yield break;
}
}
}
}
} }