From 3df532a3305ddeee8458257cece36d29bd4d7cf6 Mon Sep 17 00:00:00 2001 From: safia Date: Thu, 18 Apr 2024 07:18:47 +0400 Subject: [PATCH] LabWork03 --- .../AbstractCompany.cs | 106 ++++++++++ .../CollectionGenericObjects/Docks.cs | 73 +++++++ .../ICollectionGenericObjects.cs | 42 ++++ .../MassiveGenericObjects.cs | 94 +++++++++ .../{ => DrawingObject}/DirectionType.cs | 2 +- .../{ => DrawingObject}/DrawingBattleship.cs | 0 .../{ => DrawingObject}/DrawingWarship.cs | 0 .../{ => Entities}/EntityBattleship.cs | 0 .../{ => Entities}/EntityWarship.cs | 0 .../FormBattleship.Designer.cs | 50 ++--- .../ProjectBattleship/FormBattleship.cs | 79 ++------ .../FormWarshipCollection.Designer.cs | 168 ++++++++++++++++ .../FormWarshipCollection.cs | 181 ++++++++++++++++++ .../FormWarshipCollection.resx | 120 ++++++++++++ .../ProjectBattleship/Program.cs | 2 +- 15 files changed, 818 insertions(+), 99 deletions(-) create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/Docks.cs create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs rename ProjectBattleship/ProjectBattleship/{ => DrawingObject}/DirectionType.cs (91%) rename ProjectBattleship/ProjectBattleship/{ => DrawingObject}/DrawingBattleship.cs (100%) rename ProjectBattleship/ProjectBattleship/{ => DrawingObject}/DrawingWarship.cs (100%) rename ProjectBattleship/ProjectBattleship/{ => Entities}/EntityBattleship.cs (100%) rename ProjectBattleship/ProjectBattleship/{ => Entities}/EntityWarship.cs (100%) create mode 100644 ProjectBattleship/ProjectBattleship/FormWarshipCollection.Designer.cs create mode 100644 ProjectBattleship/ProjectBattleship/FormWarshipCollection.cs create mode 100644 ProjectBattleship/ProjectBattleship/FormWarshipCollection.resx diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..28f64c2 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,106 @@ +using ProjectBattleship.DrawingObject; + +namespace ProjectBattleship.CollectionGenericObjects; +/// +/// Абстракция компании, хранящий коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 80; + /// + /// Ширина окна + /// + 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, + DrawingWarship warship) + { + return company._collection?.Insert(warship) ?? 0; + } + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawingWarship? operator -(AbstractCompany company, + int position) + { + return company._collection?.Remove(position); + } + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawingWarship? 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) + { + DrawingWarship? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + return bitmap; + } + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); +} diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/Docks.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/Docks.cs new file mode 100644 index 0000000..5535ff2 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/Docks.cs @@ -0,0 +1,73 @@ +using ProjectBattleship.DrawingObject; + +namespace ProjectBattleship.CollectionGenericObjects; +/// +/// Реализация абстрактной компании - доки +/// +public class Docks : AbstractCompany +{ + /// + /// Конструктор + /// + /// + /// + /// + public Docks(int picWidth, int picHeight, + ICollectionGenericObjects collection) : + base(picWidth, picHeight, collection) + { + } + protected override void DrawBackgound(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + Brush brush = new SolidBrush(Color.Black); + for (int i = 0; i < width; ++i) + { + for (int j = 0; j < height; ++j) + { + g.FillRectangle(brush, i * _placeSizeWidth, + j * _placeSizeHeight, 200, 5); + g.FillRectangle(brush, i * _placeSizeWidth, + j * _placeSizeHeight, 5, 80); + } + } + for (int j = 0; j < height - 4; ++j) + { + g.FillRectangle(brush, j * _placeSizeWidth, + height * _placeSizeHeight, 200, 5); + } + } + protected override void SetObjectsPosition() + { + if (_collection == null) return; + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = 0; + + for (int i = 0; i < _collection.Count; i++) + { + DrawingWarship? _warship = _collection.Get(i); + if (_warship != null) + { + if (_warship.SetPictureSize(_pictureWidth, _pictureHeight)) + { + _warship.SetPosition(_placeSizeWidth * curWidth + 20, + curHeight * _placeSizeHeight + 15); + } + } + curWidth--; + if (curWidth < 0) + { + curHeight++; + curWidth = width - 1; + } + if (curHeight >= height) + { + return; + } + } + } +} diff --git a/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..a01ac3a --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,42 @@ +namespace ProjectBattleship.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/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..75ba788 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,94 @@ +namespace ProjectBattleship.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 null; + } + return _collection[position]; + } + 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 + 1; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return position; + } + } + + for (int i = position - 1; i >= 0; i--) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return position; + } + } + + return -1; + } + public T? Remove(int position) + { + if (position < 0 || position > Count || _collection[position] == null) + { + return null; + } + + T? obj = _collection[position]; + _collection[position] = null; + return obj; + } +} diff --git a/ProjectBattleship/ProjectBattleship/DirectionType.cs b/ProjectBattleship/ProjectBattleship/DrawingObject/DirectionType.cs similarity index 91% rename from ProjectBattleship/ProjectBattleship/DirectionType.cs rename to ProjectBattleship/ProjectBattleship/DrawingObject/DirectionType.cs index 7a34dbb..0cd3f23 100644 --- a/ProjectBattleship/ProjectBattleship/DirectionType.cs +++ b/ProjectBattleship/ProjectBattleship/DrawingObject/DirectionType.cs @@ -1,4 +1,4 @@ -namespace ProjectBattleship; +namespace ProjectBattleship.DrawingObject; /// /// Направление перемещения /// diff --git a/ProjectBattleship/ProjectBattleship/DrawingBattleship.cs b/ProjectBattleship/ProjectBattleship/DrawingObject/DrawingBattleship.cs similarity index 100% rename from ProjectBattleship/ProjectBattleship/DrawingBattleship.cs rename to ProjectBattleship/ProjectBattleship/DrawingObject/DrawingBattleship.cs diff --git a/ProjectBattleship/ProjectBattleship/DrawingWarship.cs b/ProjectBattleship/ProjectBattleship/DrawingObject/DrawingWarship.cs similarity index 100% rename from ProjectBattleship/ProjectBattleship/DrawingWarship.cs rename to ProjectBattleship/ProjectBattleship/DrawingObject/DrawingWarship.cs diff --git a/ProjectBattleship/ProjectBattleship/EntityBattleship.cs b/ProjectBattleship/ProjectBattleship/Entities/EntityBattleship.cs similarity index 100% rename from ProjectBattleship/ProjectBattleship/EntityBattleship.cs rename to ProjectBattleship/ProjectBattleship/Entities/EntityBattleship.cs diff --git a/ProjectBattleship/ProjectBattleship/EntityWarship.cs b/ProjectBattleship/ProjectBattleship/Entities/EntityWarship.cs similarity index 100% rename from ProjectBattleship/ProjectBattleship/EntityWarship.cs rename to ProjectBattleship/ProjectBattleship/Entities/EntityWarship.cs diff --git a/ProjectBattleship/ProjectBattleship/FormBattleship.Designer.cs b/ProjectBattleship/ProjectBattleship/FormBattleship.Designer.cs index 152b317..20ecc22 100644 --- a/ProjectBattleship/ProjectBattleship/FormBattleship.Designer.cs +++ b/ProjectBattleship/ProjectBattleship/FormBattleship.Designer.cs @@ -29,14 +29,12 @@ private void InitializeComponent() { pictureBoxBattleship = new PictureBox(); - buttonCreateBattleship = new Button(); buttonLeft = new Button(); buttonDown = new Button(); buttonRight = new Button(); buttonUp = new Button(); comboBoxStrategy = new ComboBox(); - button1 = new Button(); - buttonCreateWarship = new Button(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit(); SuspendLayout(); // @@ -50,18 +48,6 @@ pictureBoxBattleship.TabIndex = 0; pictureBoxBattleship.TabStop = false; // - // buttonCreateBattleship - // - buttonCreateBattleship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateBattleship.Location = new Point(11, 384); - buttonCreateBattleship.Margin = new Padding(2); - buttonCreateBattleship.Name = "buttonCreateBattleship"; - buttonCreateBattleship.Size = new Size(201, 40); - buttonCreateBattleship.TabIndex = 1; - buttonCreateBattleship.Text = "Создать линкор"; - buttonCreateBattleship.UseVisualStyleBackColor = true; - buttonCreateBattleship.Click += ButtonCreateBattleship_Click; - // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -126,38 +112,26 @@ // // button1 // - button1.Location = new Point(752, 72); - button1.Name = "button1"; - button1.Size = new Size(104, 43); - button1.TabIndex = 7; - button1.Text = "шаг"; - button1.TextAlign = ContentAlignment.TopCenter; - button1.UseVisualStyleBackColor = true; - button1.Click += ButtonStrategyStep_Click; - // - // buttonCreateWarship - // - buttonCreateWarship.Location = new Point(217, 384); - buttonCreateWarship.Name = "buttonCreateWarship"; - buttonCreateWarship.Size = new Size(200, 40); - buttonCreateWarship.TabIndex = 8; - buttonCreateWarship.Text = "Создать корабль"; - buttonCreateWarship.UseVisualStyleBackColor = true; - buttonCreateWarship.Click += ButtonCreateWarship_Click; + buttonStrategyStep.Location = new Point(752, 72); + buttonStrategyStep.Name = "button1"; + buttonStrategyStep.Size = new Size(104, 43); + buttonStrategyStep.TabIndex = 7; + buttonStrategyStep.Text = "шаг"; + buttonStrategyStep.TextAlign = ContentAlignment.TopCenter; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; // // FormBattleship // AutoScaleDimensions = new SizeF(12F, 30F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(876, 436); - Controls.Add(buttonCreateWarship); - Controls.Add(button1); + Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonLeft); - Controls.Add(buttonCreateBattleship); Controls.Add(pictureBoxBattleship); Margin = new Padding(2); Name = "FormBattleship"; @@ -171,13 +145,11 @@ #endregion private PictureBox pictureBoxBattleship; - private Button buttonCreateBattleship; private Button buttonLeft; private Button buttonDown; private Button buttonRight; private Button buttonUp; private ComboBox comboBoxStrategy; - private Button button1; - private Button buttonCreateWarship; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/FormBattleship.cs b/ProjectBattleship/ProjectBattleship/FormBattleship.cs index 8e6e7ca..430c6c5 100644 --- a/ProjectBattleship/ProjectBattleship/FormBattleship.cs +++ b/ProjectBattleship/ProjectBattleship/FormBattleship.cs @@ -15,6 +15,21 @@ public partial class FormBattleship : Form /// private AbstractStrategy? _strategy; /// + /// + /// + public DrawingWarship SetWarship + { + set + { + _drawingWarship = value; + _drawingWarship.SetPictureSize(pictureBoxBattleship.Width, + pictureBoxBattleship.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + /// /// /// public FormBattleship() @@ -23,7 +38,7 @@ public partial class FormBattleship : Form _strategy = null; } /// - /// + /// /// private void Draw() { @@ -31,62 +46,13 @@ public partial class FormBattleship : Form { return; } - Bitmap bmp = new(pictureBoxBattleship.Width, - pictureBoxBattleship.Height); + Bitmap bmp = new(pictureBoxBattleship.Width, + pictureBoxBattleship.Height); Graphics gr = Graphics.FromImage(bmp); _drawingWarship.DrawTransport(gr); pictureBoxBattleship.Image = bmp; } /// - /// - - /// - /// - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawingWarship): - _drawingWarship = new DrawingWarship(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(DrawingBattleship): - _drawingWarship = new DrawingBattleship(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; - } - _drawingWarship.SetPictureSize(pictureBoxBattleship.Width, - pictureBoxBattleship.Height); - _drawingWarship.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - /// - /// " " - /// - /// - /// - private void ButtonCreateBattleship_Click(object sender, EventArgs e) => - CreateObject(nameof(DrawingBattleship)); - /// - /// " " - /// - /// - /// - private void ButtonCreateWarship_Click(object sender, EventArgs e) => - CreateObject(nameof(DrawingWarship)); - /// /// ( ) /// /// @@ -111,8 +77,7 @@ public partial class FormBattleship : Form result = _drawingWarship.MoveTransport(DirectionType.Left); break; case "buttonRight": - result = - _drawingWarship.MoveTransport(DirectionType.Right); + result = _drawingWarship.MoveTransport(DirectionType.Right); break; } if (result) @@ -143,8 +108,8 @@ public partial class FormBattleship : Form { return; } - _strategy.SetData(new MoveableWarship(_drawingWarship), - pictureBoxBattleship.Width, pictureBoxBattleship.Height); + _strategy.SetData(new MoveableWarship(_drawingWarship), + pictureBoxBattleship.Width, pictureBoxBattleship.Height); } if (_strategy == null) { @@ -159,6 +124,4 @@ public partial class FormBattleship : Form _strategy = null; } } - - } diff --git a/ProjectBattleship/ProjectBattleship/FormWarshipCollection.Designer.cs b/ProjectBattleship/ProjectBattleship/FormWarshipCollection.Designer.cs new file mode 100644 index 0000000..0a77861 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/FormWarshipCollection.Designer.cs @@ -0,0 +1,168 @@ +using System.Windows.Forms; + +namespace ProjectBattleship +{ + 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() + { + groupBoxCollectionTools = new GroupBox(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonRemoveWarship = new Button(); + buttonAddBattleship = new Button(); + buttonAddWarship = new Button(); + comboBoxSelectorCompany = new ComboBox(); + pictureBox = new PictureBox(); + groupBoxCollectionTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxCollectionTools + // + groupBoxCollectionTools.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; + groupBoxCollectionTools.Controls.Add(maskedTextBoxPosition); + groupBoxCollectionTools.Controls.Add(buttonRefresh); + groupBoxCollectionTools.Controls.Add(buttonGoToCheck); + groupBoxCollectionTools.Controls.Add(buttonRemoveWarship); + groupBoxCollectionTools.Controls.Add(buttonAddBattleship); + groupBoxCollectionTools.Controls.Add(buttonAddWarship); + groupBoxCollectionTools.Controls.Add(comboBoxSelectorCompany); + groupBoxCollectionTools.Location = new Point(777, 0); + groupBoxCollectionTools.Name = "groupBoxCollectionTools"; + groupBoxCollectionTools.Size = new Size(196, 594); + groupBoxCollectionTools.TabIndex = 1; + groupBoxCollectionTools.TabStop = false; + groupBoxCollectionTools.Text = "Инструменты"; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(6, 271); + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Size = new Size(183, 31); + maskedTextBoxPosition.TabIndex = 8; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRefresh + // + buttonRefresh.Location = new Point(6, 514); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(183, 68); + buttonRefresh.TabIndex = 7; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Location = new Point(6, 410); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(183, 68); + buttonGoToCheck.TabIndex = 6; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRemoveWarship + // + buttonRemoveWarship.Location = new Point(6, 308); + buttonRemoveWarship.Name = "buttonRemoveWarship"; + buttonRemoveWarship.Size = new Size(183, 68); + buttonRemoveWarship.TabIndex = 5; + buttonRemoveWarship.Text = "Удалить военный корабль"; + buttonRemoveWarship.UseVisualStyleBackColor = true; + buttonRemoveWarship.Click += ButtonRemoveWarship_Click; + // + // buttonAddBattleship + // + buttonAddBattleship.Location = new Point(6, 172); + buttonAddBattleship.Name = "buttonAddBattleship"; + buttonAddBattleship.Size = new Size(183, 68); + buttonAddBattleship.TabIndex = 3; + buttonAddBattleship.Text = "Добавление линкора"; + buttonAddBattleship.UseVisualStyleBackColor = true; + buttonAddBattleship.Click += ButtonAddBattleship_Click; + // + // buttonAddWarship + // + buttonAddWarship.Location = new Point(6, 98); + buttonAddWarship.Name = "buttonAddWarship"; + buttonAddWarship.Size = new Size(183, 68); + buttonAddWarship.TabIndex = 2; + buttonAddWarship.Text = "Добавление военного корабля"; + buttonAddWarship.UseVisualStyleBackColor = true; + buttonAddWarship.Click += ButtonAddWarship_Click; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.Location = new Point(6, 30); + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(183, 33); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(771, 594); + pictureBox.TabIndex = 2; + pictureBox.TabStop = false; + // + // FormWarshipCollection + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(978, 594); + Controls.Add(pictureBox); + Controls.Add(groupBoxCollectionTools); + Name = "FormWarshipCollection"; + Text = "Коллекция военных кораблей"; + groupBoxCollectionTools.ResumeLayout(false); + groupBoxCollectionTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + private GroupBox groupBoxCollectionTools; + private Button buttonAddBattleship; + private Button buttonAddWarship; + private ComboBox comboBoxSelectorCompany; + private Button buttonRefresh; + private Button buttonGoToCheck; + private Button buttonRemoveWarship; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + } +} \ No newline at end of file diff --git a/ProjectBattleship/ProjectBattleship/FormWarshipCollection.cs b/ProjectBattleship/ProjectBattleship/FormWarshipCollection.cs new file mode 100644 index 0000000..12b4fc9 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/FormWarshipCollection.cs @@ -0,0 +1,181 @@ +using ProjectBattleship.CollectionGenericObjects; +using ProjectBattleship.DrawingObject; + +namespace ProjectBattleship; +/// +/// Форма работы с компанией и ее коллекцией +/// +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 Docks(pictureBox.Width, + pictureBox.Height, + new MassiveGenericObjects()); + break; + } + } + /// + /// Добавление военного корабля + /// + /// + /// + private void ButtonAddWarship_Click(object sender, EventArgs e) => + CreateObject(nameof(DrawingWarship)); + /// + /// Добавление линкора + /// + /// + /// + private void ButtonAddBattleship_Click(object sender, EventArgs e) => + CreateObject(nameof(DrawingBattleship)); + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + Random random = new(); + DrawingWarship drawingWarship; + switch (type) + { + case nameof(DrawingWarship): + drawingWarship = new DrawingWarship(random.Next(100, 300), + random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawingBattleship): + drawingWarship = new DrawingBattleship(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 + drawingWarship != -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 ButtonRemoveWarship_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; + } + DrawingWarship? warship = null; + int counter = 100; + while (warship == null) + { + warship = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (warship == null) + { + return; + } + FormBattleship form = new() + { + SetWarship = warship + }; + form.ShowDialog(); + } + /// + /// Перерисовка коллекции + /// + /// + /// + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + pictureBox.Image = _company.Show(); + } +} + diff --git a/ProjectBattleship/ProjectBattleship/FormWarshipCollection.resx b/ProjectBattleship/ProjectBattleship/FormWarshipCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectBattleship/ProjectBattleship/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/ProjectBattleship/ProjectBattleship/Program.cs b/ProjectBattleship/ProjectBattleship/Program.cs index 5e455fb..a584329 100644 --- a/ProjectBattleship/ProjectBattleship/Program.cs +++ b/ProjectBattleship/ProjectBattleship/Program.cs @@ -9,7 +9,7 @@ namespace ProjectBattleship static void Main() { ApplicationConfiguration.Initialize(); - Application.Run(new FormBattleship()); + Application.Run(new FormWarshipCollection()); } } } \ No newline at end of file -- 2.25.1