From bf9dbaa9dcf459dd791a23339abeddb04362ad49 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:27:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B5=D1=82=D0=B2=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?.=20=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=BD?= =?UTF-8?q?=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 --- .../ContainerShip/MapWithSetShipsGeneric.cs | 26 +++--- .../ContainerShip/SetShipsGeneric.cs | 89 +++++++++---------- 2 files changed, 56 insertions(+), 59 deletions(-) diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs index dfda657..56c9a2d 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -44,7 +44,7 @@ namespace ContainerShip { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; - _setShips = new SetShipsGeneric(width * height); + _setShips = new SetShipsGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; @@ -68,13 +68,9 @@ namespace ContainerShip 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); } @@ -99,11 +95,11 @@ namespace ContainerShip 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); @@ -141,12 +137,14 @@ namespace ContainerShip /// private void DrawShips(Graphics g) { - for (int i = 0; i < _setShips.Count; i++) + int i = 0; + int countOfShipsInLine = _pictureWidth / _placeSizeWidth; + int countOfShipsInColumn = _pictureHeight / _placeSizeHeight; + foreach (var ship in _setShips.GetShips()) { - int countOfShipsInLine = _pictureWidth / _placeSizeWidth; - int countOfShipsInColumn = _pictureHeight / _placeSizeHeight; - _setShips.Get(i)?.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setShips.Get(i)?.DrawingObject(g); + ship.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); + ship.DrawingObject(g); + i++; } } /// diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index 883942d..04cbea5 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -10,20 +10,22 @@ namespace ContainerShip 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(); } /// /// Добавление объекта в набор @@ -41,41 +43,10 @@ namespace ContainerShip /// Позиция /// public int Insert(T ship, int position) - { - if (position < 0 || position >= _places.Length) - { - return -1; - } - if (_places[position] == null) - { - - _places[position] = ship; - } - else - { - int freePlace = -1; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - freePlace = i; - } - } - if (freePlace == -1) - { - return -1; - } - else - { - for (int i = freePlace; i > position; i--) - { - T buf = _places[i]; - _places[i] = _places[i - 1]; - _places[i - 1] = buf; - } - _places[position] = ship; - } - } + { + if (position < 0 || position >= _maxCount) return -1; + if (_places.Count + 1 >= _maxCount) return -1; + _places.Add(ship); return position; } /// @@ -85,7 +56,7 @@ namespace ContainerShip /// public T Remove(int position) { - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= _places.Count) { return null; } @@ -98,13 +69,41 @@ namespace ContainerShip /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= _places.Length) + get { - return null; + // TODO проверка позиции + if (position < 0 || position > _maxCount) return null; + return _places[position]; + } + set + { + if (position >= 0 || position < _maxCount) + { + Insert(value, position); + } + // TODO проверка позиции + // TODO вставка в список по позиции + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } } }