diff --git a/Battleship/Battleship/CollectionGenericObjects/ICollectionGenericObjects.cs b/Battleship/Battleship/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..98e560d --- /dev/null +++ b/Battleship/Battleship/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace Battleship.CollectionGenericObjects; + +/// +/// Интерфейс описания действий для набора хранимых объектов +/// +/// Параметр: ограничение - ссылочный тип +public interface ICollectionGenericObjects + where T : class +{ + /// + /// Количество объектов в коллекции + /// + int Count { get; } + + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - удачно, false - вставка не удалась + bool Insert(T obj); + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// true - удачно, false - вставка не удалась + bool Insert(T obj, int position); + + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// true - удачно, false - удаление не удалось + bool Remove(int position); + + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); +} diff --git a/Battleship/Battleship/CollectionGenericObjects/MassiveGenericObjects.cs b/Battleship/Battleship/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..ad719bb --- /dev/null +++ b/Battleship/Battleship/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace Battleship.CollectionGenericObjects; +/// +/// Параметризованный набор объектов +/// +/// Параметр: ограничение - ссылочный тип +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + /// + /// Массив объектов, которые храним + /// + private T?[] _collection; + public int Count => _collection.Length; + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } + + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position < 0 || position >= _collection.Length) + return null; + return _collection[position]; + } + + public bool Insert(T obj) + { + for (int i = 0; i < _collection.Length; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return true; + } + } + return false; + } + + public bool Insert(T obj, int position) + { + if (position < 0 || position >= _collection.Length) // проверка позиции + return false; + if (_collection[position] == null) // Попытка вставить на указанную позицию + { + _collection[position] = obj; + return true; + } + for (int i = position; i < _collection.Length; i++) // попытка вставить объект на позицию после указанной + { + if (_collection[i] == null) + { + _collection[i] = obj; + return true; + } + } + for (int i = 0; i < position; i++) // попытка вставить объект на позицию до указанной + { + if (_collection[i] == null) + { + _collection[i] = obj; + return true; + } + } + return false; + } + + public bool Remove(int position) + { + if (position < 0 || position >= _collection.Length || _collection[position]!=null) // проверка позиции и наличия объекта + return false; + _collection[position] = null; + return true; + } +}