From dea0c71159364ebf2025acaf0ec2912aa0c041b5 Mon Sep 17 00:00:00 2001 From: "kagbie3nn@mail.ru" Date: Sun, 19 Nov 2023 15:13:34 +0400 Subject: [PATCH] =?UTF-8?q?3=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectWarmlyShip/DrawingWarmlyShip.cs | 10 +- .../ProjectWarmlyShip/DrawningShip.cs | 5 + .../FormShipCollection.Designer.cs | 126 +++++++++++++++ .../ProjectWarmlyShip/FormShipCollection.cs | 94 +++++++++++ .../ProjectWarmlyShip/FormShipCollection.resx | 120 ++++++++++++++ .../FormWarmlyShip.Designer.cs | 13 ++ .../ProjectWarmlyShip/FormWarmlyShip.cs | 44 +++++- .../ProjectWarmlyShip/FormWarmlyShip.resx | 8 +- .../ProjectWarmlyShip/Program.cs | 2 +- .../ProjectWarmlyShip/SetGeneric.cs | 115 ++++++++++++++ .../ShipsGenericCollection.cs | 147 ++++++++++++++++++ 11 files changed, 668 insertions(+), 16 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.resx create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs index 3c80d29..3221142 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawingWarmlyShip.cs @@ -37,20 +37,16 @@ namespace ProjectWarmlyShip.DrawningObjects return; } Brush additionalBrush = new SolidBrush(warmlyShip.AdditionalColor); - //трубы if (warmlyShip.ShipPipes) { - Brush brBl = new SolidBrush(Color.Black); - g.FillRectangle(brBl, _startPosX + 180, _startPosY + 2, 3, 12); - g.FillRectangle(brBl, _startPosX + 185, _startPosY + 4, 5, 10); + g.FillRectangle(additionalBrush, _startPosX + 180, _startPosY + 2, 3, 12); + g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 4, 5, 10); } - //топливо if (warmlyShip.ShipFuel) { - Brush bryell = new SolidBrush(Color.Yellow); - g.FillRectangle(bryell, _startPosX + 12, _startPosY + 5, 27, 7); + g.FillRectangle(additionalBrush, _startPosX + 12, _startPosY + 5, 27, 7); } base.DrawTransport(g); } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/DrawningShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/DrawningShip.cs index 5dc9241..ebbc0e0 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/DrawningShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/DrawningShip.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using static ProjectWarmlyShip.DirectionType; using ProjectWarmlyShip.Entities; +using ProjectWarmlyShip.MovementStrategy; namespace ProjectWarmlyShip.DrawningObjects { @@ -192,5 +193,9 @@ namespace ProjectWarmlyShip.DrawningObjects Brush brBl = new SolidBrush(Color.LightBlue); g.FillRectangle(brBl, _startPosX + 50, _startPosY + 0, 125, 12); } + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectShip(this); } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs new file mode 100644 index 0000000..5428e21 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -0,0 +1,126 @@ +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() + { + pictureBoxCollection = new PictureBox(); + groupBox1 = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRefreshCollection = new Button(); + buttonRemoveShip = new Button(); + ButtonAddShip = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(800, 450); + pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize; + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // groupBox1 + // + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(buttonRemoveShip); + groupBox1.Controls.Add(ButtonAddShip); + groupBox1.Location = new Point(625, 0); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(175, 450); + groupBox1.TabIndex = 1; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(16, 175); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(136, 23); + maskedTextBoxNumber.TabIndex = 3; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(16, 305); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(136, 23); + buttonRefreshCollection.TabIndex = 2; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // buttonRemoveShip + // + buttonRemoveShip.Location = new Point(16, 214); + buttonRemoveShip.Name = "buttonRemoveShip"; + buttonRemoveShip.Size = new Size(136, 23); + buttonRemoveShip.TabIndex = 1; + buttonRemoveShip.Text = "Удалить корабль"; + buttonRemoveShip.UseVisualStyleBackColor = true; + buttonRemoveShip.Click += ButtonRemoveShip_Click; + // + // ButtonAddShip + // + ButtonAddShip.Location = new Point(16, 39); + ButtonAddShip.Name = "ButtonAddShip"; + ButtonAddShip.Size = new Size(136, 23); + ButtonAddShip.TabIndex = 0; + ButtonAddShip.Text = "Добавить корабль"; + ButtonAddShip.UseVisualStyleBackColor = true; + ButtonAddShip.Click += ButtonAddShip_Click; + // + // FormShipCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormShipCollection"; + Text = "FormShipCollection"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private PictureBox pictureBoxCollection; + private GroupBox groupBox1; + private Button buttonRefreshCollection; + private Button buttonRemoveShip; + private Button ButtonAddShip; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs new file mode 100644 index 0000000..e915775 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -0,0 +1,94 @@ +using ProjectWarmlyShip.DrawningObjects; +using ProjectWarmlyShip.Generics; +using ProjectWarmlyShip.MovementStrategy; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ProjectWarmlyShip +{ + /// + /// Форма для работы с набором объектов класса DrawningCar + /// + public partial class FormShipCollection : Form + { + /// + /// Набор объектов + /// + private readonly ShipsGenericCollection _ships; + /// + /// Конструктор + /// + public FormShipCollection() + { + InitializeComponent(); + _ships = new ShipsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddShip_Click(object sender, EventArgs e) + { + FormWarmlyShip form = new FormWarmlyShip(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_ships + form.SelectedShip != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _ships.ShowShips(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveShip_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = -1; + try + { + pos = Convert.ToInt32(maskedTextBoxNumber.Text); + } + catch (Exception ex) { } + if (_ships - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _ships.ShowShips(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _ships.ShowShips(); + } + } + +} 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 2ca9c89..e657f6c 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.Designer.cs @@ -38,6 +38,7 @@ buttonCreateDeckShip = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + buttonSelectShip = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyShip).BeginInit(); SuspendLayout(); // @@ -134,11 +135,22 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += ButtonStep_Click; // + // buttonSelectShip + // + buttonSelectShip.Location = new Point(276, 415); + buttonSelectShip.Name = "buttonSelectShip"; + buttonSelectShip.Size = new Size(75, 23); + buttonSelectShip.TabIndex = 9; + buttonSelectShip.Text = "выбрать"; + buttonSelectShip.UseVisualStyleBackColor = true; + buttonSelectShip.Click += ButtonSelectShip_Click; + // // FormWarmlyShip // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonSelectShip); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateDeckShip); @@ -166,5 +178,6 @@ private Button buttonCreateDeckShip; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button buttonSelectShip; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs index 1035655..846fe9a 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.cs @@ -20,6 +20,10 @@ namespace ProjectWarmlyShip /// /// /// + /// + /// + /// + public DrawningShip? SelectedShip { get; private set; } public FormWarmlyShip() { InitializeComponent(); @@ -48,8 +52,15 @@ namespace ProjectWarmlyShip private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawingWarmlyShip = new DrawningShip(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); + color, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawingWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -62,10 +73,25 @@ namespace ProjectWarmlyShip private void buttonCreateDeckShip_Click(object sender, EventArgs e) { Random random = new(); + + //TODO + Color coloros = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + coloros = dialog.Color; + } + //TODO + Color colorad = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog2 = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + colorad = dialog2.Color; + } _drawingWarmlyShip = 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)), + coloros, colorad, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawingWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -138,5 +164,15 @@ namespace ProjectWarmlyShip _abstractStrategy = null; } } + /// + /// + /// + /// + /// + private void ButtonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _drawingWarmlyShip; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.resx index 9d790b8..38f1b1b 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.resx +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormWarmlyShip.resx @@ -123,7 +123,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvQAADr0BR/uQrQAAAkFJREFUeF7t3GtT2zAQhWErF5JAnIQQSrmFW///b+wCR6LY/Vg3nqP3+cJE + AAAOvAAADrwBlbxySQAAAkFJREFUeF7t3GtT2zAQhWErF5JAnIQQSrmFW///b+wCR6LY/Vg3nqP3+cJE O5Nhh1kjrSQ3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAACMyVQ/Tc02P1J6WuuToUP6dKfPds6VYErXGjFz pfTCRkNWpgtl926uQSeHeMR8WWjUyFcJfpho2MdGmWVuD9PpTyWWPSngYvuqxLKVAi7OlFfxqICLnfIq bhQwseyW4MJsOrP+pcSy1VIRE3fKq7hVwMW98iouFTCxbJVX9mC2Llw/KLFsYlaCl8qruFfAxa3yKsxm @@ -141,7 +141,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvQAADr0BR/uQrQAAA01JREFUeF7t3dluo0AURVHwEGdO7DjznPT/f2PHcAgULsD90FKd0l5P7jKR + AAAOvAAADrwBlbxySQAAA01JREFUeF7t3dluo0AURVHwEGdO7DjznPT/f2PHcAgULsD90FKd0l5P7jKR 2FF0jYuWXQAAAAAAAAAAAAAAAADA/7JZP643epyls3LnTP/K0GUVWJaX+nd2vhVYlt9aycyV8nautJaV xZvqdt4WWs3JUnG1pVYz0kyZRnbTpp0yjcymzVZZXVs9l4W5okJzPZuDcMo0Mpo2/SnTyGbanCpo36mO MHevnJh7HWMtPmUaOUyblVriVjrK2I1ShtzoOFuvChn2qiNNjU2ZhvW02ShinPHGzfGLGirXF3pQlhfX @@ -163,7 +163,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvQAADr0BR/uQrQAAAipJREFUeF7t3NlS20AQRmEJGzAgL+ybIQTy/s8YOfyawtJtZIWT891QVleB + AAAOvAAADrwBlbxySQAAAipJREFUeF7t3NlS20AQRmEJGzAgL+ybIQTy/s8YOfyawtJtZIWT891QVleB u6iWZnpmVEmSJEmSJEmSJEmSJEmSJEmSJEmSJEmS9F3N8hOqWdb13WqeT0BP9aebfMa5TYJ1fZ4rNKvk 17rOJZjjpLdzirzjnCa7P+6IxXiU5AJYjN2ttLPKdZAfSa3zwCvGs6TWeV8nwPGS1IqTBDjuk1mxSYDj du+Z0XrADVMX/WJ8axLheExqxVMCHJfJrHhOgKPZJrXOcpEIxqKdCu/Z8orxOakVlwlw9Eep9WMCHM1b @@ -180,7 +180,7 @@ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAABGdBTUEAALGPC/xhBQAAAG9QTFRF//// AAAAWVlZ8fHx9PT0REREtLS0Ojo6+Pj4a2trp6enj4+P3t7eXl5efX1919fXh4eHw8PD6+vrdXV10dHR 5ubmDAwMy8vLTU1NlZWVvLy8iIiINjY2nJycFhYWYWFhJycnUVFRHR0dr6+vKioqoesKnwAAAAlwSFlz - AAAOvQAADr0BR/uQrQAAA2JJREFUeF7t3dtyolAQheHgIZqTicacMznO+z/jBFgoLRtwLqame9f/XUnj + AAAOvAAADrwBlbxySQAAA2JJREFUeF7t3dtyolAQheHgIZqTicacMznO+z/jBFgoLRtwLqame9f/XUnj xV6W1UBTBScAAAAAAAAAAAAAAAAA8K/MtqfbqT5n6fytKIq3c21l6OInX+lC29m5VMCiuFYlNzfKVxQr VXIzUb6imKiSGxLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgf CeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhLGR8L4SBgfCeMjYXwkjI+E8ZEwPhK6d7ea 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 diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs new file mode 100644 index 0000000..d5c13e4 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{/// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый корабль + /// + public int Insert(T ship) + { + int index = -1; + for (int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + { + return -1; + } + for (int i = index; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = ship; + return 0; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый корабль + /// Позиция + /// + public int Insert(T plane, int position) + { + if (position < 0 || position >= Count) + return -1; + if (_places[position] == null) + { + _places[position] = plane; + return position; + } + int index = -1; + for (int i = position; i < Count; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + return -1; + for (int i = index; index > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = plane; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length) + return false; + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= _places.Length) + return null; + + return _places[position]; + } + } + +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs new file mode 100644 index 0000000..6ce2838 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs @@ -0,0 +1,147 @@ +using ProjectWarmlyShip.DrawningObjects; +using ProjectWarmlyShip.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectWarmlyShip.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningCar + /// + /// + /// + internal class ShipsGenericCollection + where T : DrawningShip + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public ShipsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int? operator +(ShipsGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(ShipsGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect._collection.Remove(pos); + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowShips() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + // TODO получение объекта + DrawningShip? Ship = _collection.Get(i); + if (Ship == null) + continue; + int r = i / width; + int s = width - 1 - (i % width); + // TODO установка позиции + Ship.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight + 50); + // TODO прорисовка объекта + Ship.DrawTransport(g); + } + } + } +}