diff --git a/Sailboat/FormMapWithSetBoats.cs b/Sailboat/FormMapWithSetBoats.cs index ff45ed5..184e184 100644 --- a/Sailboat/FormMapWithSetBoats.cs +++ b/Sailboat/FormMapWithSetBoats.cs @@ -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(); diff --git a/Sailboat/MapWithSetBoatsGeneric.cs b/Sailboat/MapWithSetBoatsGeneric.cs index 23bcf57..e6fa5f0 100644 --- a/Sailboat/MapWithSetBoatsGeneric.cs +++ b/Sailboat/MapWithSetBoatsGeneric.cs @@ -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; } diff --git a/Sailboat/SetBoatsGeneric.cs b/Sailboat/SetBoatsGeneric.cs index e35c492..a13c350 100644 --- a/Sailboat/SetBoatsGeneric.cs +++ b/Sailboat/SetBoatsGeneric.cs @@ -8,96 +8,62 @@ namespace Sailboat { class SetBoatsGeneric where T : class { - private readonly List _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(); + _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]; - } - /// - /// Получение объекта из набора по позиции - /// - /// - /// - 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 GetBoats() - { - foreach (var boat in _places) - { - if (boat != null) - { - yield return boat; - } - else - { - yield break; - } - } + return _places[position]; } } }