diff --git a/Locomative/Locomative/MapWithSetLocoGeneric.cs b/Locomative/Locomative/MapWithSetLocoGeneric.cs index 896bce3..d3b2b7a 100644 --- a/Locomative/Locomative/MapWithSetLocoGeneric.cs +++ b/Locomative/Locomative/MapWithSetLocoGeneric.cs @@ -46,13 +46,9 @@ namespace Locomative public Bitmap ShowOnMap() { Shaking(); - for(int i =0; i < _setLoco.Count; i++) + foreach (var loco in _setLoco.GetLoco()) { - var loco = _setLoco.Get(i); - if(loco != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, loco); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, loco); } return new(_pictureWidth, _pictureHeight); } @@ -69,11 +65,11 @@ namespace Locomative int j = _setLoco.Count - 1; for(int i = 0; i < _setLoco.Count; i++) { - if(_setLoco.Get(i) == null) + if (_setLoco[j] == null) { for(; j>1; j--) { - var car = _setLoco.Get(j); + var car = _setLoco[j]; if (car != null) { _setLoco.Insert(car, i); @@ -107,40 +103,31 @@ namespace Locomative int CountWidth = _pictureWidth / _placeSizeWidth; int x = 10; - int y = _pictureHeight - _placeSizeHeight + 5; - - for (int k = 0; k < _setLoco.Count; k++) + int y = _pictureHeight - _placeSizeHeight; + int k = 0; + foreach (var plain in _setLoco.GetLoco()) { - if (_setLoco.Get(k) != null) - { - if ((k - 1) % CountWidth != 0 || k == 0) - { - _setLoco.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight); - _setLoco.Get(k)?.DrawningObject(g); - x += _placeSizeWidth; - } - else - { - _setLoco.Get(k)?.SetObject(x, y, _pictureWidth, _pictureHeight); - _setLoco.Get(k)?.DrawningObject(g); - x = 10; - y -= _placeSizeHeight; - - } - } - if (_setLoco.Get(k) == null) + if ((k + 1) % CountWidth != 0 || k == 0) { - if ((k + 1) % CountWidth != 0 || k == 0) - x += _placeSizeWidth; - else - { - x = 10; - y -= _placeSizeHeight; - } + plain?.SetObject(x, y, _pictureWidth, _pictureHeight); + plain?.DrawningObject(g); + x += _placeSizeWidth; } + else + { + + plain?.SetObject(x, y, _pictureWidth, _pictureHeight); + plain?.DrawningObject(g); + x = 10; + y -= _placeSizeHeight; + + } + + k++; } } + } } diff --git a/Locomative/Locomative/MapsCollection.cs b/Locomative/Locomative/MapsCollection.cs new file mode 100644 index 0000000..714dc48 --- /dev/null +++ b/Locomative/Locomative/MapsCollection.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Locomative +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorages; + public List Keys => _mapStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddMap(string name, AbstractMap map) + { + if (_mapStorages.ContainsKey(name)) + return; + Keys.Add(name); + _mapStorages.Add(name, new MapWithSetLocoGeneric(_pictureWidth, _pictureHeight, map)); + + } + + public void DelMap(string name) + { + if (!_mapStorages.ContainsKey(name)) + return; + Keys.Remove(name); + _mapStorages.Remove(name); + } + + public MapWithSetLocoGeneric this[string ind] + { + get + { + + if (!_mapStorages.ContainsKey(ind)) + return null; + else + return _mapStorages[ind]; + } + } + } +} + diff --git a/Locomative/Locomative/SetLocoGeneric.cs b/Locomative/Locomative/SetLocoGeneric.cs index d24f61d..dd515db 100644 --- a/Locomative/Locomative/SetLocoGeneric.cs +++ b/Locomative/Locomative/SetLocoGeneric.cs @@ -9,73 +9,75 @@ namespace Locomative internal class SetLocoGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; public SetLocoGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } - public int Insert(T locomative) + public int Insert(T plain) { - int i; - for (i = 0; i < Count; i++) - { - if (_places[i] == null) - { - for (int j = i; j >= 1; j--) - { - _places[j] = _places[j - 1]; - } - _places[0] = locomative; - return i; - } - } - return -1; + Insert(plain, 0); + return _places.Count; } - public int Insert(T locomative, int position) + public int Insert(T loco, int position) { - if (position > Count || position < 0) + if (position < 0 || _places.Count < position || position > _maxCount || _places.Count == _maxCount) return -1; - if (_places[position] == null) - { - _places[position] = locomative; - return position; - } - else - { - for (int i = position; i < Count; i++) - { - if (_places[i] == null) - { - for (int j = i; j >= position + 1; j--) - { - _places[j] = _places[j - 1]; - } - _places[position] = locomative; - return position; - } - } - } - return -1; + else if (loco == null) + return -1; + _places.Insert(position, loco); + return position; } public T Remove(int position) { T mid; - if (_places[position] != null && position < _places.Length) + if (position < 0 || _places.Count < position || position > _maxCount) + return null; + else if (_places[position] == null) + return null; + else { mid = _places[position]; - _places[position] = null; - return mid; - + _places.RemoveAt(position); } - else - return null; + return mid; } - public T Get(int position) + public T this[int position] { - return _places[position]; + get + { + if (position < 0 || _places.Count < position || position > _maxCount) + return null; + else if (_places[position] == null) + return null; + else + return _places[position]; + } + set + { + if (position < 0 || _places.Count < position || position > _maxCount) + return; + else if (_places.Count == _maxCount) + return; + else + _places[position] = value; + } } + public IEnumerable GetLoco() + { + foreach (var loco in _places) + { + if (loco != null) + yield return loco; + else + yield break; + } + } + } -} +} \ No newline at end of file