From 83b916307230215c658687e8d71669cea7cfc906 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 18 Oct 2022 12:19:05 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=20Generic=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ContainerShip/MapWithSetShipsGeneric.cs | 170 ++++++++++++++++++ .../ContainerShip/SetShipsGeneric.cs | 109 +++++++++++ 2 files changed, 279 insertions(+) create mode 100644 ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs create mode 100644 ContainerShip/ContainerShip/SetShipsGeneric.cs diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs new file mode 100644 index 0000000..4b69aa7 --- /dev/null +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class MapWithSetShipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 135; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 70; + /// + /// Набор объектов + /// + private readonly SetShipsGeneric _setShips; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setShips = new SetShipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(MapWithSetShipsGeneric map, T ship) + { + return map._setShips.Insert(ship); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MapWithSetShipsGeneric map, int position) + { + return map._setShips.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawShips(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setShips.Count; i++) + { + var ship = _setShips.Get(i); + if (ship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); + } + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + private void Shaking() + { + int j = _setShips.Count - 1; + for (int i = 0; i < _setShips.Count; i++) + { + if (_setShips.Get(i) == null) + { + for (; j > i; j--) + { + var ship = _setShips.Get(j); + if (ship != null) + { + _setShips.Insert(ship, i); + _setShips.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Brown, 3); + g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawShips(Graphics g) + { + for (int i = 0; i < _setShips.Count; i++) + { + //TODO установка позиции + } + } + } +} diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs new file mode 100644 index 0000000..c299746 --- /dev/null +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class SetShipsGeneric + where T: class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetShipsGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public bool Insert(T ship) + { + return Insert(ship, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый корабль + /// Позиция + /// + public bool Insert(T ship, int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + if (_places[position] == null) + { + + _places[position] = ship; + } + else + { + int freePlace = -1; + for (int i = position; i < _places.Length; i++) + { + if (_places[i] == null) + { + freePlace = i; + } + } + if (freePlace == -1) + { + return false; + } + else + { + for (int i = freePlace; i > position; i--) + { + T buf = _places[i]; + _places[i] = _places[i - 1]; + _places[i - 1] = buf; + } + _places[position] = ship; + } + } + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + return _places[position]; + } + } +}