Laba4 PIbd-22 Kalyshev Y V #7
@ -89,13 +89,9 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setBoats.Count; i++)
|
||||
foreach (var boat in _setBoats.GetBoats())
|
||||
{
|
||||
var boat = _setBoats.Get(i);
|
||||
if (boat != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
|
||||
}
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
|
||||
}
|
||||
return new(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
@ -120,11 +116,11 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
int j = _setBoats.Count - 1;
|
||||
for (int i = 0; i < _setBoats.Count; i++)
|
||||
{
|
||||
if (_setBoats.Get(i) == null)
|
||||
if (_setBoats[i] == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var boat = _setBoats.Get(j);
|
||||
var boat = _setBoats[j];
|
||||
if (boat != null)
|
||||
{
|
||||
_setBoats.Insert(boat, i);
|
||||
@ -164,10 +160,10 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
{
|
||||
int x = 5;
|
||||
int y = 5;
|
||||
for (int i = 0; i < _setBoats.Count; i++)
|
||||
foreach (var boat in _setBoats.GetBoats())
|
||||
{
|
||||
_setBoats.Get(i)?.SetObject(x, y, _pictureWidth, _pictureHeight);
|
||||
_setBoats.Get(i)?.DrawningObject(g);
|
||||
boat?.SetObject(x, y, _pictureWidth, _pictureHeight);
|
||||
boat?.DrawningObject(g);
|
||||
x += _placeSizeWidth;
|
||||
if (x + _placeSizeWidth > _pictureWidth)
|
||||
{
|
||||
|
@ -11,20 +11,23 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
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;
|
||||
|
||||
private readonly int _maxCount;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public SetBoatsGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -33,12 +36,15 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
/// <returns></returns>
|
||||
public int Insert(T boat)
|
||||
{
|
||||
for (int i = 0; i < _places.Length; i++)
|
||||
if (_places.Count < _maxCount)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
_places.Add(boat);
|
||||
for (int i = 0; i < _places.Count; i++)
|
||||
{
|
||||
_places[i] = boat;
|
||||
return i;
|
||||
if (_places[i] == boat)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
@ -51,33 +57,12 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
/// <returns></returns>
|
||||
public bool Insert(T boat, int position)
|
||||
{
|
||||
if (position < 0 || position > _places.Length)
|
||||
if (position < 0 || position > _places.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_places[position] != null)
|
||||
{
|
||||
int pos = -1;
|
||||
for (int i = position+1; i < _places.Length; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
pos = i;
|
||||
}
|
||||
}
|
||||
if (pos != -1)
|
||||
{
|
||||
for (int i = pos; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
} else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_places[position] = boat;
|
||||
return false;
|
||||
_places.Insert(position, boat);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
@ -86,12 +71,16 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
/// <returns></returns>
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position > _places.Length)
|
||||
if (position < 0 || position > _places.Count)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (_places[position] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
T removed = _places[position];
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return removed;
|
||||
}
|
||||
/// <summary>
|
||||
@ -99,13 +88,42 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
if (position < 0 || position > _places.Length)
|
||||
get
|
||||
{
|
||||
return null;
|
||||
if (position < 0 || position > _places.Count)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position < 0 || position > _places.Count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_places.Insert(position, value);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Проход по набору до первого пустого
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T> GetBoats()
|
||||
{
|
||||
foreach (var boat in _places)
|
||||
{
|
||||
if (boat != null)
|
||||
{
|
||||
yield return boat;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user