From d75256eac853a91e3afab190e190dd86570f1df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=A7=D0=B5=D1=80?= =?UTF-8?q?=D0=BD=D1=8B=D1=88=D0=BE=D0=B2?= Date: Fri, 11 Nov 2022 11:20:46 +0400 Subject: [PATCH] Generic classes and changes forms --- Warship/Warship/AbstractMap.cs | 4 +- Warship/Warship/Direction.cs | 2 +- Warship/Warship/DrawingObjectWarship.cs | 25 +- Warship/Warship/DrawingPlaneWarship.cs | 8 +- Warship/Warship/DrawingWarship.cs | 2 +- Warship/Warship/EntityWarship.cs | 2 +- Warship/Warship/FormMap.Designer.cs | 208 ---------------- Warship/Warship/FormMap.cs | 105 -------- .../Warship/FormMapWithSetWarship.Designer.cs | 233 ++++++++++++++++++ Warship/Warship/FormMapWithSetWarship.cs | 165 +++++++++++++ ...ormMap.resx => FormMapWithSetWarship.resx} | 3 - Warship/Warship/FormShip.Designer.cs | 14 ++ Warship/Warship/FormShip.cs | 39 ++- Warship/Warship/IDrawingObject.cs | 3 +- Warship/Warship/MapWithSetWarshipGeneric.cs | 186 ++++++++++++++ Warship/Warship/Program.cs | 2 +- Warship/Warship/SetWarshipGeneric.cs | 146 +++++++++++ 17 files changed, 800 insertions(+), 347 deletions(-) delete mode 100644 Warship/Warship/FormMap.Designer.cs delete mode 100644 Warship/Warship/FormMap.cs create mode 100644 Warship/Warship/FormMapWithSetWarship.Designer.cs create mode 100644 Warship/Warship/FormMapWithSetWarship.cs rename Warship/Warship/{FormMap.resx => FormMapWithSetWarship.resx} (93%) create mode 100644 Warship/Warship/MapWithSetWarshipGeneric.cs create mode 100644 Warship/Warship/SetWarshipGeneric.cs diff --git a/Warship/Warship/AbstractMap.cs b/Warship/Warship/AbstractMap.cs index beceb56..18b1268 100644 --- a/Warship/Warship/AbstractMap.cs +++ b/Warship/Warship/AbstractMap.cs @@ -147,6 +147,4 @@ namespace AircraftCarrier protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); } -} - - +} \ No newline at end of file diff --git a/Warship/Warship/Direction.cs b/Warship/Warship/Direction.cs index 29854e5..96f29cb 100644 --- a/Warship/Warship/Direction.cs +++ b/Warship/Warship/Direction.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Направление перемещения /// - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/Warship/Warship/DrawingObjectWarship.cs b/Warship/Warship/DrawingObjectWarship.cs index 97987dd..c0e0d58 100644 --- a/Warship/Warship/DrawingObjectWarship.cs +++ b/Warship/Warship/DrawingObjectWarship.cs @@ -1,4 +1,5 @@ -using System; +using AircraftCarrier; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,27 +9,27 @@ namespace AircraftCarrier { internal class DrawingObjectWarship : IDrawingObject { - private DrawingWarship _ship = null; - public DrawingObjectWarship(DrawingWarship ship) + private DrawingWarship _warship = null; + public DrawingObjectWarship(DrawingWarship warship) { - _ship = ship; + _warship = warship; + } + public float Step => _warship?.Ship?.Step ?? 0; + public void DrawningObject(Graphics g) + { + _warship.DrawTransport(g); } - public float Step => _ship?.Ship?.Step ?? 0; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { - return _ship?.GetCurrentPosition() ?? default; + return _warship?.GetCurrentPosition() ?? default; } public void MoveObject(Direction direction) { - _ship?.MoveTransport(direction); + _warship?.MoveTransport(direction); } public void SetObject(int x, int y, int width, int height) { - _ship.SetPosition(x, y, width, height); - } - public void DrawningObject(Graphics g) - { - _ship.DrawTransport(g); + _warship.SetPosition(x, y, width, height); } } } diff --git a/Warship/Warship/DrawingPlaneWarship.cs b/Warship/Warship/DrawingPlaneWarship.cs index facf69f..f3c3f4e 100644 --- a/Warship/Warship/DrawingPlaneWarship.cs +++ b/Warship/Warship/DrawingPlaneWarship.cs @@ -47,15 +47,15 @@ namespace AircraftCarrier { pointShipArmor1, pointShipArmor2, pointShipArmor3, pointShipArmor4, }; - g.FillPolygon(br, PointsShipArmor); + g.FillPolygon(dopBrush, PointsShipArmor); g.DrawPolygon(pen, PointsShipArmor); g.DrawEllipse(pen, _startPosX + 224, _startPosY + 37, 20, 20); - g.FillEllipse(br, _startPosX + 224, _startPosY + 37, 20, 20); + g.FillEllipse(dopBrush, _startPosX + 224, _startPosY + 37, 20, 20); // торпеда g.DrawRectangle(pen, _startPosX + 30, _startPosY + 8, 105, 15); - g.FillRectangle(br, _startPosX + 30, _startPosY + 8, 105, 15); + g.FillRectangle(dopBrush, _startPosX + 30, _startPosY + 8, 105, 15); g.DrawEllipse(pen, _startPosX + 130, _startPosY + 5, 35, 20); - g.FillEllipse(br, _startPosX + 130, _startPosY + 5, 35, 20); + g.FillEllipse(dopBrush, _startPosX + 130, _startPosY + 5, 35, 20); } _startPosX += 22; diff --git a/Warship/Warship/DrawingWarship.cs b/Warship/Warship/DrawingWarship.cs index 501e047..06b9d4f 100644 --- a/Warship/Warship/DrawingWarship.cs +++ b/Warship/Warship/DrawingWarship.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawingWarship + public class DrawingWarship { /// /// Класс-сущность diff --git a/Warship/Warship/EntityWarship.cs b/Warship/Warship/EntityWarship.cs index 35da7f8..d63c221 100644 --- a/Warship/Warship/EntityWarship.cs +++ b/Warship/Warship/EntityWarship.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Класс-сущность "Военный корабль" /// - internal class EntityWarship + public class EntityWarship { /// /// Скорость diff --git a/Warship/Warship/FormMap.Designer.cs b/Warship/Warship/FormMap.Designer.cs deleted file mode 100644 index b1a59fb..0000000 --- a/Warship/Warship/FormMap.Designer.cs +++ /dev/null @@ -1,208 +0,0 @@ -namespace AircraftCarrier -{ - partial class FormMap - { - /// - /// 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() - { - this.pictureBoxShip = new System.Windows.Forms.PictureBox(); - this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel(); - this.buttonCreate = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonCreateModif = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); - this.statusStrip1.SuspendLayout(); - this.SuspendLayout(); - // - // pictureBoxShip - // - this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxShip.Location = new System.Drawing.Point(0, 0); - this.pictureBoxShip.Name = "pictureBoxShip"; - this.pictureBoxShip.Size = new System.Drawing.Size(800, 450); - this.pictureBoxShip.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; - this.pictureBoxShip.TabIndex = 0; - this.pictureBoxShip.TabStop = false; - // - // statusStrip1 - // - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelColor}); - this.statusStrip1.Location = new System.Drawing.Point(0, 428); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 22); - this.statusStrip1.TabIndex = 1; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 17); - this.toolStripStatusLabelSpeed.Text = " Скорость"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17); - this.toolStripStatusLabelWeight.Text = "Вес"; - // - // toolStripStatusLabelColor - // - this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; - this.toolStripStatusLabelColor.Size = new System.Drawing.Size(33, 17); - this.toolStripStatusLabelColor.Text = "Цвет"; - // - // buttonCreate - // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.Location = new System.Drawing.Point(12, 382); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(75, 23); - this.buttonCreate.TabIndex = 2; - this.buttonCreate.Text = "Создать"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); - // - // buttonLeft - // - this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелка; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(657, 375); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 3; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonDown - // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаbott; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(693, 375); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 4; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonUp - // - this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаtop; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(693, 339); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 5; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonRight - // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаright; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(729, 375); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 6; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonCreateModif - // - this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreateModif.Location = new System.Drawing.Point(107, 382); - this.buttonCreateModif.Name = "buttonCreateModif"; - this.buttonCreateModif.Size = new System.Drawing.Size(130, 23); - this.buttonCreateModif.TabIndex = 7; - this.buttonCreateModif.Text = "Модификация"; - this.buttonCreateModif.UseVisualStyleBackColor = true; - this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); - // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Морская карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(8, 8); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); - this.comboBoxSelectorMap.TabIndex = 8; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - // - // FormMap - // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.comboBoxSelectorMap); - this.Controls.Add(this.statusStrip1); - this.Controls.Add(this.buttonCreateModif); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonCreate); - this.Controls.Add(this.pictureBoxShip); - this.Name = "FormMap"; - this.Text = "Карта"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit(); - this.statusStrip1.ResumeLayout(false); - this.statusStrip1.PerformLayout(); - this.ResumeLayout(false); - this.PerformLayout(); - - } - - #endregion - - private PictureBox pictureBoxShip; - private StatusStrip statusStrip1; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabelColor; - private Button buttonCreate; - private Button buttonLeft; - private Button buttonDown; - private Button buttonUp; - private Button buttonRight; - private Button buttonCreateModif; - private ComboBox comboBoxSelectorMap; - } -} \ No newline at end of file diff --git a/Warship/Warship/FormMap.cs b/Warship/Warship/FormMap.cs deleted file mode 100644 index 529cef1..0000000 --- a/Warship/Warship/FormMap.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace AircraftCarrier -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - /// - /// Заполнение информации по объекту - /// - /// - private void SetData(DrawingWarship ship) - { - toolStripStatusLabelSpeed.Text = $"Скорость: {ship.Ship.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {ship.Ship.Weight}"; - toolStripStatusLabelColor.Text = $"Цвет: {ship.Ship.BodyColor.Name}"; - pictureBoxShip.Image = _abstractMap.CreateMap(pictureBoxShip.Width, pictureBoxShip.Height, - new DrawingObjectWarship(ship)); - } - /// - /// Обработка нажатия кнопки "Создать" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var ship = new DrawingWarship(rnd.Next(1000, 2000), rnd.Next(15000, 20000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(ship); - } - /// - /// Изменение размеров формы - /// - /// - /// - private void ButtonMove_Click(object sender, EventArgs e) - { - //получаем имя кнопки - string name = ((Button)sender)?.Name ?? string.Empty; - Direction dir = Direction.None; - switch (name) - { - case "buttonUp": - dir = Direction.Up; - break; - case "buttonDown": - dir = Direction.Down; - break; - case "buttonLeft": - dir = Direction.Left; - break; - case "buttonRight": - dir = Direction.Right; - break; - } - pictureBoxShip.Image = _abstractMap?.MoveObject(dir); - } - /// - /// Обработка нажатия кнопки "Модификация" - /// - /// - /// - private void ButtonCreateModif_Click(object sender, EventArgs e) - { - Random rnd = new(); - var ship = new DrawingPlaneWarship(rnd.Next(1000, 2000), rnd.Next(15000, 20000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); - SetData(ship); - } - /// - /// Смена карты - /// - /// - /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Морская карта": - _abstractMap = new SeaMap(); - break; - } - } - } -} diff --git a/Warship/Warship/FormMapWithSetWarship.Designer.cs b/Warship/Warship/FormMapWithSetWarship.Designer.cs new file mode 100644 index 0000000..4036d04 --- /dev/null +++ b/Warship/Warship/FormMapWithSetWarship.Designer.cs @@ -0,0 +1,233 @@ +namespace AircraftCarrier +{ + partial class FormMapWithSetWarship + { + /// + /// 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() + { + this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonRemoveTruck = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonAddTruck = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxTools + // + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonRemoveTruck); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonAddTruck); + this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(927, 0); + this.groupBoxTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxTools.Size = new System.Drawing.Size(233, 739); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 221); + this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(199, 27); + this.maskedTextBoxPosition.TabIndex = 2; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRemoveTruck + // + this.buttonRemoveTruck.Location = new System.Drawing.Point(19, 260); + this.buttonRemoveTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRemoveTruck.Name = "buttonRemoveWarship"; + this.buttonRemoveTruck.Size = new System.Drawing.Size(200, 47); + this.buttonRemoveTruck.TabIndex = 3; + this.buttonRemoveTruck.Text = "Удалить военный корбаль"; + this.buttonRemoveTruck.UseMnemonic = false; + this.buttonRemoveTruck.UseVisualStyleBackColor = true; + this.buttonRemoveTruck.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(19, 383); + this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(200, 47); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаbott; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(104, 672); + this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(34, 40); + this.buttonDown.TabIndex = 10; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаright; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(145, 672); + this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(34, 40); + this.buttonRight.TabIndex = 9; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелка; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(63, 672); + this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(34, 40); + this.buttonLeft.TabIndex = 8; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаtop; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(104, 624); + this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(34, 40); + this.buttonUp.TabIndex = 7; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(19, 521); + this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(200, 47); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonAddTruck + // + this.buttonAddTruck.Location = new System.Drawing.Point(19, 141); + this.buttonAddTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAddTruck.Name = "buttonAddTruck"; + this.buttonAddTruck.Size = new System.Drawing.Size(200, 47); + this.buttonAddTruck.TabIndex = 1; + this.buttonAddTruck.Text = "Добавить военный корабль"; + this.buttonAddTruck.UseVisualStyleBackColor = true; + this.buttonAddTruck.Click += new System.EventHandler(this.ButtonAddWarship_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Морская карта", + }); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(19, 43); + this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(199, 28); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // pictureBox + // + this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.pictureBox.Name = "pictureBox"; + this.pictureBox.Size = new System.Drawing.Size(927, 739); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetTrucks + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1160, 739); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Name = "FormMapWithSetTrucks"; + this.Text = "Карта с набором объектов"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxTools; + private PictureBox pictureBox; + private ComboBox comboBoxSelectorMap; + private Button buttonShowOnMap; + private Button buttonAddTruck; + private Button buttonDown; + private Button buttonRight; + private Button buttonLeft; + private Button buttonUp; + private Button buttonShowStorage; + private Button buttonRemoveTruck; + private MaskedTextBox maskedTextBoxPosition; + } +} \ No newline at end of file diff --git a/Warship/Warship/FormMapWithSetWarship.cs b/Warship/Warship/FormMapWithSetWarship.cs new file mode 100644 index 0000000..fc11391 --- /dev/null +++ b/Warship/Warship/FormMapWithSetWarship.cs @@ -0,0 +1,165 @@ +using AircraftCarrier; +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; +using static System.Windows.Forms.DataFormats; + +namespace AircraftCarrier +{ + public partial class FormMapWithSetWarship : Form + { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + /// + /// Конструктор + /// + public FormMapWithSetWarship() + { + InitializeComponent(); + } + /// + /// Выбор карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Морская карта": + map = new SeaMap(); + break; + } + if (map != null) + { + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric(pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddWarship_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + FormShip form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawingObjectWarship warship = new(form.SelectedWarship); + if (_mapWarshipsCollectionGeneric + warship != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveWarship_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapWarshipsCollectionGeneric - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction direction = Direction.None; + switch (name) + { + case "buttonUp": + direction = Direction.Up; + break; + case "buttonDown": + direction = Direction.Down; + break; + case "buttonLeft": + direction = Direction.Left; + break; + case "buttonRight": + direction = Direction.Right; + break; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.MoveObject(direction); + } + } + +} diff --git a/Warship/Warship/FormMap.resx b/Warship/Warship/FormMapWithSetWarship.resx similarity index 93% rename from Warship/Warship/FormMap.resx rename to Warship/Warship/FormMapWithSetWarship.resx index 5cb320f..f298a7b 100644 --- a/Warship/Warship/FormMap.resx +++ b/Warship/Warship/FormMapWithSetWarship.resx @@ -57,7 +57,4 @@ 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/Warship/Warship/FormShip.Designer.cs b/Warship/Warship/FormShip.Designer.cs index 4f9054e..2ebe797 100644 --- a/Warship/Warship/FormShip.Designer.cs +++ b/Warship/Warship/FormShip.Designer.cs @@ -39,6 +39,7 @@ this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.ButtonSelectWarship = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -153,11 +154,23 @@ this.buttonCreateModif.UseVisualStyleBackColor = true; this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); // + // ButtonSelectWarship + // + this.ButtonSelectWarship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonSelectWarship.Location = new System.Drawing.Point(548, 382); + this.ButtonSelectWarship.Name = "ButtonSelectWarship"; + this.ButtonSelectWarship.Size = new System.Drawing.Size(75, 23); + this.ButtonSelectWarship.TabIndex = 8; + this.ButtonSelectWarship.Text = "Выбрать"; + this.ButtonSelectWarship.UseVisualStyleBackColor = true; + this.ButtonSelectWarship.Click += new System.EventHandler(this.ButtonSelectWarship_Click_1); + // // FormShip // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonSelectWarship); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonUp); @@ -189,5 +202,6 @@ private Button buttonUp; private Button buttonRight; private Button buttonCreateModif; + private Button ButtonSelectWarship; } } \ No newline at end of file diff --git a/Warship/Warship/FormShip.cs b/Warship/Warship/FormShip.cs index 7bba65f..6b73375 100644 --- a/Warship/Warship/FormShip.cs +++ b/Warship/Warship/FormShip.cs @@ -3,6 +3,8 @@ namespace AircraftCarrier public partial class FormShip : Form { private DrawingWarship _ship; + + public DrawingWarship SelectedWarship { get; private set; } public FormShip() { InitializeComponent(); @@ -36,8 +38,13 @@ namespace AircraftCarrier private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - _ship = new DrawingWarship(rnd.Next(1000, 3000), rnd.Next(10000, 20000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _ship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), color); Setdata(); Draw(); } @@ -76,13 +83,33 @@ namespace AircraftCarrier private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - _ship = new DrawingPlaneWarship(rnd.Next(1000, 3000), rnd.Next(10000, 20000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0, 2))); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialogDop = new(); + if (dialogDop.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDop.Color; + } + _ship = new DrawingPlaneWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), color, dopColor, + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); Setdata(); Draw(); } + /// + /// "" + /// + /// + /// + private void ButtonSelectWarship_Click_1(object sender, EventArgs e) + { + SelectedWarship = _ship; + DialogResult = DialogResult.OK; + } } } diff --git a/Warship/Warship/IDrawingObject.cs b/Warship/Warship/IDrawingObject.cs index 284bbbb..7e90bf8 100644 --- a/Warship/Warship/IDrawingObject.cs +++ b/Warship/Warship/IDrawingObject.cs @@ -37,7 +37,6 @@ namespace AircraftCarrier /// Получение текущей позиции объекта /// /// - (float Left, float Right, float Top, float Bottom) - GetCurrentPosition(); + (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); } } diff --git a/Warship/Warship/MapWithSetWarshipGeneric.cs b/Warship/Warship/MapWithSetWarshipGeneric.cs new file mode 100644 index 0000000..1afb6dc --- /dev/null +++ b/Warship/Warship/MapWithSetWarshipGeneric.cs @@ -0,0 +1,186 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + /// + /// Карта с набром объектов под нее + /// + /// + /// + internal class MapWithSetWarshipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 260; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 137; + /// + /// Набор объектов + /// + private readonly SetWarshipsGeneric _setWarships; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setWarships = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(MapWithSetWarshipsGeneric map, T warship) + { + return map._setWarships.Insert(warship); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetWarshipsGeneric map, int position) + { + return map._setWarships.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawWarship(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + { + Shaking(); + for (int i = 0; i < _setWarships.Count; i++) + { + var warship = _setWarships.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + return new(_pictureWidth, _pictureHeight); + } + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + private void Shaking() + { + int j = _setWarships.Count - 1; + for (int i = 0; i < _setWarships.Count; i++) + { + if (_setWarships.Get(i) == null) + { + for (; j > i; j--) + { + var warship = _setWarships.Get(j); + if (warship != null) + { + _setWarships.Insert(warship, i); + _setWarships.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.DimGray, 3); + g.FillRectangle(Brushes.Aquamarine, 0, 0, _pictureWidth, _pictureHeight); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2); + g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2); + g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth + 20, _pictureHeight / _placeSizeHeight * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), _pictureHeight / _placeSizeHeight * _placeSizeHeight + 2); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawWarship(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setWarships.Count; i++) + { + if (_setWarships.Get(i) != null) + { + _setWarships.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 25, _pictureWidth, _pictureHeight); + _setWarships.Get(i)?.DrawningObject(g); + } + } + } + } +} diff --git a/Warship/Warship/Program.cs b/Warship/Warship/Program.cs index 4080ebd..6a804d0 100644 --- a/Warship/Warship/Program.cs +++ b/Warship/Warship/Program.cs @@ -11,7 +11,7 @@ namespace AircraftCarrier // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetWarship()); } } } \ No newline at end of file diff --git a/Warship/Warship/SetWarshipGeneric.cs b/Warship/Warship/SetWarshipGeneric.cs new file mode 100644 index 0000000..f80b510 --- /dev/null +++ b/Warship/Warship/SetWarshipGeneric.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetWarshipsGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + + /// + /// Конструктор + /// + /// + public SetWarshipsGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый корабль + /// + public int Insert(T ship) + { + bool CheckEmptySpace = false; + int indexOfEmptyPlace = 0; + for (int i = 0; i < Count; ++i) + { + if (_places[i] == null || _places[i] == default(T)) + { + CheckEmptySpace = true; + indexOfEmptyPlace = i; + break; + } + } + if (CheckEmptySpace) + { + for (int i = indexOfEmptyPlace; i > 0; --i) + { + _places[i] = _places[i - 1]; + } + _places[0] = ship; + return 0; + } + else + { + return -1; + + } + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый корабль + /// Позиция + /// + public int Insert(T ship, int position) + { + if (position < 0 || position >= Count) + { + return -1; + } + if (_places[position] == null || _places[position] == default(T)) + { + _places[position] = ship; + return position; + } + else + { + bool hasEmptySpace = false; + int indexOfEmptyPlace = 0; + for (int i = position + 1; i < Count; ++i) + { + if (_places[i] == null || _places[i] == default(T)) + { + hasEmptySpace = true; + indexOfEmptyPlace = i; + break; + } + } + if (hasEmptySpace) + { + for (int i = indexOfEmptyPlace; i > position; --i) + { + _places[i] = _places[i - 1]; + } + _places[position] = ship; + return position; + } + else + { + return -1; + } + } + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T Remove(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + T memoryObj = _places[position]; + _places[position] = null; + return memoryObj; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + + } + } +} + + + + +