From ef90e5fc09d9df24b8e9009649304da42da6dd1f Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:02:13 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=9A=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ICollectionGenericObjects.cs | 45 +++++++++++++++++ .../MassiveGenericObjects.cs | 50 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..5a896aa --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.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/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..a843517 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.CollectionGenericObjects; + +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + /// + /// Массив объектов, которые храним + /// + private T?[] _collection; + public int Count => _collection.Length; + public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + public T? Get(int position) + { + // TODO проверка позиции + return _collection[position]; + } + public bool Insert(T obj) + { + // TODO вставка в свободное место набора + return false; + } + public bool Insert(T obj, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // ищется свободное место после этой позиции и идет вставка туда + // если нет после, ищем до + // TODO вставка + return false; + } + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива значение null + return true; + } +} \ No newline at end of file -- 2.25.1 From 82f06ed48ba25c05ca8296ebb98626bb1004ff3d Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Sun, 3 Mar 2024 14:13:48 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 99 ++++++++++ .../ICollectionGenericObjects.cs | 8 +- .../MassiveGenericObjects.cs | 48 ++++- .../ShipPortService.cs | 58 ++++++ .../FormShipCollection.Designer.cs | 173 ++++++++++++++++++ .../ProjectWarmlyShip/FormShipCollection.cs | 132 +++++++++++++ .../ProjectWarmlyShip/FormShipCollection.resx | 120 ++++++++++++ .../FormWarmlyShip.Designer.cs | 34 ---- .../ProjectWarmlyShip/FormWarmlyShip.cs | 55 ++---- .../ProjectWarmlyShip/Program.cs | 2 +- 10 files changed, 638 insertions(+), 91 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..af3b83b --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,99 @@ +using ProjectWarmlyShip.Drawnings; + +namespace ProjectWarmlyShip.CollectionGenericObjects; + +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 150; + /// + /// Ширина окна + /// + protected readonly int _pictureWidth; + /// + /// Высота окна + /// + protected readonly int _pictureHeight; + /// + /// Коллекция судов + /// + protected ICollectionGenericObjects? _collection = null; + /// + /// Вычисление максимального количества элементов, который можно разместить в окне + /// + private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + /// + /// Конструктор + /// + /// Ширина окна + /// Высота окна + /// Коллекция автомобилей + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static bool operator +(AbstractCompany company, DrawningShip ship) + { + return company._collection?.Insert(ship) ?? false; + } + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static bool operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position) ?? false; + } + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningShip? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + /// + /// Вывод всей коллекции + /// + /// + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackgound(graphics); + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningShip? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + return bitmap; + } + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs index 5a896aa..e36ab6e 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectWarmlyShip.CollectionGenericObjects; +namespace ProjectWarmlyShip.CollectionGenericObjects; public interface ICollectionGenericObjects where T : class diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs index a843517..3106b42 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectWarmlyShip.CollectionGenericObjects; +namespace ProjectWarmlyShip.CollectionGenericObjects; public class MassiveGenericObjects : ICollectionGenericObjects where T : class @@ -25,11 +19,22 @@ public class MassiveGenericObjects : ICollectionGenericObjects public T? Get(int position) { // TODO проверка позиции + if (position >= _collection.Length || position < 0) return null; return _collection[position]; } public bool Insert(T obj) { // TODO вставка в свободное место набора + int index = 0; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return true; + } + ++index; + } return false; } public bool Insert(T obj, int position) @@ -39,12 +44,41 @@ public class MassiveGenericObjects : ICollectionGenericObjects // ищется свободное место после этой позиции и идет вставка туда // если нет после, ищем до // TODO вставка + if (position >= _collection.Length || position < 0) + return false; + if (_collection[position] == null) { + _collection[position] = obj; + return true; + } + int index = position + 1; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return true; + } + ++index; + } + index = position - 1; + while (index >= 0) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return true; + } + --index; + } return false; } public bool Remove(int position) { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null + if (position >= _collection.Length || position < 0) + return false; + _collection[position] = null; return true; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs new file mode 100644 index 0000000..0531e8b --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs @@ -0,0 +1,58 @@ +using ProjectWarmlyShip.Drawnings; +using System.Drawing; + +namespace ProjectWarmlyShip.CollectionGenericObjects; + +public class ShipPortService : AbstractCompany +{ + public ShipPortService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + //рисуем пристань + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + Pen pen = new(Color.Black, 3); + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, + i * _placeSizeWidth + _placeSizeWidth - 5, j * _placeSizeHeight); + } + } + } + + protected override void SetObjectsPosition() + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = 0; + + for (int i = 0; i < (_collection?.Count ?? 0); i++) + { + if (_collection.Get(i) != null) + { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 20, curHeight * _placeSizeHeight + 4); + } + + if (curWidth > 0) + curWidth--; + else + { + curWidth = width - 1; + curHeight++; + } + if (curHeight > height) + { + return; + } + } + } +} + diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs new file mode 100644 index 0000000..8701e97 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectWarmlyShip +{ + 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() + { + groupBoxTools = new GroupBox(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonRemoveShip = new Button(); + maskedTextBox = new MaskedTextBox(); + buttonAddWarmlyShip = new Button(); + buttonAddShip = new Button(); + comboBoxSelectorCompany = new ComboBox(); + pictureBox = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonRemoveShip); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddWarmlyShip); + groupBoxTools.Controls.Add(buttonAddShip); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(887, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(146, 596); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 544); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(128, 40); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(6, 324); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(128, 40); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonRemoveShip + // + buttonRemoveShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveShip.Location = new Point(6, 250); + buttonRemoveShip.Name = "buttonRemoveShip"; + buttonRemoveShip.Size = new Size(128, 40); + buttonRemoveShip.TabIndex = 4; + buttonRemoveShip.Text = "Удаление"; + buttonRemoveShip.UseVisualStyleBackColor = true; + buttonRemoveShip.Click += buttonRemoveShip_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(6, 221); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(128, 23); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddWarmlyShip + // + buttonAddWarmlyShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddWarmlyShip.Location = new Point(6, 131); + buttonAddWarmlyShip.Name = "buttonAddWarmlyShip"; + buttonAddWarmlyShip.Size = new Size(128, 40); + buttonAddWarmlyShip.TabIndex = 2; + buttonAddWarmlyShip.Text = "Добавление теплохода"; + buttonAddWarmlyShip.UseVisualStyleBackColor = true; + buttonAddWarmlyShip.Click += buttonAddWarmlyShip_Click; + // + // buttonAddShip + // + buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddShip.Location = new Point(6, 87); + buttonAddShip.Name = "buttonAddShip"; + buttonAddShip.Size = new Size(128, 38); + buttonAddShip.TabIndex = 1; + buttonAddShip.Text = "Добавление судна"; + buttonAddShip.UseVisualStyleBackColor = true; + buttonAddShip.Click += buttonAddShip_Click; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 22); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(134, 23); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += comboBoxSelectorCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(887, 596); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormShipCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1033, 596); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormShipCollection"; + Text = "Коллекция судов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddWarmlyShip; + private Button buttonAddShip; + private Button buttonGoToCheck; + private Button buttonRemoveShip; + private MaskedTextBox maskedTextBox; + private PictureBox pictureBox; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs new file mode 100644 index 0000000..404b0f4 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -0,0 +1,132 @@ +using ProjectWarmlyShip.CollectionGenericObjects; +using ProjectWarmlyShip.Drawnings; + +namespace ProjectWarmlyShip; + +public partial class FormShipCollection : Form +{ + private AbstractCompany? _company = null; + public FormShipCollection() + { + InitializeComponent(); + } + private void comboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new ShipPortService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + Random random = new(); + DrawningShip drawningShip; + switch (type) + { + case nameof(DrawningShip): + drawningShip = new DrawningShip(random.Next(100, 300), + random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningWarmlyShip): + // TODO вызов диалогового окна для выбора цвета (made) + drawningShip = new DrawningWarmlyShip(random.Next(100, 300), random.Next(1000, 3000), + GetColor(random), GetColor(random), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + if (_company + drawningShip) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + private static Color GetColor(Random random) + { + 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; + } + return color; + } + + private void buttonAddShip_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningShip)); + } + + private void buttonAddWarmlyShip_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningWarmlyShip)); + } + + private void buttonRemoveShip_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBox.Text); + if (_company - pos) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + private void buttonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + DrawningShip? ship = null; + int counter = 100; + while (ship == null) + { + ship = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (ship == null) + { + return; + } + FormWarmlyShip form = new() + { + SetShip = ship + }; + form.ShowDialog(); + } + private void buttonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + pictureBox.Image = _company.Show(); + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/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/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs index 2d76590..f1c08cb 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs @@ -29,12 +29,10 @@ private void InitializeComponent() { pictureBoxWarmlyShip = new PictureBox(); - buttonCreate = new Button(); buttonUp = new Button(); buttonRight = new Button(); buttonLeft = new Button(); buttonDown = new Button(); - buttonСreateShip = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); @@ -50,20 +48,6 @@ pictureBoxWarmlyShip.TabIndex = 0; pictureBoxWarmlyShip.TabStop = false; // - // buttonCreate - // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.BackColor = SystemColors.ButtonHighlight; - buttonCreate.Cursor = Cursors.IBeam; - buttonCreate.ForeColor = SystemColors.ControlText; - buttonCreate.Location = new Point(12, 331); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(118, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать теплоход"; - buttonCreate.UseVisualStyleBackColor = false; - buttonCreate.Click += ButtonCreate_Click; - // // buttonUp // buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -112,20 +96,6 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // - // buttonСreateShip - // - buttonСreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonСreateShip.BackColor = SystemColors.ButtonHighlight; - buttonСreateShip.Cursor = Cursors.IBeam; - buttonСreateShip.ForeColor = SystemColors.ControlText; - buttonСreateShip.Location = new Point(146, 331); - buttonСreateShip.Name = "buttonСreateShip"; - buttonСreateShip.Size = new Size(118, 23); - buttonСreateShip.TabIndex = 6; - buttonСreateShip.Text = "Создать судно"; - buttonСreateShip.UseVisualStyleBackColor = false; - buttonСreateShip.Click += buttonСreateShip_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -153,12 +123,10 @@ ClientSize = new Size(830, 366); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonСreateShip); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonRight); Controls.Add(buttonUp); - Controls.Add(buttonCreate); Controls.Add(pictureBoxWarmlyShip); Name = "FormWarmlyShip"; Text = "WarmlyShip"; @@ -170,12 +138,10 @@ #endregion private PictureBox pictureBoxWarmlyShip; - private Button buttonCreate; private Button buttonUp; private Button buttonRight; private Button buttonLeft; private Button buttonDown; - private Button buttonСreateShip; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs index 65c0832..2e0b825 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs @@ -7,6 +7,19 @@ namespace ProjectWarmlyShip { private DrawningShip? _drawningShip; private AbstractStrategy? _strategy; + /// + /// Получение объекта + /// + public DrawningShip SetShip + { + set + { + _drawningShip = value; + _drawningShip.SetPictureSize(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + comboBoxStrategy.Enabled = true; + Draw(); + } + } public FormWarmlyShip() { InitializeComponent(); @@ -23,48 +36,6 @@ namespace ProjectWarmlyShip _drawningShip.DrawTransport(gr); pictureBoxWarmlyShip.Image = bmp; } - private void CreateObject(string type) - { - Random random = new Random(); - switch (type) - { - case nameof(DrawningShip): - _drawningShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); - break; - case nameof(DrawningWarmlyShip): - _drawningShip = new DrawningWarmlyShip(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))); - break; - default: - return; - } - _drawningShip.SetPictureSize(pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); - _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - /// - /// Кнопка создания теплохода - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawningWarmlyShip)); - } - /// - /// Кнопка создания судна - /// - /// - /// - private void buttonСreateShip_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawningShip)); - } private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningShip == null) diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs b/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs index 4b1fb4f..2c7e667 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/Program.cs @@ -11,7 +11,7 @@ namespace ProjectWarmlyShip // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormWarmlyShip()); + Application.Run(new FormShipCollection()); } } } \ No newline at end of file -- 2.25.1 From 0c3397fcee860d2ea8a8867b9e3c183fc5b5d6f9 Mon Sep 17 00:00:00 2001 From: "Dmitriev.An" Date: Mon, 4 Mar 2024 10:06:30 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20#3=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 10 ++++---- .../ICollectionGenericObjects.cs | 6 ++--- .../MassiveGenericObjects.cs | 25 ++++++++++--------- .../ShipPortService.cs | 4 +-- .../ProjectWarmlyShip/FormShipCollection.cs | 4 +-- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs index af3b83b..c7c30c6 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs @@ -23,7 +23,7 @@ public abstract class AbstractCompany /// /// Коллекция судов /// - protected ICollectionGenericObjects? _collection = null; + public ICollectionGenericObjects? _collection = null; /// /// Вычисление максимального количества элементов, который можно разместить в окне /// @@ -47,9 +47,9 @@ public abstract class AbstractCompany /// Компания /// Добавляемый объект /// - public static bool operator +(AbstractCompany company, DrawningShip ship) + public static int operator +(AbstractCompany company, DrawningShip ship) { - return company._collection?.Insert(ship) ?? false; + return company._collection.Insert(ship); } /// /// Перегрузка оператора удаления для класса @@ -57,9 +57,9 @@ public abstract class AbstractCompany /// Компания /// Номер удаляемого объекта /// - public static bool operator -(AbstractCompany company, int position) + public static DrawningShip operator -(AbstractCompany company, int position) { - return company._collection?.Remove(position) ?? false; + return company._collection?.Remove(position); } /// /// Получение случайного объекта из коллекции diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs index e36ab6e..bca9d15 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -16,20 +16,20 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка неудалась - bool Insert(T obj); + int Insert(T obj); /// /// Добавление объекта в коллекцию на конкретную позицию /// /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка неудалась - bool Insert(T obj, int position); + int Insert(T obj, int position); /// /// Удаление объекта из коллекции с конкретной позиции /// /// Позиция /// true - удаление прошло удачно, false - удаление неудалось - bool Remove(int position); + T Remove(int position); /// /// Получение объекта по позиции /// diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs index 3106b42..333e7a1 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/MassiveGenericObjects.cs @@ -22,7 +22,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (position >= _collection.Length || position < 0) return null; return _collection[position]; } - public bool Insert(T obj) + public int Insert(T obj) { // TODO вставка в свободное место набора int index = 0; @@ -31,13 +31,13 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[index] = obj; - return true; + return index; } ++index; } - return false; + return -1; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то @@ -45,10 +45,10 @@ public class MassiveGenericObjects : ICollectionGenericObjects // если нет после, ищем до // TODO вставка if (position >= _collection.Length || position < 0) - return false; + return -1; if (_collection[position] == null) { _collection[position] = obj; - return true; + return position; } int index = position + 1; while (index < _collection.Length) @@ -56,7 +56,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[index] = obj; - return true; + return index; } ++index; } @@ -66,19 +66,20 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[index] = obj; - return true; + return index; } --index; } - return false; + return -1; } - public bool Remove(int position) + public T Remove(int position) { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null if (position >= _collection.Length || position < 0) - return false; + return null; + T obj = _collection[position]; _collection[position] = null; - return true; + return obj; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs index 0531e8b..97f46c0 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs @@ -19,8 +19,7 @@ public class ShipPortService : AbstractCompany { for (int j = 0; j < height + 1; ++j) { - g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, - i * _placeSizeWidth + _placeSizeWidth - 5, j * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth - 5, j * _placeSizeHeight); } } } @@ -40,7 +39,6 @@ public class ShipPortService : AbstractCompany _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 20, curHeight * _placeSizeHeight + 4); } - if (curWidth > 0) curWidth--; else diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index 404b0f4..e0ad221 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -42,7 +42,7 @@ public partial class FormShipCollection : Form default: return; } - if (_company + drawningShip) + if (_company + drawningShip != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); @@ -84,7 +84,7 @@ public partial class FormShipCollection : Form return; } int pos = Convert.ToInt32(maskedTextBox.Text); - if (_company - pos) + if (_company - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _company.Show(); -- 2.25.1 From 1460d8e2a0c1061cb3815910890116ae663418c6 Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Fri, 8 Mar 2024 15:05:41 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CollectionGenericObjects/AbstractCompany.cs | 2 +- .../CollectionGenericObjects/ShipPortService.cs | 2 -- ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs index c7c30c6..013d334 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/AbstractCompany.cs @@ -23,7 +23,7 @@ public abstract class AbstractCompany /// /// Коллекция судов /// - public ICollectionGenericObjects? _collection = null; + protected ICollectionGenericObjects? _collection = null; /// /// Вычисление максимального количества элементов, который можно разместить в окне /// diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs index 97f46c0..8180406 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/CollectionGenericObjects/ShipPortService.cs @@ -8,7 +8,6 @@ public class ShipPortService : AbstractCompany public ShipPortService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) { } - protected override void DrawBackgound(Graphics g) { //рисуем пристань @@ -23,7 +22,6 @@ public class ShipPortService : AbstractCompany } } } - protected override void SetObjectsPosition() { int width = _pictureWidth / _placeSizeWidth; diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index e0ad221..e61d74c 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -67,12 +67,10 @@ public partial class FormShipCollection : Form { CreateObject(nameof(DrawningShip)); } - private void buttonAddWarmlyShip_Click(object sender, EventArgs e) { CreateObject(nameof(DrawningWarmlyShip)); } - private void buttonRemoveShip_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) -- 2.25.1