From 39ac5fc6aadec2a7ce3e94d43553f4a1d5cce23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sat, 4 Nov 2023 20:53:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D0=BD=D0=B0=D0=B4=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D0=B9=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/BoatsGenericCollection.cs | 25 +++--- Sailboat/Sailboat/BoatsGenericStorage.cs | 84 +++++++++++++++++++++ Sailboat/Sailboat/SetGeneric.cs | 45 +++++++++-- 3 files changed, 131 insertions(+), 23 deletions(-) create mode 100644 Sailboat/Sailboat/BoatsGenericStorage.cs diff --git a/Sailboat/Sailboat/BoatsGenericCollection.cs b/Sailboat/Sailboat/BoatsGenericCollection.cs index 37a71b4..381661f 100644 --- a/Sailboat/Sailboat/BoatsGenericCollection.cs +++ b/Sailboat/Sailboat/BoatsGenericCollection.cs @@ -6,9 +6,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using Sailboat.DrawingObjects; -using Sailboat.MovementStrategy; - namespace Sailboat.Generics { /// @@ -59,8 +56,7 @@ namespace Sailboat.Generics /// /// /// - public static int operator +(BoatsGenericCollection collect, T? - obj) + public static int operator +(BoatsGenericCollection collect, T? obj) { if (obj == null) { @@ -74,16 +70,16 @@ namespace Sailboat.Generics /// /// /// - public static bool operator -(BoatsGenericCollection collect, int - pos) + public static T? operator -(BoatsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); } - return false; + return obj; } + /// /// Получение объекта IMoveableObject /// @@ -91,7 +87,7 @@ namespace Sailboat.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -131,18 +127,17 @@ namespace Sailboat.Generics /// private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var boat in _collection.GetBoats()) { - DrawingBoat boat = _collection.Get(i); - if (boat != null) { int width = _pictureWidth / _placeSizeWidth; - boat.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + boat.SetPosition(i % width * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / width + 1) * _placeSizeHeight); boat.DrawTransport(g); } + i++; } } - } } diff --git a/Sailboat/Sailboat/BoatsGenericStorage.cs b/Sailboat/Sailboat/BoatsGenericStorage.cs new file mode 100644 index 0000000..8dd5aaa --- /dev/null +++ b/Sailboat/Sailboat/BoatsGenericStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +using Sailboat.MovementStrategy; + +namespace Sailboat.Generics +{ + internal class BoatsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _boatStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _boatStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public BoatsGenericStorage(int pictureWidth, int pictureHeight) + { + _boatStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (_boatStorages.ContainsKey(name)) + { + return; + } + _boatStorages[name] = new BoatsGenericCollection(_pictureWidth, _pictureHeight); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_boatStorages.ContainsKey(name)) + { + return; + } + _boatStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public BoatsGenericCollection? + this[string ind] + { + get + { + if (_boatStorages.ContainsKey(ind)) + { + return _boatStorages[ind]; + } + return null; + } + } + } +} diff --git a/Sailboat/Sailboat/SetGeneric.cs b/Sailboat/Sailboat/SetGeneric.cs index 9fe38ca..2a67125 100644 --- a/Sailboat/Sailboat/SetGeneric.cs +++ b/Sailboat/Sailboat/SetGeneric.cs @@ -4,25 +4,31 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +//пофиксить чота namespace Sailboat.Generics { internal class SetGeneric where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор @@ -90,13 +96,36 @@ namespace Sailboat.Generics /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) + get { - return null; + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetBoats(int? maxBoats = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxBoats.HasValue && i == maxBoats.Value) + { + yield break; + } } - return _places[position]; } } }