From da9d37f184023e5c4b9aa275d4906f60abf84d0e Mon Sep 17 00:00:00 2001 From: Aidar Date: Fri, 19 Apr 2024 22:59:06 +0400 Subject: [PATCH] =?UTF-8?q?=20=D0=9B=D0=B0=D0=B1=D0=BE=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 122 ++++++++++++ .../ICollectionGenericObjects.cs | 48 +++++ .../MassiveGenericObjects.cs | 101 ++++++++++ .../ShipSharingService.cs | 66 +++++++ .../ProjectLiner/FormLiner.Designer.cs | 46 +---- ProjectLiner/ProjectLiner/FormLiner.cs | 64 ++----- .../FormShipCollection.Designer.cs | 175 +++++++++++++++++ .../ProjectLiner/FormShipCollection.cs | 179 ++++++++++++++++++ .../ProjectLiner/FormShipCollection.resx | 123 ++++++++++++ ProjectLiner/ProjectLiner/Program.cs | 2 +- 10 files changed, 841 insertions(+), 85 deletions(-) create mode 100644 ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs create mode 100644 ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs create mode 100644 ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs create mode 100644 ProjectLiner/ProjectLiner/FormShipCollection.cs create mode 100644 ProjectLiner/ProjectLiner/FormShipCollection.resx diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..9dbf734 --- /dev/null +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,122 @@ +using ProjectLiner.Drawnings; + +namespace ProjectLiner.CollectionGenericObjects; + +/// +/// Абстракция компании, хранящий коллекцию кораблей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 159; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 73; + + /// + /// Ширина окна + /// + protected readonly int _pictureWidth; + + /// + /// Высота окна + /// + protected readonly int _pictureHeight; + + /// + /// Коллекция кораблей + /// + protected ICollectionGenericObjects? _collection = null; + + /// + /// Вычисление максимального количества элементов, который можно разместить в окне + /// + private int GetMaxCount =>(_pictureWidth / _placeSizeWidth) * (_pictureHeight / _placeSizeHeight); + + /// + /// Конструктор + /// + /// Ширина окна + /// Высота окна + /// Коллекция автомобилей + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static int operator +(AbstractCompany company, DrawningShip ship) + { + if (company._collection == null) + { + return -1; + } + return company._collection.Insert(ship); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningShip operator -(AbstractCompany company, int position) + { + if (company._collection == null) + { + return null; + } + return company._collection.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + 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(); +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..01a29a4 --- /dev/null +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,48 @@ +namespace ProjectLiner.CollectionGenericObjects; + +/// +/// Интерфейс описания действий для набора хранимых объектов +/// +/// Параметр: ограничение - ссылочный тип +public interface ICollectionGenericObjects + where T : class +{ + /// + /// Количество объектов в коллекции + /// + int Count { get; } + + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj); + + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj, int position); + + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// true - удаление прошло удачно, false - удаление не удалось + T? Remove(int position); + + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..f7d2cf5 --- /dev/null +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,101 @@ + +namespace ProjectLiner.CollectionGenericObjects; + +/// +/// Параметризованный набор объектов +/// +/// Параметр: ограничение - ссылочный тип +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + /// + /// Массив объектов, которые храним + /// + private T?[] _collection; + + public int Count => _collection.Length; + + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } + + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position >= 0 && position < Count) return _collection[position]; + else return null; + } + + public int Insert(T obj) + { + return Insert(obj, 0); + } + + public int Insert(T obj, int position) + { + if (position < 0 || position >= Count) + { + return -1; + } + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + + for (int i = position; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + for (int i = position - 1; i >= 0; --i) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + return -1; + } + + public T? Remove(int position) + { + if (position < 0 || position >= _collection.Count()) + { + return null; + } + + if (_collection[position] != null) + { + T obj = _collection[position]; + _collection[position] = null; + return obj; + } + return null; + } +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs new file mode 100644 index 0000000..2efdeea --- /dev/null +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ShipSharingService.cs @@ -0,0 +1,66 @@ +using ProjectLiner.Drawnings; + +namespace ProjectLiner.CollectionGenericObjects; + +/// +/// Реализация абстрактной компании - шипшеринг +/// +public class ShipSharingService : AbstractCompany +{ + private List> locCoord = new List>(); + private int countInRow; + private int countOfRows; + + /// + /// Конструктор + /// + /// + /// + /// + public ShipSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : + base(picWidth, picHeight, collection) {} + + protected override void DrawBackgound(Graphics g) + { + Pen pen = new Pen(Color.Brown); + int x = 0, y = 0; + while (y + _placeSizeHeight <= _pictureHeight) + { + int count = 0; + while (x + _placeSizeWidth <= _pictureWidth) + { + count++; + g.DrawLine(pen, x, y, x + _placeSizeWidth, y); + g.DrawLine(pen, x, y, x, y + _placeSizeHeight); + g.DrawLine(pen, x, y + _placeSizeHeight, x + _placeSizeWidth, y + _placeSizeHeight); + g.DrawLine(pen, x + _placeSizeWidth, y + _placeSizeHeight, x + _placeSizeWidth, y); + locCoord.Add(new Tuple(x, y)); + x += _placeSizeWidth + 2; + } + countInRow = count; + x = 1; + y += _placeSizeHeight + 5; + countOfRows++; + } + } + + protected override void SetObjectsPosition() + { + if (locCoord == null || _collection == null) + { + return; + } + int row = 1, col = 1; + for (int i = 0; i < _collection?.Count; i++, col++) + { + _collection?.Get(i)?.SetPictureSize(_pictureWidth, _pictureHeight); + _collection?.Get(i)?.SetPosition(locCoord[row * countInRow - col].Item1 + 5, locCoord[row * countInRow - col].Item2 + 5); + if (col == countInRow) + { + col = 0; + row++ + ; + } + } + } +} diff --git a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs index a5b99c9..87e6b15 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs @@ -29,12 +29,10 @@ private void InitializeComponent() { pictureBoxLiner = new PictureBox(); - buttonCreateShip = new Button(); buttonRight = new Button(); buttonDown = new Button(); buttonLeft = new Button(); buttonUp = new Button(); - buttonCreateLiner = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit(); @@ -45,27 +43,16 @@ pictureBoxLiner.Dock = DockStyle.Fill; pictureBoxLiner.Location = new Point(0, 0); pictureBoxLiner.Name = "pictureBoxLiner"; - pictureBoxLiner.Size = new Size(884, 461); + pictureBoxLiner.Size = new Size(1184, 661); pictureBoxLiner.TabIndex = 0; pictureBoxLiner.TabStop = false; // - // buttonCreateShip - // - buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateShip.Location = new Point(12, 426); - buttonCreateShip.Name = "buttonCreateShip"; - buttonCreateShip.Size = new Size(154, 23); - buttonCreateShip.TabIndex = 1; - buttonCreateShip.Text = "Создать корабль"; - buttonCreateShip.UseVisualStyleBackColor = true; - buttonCreateShip.Click += ButtonCreateShip_Click; - // // buttonRight // buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.buttonRight; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(842, 419); + buttonRight.Location = new Point(1142, 619); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 2; @@ -77,7 +64,7 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.buttonDown; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(806, 419); + buttonDown.Location = new Point(1106, 619); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 3; @@ -89,7 +76,7 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.buttonLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(770, 419); + buttonLeft.Location = new Point(1070, 619); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 4; @@ -101,37 +88,28 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.buttonUp; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(806, 383); + buttonUp.Location = new Point(1106, 583); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 5; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; // - // buttonCreateLiner - // - buttonCreateLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateLiner.Location = new Point(172, 426); - buttonCreateLiner.Name = "buttonCreateLiner"; - buttonCreateLiner.Size = new Size(154, 23); - buttonCreateLiner.TabIndex = 6; - buttonCreateLiner.Text = "Создать лайнер"; - buttonCreateLiner.UseVisualStyleBackColor = true; - buttonCreateLiner.Click += ButtonCreateLiner_Click; - // // comboBoxStrategy // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); - comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Location = new Point(1051, 12); comboBoxStrategy.Name = "comboBoxStrategy"; comboBoxStrategy.Size = new Size(121, 23); comboBoxStrategy.TabIndex = 7; // // buttonStrategyStep // - buttonStrategyStep.Location = new Point(797, 41); + buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStrategyStep.Location = new Point(1097, 41); buttonStrategyStep.Name = "buttonStrategyStep"; buttonStrategyStep.Size = new Size(75, 23); buttonStrategyStep.TabIndex = 8; @@ -143,15 +121,13 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(884, 461); + ClientSize = new Size(1184, 661); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateLiner); Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(buttonDown); Controls.Add(buttonRight); - Controls.Add(buttonCreateShip); Controls.Add(pictureBoxLiner); Name = "FormLiner"; StartPosition = FormStartPosition.CenterScreen; @@ -163,12 +139,10 @@ #endregion private PictureBox pictureBoxLiner; - private Button buttonCreateShip; private Button buttonRight; private Button buttonDown; private Button buttonLeft; private Button buttonUp; - private Button buttonCreateLiner; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectLiner/ProjectLiner/FormLiner.cs b/ProjectLiner/ProjectLiner/FormLiner.cs index 99c5601..58f6ec8 100644 --- a/ProjectLiner/ProjectLiner/FormLiner.cs +++ b/ProjectLiner/ProjectLiner/FormLiner.cs @@ -18,6 +18,21 @@ public partial class FormLiner : Form /// private AbstractStrategy? _strategy; + /// + /// Получение объекта + /// + public DrawningShip SetShip + { + set + { + _drawningShip = value; + _drawningShip.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + /// /// Конструктор формы /// @@ -36,59 +51,12 @@ public partial class FormLiner : Form { return; } - Bitmap bmp = new(pictureBoxLiner.Width, - pictureBoxLiner.Height); + Bitmap bmp = new(pictureBoxLiner.Width, pictureBoxLiner.Height); Graphics gr = Graphics.FromImage(bmp); _drawningShip.DrawTransport(gr); pictureBoxLiner.Image = bmp; } - /// - /// Создание объекта класса-перемещения - /// - /// Тип создаваемого объекта - private void CreateObject(string type) - { - Random random = new(); - 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(DrawningLiner): - _drawningShip = new DrawningLiner( - 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(pictureBoxLiner.Width, pictureBoxLiner.Height); - _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - - /// - /// Обработка нажатия кнопки "Создать лайнер" - /// - /// - /// - private void ButtonCreateLiner_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLiner)); - - /// - /// Обработка нажатия кнопки "Создать корабль" - /// - /// - /// - private void ButtonCreateShip_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningShip)); - /// /// Обработка нажатия кнопок навигации движения /// diff --git a/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs b/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs new file mode 100644 index 0000000..05c1501 --- /dev/null +++ b/ProjectLiner/ProjectLiner/FormShipCollection.Designer.cs @@ -0,0 +1,175 @@ +namespace ProjectLiner +{ + 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(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddLiner = new Button(); + buttonAddShip = new Button(); + ComboBoxSelectorCompany = new ComboBox(); + pictureBox = new PictureBox(); + colorDialog1 = new ColorDialog(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonRemoveShip); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddLiner); + groupBoxTools.Controls.Add(buttonAddShip); + groupBoxTools.Controls.Add(ComboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(1006, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(178, 561); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(0, 302); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(178, 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(0, 256); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(178, 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(0, 191); + buttonRemoveShip.Name = "buttonRemoveShip"; + buttonRemoveShip.Size = new Size(178, 40); + buttonRemoveShip.TabIndex = 4; + buttonRemoveShip.Text = "Удаление корабля"; + buttonRemoveShip.UseVisualStyleBackColor = true; + buttonRemoveShip.Click += ButtonRemoveShip_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(0, 162); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(178, 23); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddLiner + // + buttonAddLiner.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddLiner.Location = new Point(0, 97); + buttonAddLiner.Name = "buttonAddLiner"; + buttonAddLiner.Size = new Size(178, 40); + buttonAddLiner.TabIndex = 2; + buttonAddLiner.Text = "Добавление лайнера"; + buttonAddLiner.UseVisualStyleBackColor = true; + buttonAddLiner.Click += buttonAddLiner_Click; + // + // buttonAddShip + // + buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddShip.Location = new Point(0, 51); + buttonAddShip.Name = "buttonAddShip"; + buttonAddShip.Size = new Size(178, 40); + 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(0, 22); + ComboBoxSelectorCompany.Name = "ComboBoxSelectorCompany"; + ComboBoxSelectorCompany.Size = new Size(178, 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(1006, 561); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormShipCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1184, 561); + 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 buttonAddLiner; + private Button buttonAddShip; + private Button buttonRefresh; + private Button buttonGoToCheck; + private Button buttonRemoveShip; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + private ColorDialog colorDialog1; + } +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormShipCollection.cs b/ProjectLiner/ProjectLiner/FormShipCollection.cs new file mode 100644 index 0000000..5e2979a --- /dev/null +++ b/ProjectLiner/ProjectLiner/FormShipCollection.cs @@ -0,0 +1,179 @@ +using ProjectLiner.CollectionGenericObjects; +using ProjectLiner.Drawnings; + +namespace ProjectLiner; + +/// +/// Форма работы с компанией и ее коллекцией +/// +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 ShipSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Добавление корабля + /// + /// + /// + private void buttonAddShip_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningShip)); + + /// + /// Добавление лайнера + /// + /// + /// + private void buttonAddLiner_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLiner)); + + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + 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(DrawningLiner): + drawningShip = new DrawningLiner(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 != -1) + { + 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 ButtonRemoveShip_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_company - pos != null) + { + 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; + } + + FormLiner form = new(); + form.SetShip = ship; + form.ShowDialog(); + } + + /// + /// Перерисовка коллекции + /// + /// + /// + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + pictureBox.Image = _company.Show(); + } +} \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormShipCollection.resx b/ProjectLiner/ProjectLiner/FormShipCollection.resx new file mode 100644 index 0000000..818dfae --- /dev/null +++ b/ProjectLiner/ProjectLiner/FormShipCollection.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/Program.cs b/ProjectLiner/ProjectLiner/Program.cs index b3dc92e..a294868 100644 --- a/ProjectLiner/ProjectLiner/Program.cs +++ b/ProjectLiner/ProjectLiner/Program.cs @@ -11,7 +11,7 @@ namespace ProjectLiner // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormLiner()); + Application.Run(new FormShipCollection()); } } } \ No newline at end of file