From 32edffdb33c5e562501ce50ea7da8175985cac3d Mon Sep 17 00:00:00 2001 From: the Date: Tue, 11 Oct 2022 10:59:59 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A5=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B2=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ship/Ship/MapWithSetShipsGeneric.cs | 18 +++---- Ship/Ship/SetShipsGeneric.cs | 83 +++++++++++++++-------------- 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/Ship/Ship/MapWithSetShipsGeneric.cs b/Ship/Ship/MapWithSetShipsGeneric.cs index 9bf0e93..85a09f7 100644 --- a/Ship/Ship/MapWithSetShipsGeneric.cs +++ b/Ship/Ship/MapWithSetShipsGeneric.cs @@ -92,7 +92,7 @@ namespace Ship Shaking(); for (int i = 0; i < _setShips.Count; i++) { - var car = _setShips.Get(i); + var car = _setShips[i]; if (car != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, car); @@ -121,11 +121,11 @@ namespace Ship 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 car = _setShips.Get(j); + var car = _setShips[j]; if (car != null) { _setShips.Insert(car, i); @@ -174,14 +174,12 @@ namespace Ship int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _setShips.Count; i++) + int i = 0; + foreach (var ship in _setShips.GetShips()) { - var ship = _setShips.Get(i); - if (ship != null) - { - ship.SetObject((width - i % width - 1) * _placeSizeWidth + 10, (i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - ship.DrawningObject(g); - } + ship.SetObject((width - i % width - 1) * _placeSizeWidth + 10, (i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + ship.DrawningObject(g); + i++; } } } diff --git a/Ship/Ship/SetShipsGeneric.cs b/Ship/Ship/SetShipsGeneric.cs index e924df8..04a95e3 100644 --- a/Ship/Ship/SetShipsGeneric.cs +++ b/Ship/Ship/SetShipsGeneric.cs @@ -12,18 +12,21 @@ namespace Ship /// /// Массив объектов, которые храним /// - 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(); } /// /// Добавление объекта в набор @@ -42,38 +45,9 @@ namespace Ship /// public int Insert(T ship, int position) { - if (position < 0 || position >= Count) - { - return -1; - } + if (position > _maxCount || Count == _maxCount || position < 0) return -1; - if (_places[position] == null) - { - _places[position] = ship; - return position; - } - - int firstNull = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - firstNull = i; - break; - } - } - - if (firstNull == -1) - { - return -1; - } - - for (int i = firstNull; i > position; i--) - { - (_places[i], _places[i - 1]) = (_places[i - 1], _places[i]); - } - _places[position] = ship; + _places.Insert(position, ship); return position; } /// @@ -83,14 +57,14 @@ namespace Ship /// public T Remove(int position) { - if (position < 0 || position >= Count || _places[position] == null) + if (position < 0 || position >= Count) { return null; } var result = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return result; } /// @@ -98,14 +72,41 @@ namespace Ship /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) + get { - return null; + if (position >= Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position < Count && position >= 0) + { + Insert(value, position); + } + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } - } }