From a23192133f2373c89372c00663e88619a1e154f5 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Wed, 19 Oct 2022 17:20:36 +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 --- Liner/Liner/MapWithSetShipsGeneric.cs | 18 +++--- Liner/Liner/SetShipsGeneric.cs | 79 +++++++++++++++------------ 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/Liner/Liner/MapWithSetShipsGeneric.cs b/Liner/Liner/MapWithSetShipsGeneric.cs index b8e16b9..5d9931f 100644 --- a/Liner/Liner/MapWithSetShipsGeneric.cs +++ b/Liner/Liner/MapWithSetShipsGeneric.cs @@ -44,13 +44,9 @@ namespace Liner 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); } @@ -67,11 +63,11 @@ namespace Liner 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); @@ -107,11 +103,11 @@ namespace Liner int heightEl = _pictureHeight / _placeSizeHeight; int curWidth = 1; int curHeight = 0; - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - _setShips.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - (_pictureWidth / 7), + ship?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 10, curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setShips.Get(i)?.DrawningObject(g); + ship?.DrawningObject(g); if (curWidth < widthEl) curWidth++; diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 0f4b0f0..5c9bfad 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -9,64 +9,71 @@ namespace Liner internal class SetShipsGeneric 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 SetShipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T ship) { + if (_places.Count > _maxCount) + { + return -1; + } return Insert(ship, 0); } public int Insert(T ship, int position) { - if (position >= _places.Length || position < 0) - { - return -1; - } - if (_places[position] == null) - { - _places[position] = ship; - return position; - } - int emptyIndex = -1; - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - emptyIndex = i; - break; - } - } - if (emptyIndex < 0) - { - return -1; - } - for (int i = emptyIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = ship; + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, ship); return position; } public T Remove(int position) { - if (position >= _places.Length || position < 0) + if (position >= _maxCount || position < 0) { return null; } T ship = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return ship; } - public T Get(int position) + public T this[int position] { - if (position >= _places.Length || position < 0) + get { - return null; + if (position >= _places.Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position >= _places.Count || position < 0) + { + return; + } + Insert(value, position); + + } + } + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } } } \ No newline at end of file