From 5c698b6514bbf7000398ba936a660610fe7eb8c3 Mon Sep 17 00:00:00 2001 From: KirillFilippow Date: Wed, 6 Dec 2023 13:45:35 +0400 Subject: [PATCH] Laba03 --- .../ContainerShip/AbstractStrategy.cs | 3 +- .../ContainerGenericCollection.cs | 146 ++++++++++++++++++ ContainerShip/ContainerShip/Direction.cs | 1 + .../ContainerShip/DrawingContainerShip.cs | 23 +-- .../ContainerShip/DrawningObjectShip.cs | 2 +- ContainerShip/ContainerShip/DrawningShip.cs | 13 +- .../ContainerShip/EntityContainer.cs | 40 ----- .../ContainerShip/EntityContainerShip.cs | 4 +- .../FormContainerShip.Designer.cs | 46 ++++-- .../ContainerShip/FormContainerShip.cs | 60 +++++-- .../ContainerShip/FormContainerShip.resx | 8 +- .../FormShipCollection.Designer.cs | 121 +++++++++++++++ .../ContainerShip/FormShipCollection.cs | 82 ++++++++++ .../ContainerShip/FormShipCollection.resx | 120 ++++++++++++++ ContainerShip/ContainerShip/MoveToBorder.cs | 2 +- ContainerShip/ContainerShip/Program.cs | 7 +- ContainerShip/ContainerShip/SetGeneric.cs | 105 +++++++++++++ 17 files changed, 675 insertions(+), 108 deletions(-) create mode 100644 ContainerShip/ContainerShip/ContainerGenericCollection.cs delete mode 100644 ContainerShip/ContainerShip/EntityContainer.cs create mode 100644 ContainerShip/ContainerShip/FormShipCollection.Designer.cs create mode 100644 ContainerShip/ContainerShip/FormShipCollection.cs create mode 100644 ContainerShip/ContainerShip/FormShipCollection.resx create mode 100644 ContainerShip/ContainerShip/SetGeneric.cs diff --git a/ContainerShip/ContainerShip/AbstractStrategy.cs b/ContainerShip/ContainerShip/AbstractStrategy.cs index 927a3b5..dd957ac 100644 --- a/ContainerShip/ContainerShip/AbstractStrategy.cs +++ b/ContainerShip/ContainerShip/AbstractStrategy.cs @@ -90,8 +90,7 @@ namespace ProjectContainerShip.MovementStrategy /// /// Параметры объекта /// - protected ObjectParameters? GetObjectParameters => - _moveableObject?.GetObjectPosition; + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; /// /// Шаг объекта /// diff --git a/ContainerShip/ContainerShip/ContainerGenericCollection.cs b/ContainerShip/ContainerShip/ContainerGenericCollection.cs new file mode 100644 index 0000000..968f6df --- /dev/null +++ b/ContainerShip/ContainerShip/ContainerGenericCollection.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectContainerShip.DrawningObjects; +using ProjectContainerShip.Generics; +using ProjectContainerShip.MovementStrategy; + +namespace ProjectContainerShip.Generics +{ + /// + /// Параметризованный класс для набора объектов + /// + /// + /// + internal class ContainerGenericCollection + where T : DrawningShip + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 200; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public ContainerGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int? operator +(ContainerGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(ContainerGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj is not null) + { + collect._collection.Remove(pos); + return true; + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetObjectPosition; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowContainer() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 2); + 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 , j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + DrawningShip? ship = _collection.Get(i); + if (ship == null) + continue; + int row = height - 1 - (i / width); + int col = width - 1 - (i % width); + ship.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight + 25 ); + ship.DrawTransport(g); + } + } + } +} + diff --git a/ContainerShip/ContainerShip/Direction.cs b/ContainerShip/ContainerShip/Direction.cs index d0b2349..7c15193 100644 --- a/ContainerShip/ContainerShip/Direction.cs +++ b/ContainerShip/ContainerShip/Direction.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; diff --git a/ContainerShip/ContainerShip/DrawingContainerShip.cs b/ContainerShip/ContainerShip/DrawingContainerShip.cs index 34caacc..44b9cbf 100644 --- a/ContainerShip/ContainerShip/DrawingContainerShip.cs +++ b/ContainerShip/ContainerShip/DrawingContainerShip.cs @@ -39,24 +39,15 @@ namespace ProjectContainerShip.DrawningObjects } Brush additionalBrush = new SolidBrush(containerShip.AdditionalColor); // Контейнер - if (containerShip.Container) - { - g.FillRectangle(additionalBrush, _startPosX + 55, _startPosY + 7, 35, 5); - } + g.FillRectangle(additionalBrush, _startPosX + 55, _startPosY + 7, 35, 5); //кран - if (containerShip.Crane) - { - Brush brBl = new SolidBrush(Color.Black); - g.FillRectangle(brBl, _startPosX + 110, _startPosY + 0, 3, 12); - g.FillRectangle(brBl, _startPosX + 105, _startPosY + 1, 20, 2); - } + Brush brBl = new SolidBrush(Color.Black); + g.FillRectangle(brBl, _startPosX + 110, _startPosY + 0, 3, 12); + g.FillRectangle(brBl, _startPosX + 105, _startPosY + 1, 20, 2); // несколько контенеров - if (containerShip.Container) - { - g.FillRectangle(additionalBrush, _startPosX + 55, _startPosY + 2, 35, 5); - g.FillRectangle(additionalBrush, _startPosX + 135, _startPosY + 7, 35, 5); - g.FillRectangle(additionalBrush, _startPosX + 135, _startPosY + 2, 35, 5); - } + g.FillRectangle(additionalBrush, _startPosX + 55, _startPosY + 2, 35, 5); + g.FillRectangle(additionalBrush, _startPosX + 135, _startPosY + 7, 35, 5); + g.FillRectangle(additionalBrush, _startPosX + 135, _startPosY + 2, 35, 5); base.DrawTransport(g); } } diff --git a/ContainerShip/ContainerShip/DrawningObjectShip.cs b/ContainerShip/ContainerShip/DrawningObjectShip.cs index d06d13a..a08e165 100644 --- a/ContainerShip/ContainerShip/DrawningObjectShip.cs +++ b/ContainerShip/ContainerShip/DrawningObjectShip.cs @@ -9,7 +9,7 @@ using static ProjectContainerShip.Direction; namespace ProjectContainerShip.MovementStrategy { /// - /// Реализация интерфейса IDrawningObject для работы с объектом DrawningShip (паттерн Adapter) + /// Реализация интерфейса IMoveableObject для работы с объектом DrawningShip /// public class DrawningObjectShip : IMoveableObject { diff --git a/ContainerShip/ContainerShip/DrawningShip.cs b/ContainerShip/ContainerShip/DrawningShip.cs index 968b443..60bfd79 100644 --- a/ContainerShip/ContainerShip/DrawningShip.cs +++ b/ContainerShip/ContainerShip/DrawningShip.cs @@ -97,7 +97,6 @@ namespace ProjectContainerShip.DrawningObjects else _startPosX = 0; if ((y > 0) && (y < _pictureHeight)) _startPosY = y; - else _startPosY = 0; _startPosX = x; _startPosY = y; @@ -177,23 +176,23 @@ namespace ProjectContainerShip.DrawningObjects /// /// public virtual void DrawTransport(Graphics g) - { + { if (EntityShip == null) { return; - } + } + Brush bodyBrush = new SolidBrush(EntityShip.BodyColor); //границы корабля - Brush brRD = new SolidBrush(Color.Red); + Brush brRd = new SolidBrush(Color.Red); Point point1 = new Point(_startPosX, _startPosY + 12); Point point2 = new Point(_startPosX + 25, _startPosY + 35); Point point3 = new Point(_startPosX + 175, _startPosY + 35); Point point4 = new Point(_startPosX + 200, _startPosY + 12); Point[] curvePoints1 = { point1, point2, point3, point4 }; - g.FillPolygon(brRD, curvePoints1); - + g.FillPolygon(brRd, curvePoints1); //граница палубы Brush brBlue = new SolidBrush(Color.Blue); - g.FillRectangle(brBlue, _startPosX + 35, _startPosY + 0, 16, 12); + g.FillRectangle(bodyBrush, _startPosX + 35, _startPosY + 0, 16, 12); } } } diff --git a/ContainerShip/ContainerShip/EntityContainer.cs b/ContainerShip/ContainerShip/EntityContainer.cs deleted file mode 100644 index 27a8272..0000000 --- a/ContainerShip/ContainerShip/EntityContainer.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectContainerShip -{ - public class EntityContainer - { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса конетейнеровоза - /// - /// Скорость - /// Вес Контейнеровоза - /// Основной цвет - public void Init(int speed, double weight, Color bodyColor) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - } - } -} diff --git a/ContainerShip/ContainerShip/EntityContainerShip.cs b/ContainerShip/ContainerShip/EntityContainerShip.cs index 81c8a77..e1bfc29 100644 --- a/ContainerShip/ContainerShip/EntityContainerShip.cs +++ b/ContainerShip/ContainerShip/EntityContainerShip.cs @@ -28,8 +28,8 @@ namespace ProjectContainerShip.Entities /// Вес /// Основной цвет /// Дополнительный цвет - /// Признак наличия крана - /// Признак наличия контейнеров + /// Признак наличия труб + /// Признак наличия топлива public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, bool crane, bool container) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; diff --git a/ContainerShip/ContainerShip/FormContainerShip.Designer.cs b/ContainerShip/ContainerShip/FormContainerShip.Designer.cs index a33d0ae..238861c 100644 --- a/ContainerShip/ContainerShip/FormContainerShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormContainerShip.Designer.cs @@ -1,4 +1,4 @@ -namespace ProjectWarmlyShip +namespace ProjectContainerShip { partial class FormContainerShip { @@ -29,7 +29,7 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormContainerShip)); - pictureBoxWarmlyShip = new PictureBox(); + pictureBoxContainerShip = new PictureBox(); buttonCreate = new Button(); buttonRight = new Button(); buttonUp = new Button(); @@ -38,19 +38,20 @@ comboBoxStrategy = new ComboBox(); buttonStep = new Button(); buttonCreateDeckShip = new Button(); - ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); + buttonSelectShip = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxContainerShip).BeginInit(); SuspendLayout(); // - // pictureBoxWarmlyShip + // pictureBoxContainerShip // - pictureBoxWarmlyShip.Dock = DockStyle.Fill; - pictureBoxWarmlyShip.Location = new Point(0, 0); - pictureBoxWarmlyShip.Margin = new Padding(3, 4, 3, 4); - pictureBoxWarmlyShip.Name = "pictureBoxWarmlyShip"; - pictureBoxWarmlyShip.Size = new Size(1010, 615); - pictureBoxWarmlyShip.SizeMode = PictureBoxSizeMode.AutoSize; - pictureBoxWarmlyShip.TabIndex = 0; - pictureBoxWarmlyShip.TabStop = false; + pictureBoxContainerShip.Dock = DockStyle.Fill; + pictureBoxContainerShip.Location = new Point(0, 0); + pictureBoxContainerShip.Margin = new Padding(3, 4, 3, 4); + pictureBoxContainerShip.Name = "pictureBoxContainerShip"; + pictureBoxContainerShip.Size = new Size(1010, 615); + pictureBoxContainerShip.SizeMode = PictureBoxSizeMode.AutoSize; + pictureBoxContainerShip.TabIndex = 0; + pictureBoxContainerShip.TabStop = false; // // buttonCreate // @@ -142,11 +143,23 @@ buttonCreateDeckShip.UseVisualStyleBackColor = true; buttonCreateDeckShip.Click += buttonCreateDeckShip_Click; // + // buttonSelectShip + // + buttonSelectShip.Location = new Point(317, 564); + buttonSelectShip.Margin = new Padding(3, 4, 3, 4); + buttonSelectShip.Name = "buttonSelectShip"; + buttonSelectShip.Size = new Size(86, 31); + buttonSelectShip.TabIndex = 9; + buttonSelectShip.Text = "Выбрать"; + buttonSelectShip.UseVisualStyleBackColor = true; + buttonSelectShip.Click += buttonSelectShip_Click; + // // FormContainerShip // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1010, 615); + Controls.Add(buttonSelectShip); Controls.Add(buttonCreateDeckShip); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); @@ -155,19 +168,19 @@ Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonCreate); - Controls.Add(pictureBoxWarmlyShip); + Controls.Add(pictureBoxContainerShip); Margin = new Padding(3, 4, 3, 4); Name = "FormContainerShip"; StartPosition = FormStartPosition.CenterScreen; - Text = "WarmlyShip"; - ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).EndInit(); + Load += FormContainerShip_Load; + ((System.ComponentModel.ISupportInitialize)pictureBoxContainerShip).EndInit(); ResumeLayout(false); PerformLayout(); } #endregion - private PictureBox pictureBoxWarmlyShip; + private PictureBox pictureBoxContainerShip; private Button buttonCreate; private Button buttonRight; private Button buttonUp; @@ -176,5 +189,6 @@ private ComboBox comboBoxStrategy; private Button buttonStep; private Button buttonCreateDeckShip; + private Button buttonSelectShip; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormContainerShip.cs b/ContainerShip/ContainerShip/FormContainerShip.cs index a0dc5fd..a46428f 100644 --- a/ContainerShip/ContainerShip/FormContainerShip.cs +++ b/ContainerShip/ContainerShip/FormContainerShip.cs @@ -2,7 +2,7 @@ using ProjectContainerShip.DrawningObjects; using ProjectContainerShip.MovementStrategy; using static ProjectContainerShip.Direction; -namespace ProjectWarmlyShip +namespace ProjectContainerShip { /// /// "" @@ -17,14 +17,15 @@ namespace ProjectWarmlyShip /// /// private AbstractStrategy? _abstractStrategy; + public DrawningShip? SelectedShip { get; private set; } /// /// /// public FormContainerShip() { InitializeComponent(); - comboBoxStrategy.Items.Add(0); - comboBoxStrategy.Items.Add(1); + _abstractStrategy = null; + _drawingContainerShip = null; } /// /// @@ -35,10 +36,10 @@ namespace ProjectWarmlyShip { return; } - Bitmap bmp = new(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + Bitmap bmp = new(pictureBoxContainerShip.Width, pictureBoxContainerShip.Height); Graphics gr = Graphics.FromImage(bmp); _drawingContainerShip.DrawTransport(gr); - pictureBoxWarmlyShip.Image = bmp; + pictureBoxContainerShip.Image = bmp; } /// /// "" @@ -48,8 +49,15 @@ namespace ProjectWarmlyShip private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawingContainerShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _drawingContainerShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), color, + pictureBoxContainerShip.Width, pictureBoxContainerShip.Height); _drawingContainerShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -61,11 +69,21 @@ namespace ProjectWarmlyShip private void buttonCreateDeckShip_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog2 = new(); + if (dialog2.ShowDialog() == DialogResult.OK) + { + dopColor = dialog2.Color; + } _drawingContainerShip = new DrawningContainerShip(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), - pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + color, dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), + pictureBoxContainerShip.Width, pictureBoxContainerShip.Height); _drawingContainerShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -85,16 +103,16 @@ namespace ProjectWarmlyShip { case "buttonUp": _drawingContainerShip.MoveTransport(DirectionType.Up); - break; + break; case "buttonDown": _drawingContainerShip.MoveTransport(DirectionType.Down); - break; + break; case "buttonLeft": _drawingContainerShip.MoveTransport(DirectionType.Left); - break; + break; case "buttonRight": _drawingContainerShip.MoveTransport(DirectionType.Right); - break; + break; } Draw(); } @@ -116,13 +134,13 @@ namespace ProjectWarmlyShip { 0 => new MoveToCenter(), 1 => new MoveToBorder(), - + _ => null, }; if (_abstractStrategy == null) { return; } - _abstractStrategy.SetData(new DrawningObjectShip(_drawingContainerShip), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + _abstractStrategy.SetData(new DrawningObjectShip(_drawingContainerShip), pictureBoxContainerShip.Width, pictureBoxContainerShip.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) @@ -137,5 +155,13 @@ namespace ProjectWarmlyShip _abstractStrategy = null; } } + private void buttonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _drawingContainerShip; + DialogResult = DialogResult.OK; + } + private void FormContainerShip_Load(object sender, EventArgs e) + { + } } } diff --git a/ContainerShip/ContainerShip/FormContainerShip.resx b/ContainerShip/ContainerShip/FormContainerShip.resx index 729e68d..0a48d62 100644 --- a/ContainerShip/ContainerShip/FormContainerShip.resx +++ b/ContainerShip/ContainerShip/FormContainerShip.resx @@ -123,7 +123,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvgAADr4B6kKxwAAAAipJREFUeF7t3NlS20AQRmEJGzAgL+ybIQTy/s8YOfyawtJtZIWT891QVleB + AAAOvAAADrwBlbxySQAAAipJREFUeF7t3NlS20AQRmEJGzAgL+ybIQTy/s8YOfyawtJtZIWT891QVleB u6iWZnpmVEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmS9F3N8hOqWdb13WqeT0BP9aebfMa5TYJ1fZ4rNKvk 17rOJZjjpLdzirzjnCa7P+6IxXiU5AJYjN2ttLPKdZAfSa3zwCvGs6TWeV8nwPGS1IqTBDjuk1mxSYDj du+Z0XrADVMX/WJ8axLheExqxVMCHJfJrHhOgKPZJrXOcpEIxqKdCu/Z8orxOakVlwlw9Eep9WMCHM1b @@ -140,7 +140,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvgAADr4B6kKxwAAAA01JREFUeF7t3dluo0AURVHwEGdO7DjznPT/f2PHcAgULsD90FKd0l5P7jKR + AAAOvAAADrwBlbxySQAAA01JREFUeF7t3dluo0AURVHwEGdO7DjznPT/f2PHcAgULsD90FKd0l5P7jKR 2FF0jYuWXQAAAAAAAAAAAAAAAADA/7JZP643epyls3LnTP/K0GUVWJaX+nd2vhVYlt9aycyV8nautJaV xZvqdt4WWs3JUnG1pVYz0kyZRnbTpp0yjcymzVZZXVs9l4W5okJzPZuDcMo0Mpo2/SnTyGbanCpo36mO MHevnJh7HWMtPmUaOUyblVriVjrK2I1ShtzoOFuvChn2qiNNjU2ZhvW02ShinPHGzfGLGirXF3pQlhfX @@ -162,7 +162,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvgAADr4B6kKxwAAAA2JJREFUeF7t3dtyolAQheHgIZqTicacMznO+z/jBFgoLRtwLqame9f/XUnj + AAAOvAAADrwBlbxySQAAA2JJREFUeF7t3dtyolAQheHgIZqTicacMznO+z/jBFgoLRtwLqame9f/XUnj xV6W1UBTBScAAAAAAAAAAAAAAAAA8K/MtqfbqT5n6fytKIq3c21l6OInX+lC29m5VMCiuFYlNzfKVxQr VXIzUb6imKiSGxLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgf CeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhK6d7ea @@ -185,7 +185,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvgAADr4B6kKxwAAAAkFJREFUeF7t3GtT2zAQhWErF5JAnIQQSrmFW///b+wCR6LY/Vg3nqP3+cJE + AAAOvAAADrwBlbxySQAAAkFJREFUeF7t3GtT2zAQhWErF5JAnIQQSrmFW///b+wCR6LY/Vg3nqP3+cJE O5Nhh1kjrSQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAACMyVQ/Tc02P1J6WuuToUP6dKfPds6VYErXGjFz pfTCRkNWpgtl926uQSeHeMR8WWjUyFcJfpho2MdGmWVuD9PpTyWWPSngYvuqxLKVAi7OlFfxqICLnfIq bhQwseyW4MJsOrP+pcSy1VIRE3fKq7hVwMW98iouFTCxbJVX9mC2Llw/KLFsYlaCl8qruFfAxa3yKsxm diff --git a/ContainerShip/ContainerShip/FormShipCollection.Designer.cs b/ContainerShip/ContainerShip/FormShipCollection.Designer.cs new file mode 100644 index 0000000..1e8dcd5 --- /dev/null +++ b/ContainerShip/ContainerShip/FormShipCollection.Designer.cs @@ -0,0 +1,121 @@ +namespace ProjectContainerShip +{ + partial class FormShipCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + pictureBoxCollection = new PictureBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonAddShip = new Button(); + buttonRemoveShip = new Button(); + buttonRefreshCollection = new Button(); + groupBox1 = new GroupBox(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(932, 503); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(6, 120); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(108, 27); + maskedTextBoxNumber.TabIndex = 1; + // + // buttonAddShip + // + buttonAddShip.Location = new Point(6, 57); + buttonAddShip.Name = "buttonAddShip"; + buttonAddShip.Size = new Size(108, 30); + buttonAddShip.TabIndex = 2; + buttonAddShip.Text = "Добавить "; + buttonAddShip.UseVisualStyleBackColor = true; + buttonAddShip.Click += ButtonAddShip_Click; + // + // buttonRemoveShip + // + buttonRemoveShip.Location = new Point(6, 166); + buttonRemoveShip.Name = "buttonRemoveShip"; + buttonRemoveShip.Size = new Size(108, 30); + buttonRemoveShip.TabIndex = 3; + buttonRemoveShip.Text = "Удалить "; + buttonRemoveShip.UseVisualStyleBackColor = true; + buttonRemoveShip.Click += ButtonRemoveShip_Click; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(6, 230); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(108, 30); + buttonRefreshCollection.TabIndex = 4; + buttonRefreshCollection.Text = "Обновить "; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // groupBox1 + // + groupBox1.Controls.Add(buttonAddShip); + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonRemoveShip); + groupBox1.Location = new Point(812, 21); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(120, 461); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // FormShipCollection + // + ClientSize = new Size(932, 503); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormShipCollection"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonAddShip; + private Button buttonRemoveShip; + private Button buttonRefreshCollection; + private GroupBox groupBox1; + } +} \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShipCollection.cs b/ContainerShip/ContainerShip/FormShipCollection.cs new file mode 100644 index 0000000..744252c --- /dev/null +++ b/ContainerShip/ContainerShip/FormShipCollection.cs @@ -0,0 +1,82 @@ +using ProjectContainerShip.DrawningObjects; +using ProjectContainerShip.Generics; +using ProjectContainerShip.MovementStrategy; + +namespace ProjectContainerShip +{ + /// + /// Форма для работы с набором объектов класса + /// + public partial class FormShipCollection : Form + { + /// + /// Набор объектов + /// + private readonly ContainerGenericCollection _containerShip; + /// + /// Конструктор + /// + public FormShipCollection() + { + InitializeComponent(); + _containerShip = new ContainerGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddShip_Click(object sender, EventArgs e) + { + FormContainerShip form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_containerShip + form.SelectedShip > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _containerShip.ShowContainer(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveShip_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_containerShip - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _containerShip.ShowContainer(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _containerShip.ShowContainer(); + } + } +} + + diff --git a/ContainerShip/ContainerShip/FormShipCollection.resx b/ContainerShip/ContainerShip/FormShipCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ContainerShip/ContainerShip/FormShipCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ContainerShip/ContainerShip/MoveToBorder.cs b/ContainerShip/ContainerShip/MoveToBorder.cs index ab74480..b41aa70 100644 --- a/ContainerShip/ContainerShip/MoveToBorder.cs +++ b/ContainerShip/ContainerShip/MoveToBorder.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ProjectContainerShip.MovementStrategy { /// - /// Стратегия перемещения объекта к краю + /// Стратегия перемещения объекта в центр экрана /// public class MoveToBorder : AbstractStrategy { diff --git a/ContainerShip/ContainerShip/Program.cs b/ContainerShip/ContainerShip/Program.cs index c85764e..a57705c 100644 --- a/ContainerShip/ContainerShip/Program.cs +++ b/ContainerShip/ContainerShip/Program.cs @@ -1,4 +1,4 @@ -using ProjectWarmlyShip; +using ProjectContainerShip; namespace ProjectContainerShip { @@ -13,8 +13,11 @@ namespace ProjectContainerShip // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormContainerShip()); + Application.Run(new FormShipCollection()); + } } } + + diff --git a/ContainerShip/ContainerShip/SetGeneric.cs b/ContainerShip/ContainerShip/SetGeneric.cs new file mode 100644 index 0000000..9b636f0 --- /dev/null +++ b/ContainerShip/ContainerShip/SetGeneric.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectContainerShip.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый корабль + /// + public int Insert(T ship) + { + return Insert(ship, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый корабля + /// Позиция + /// + public int Insert(T ship, int position) + { + if (position < 0 && position > Count) + { + return -1; + } + if (_places[position] != null) + { + int d = 0; + for (int j = 1; j < Count - position; j++) + { + if (_places[position + j] == null) + { + d = position + j; + break; + } + } + if (d == 0) + { + return -1; + } + for (int j = d; j > position; j--) + { + _places[j] = _places[j - 1]; + } + } + _places[position] = ship; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + 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]; + } + } +} + + + -- 2.25.1