Лабораторная работа 4. Первый этап: список
This commit is contained in:
parent
91aeb23ae0
commit
7d300205a3
@ -90,7 +90,7 @@ namespace AirBomber
|
|||||||
Shaking();
|
Shaking();
|
||||||
for (int i = 0; i < _setAirBombers.Count; i++)
|
for (int i = 0; i < _setAirBombers.Count; i++)
|
||||||
{
|
{
|
||||||
var airBomber = _setAirBombers.Get(i);
|
var airBomber = _setAirBombers[i];
|
||||||
if (airBomber != null)
|
if (airBomber != null)
|
||||||
{
|
{
|
||||||
return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
|
return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
|
||||||
@ -119,11 +119,11 @@ namespace AirBomber
|
|||||||
int j = _setAirBombers.Count - 1;
|
int j = _setAirBombers.Count - 1;
|
||||||
for (int i = 0; i < _setAirBombers.Count; i++)
|
for (int i = 0; i < _setAirBombers.Count; i++)
|
||||||
{
|
{
|
||||||
if (_setAirBombers.Get(i) == null)
|
if (_setAirBombers[i] == null)
|
||||||
{
|
{
|
||||||
for (; j > i; j--)
|
for (; j > i; j--)
|
||||||
{
|
{
|
||||||
var car = _setAirBombers.Get(j);
|
var car = _setAirBombers[j];
|
||||||
if (car != null)
|
if (car != null)
|
||||||
{
|
{
|
||||||
_setAirBombers.Insert(car, i);
|
_setAirBombers.Insert(car, i);
|
||||||
@ -165,8 +165,8 @@ namespace AirBomber
|
|||||||
int numOfObjectsInRow = _pictureWidth / _placeSizeWidth;
|
int numOfObjectsInRow = _pictureWidth / _placeSizeWidth;
|
||||||
for (int i = 0; i < _setAirBombers.Count; i++)
|
for (int i = 0; i < _setAirBombers.Count; i++)
|
||||||
{
|
{
|
||||||
_setAirBombers.Get(i)?.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
_setAirBombers[i]?.SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||||||
_setAirBombers.Get(i)?.DrawingObject(g);
|
_setAirBombers[i]?.DrawingObject(g);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,20 +10,24 @@ namespace AirBomber
|
|||||||
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;
|
||||||
|
|
||||||
|
private readonly int _maxCount;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="count"></param>
|
/// <param name="count"></param>
|
||||||
|
///
|
||||||
public SetAirBombersGeneric(int count)
|
public SetAirBombersGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = new T[count];
|
_maxCount = count;
|
||||||
|
_places = new List<T>();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор
|
/// Добавление объекта в набор
|
||||||
@ -32,7 +36,9 @@ namespace AirBomber
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T airBomber)
|
public int Insert(T airBomber)
|
||||||
{
|
{
|
||||||
return Insert(airBomber, 0);
|
if (_places.Count >= _maxCount) return -1;
|
||||||
|
_places.Insert(0, airBomber);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор на конкретную позицию
|
/// Добавление объекта в набор на конкретную позицию
|
||||||
@ -42,30 +48,9 @@ namespace AirBomber
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T airBomber, int position)
|
public int Insert(T airBomber, int position)
|
||||||
{
|
{
|
||||||
if (position >= _places.Length)
|
if (position < 0 || position >= _maxCount) return -1;
|
||||||
{
|
if (_places.Count >= _maxCount) return -1;
|
||||||
return -1;
|
_places.Insert(position, airBomber);
|
||||||
}
|
|
||||||
if (_places[position] != null)
|
|
||||||
{
|
|
||||||
int indexNull = -1;
|
|
||||||
for (int i = position; i < _places.Length; i++)
|
|
||||||
{
|
|
||||||
if (_places[i] == null)
|
|
||||||
{
|
|
||||||
indexNull = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (indexNull == -1) return -1;
|
|
||||||
for (int i = indexNull; i > position; i--)
|
|
||||||
{
|
|
||||||
T tmp = _places[i];
|
|
||||||
_places[i] = _places[i - 1];
|
|
||||||
_places[i - 1] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_places[position] = airBomber;
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -75,12 +60,9 @@ namespace AirBomber
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position >= _places.Length)
|
if (position < 0 || position >= _maxCount) return null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
T removedObject = _places[position];
|
T removedObject = _places[position];
|
||||||
_places[position] = null;
|
_places.RemoveAt(position);
|
||||||
return removedObject;
|
return removedObject;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -88,13 +70,17 @@ 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]
|
||||||
{
|
{
|
||||||
if (position >= _places.Length)
|
get
|
||||||
{
|
{
|
||||||
return null;
|
if (position < 0 || position >= _maxCount) return null;
|
||||||
}
|
|
||||||
return _places[position];
|
return _places[position];
|
||||||
}
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= _maxCount) Insert(value, position);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user