This commit is contained in:
Valitov.Damir 2023-04-14 21:28:29 +04:00
parent b00edd8ae9
commit ee5d4a6878
3 changed files with 39 additions and 73 deletions

View File

@ -60,9 +60,9 @@ namespace Sailboat
return;
}
DrawingObjectBoat bus = new(form.SelectedBoat);
DrawingObjectBoat boat = new(form.SelectedBoat);
if (_mapBoatsCollectionGeneric + bus != -1)
if (_mapBoatsCollectionGeneric + boat != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();

View File

@ -49,9 +49,13 @@ namespace Sailboat
public Bitmap ShowOnMap()
{
Shaking();
foreach (var boat in _setBoats.GetBoats())
for (int i = 0; i < _setBoats.Count; i++)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
var ship = _setBoats.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
return new(_pictureWidth, _pictureHeight);
}
@ -112,26 +116,22 @@ namespace Sailboat
int currentWidth = 0;
int currentHeight = 0;
foreach (var machine in _setBoats.GetBoats())
for (int i = 0; i < _setBoats.Count; i++)
{
int dop = 0;
if (currentWidth > 0 && currentHeight > 0)
{
dop = 30;
}
machine.SetObject(currentWidth * _placeSizeWidth + dop,
currentHeight * _placeSizeHeight + dop,
_setBoats.Get(i)?.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight);
machine?.DrawingObject(g);
_setBoats.Get(i)?.DrawingObject(g);
if (currentWidth < width - 1)
{
currentWidth++;
}
else
{
currentWidth = 0;
currentHeight++;
currentWidth = 0;
}
if (currentHeight > height) return;
}

View File

@ -8,96 +8,62 @@ namespace Sailboat
{
class SetBoatsGeneric<T> where T : class
{
private readonly List<T> _places;
private readonly int _maxCount;
private readonly T[] _places;
public int Count => _places.Count;
public int Count => _places.Length;
private int BusyPlaces = 0;
public SetBoatsGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
_places = new T[count]; ;
}
public int Insert(T boat)
{
if (Count + 1 <= _maxCount) return Insert(boat, 0);
else return -1;
return Insert(boat, 0);
}
public int Insert(T boat, int position)
{
if (position >= _maxCount && position < 0)
if (position < 0 || position >= _places.Length)
{
return -1;
}
_places.Insert(position, boat);
BusyPlaces++;
while (_places[position] != null)
{
for (int i = 0; i < BusyPlaces; i++)
{
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = boat;
return position;
}
public T Remove(int position)
{
if (position < _maxCount && position >= 0)
if (position < 0 || position >= _places.Length)
{
if (_places.ElementAt(position) != null)
{
T result = _places.ElementAt(position);
_places.RemoveAt(position);
return result;
}
return null;
}
return null;
BusyPlaces--;
T boat = _places[position];
_places[position] = null;
return boat;
}
public T Get(int position)
{
if (position < 0 || position >= _places.Count) return null;
return _places[position];
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
if (position < 0 || position >= _places.Length)
{
if (position < _maxCount && position >= 0)
{
return _places.ElementAt(position);
}
return null;
}
set
{
if (position < _maxCount && position >= 0)
{
Insert(this[position], position);
}
}
}
public IEnumerable<T> GetBoats()
{
foreach (var boat in _places)
{
if (boat != null)
{
yield return boat;
}
else
{
yield break;
}
}
return _places[position];
}
}
}