From 9d3083ae94636a29974e4f263b1cf4f33cd16b7d 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: Thu, 22 Dec 2022 21:51:04 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Warship/Warship/DrawingObjectWarship.cs | 27 +++++++++++++++++++++ Warship/Warship/IDrawingObject.cs | 2 +- Warship/Warship/MapWithSetWarshipGeneric.cs | 2 +- Warship/Warship/SetWarshipGeneric.cs | 16 +++++++++++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Warship/Warship/DrawingObjectWarship.cs b/Warship/Warship/DrawingObjectWarship.cs index 05fedb7..c9e162c 100644 --- a/Warship/Warship/DrawingObjectWarship.cs +++ b/Warship/Warship/DrawingObjectWarship.cs @@ -33,5 +33,32 @@ namespace AircraftCarrier } public string GetInfo() => _warship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship()); + public bool Equals(IDrawingObject? other) + { + if (other is not DrawingObjectWarship otherWarship) + { + return false; + } + var warship = _warship.Ship; + var otherEntity = otherWarship._warship.Ship; + if (warship.GetType() != otherEntity.GetType() || + warship.Speed != otherEntity.Speed || + warship.Weight != otherEntity.Weight || + warship.BodyColor != otherEntity.BodyColor) + { + return false; + } + + if (warship is EntityPlaneWarship entityPlaneWarship && + otherEntity is EntityPlaneWarship otherEntityPlaneWarship && ( + entityPlaneWarship.BodyKit != otherEntityPlaneWarship.BodyKit || + entityPlaneWarship.СontrolPlace != otherEntityPlaneWarship.СontrolPlace || + entityPlaneWarship.RunWay != otherEntityPlaneWarship.RunWay || + entityPlaneWarship.DopColor != otherEntityPlaneWarship.DopColor)) + { + return false; + } + return true; + } } } diff --git a/Warship/Warship/IDrawingObject.cs b/Warship/Warship/IDrawingObject.cs index 71b9ea5..2ea3196 100644 --- a/Warship/Warship/IDrawingObject.cs +++ b/Warship/Warship/IDrawingObject.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Warship/Warship/MapWithSetWarshipGeneric.cs b/Warship/Warship/MapWithSetWarshipGeneric.cs index 0c9b274..de34607 100644 --- a/Warship/Warship/MapWithSetWarshipGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipGeneric.cs @@ -12,7 +12,7 @@ namespace AircraftCarrier /// /// internal class MapWithSetWarshipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// diff --git a/Warship/Warship/SetWarshipGeneric.cs b/Warship/Warship/SetWarshipGeneric.cs index e52c7d7..8c5ca5a 100644 --- a/Warship/Warship/SetWarshipGeneric.cs +++ b/Warship/Warship/SetWarshipGeneric.cs @@ -11,7 +11,7 @@ namespace AircraftCarrier /// /// internal class SetWarshipsGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -53,6 +53,8 @@ namespace AircraftCarrier /// public int Insert(T ship, int position) { + if (_places.Contains(ship)) + return -1; if (Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!isCorrectPosition(position)) @@ -107,6 +109,18 @@ namespace AircraftCarrier } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } -- 2.25.1 From 4c5babaa515d7ac01a752a8c7ce88484d96e6e77 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: Thu, 22 Dec 2022 22:08:03 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A1=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0.=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20AbstractMap.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Warship/Warship/AbstractMap.cs | 23 +++++++- Warship/Warship/DrawingObjectWarship.cs | 1 + .../Warship/FormMapWithSetWarship.Designer.cs | 54 ++++++++++++++----- Warship/Warship/FormMapWithSetWarship.cs | 18 +++++++ Warship/Warship/MapWithSetWarshipGeneric.cs | 8 +++ Warship/Warship/WarshipCompareByColor.cs | 38 +++++++++++++ Warship/Warship/WarshipCompareByType.cs | 43 +++++++++++++++ 7 files changed, 170 insertions(+), 15 deletions(-) create mode 100644 Warship/Warship/WarshipCompareByColor.cs create mode 100644 Warship/Warship/WarshipCompareByType.cs diff --git a/Warship/Warship/AbstractMap.cs b/Warship/Warship/AbstractMap.cs index 18b1268..bf25547 100644 --- a/Warship/Warship/AbstractMap.cs +++ b/Warship/Warship/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AircraftCarrier { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObjectWarship = null; protected int[,] _map = null; @@ -146,5 +146,26 @@ namespace AircraftCarrier protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); + public bool Equals(AbstractMap? other) + { + if (other == null || _map != other._map || _width != other._width || + _size_x != other._size_x || _size_y != other._size_y || _height != other._height || + GetType() != other.GetType() || _map.GetLength(0) != other._map.GetLength(0) || + _map.GetLength(1) != other._map.GetLength(1)) + { + return false; + } + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] != other._map[i, j]) + { + return false; + } + } + } + return true; + } } } \ No newline at end of file diff --git a/Warship/Warship/DrawingObjectWarship.cs b/Warship/Warship/DrawingObjectWarship.cs index c9e162c..193641e 100644 --- a/Warship/Warship/DrawingObjectWarship.cs +++ b/Warship/Warship/DrawingObjectWarship.cs @@ -15,6 +15,7 @@ namespace AircraftCarrier _warship = warship; } public float Step => _warship?.Ship?.Step ?? 0; + public DrawingWarship GetWarship => _warship; public void DrawningObject(Graphics g) { _warship.DrawTransport(g); diff --git a/Warship/Warship/FormMapWithSetWarship.Designer.cs b/Warship/Warship/FormMapWithSetWarship.Designer.cs index cd7ac9d..2777824 100644 --- a/Warship/Warship/FormMapWithSetWarship.Designer.cs +++ b/Warship/Warship/FormMapWithSetWarship.Designer.cs @@ -25,6 +25,8 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonAddMap = new System.Windows.Forms.Button(); this.buttonDeleteMap = new System.Windows.Forms.Button(); @@ -55,6 +57,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByType); + this.groupBoxTools.Controls.Add(this.buttonSortByColor); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowStorage); @@ -68,11 +72,31 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(841, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(217, 650); + this.groupBoxTools.Size = new System.Drawing.Size(217, 683); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(20, 285); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(180, 35); + this.buttonSortByType.TabIndex = 21; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(20, 326); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(180, 35); + this.buttonSortByColor.TabIndex = 20; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonAddMap); @@ -139,7 +163,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(20, 489); + this.buttonShowOnMap.Location = new System.Drawing.Point(20, 531); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(180, 35); this.buttonShowOnMap.TabIndex = 18; @@ -149,7 +173,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(20, 448); + this.buttonShowStorage.Location = new System.Drawing.Point(20, 490); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(180, 35); this.buttonShowStorage.TabIndex = 17; @@ -159,7 +183,7 @@ // // buttonRemoveWarship // - this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 407); + this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 449); this.buttonRemoveWarship.Name = "buttonRemoveWarship"; this.buttonRemoveWarship.Size = new System.Drawing.Size(180, 35); this.buttonRemoveWarship.TabIndex = 16; @@ -169,7 +193,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 378); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 420); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(180, 23); @@ -178,7 +202,7 @@ // // buttonAddWarship // - this.buttonAddWarship.Location = new System.Drawing.Point(20, 337); + this.buttonAddWarship.Location = new System.Drawing.Point(20, 379); this.buttonAddWarship.Name = "buttonAddWarship"; this.buttonAddWarship.Size = new System.Drawing.Size(180, 35); this.buttonAddWarship.TabIndex = 14; @@ -191,7 +215,7 @@ 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(126, 610); + this.buttonRight.Location = new System.Drawing.Point(126, 643); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 13; @@ -204,7 +228,7 @@ 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(54, 610); + this.buttonLeft.Location = new System.Drawing.Point(54, 643); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 12; @@ -217,7 +241,7 @@ 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(90, 574); + this.buttonUp.Location = new System.Drawing.Point(90, 607); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 11; @@ -230,7 +254,7 @@ 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(90, 610); + this.buttonDown.Location = new System.Drawing.Point(90, 643); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -243,7 +267,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(841, 650); + this.pictureBox.Size = new System.Drawing.Size(841, 683); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -268,14 +292,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(144, 22); this.SaveToolStripMenuItem.Text = "Сохранение "; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(144, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -291,7 +315,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1058, 674); + this.ClientSize = new System.Drawing.Size(1058, 707); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -335,5 +359,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/Warship/Warship/FormMapWithSetWarship.cs b/Warship/Warship/FormMapWithSetWarship.cs index 2f22969..89fb159 100644 --- a/Warship/Warship/FormMapWithSetWarship.cs +++ b/Warship/Warship/FormMapWithSetWarship.cs @@ -306,7 +306,25 @@ namespace AircraftCarrier } } } + /// + /// Сортировка по типу и цвету + /// + /// + /// + private void Sorting(IComparer comparer) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + private void ButtonSortByType_Click(object sender, EventArgs e) => Sorting(new WarshipCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => Sorting(new WarshipCompareByColor()); } } + + diff --git a/Warship/Warship/MapWithSetWarshipGeneric.cs b/Warship/Warship/MapWithSetWarshipGeneric.cs index de34607..d1d056c 100644 --- a/Warship/Warship/MapWithSetWarshipGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipGeneric.cs @@ -140,6 +140,14 @@ namespace AircraftCarrier } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setWarships.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/Warship/Warship/WarshipCompareByColor.cs b/Warship/Warship/WarshipCompareByColor.cs new file mode 100644 index 0000000..44c4dd3 --- /dev/null +++ b/Warship/Warship/WarshipCompareByColor.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class WarshipCompareByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xWarship = x as DrawingObjectWarship; + var yWarship = y as DrawingObjectWarship; + if (xWarship == yWarship) + { + return 0; + } + if (xWarship == null) + { + return 1; + } + if (yWarship == null) + { + return -1; + } + var xEntity = xWarship.GetWarship.Ship; + var yEntity = yWarship.GetWarship.Ship; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || xEntity is not EntityPlaneWarship xEntityPlaneWarship || + yEntity is not EntityPlaneWarship yEntityWarmlyShip) + { + return colorWeight; + } + return xEntityPlaneWarship.DopColor.ToArgb().CompareTo(yEntityWarmlyShip.DopColor.ToArgb()); + } + } +} diff --git a/Warship/Warship/WarshipCompareByType.cs b/Warship/Warship/WarshipCompareByType.cs new file mode 100644 index 0000000..59a29ce --- /dev/null +++ b/Warship/Warship/WarshipCompareByType.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + internal class WarshipCompareByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xWarship = x as DrawingObjectWarship; + var yWarship = y as DrawingObjectWarship; + if (xWarship == yWarship) + { + return 0; + } + if (xWarship == null) + { + return 1; + } + if (yWarship == null) + { + return -1; + } + if (xWarship.GetWarship.GetType().Name != yWarship.GetWarship.GetType().Name) + { + if (xWarship.GetWarship.GetType() == typeof(DrawingWarship)) + { + return -1; + } + return 1; + } + var speedCompare = xWarship.GetWarship.Ship.Speed.CompareTo(yWarship.GetWarship.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xWarship.GetWarship.Ship.Weight.CompareTo(yWarship.GetWarship.Ship.Weight); + } + } +} -- 2.25.1