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 /// /// Массив объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор @@ -88,12 +91,36 @@ namespace Battleship.Generics /// /// /// - 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 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? collect, int pos) + public static T? operator -(ShipGenericCollection? 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 + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _shipStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _shipStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// /// + public ShipsGenericStorage(int pictureWidth, int pictureHeight) + { + _shipStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + // TODO Прописать логику для добавления + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + // TODO Прописать логику для удаления + } + /// + /// Доступ к набору + /// + /// + /// + public ShipGenericCollection? + this[string ind] + { + get + { + // TODO Продумать логику получения набора + return null; + } + } + } +} +