From b0b97b53e05e79366333f417fc35ed6df3cab7fe Mon Sep 17 00:00:00 2001 From: Daniya_Youdakova Date: Thu, 15 Dec 2022 21:59:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetAircraftCarriersGeneric.cs | 190 ++++++++++++++++++ AircraftCarrier/AircraftCarrier/Program.cs | 2 +- .../SetAircraftCarriersGeneric.cs | 99 +++++++++ AircraftCarrier/AircraftCarrier/SimpleMap.cs | 3 +- 4 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs create mode 100644 AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs new file mode 100644 index 0000000..379f1c9 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs @@ -0,0 +1,190 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class MapWithSetAircraftCarriersGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 250; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 100; + /// + /// Набор объектов + /// + private readonly SetAircraftCarriersGeneric _setAircraftCarriers; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetAircraftCarriersGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setAircraftCarriers = new SetAircraftCarriersGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(MapWithSetAircraftCarriersGeneric map, T AircraftCarrier) + { + return map._setAircraftCarriers.Insert(AircraftCarrier); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetAircraftCarriersGeneric map, int + position) + { + return map._setAircraftCarriers.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawAircraftCarriers(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setAircraftCarriers.Count; i++) + { + var aircraftcarrier = _setAircraftCarriers.Get(i); + if (aircraftcarrier != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, aircraftcarrier); + } + } + 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 = _setAircraftCarriers.Count - 1; + for (int i = 0; i < _setAircraftCarriers.Count; i++) + { + if (_setAircraftCarriers.Get(i) == null) + { + for (; j > i; j--) + { + var aircraftcarrier = _setAircraftCarriers.Get(j); + if (aircraftcarrier != null) + { + _setAircraftCarriers.Insert(aircraftcarrier, i); + _setAircraftCarriers.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + 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 DrawAircraftCarriers(Graphics g) + { + int yNumOfPlaces = _pictureHeight / _placeSizeHeight; + int xNumOfPlaces = _pictureWidth / _placeSizeWidth; + int rowNum = yNumOfPlaces - 1; + int columnNum = 0; + + for (int i = 0; i < _setAircraftCarriers.Count; i++) + { + if (_setAircraftCarriers.Get(i) != null) + { + (float Left, float Top, float Right, float Bottom) = _setAircraftCarriers.Get(i).GetCurrentPosition(); + _setAircraftCarriers.Get(i).SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight); + _setAircraftCarriers.Get(i).DrawningObject(g); + } + if (columnNum == xNumOfPlaces - 1) + { + columnNum = 0; + rowNum--; + } + else + { + columnNum++; + } + } + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/Program.cs b/AircraftCarrier/AircraftCarrier/Program.cs index 4080ebd..8aecab1 100644 --- a/AircraftCarrier/AircraftCarrier/Program.cs +++ b/AircraftCarrier/AircraftCarrier/Program.cs @@ -11,7 +11,7 @@ namespace AircraftCarrier // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetAircraftCarriers()); } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs new file mode 100644 index 0000000..017e06b --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class SetAircraftCarriersGeneric + where T : class + { + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetAircraftCarriersGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T AircraftCarrier) + { + return Insert(AircraftCarrier, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T AircraftCarrier, int position) + { + // TODO проверка позиции + if (position >= _places.Length || position < 0) + return -1; + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + if (_places[position] == null) + { + _places[position] = AircraftCarrier; + return position; + } + // проверка, что после вставляемого элемента в массиве есть пустой элемент + int findEmptyPos = -1; + + for (int i = position + 1; i < Count; i++) + { + if (_places[i] == null) + { + findEmptyPos = i; + break; + } + } + if (findEmptyPos < 0) return -1; + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + for (int i = findEmptyPos; i > position; i--) + { + _places[i] = _places[i - 1]; + } + // TODO вставка по позиции + _places[position] = AircraftCarrier; + return position; + } + + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T Remove(int position) + { + // TODO проверка позиции + if (position >= _places.Length || position < 0) return null; + // TODO удаление объекта из массива, присовив элементу массива значение null + T temp = _places[position]; + _places[position] = null; + return temp; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position >= _places.Length || position < 0) + return null; + return _places[position]; + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SimpleMap.cs b/AircraftCarrier/AircraftCarrier/SimpleMap.cs index c9cc6b8..fdd003f 100644 --- a/AircraftCarrier/AircraftCarrier/SimpleMap.cs +++ b/AircraftCarrier/AircraftCarrier/SimpleMap.cs @@ -36,7 +36,7 @@ namespace AircraftCarrier _map[i, j] = _freeRoad; } } - while (counter < 10) + while (counter < 5) { int x = _random.Next(0, 100); int y = _random.Next(0, 100); @@ -47,5 +47,6 @@ namespace AircraftCarrier } } } + } } \ No newline at end of file