From 2e12e1f2e11d96bbfb247f04c8f29de647d1cb1d Mon Sep 17 00:00:00 2001 From: crum61kg Date: Sun, 22 Jan 2023 01:35:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyShip/MapWithSetWarmlyShipGeneric.cs | 32 ++++--------- WarmlyShip/WarmlyShip/SetWarmlyShipGeneric.cs | 46 +++++++++++++++---- 2 files changed, 47 insertions(+), 31 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs index 9533481..e3901b1 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetWarmlyShipGeneric.cs @@ -93,14 +93,11 @@ namespace WarmlyShip public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setWarmlyShip.Count; i++) + foreach (var ship in _setWarmlyShip.GetShip()) { - var car = _setWarmlyShip.Get(i); - if (car != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } + return new(_pictureWidth, _pictureHeight); } /// @@ -124,11 +121,11 @@ namespace WarmlyShip int j = _setWarmlyShip.Count - 1; for (int i = 0; i < _setWarmlyShip.Count; i++) { - if (_setWarmlyShip.Get(i) == null) + if (_setWarmlyShip[i] == null) { for (; j > i; j--) { - var car = _setWarmlyShip.Get(j); + var car = _setWarmlyShip[j]; if (car != null) { _setWarmlyShip.Insert(car, i); @@ -167,23 +164,12 @@ namespace WarmlyShip /// private void DrawWarmlyShip(Graphics g) { - int i = 0; - int j = 0; - for (int k = 0; k < _setWarmlyShip.Count; k++) + foreach (var ship in _setWarmlyShip.GetShip()) { - _setWarmlyShip.Get(k)?.SetObject(j + 10, i + 20, _pictureWidth, _pictureHeight); - _setWarmlyShip.Get(k)?.DrawningObject(g); - - if (j >= _pictureWidth - 2 * _placeSizeWidth) - { - j = 0; - i += _placeSizeHeight; - } - else - { - j += _placeSizeWidth; - } + // TODO установка позиции + ship.DrawningObject(g); } + } } } diff --git a/WarmlyShip/WarmlyShip/SetWarmlyShipGeneric.cs b/WarmlyShip/WarmlyShip/SetWarmlyShipGeneric.cs index 7f883a3..e03f6fe 100644 --- a/WarmlyShip/WarmlyShip/SetWarmlyShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetWarmlyShipGeneric.cs @@ -14,20 +14,23 @@ namespace WarmlyShip 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 SetWarmlyShipGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -36,6 +39,8 @@ namespace WarmlyShip /// public int Insert(T ship) { + // + проверка на _maxCount + if (ship == null) { return -1; @@ -55,6 +60,7 @@ namespace WarmlyShip /// public int Insert(T ship, int position) { + //кое что убрать (проверка позиции) if (position < 0 || position > Count) { return -1; @@ -83,6 +89,7 @@ namespace WarmlyShip /// public T Remove(int position) { + // убрать удаление объекта if (_places[position] == null) { return null; @@ -96,13 +103,36 @@ namespace WarmlyShip /// /// /// - public T Get(int position) + public T this[int position] { - if (_places[position] != null) + get { + // TODO проверка позиции return _places[position]; } - return null; + set + { + // TODO проверка позиции + // TODO вставка в список по позиции + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetShip() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } + } } } }