From 596b0127e7de8fb22bb62615fb5288f44f4f4d8b Mon Sep 17 00:00:00 2001
From: kaznacheeva <academicacc@mail.ru>
Date: Wed, 25 Oct 2023 08:47:38 +0400
Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Battleship/Battleship/SetGeneric.cs           | 43 +++++++++--
 .../Battleship/ShipGenericCollection.cs       | 17 +++--
 Battleship/Battleship/ShipsGenericStorage.cs  | 74 +++++++++++++++++++
 3 files changed, 118 insertions(+), 16 deletions(-)
 create mode 100644 Battleship/Battleship/ShipsGenericStorage.cs

diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs
index 8047a61..4211842 100644
--- a/Battleship/Battleship/SetGeneric.cs
+++ b/Battleship/Battleship/SetGeneric.cs
@@ -16,18 +16,21 @@ namespace Battleship.Generics
         /// <summary>
         /// Массив объектов, которые храним
         /// </summary>
-        private readonly T?[] _places;
+        private readonly List<T?> _places;
         /// <summary>
         /// Количество объектов в массиве
         /// </summary>
-        public int Count => _places.Length;
+        public int Count => _places.Count;
+
+        private readonly int _maxCount;
         /// <summary>
         /// Конструктор
         /// </summary>
         /// <param name="count"></param>
         public SetGeneric(int count)
         {
-            _places = new T?[count];
+            _maxCount = count;
+            _places = new List<T?>(count);
         }
         /// <summary>
         /// Добавление объекта в набор
@@ -88,12 +91,36 @@ namespace Battleship.Generics
         /// </summary>
         /// <param name="position"></param>
         /// <returns></returns>
-        public T? Get(int position)
+        public T? this[int position]
         {
-            // TODO проверка позиции
-            if (!(position >= 0 && position < Count))
-                return null;
-            return _places[position];
+            get
+            {
+                if (position < 0 || position > _maxCount)
+                    return null;
+                return _places[position];
+
+            }
+            set
+            {
+                if(!(position >= 0 && position < Count && _places.Count < _maxCount))
+                {
+                    return;
+                }
+                _places.Insert(position, value);
+                return;
+            }
+
+        }
+        public IEnumerable<T> GetShips(int? maxShips = null)
+        {
+            for (int i = 0; i < _places.Count; ++i)
+            {
+                yield return _places[i];
+                if (maxShips.HasValue && i == maxShips.Value)
+                {
+                    yield break;
+                }
+            }
         }
     }
 }
diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs
index 9a0ab6d..9fb13ff 100644
--- a/Battleship/Battleship/ShipGenericCollection.cs
+++ b/Battleship/Battleship/ShipGenericCollection.cs
@@ -31,18 +31,18 @@ namespace Battleship.Generics
                 return collect._collection.Insert(obj);
             return -1;
         }
-        public static bool operator -(ShipGenericCollection<T, U>? collect, int pos)
+        public static T? operator -(ShipGenericCollection<T, U>? collect, int pos)
         {
-            T? obj = collect?._collection.Get(pos);
+            T? obj = collect?._collection[pos];
             if (obj != null && collect != null)
             {
-                return collect._collection.Remove(pos);
+                collect._collection.Remove(pos);
             }
-            return false;
+            return obj;
         }
         public U? GetU(int pos)
         {
-            return (U?)_collection.Get(pos)?.GetMoveableObject;
+            return (U?)_collection[pos]?.GetMoveableObject;
         }
         public Bitmap ShowShips()
         {
@@ -70,15 +70,16 @@ namespace Battleship.Generics
         }
         private void DrawObjects(Graphics g)
         {
-            for (int i = 0; i < _collection.Count; i++)
+            int i = 0;
+            foreach(var ship in _collection.GetShips())
             {
-                 DrawningShip? ship = _collection.Get(i);
                 if(ship != null)
                 {
                     int inRow = _pictureWidth / _placeSizeWidth;
-                    ship.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5);
+                    ship.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight + 5);
                     ship.DrawTransport(g);
                 }
+                i++;
             }
         }
     }
diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs
new file mode 100644
index 0000000..ffe3377
--- /dev/null
+++ b/Battleship/Battleship/ShipsGenericStorage.cs
@@ -0,0 +1,74 @@
+using Battleship.DrawningObjects;
+using Battleship.MovementStrategy;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Battleship.Generics
+{
+    internal class ShipsGenericStorage
+    {
+        /// <summary>
+        /// Словарь (хранилище)
+        /// </summary>
+        readonly Dictionary<string, ShipGenericCollection<DrawningShip,
+        DrawningObjectShip>> _shipStorages;
+        /// <summary>
+        /// Возвращение списка названий наборов
+        /// </summary>
+        public List<string> Keys => _shipStorages.Keys.ToList();
+        /// <summary>
+        /// Ширина окна отрисовки
+        /// </summary>
+        private readonly int _pictureWidth;
+        /// <summary>
+        /// Высота окна отрисовки
+        /// </summary>
+        private readonly int _pictureHeight;
+        /// <summary>
+        /// Конструктор
+        /// </summary>
+        /// <param name="pictureWidth"></param>
+        /// /// <param name="pictureHeight"></param>
+        public ShipsGenericStorage(int pictureWidth, int pictureHeight)
+        {
+            _shipStorages = new Dictionary<string,
+            ShipGenericCollection<DrawningShip, DrawningObjectShip>>();
+            _pictureWidth = pictureWidth;
+            _pictureHeight = pictureHeight;
+        }
+        /// <summary>
+        /// Добавление набора
+        /// </summary>
+        /// <param name="name">Название набора</param>
+        public void AddSet(string name)
+        {
+            // TODO Прописать логику для добавления
+        }
+        /// <summary>
+        /// Удаление набора
+        /// </summary>
+        /// <param name="name">Название набора</param>
+        public void DelSet(string name)
+        {
+            // TODO Прописать логику для удаления
+        }
+        /// <summary>
+        /// Доступ к набору
+        /// </summary>
+        /// <param name="ind"></param>
+        /// <returns></returns>
+        public ShipGenericCollection<DrawningShip, DrawningObjectShip>?
+        this[string ind]
+        {
+            get
+            {
+                // TODO Продумать логику получения набора
+                return null;
+            }
+        }
+    }
+}
+