diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..d7c91f7 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,121 @@ +using ProjectAircraftCarrier.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.CollectionGenericObjects; + +/// +/// Абстракция компании, хранящей коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 180; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 70; + + /// + /// Ширина окна + /// + 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 int operator +(AbstractCompany company, DrawningWarship warship) + { + return company._collection.Insert(warship); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningWarship operator -(AbstractCompany company, int position) + { + return company._collection.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningWarship? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + + /// + /// Вывод всей коллекции + /// + /// + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackground(graphics); + + SetObjectPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningWarship? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackground(Graphics g); + + /// + /// Расстановка объектов + /// + protected abstract void SetObjectPosition(); +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..2a3003d --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.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); +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..f3c079d --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.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 проверка позиции + if (position >= _collection.Length || position < 0) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + // TODO вставка в свободное место набора + return Insert(obj, 0); + } + + public int Insert(T obj, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // ищется свободное место после этой позиции и идёт вставка туда + // если нет после, ищем до + // TODO вставка + if (position >= _collection.Length || position < 0) + return -1; + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + int index = position + 1; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + index = position - 1; + while (index >= 0) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + --index; + } + return -1; + } + + public T? Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива значение null + if (position >= _collection.Length || position < 0) + return null; + T obj = _collection[position]; + _collection[position] = null; + return obj; + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/WarshipDockService.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/WarshipDockService.cs new file mode 100644 index 0000000..3283e58 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/CollectionGenericObjects/WarshipDockService.cs @@ -0,0 +1,62 @@ +using ProjectAircraftCarrier.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.CollectionGenericObjects; + +public class WarshipDockService : AbstractCompany +{ + public WarshipDockService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackground(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 / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + protected override void SetObjectPosition() + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int posWidth = 0; + int posHeight = height - 1; + + 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 * posWidth + 5, posHeight * _placeSizeHeight + 10); + } + if (posWidth < width - 1) + { + posWidth++; + } + else + { + posWidth = 0; + posHeight--; + } + if (posHeight > height) + { + return; + } + } + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs index b430dfd..cada459 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs @@ -29,12 +29,10 @@ private void InitializeComponent() { pictureBoxAircraftCarrier = new PictureBox(); - buttonCreateAircraftCarrier = new Button(); buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); buttonLeft = new Button(); - buttonCreateWarship = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit(); @@ -44,31 +42,22 @@ // pictureBoxAircraftCarrier.Dock = DockStyle.Fill; pictureBoxAircraftCarrier.Location = new Point(0, 0); + pictureBoxAircraftCarrier.Margin = new Padding(3, 4, 3, 4); pictureBoxAircraftCarrier.Name = "pictureBoxAircraftCarrier"; - pictureBoxAircraftCarrier.Size = new Size(803, 471); + pictureBoxAircraftCarrier.Size = new Size(918, 628); pictureBoxAircraftCarrier.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxAircraftCarrier.TabIndex = 0; pictureBoxAircraftCarrier.TabStop = false; // - // buttonCreateAircraftCarrier - // - buttonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateAircraftCarrier.Location = new Point(12, 436); - buttonCreateAircraftCarrier.Name = "buttonCreateAircraftCarrier"; - buttonCreateAircraftCarrier.Size = new Size(164, 23); - buttonCreateAircraftCarrier.TabIndex = 1; - buttonCreateAircraftCarrier.Text = "Создать авианосец"; - buttonCreateAircraftCarrier.UseVisualStyleBackColor = true; - buttonCreateAircraftCarrier.Click += ButtonCreateAircraftCarrier_Click; - // // buttonRight // buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(766, 439); + buttonRight.Location = new Point(875, 585); + buttonRight.Margin = new Padding(3, 4, 3, 4); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(26, 22); + buttonRight.Size = new Size(30, 29); buttonRight.TabIndex = 2; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; @@ -78,9 +67,10 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.arrowUp; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(735, 410); + buttonUp.Location = new Point(840, 547); + buttonUp.Margin = new Padding(3, 4, 3, 4); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(26, 22); + buttonUp.Size = new Size(30, 29); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; @@ -90,9 +80,10 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(735, 439); + buttonDown.Location = new Point(840, 585); + buttonDown.Margin = new Padding(3, 4, 3, 4); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(26, 22); + buttonDown.Size = new Size(30, 29); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; @@ -102,39 +93,31 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(704, 439); + buttonLeft.Location = new Point(805, 585); + buttonLeft.Margin = new Padding(3, 4, 3, 4); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(26, 22); + buttonLeft.Size = new Size(30, 29); buttonLeft.TabIndex = 5; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; // - // buttonCreateWarship - // - buttonCreateWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateWarship.Location = new Point(182, 436); - buttonCreateWarship.Name = "buttonCreateWarship"; - buttonCreateWarship.Size = new Size(182, 23); - buttonCreateWarship.TabIndex = 6; - buttonCreateWarship.Text = "Создать военный корабль"; - buttonCreateWarship.UseVisualStyleBackColor = true; - buttonCreateWarship.Click += ButtonCreateWarship_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); - comboBoxStrategy.Location = new Point(670, 12); + comboBoxStrategy.Location = new Point(766, 16); + comboBoxStrategy.Margin = new Padding(3, 4, 3, 4); comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.Size = new Size(138, 28); comboBoxStrategy.TabIndex = 7; // // buttonStrategyStep // - buttonStrategyStep.Location = new Point(716, 41); + buttonStrategyStep.Location = new Point(818, 55); + buttonStrategyStep.Margin = new Padding(3, 4, 3, 4); buttonStrategyStep.Name = "buttonStrategyStep"; - buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.Size = new Size(86, 31); buttonStrategyStep.TabIndex = 8; buttonStrategyStep.Text = "Шаг"; buttonStrategyStep.UseVisualStyleBackColor = true; @@ -142,18 +125,17 @@ // // FormAircraftCarrier // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(803, 471); + ClientSize = new Size(918, 628); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateWarship); Controls.Add(buttonLeft); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonRight); - Controls.Add(buttonCreateAircraftCarrier); Controls.Add(pictureBoxAircraftCarrier); + Margin = new Padding(3, 4, 3, 4); Name = "FormAircraftCarrier"; StartPosition = FormStartPosition.CenterScreen; Text = "Авианосец"; @@ -165,12 +147,10 @@ #endregion private PictureBox pictureBoxAircraftCarrier; - private Button buttonCreateAircraftCarrier; private Button buttonRight; private Button buttonUp; private Button buttonDown; private Button buttonLeft; - private Button buttonCreateWarship; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs index 75d24cc..3448bf2 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs @@ -27,6 +27,18 @@ public partial class FormAircraftCarrier : Form /// private AbstractStrategy? _strategy; + public DrawningWarship SetWarship + { + set + { + _drawningWarship = value; + _drawningWarship.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + /// /// Конструктор формы /// @@ -52,46 +64,6 @@ public partial class FormAircraftCarrier : Form pictureBoxAircraftCarrier.Image = bmp; } - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningWarship): - _drawningWarship = new DrawningWarship(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(DrawningAircraftCarrier): - _drawningWarship = new DrawningAircraftCarrier(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; - } - - _drawningWarship.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); - _drawningWarship.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - - /// - /// Обработка нажатия кнопки "Создать авианосец" - /// - /// - /// - private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAircraftCarrier)); - - /// - /// Обработка нажатия кнопки "Создать военный корабль" - /// - /// - /// - private void ButtonCreateWarship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarship)); - /// /// Перемещение объекта по форме (нажатие кнопок навигации) /// diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs new file mode 100644 index 0000000..5086a3a --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectAircraftCarrier +{ + partial class FormWarshipCollection + { + /// + /// 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(); + buttonRemoveWarship = new Button(); + maskedTextBox = new MaskedTextBox(); + buttonAddAircraftCarrier = new Button(); + buttonAddWarship = 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(buttonRemoveWarship); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddAircraftCarrier); + groupBoxTools.Controls.Add(buttonAddWarship); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(905, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(205, 682); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 549); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(193, 52); + 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, 414); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(193, 52); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveWarship + // + buttonRemoveWarship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRemoveWarship.Location = new Point(6, 300); + buttonRemoveWarship.Name = "buttonRemoveWarship"; + buttonRemoveWarship.Size = new Size(193, 52); + buttonRemoveWarship.TabIndex = 4; + buttonRemoveWarship.Text = "Удалить \r\nвоенный корабль"; + buttonRemoveWarship.UseVisualStyleBackColor = true; + buttonRemoveWarship.Click += ButtonRemoveWarship_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(6, 267); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(193, 27); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddAircraftCarrier + // + buttonAddAircraftCarrier.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddAircraftCarrier.Location = new Point(6, 180); + buttonAddAircraftCarrier.Name = "buttonAddAircraftCarrier"; + buttonAddAircraftCarrier.Size = new Size(193, 52); + buttonAddAircraftCarrier.TabIndex = 2; + buttonAddAircraftCarrier.Text = "Добавление\r\nавианосца"; + buttonAddAircraftCarrier.UseVisualStyleBackColor = true; + buttonAddAircraftCarrier.Click += ButtonAddAircraftCarrier_Click; + // + // buttonAddWarship + // + buttonAddWarship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddWarship.Location = new Point(6, 122); + buttonAddWarship.Name = "buttonAddWarship"; + buttonAddWarship.Size = new Size(193, 52); + buttonAddWarship.TabIndex = 1; + buttonAddWarship.Text = "Добавление\r\nвоенного корабля"; + buttonAddWarship.UseVisualStyleBackColor = true; + buttonAddWarship.Click += ButtonAddWarship_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, 26); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(193, 28); + 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(905, 682); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormWarshipCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1110, 682); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormWarshipCollection"; + Text = "Коллекция военных кораблей"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddWarship; + private MaskedTextBox maskedTextBox; + private Button buttonAddAircraftCarrier; + private PictureBox pictureBox; + private Button buttonRemoveWarship; + private Button buttonRefresh; + private Button buttonGoToCheck; + } +} \ No newline at end of file diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs new file mode 100644 index 0000000..c2a3d45 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.cs @@ -0,0 +1,166 @@ +using ProjectAircraftCarrier.CollectionGenericObjects; +using ProjectAircraftCarrier.Drawnings; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ProjectAircraftCarrier; + +/// +/// Форма работы с компанией и её коллекцией +/// +public partial class FormWarshipCollection : Form +{ + /// + /// Компания + /// + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormWarshipCollection() + { + InitializeComponent(); + } + + /// + /// Выбор компании + /// + /// + /// + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new WarshipDockService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Создание объекта класса-перемещение + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawningWarship drawningWarship; + switch (type) + { + case nameof(DrawningWarship): + drawningWarship = new DrawningWarship(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningAircraftCarrier): + drawningWarship = new DrawningAircraftCarrier(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 + drawningWarship != -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 ButtonAddWarship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarship)); + + private void ButtonAddAircraftCarrier_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAircraftCarrier)); + + private void ButtonRemoveWarship_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 != null) + { + MessageBox.Show("Объект удалён"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + DrawningWarship? warship = null; + int counter = 100; + while (warship == null) + { + warship = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (warship == null) + { + return; + } + + FormAircraftCarrier form = new() + { + SetWarship = warship + }; + 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/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.resx b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormWarshipCollection.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/ProjectAircraftCarrier/ProjectAircraftCarrier/Program.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Program.cs index 94348f3..2a20b85 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Program.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Program.cs @@ -11,7 +11,7 @@ namespace ProjectAircraftCarrier // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAircraftCarrier()); + Application.Run(new FormWarshipCollection()); } } } \ No newline at end of file