From ba6554f76ad5623499dfee7c1825e529320dab97 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sat, 15 Oct 2022 18:59:41 +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 --- .../MapWithSetWarshipsGeneric.cs | 22 +++-- .../AircraftCarrier/SetWarshipsGeneric.cs | 80 ++++++++++--------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs index ed82a7b..23c8cc4 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -93,13 +93,9 @@ namespace AircraftCarrier public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setWarships.Count; i++) + foreach(var warship in _setWarships.GetWarships()) { - var warship = _setWarships.Get(i); - if (warship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, warship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); } return new(_pictureWidth, _pictureHeight); } @@ -124,11 +120,11 @@ namespace AircraftCarrier int j = _setWarships.Count - 1; for (int i = 0; i < _setWarships.Count; i++) { - if (_setWarships.Get(i) == null) + if (_setWarships[i] == null) { for (; j > i; j--) { - var car = _setWarships.Get(j); + var car = _setWarships[j]; if (car != null) { _setWarships.Insert(car, i); @@ -166,13 +162,13 @@ namespace AircraftCarrier /// private void DrawWarships(Graphics g) { - int countInLine = _pictureWidth / _placeSizeWidth; - int maxLeft = (countInLine - 1) * _placeSizeWidth; - //for (int i = 0; i < _setWarships.Count; i++) + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _setWarships.Count; i++) { - _setWarships.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); - _setWarships.Get(i)?.DrawningObject(g); + var warship = _setWarships[i]; + warship?.SetObject(i % _pictureWidth * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight + 4, _pictureWidth, _pictureHeight); + warship?.DrawningObject(g); } } } diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs index e2a38df..5aba52c 100644 --- a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs @@ -14,20 +14,23 @@ namespace AircraftCarrier 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 SetWarshipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -46,32 +49,8 @@ namespace AircraftCarrier /// public int Insert(T warship, int position) { - int EmptyElement = -1; - if (position >= Count || position < 0) return -1; - - if (_places[position] == null) - { - _places[position] = warship; - return 1; - } - - else if (_places[position] != null) - { - for (int i = position + 1; i < Count; i++) - if (_places[i] == null) - { - EmptyElement = i; - break; - } - - if (EmptyElement == -1) - return -1; - - for (int i = EmptyElement; i > position; i--) - _places[i] = _places[i - 1]; - } - - _places[position] = warship; + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, warship); return 1; } /// @@ -81,22 +60,47 @@ namespace AircraftCarrier /// public T Remove(int position) { - if (position >= Count || position < 0 || _places[position] == null) + if (position >= _maxCount || position < 0) return null; - T deleted = _places[position]; - _places[position] = null; - return deleted; + var result = _places[position]; + _places.RemoveAt(position); + return result; } /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { - if (position >= _places.Length) return null; - return _places[position]; + get + { + if (position >= _maxCount || position < 0) return null; + return _places[position]; + } + set + { + Insert(value, position); + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetWarships() + { + foreach(var warship in _places) + { + if(warship != null) + { + yield return warship; + } + else + { + yield break; + } + } } } }