From 903669666a1fc09a4af4fe50e24ac454f1982eec Mon Sep 17 00:00:00 2001 From: just1valery Date: Sun, 9 Oct 2022 12:52:05 +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/MapWithSetShipsGeneric.cs | 16 +++---- WarmlyShip/WarmlyShip/SetShipsGeneric.cs | 47 +++++++++++++++---- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs index 212d2f0..bf56fd9 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs @@ -88,13 +88,9 @@ namespace WarmlyShip public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - var ship = _setShips.Get(i); - if (ship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, ship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } return new(_pictureWidth, _pictureHeight); } @@ -119,11 +115,11 @@ namespace WarmlyShip int j = _setShips.Count - 1; for (int i = 0; i < _setShips.Count; i++) { - if (_setShips.Get(i) == null) + if (_setShips[i] == null) { for (; j > i; j--) { - var ship = _setShips.Get(j); + var ship = _setShips[j]; if (ship != null) { _setShips.Insert(ship, i); @@ -170,9 +166,9 @@ namespace WarmlyShip { int countInLine = _pictureWidth / _placeSizeWidth; int maxLeft = (countInLine - 1) * _placeSizeWidth; - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - var ship = _setShips.Get(i); + var ship = _setShips[i]; ship?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); ship?.DrawningObject(g); } diff --git a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs index 8fa50b1..62df44e 100644 --- a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs @@ -10,20 +10,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 SetShipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -32,6 +35,7 @@ namespace WarmlyShip /// public bool Insert(T ship) { + //проверка на макс каунт return Insert(ship, 0); } private bool isCorrectPosition(int position) @@ -70,7 +74,7 @@ namespace WarmlyShip /// /// public bool Remove(int position) - { + { if (!isCorrectPosition(position)) return false; _places[position-1] = null; @@ -81,9 +85,36 @@ namespace WarmlyShip /// /// /// - public T Get(int position) + public T this[int position] { - return isCorrectPosition(position) ? _places[position] : null; + get + { + // TODO проверка позиции + return _places[position]; + } + set + { + // TODO проверка позиции + // TODO вставка в список по позиции + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } + } } } }