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; return;
} }
DrawingObjectBoat bus = new(form.SelectedBoat); DrawingObjectBoat boat = new(form.SelectedBoat);
if (_mapBoatsCollectionGeneric + bus != -1) if (_mapBoatsCollectionGeneric + boat != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();

View File

@ -49,9 +49,13 @@ namespace Sailboat
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); 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); return new(_pictureWidth, _pictureHeight);
} }
@ -112,26 +116,22 @@ namespace Sailboat
int currentWidth = 0; int currentWidth = 0;
int currentHeight = 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, _setBoats.Get(i)?.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight + dop, currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight); _pictureWidth, _pictureHeight);
machine?.DrawingObject(g); _setBoats.Get(i)?.DrawingObject(g);
if (currentWidth < width - 1) if (currentWidth < width - 1)
{ {
currentWidth++; currentWidth++;
} }
else else
{ {
currentWidth = 0;
currentHeight++; currentHeight++;
currentWidth = 0;
} }
if (currentHeight > height) return; if (currentHeight > height) return;
} }

View File

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