Этап 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()
{
Shaking();
for (int i = 0; i < _setAirFighters.Count; i++)
{
var airFighter = _setAirFighters.Get(i);
if (airFighter != null)
foreach (var airFighter in _setAirFighters.GetAirFighters())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, airFighter);
}
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
@ -125,11 +121,11 @@ namespace AirFighter
int j = _setAirFighters.Count - 1;
for (int i = 0; i < _setAirFighters.Count; i++)
{
if (_setAirFighters.Get(i) == null)
if (_setAirFighters[i] == null)
{
for (; j > i; j--)
{
var airFighter = _setAirFighters.Get(j);
var airFighter = _setAirFighters[j];
if (airFighter != null)
{
_setAirFighters.Insert(airFighter, i);
@ -179,10 +175,10 @@ namespace AirFighter
int currentWidth = _pictureWidth / _placeSizeWidth - 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);
_setAirFighters.Get(i)?.DrawningObject(g);
airFighter?.SetObject(currentWidth * _placeSizeWidth + 50, currentHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
airFighter?.DrawningObject(g);
if (currentWidth > 0)
{
@ -194,7 +190,8 @@ namespace AirFighter
{
currentHeight -= 1;
currentWidth = _pictureWidth / _placeSizeWidth - 1;
}else return;
}
else return;
}
}
}

View File

@ -15,40 +15,25 @@ namespace AirFighter
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;
/// <summary>
/// Максимальная длина списка
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetAirFightersGeneric(int count)
{
_places = new T[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;
_places = new List<T>();
_maxCount = count;
}
/// <summary>
/// Добавление объекта в набор
@ -67,41 +52,16 @@ namespace AirFighter
/// <returns></returns>
public int Insert(T airFighter, int position)
{
if(airFighter == null)
if (position < 0 && position > _maxCount)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = airFighter;
}
else
{
if(CheckNullPosition(position + 1))
{
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;
}
}
_places.Insert(position, airFighter);
return position;
}
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
@ -109,10 +69,10 @@ namespace AirFighter
/// <returns></returns>
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];
_places[position] = null;
_places.RemoveAt(position);
return temp;
}
else
@ -123,14 +83,42 @@ namespace AirFighter
/// </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 < _maxCount)
{
return _places[position];
}
else
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;
}
}
}
}
}