From d998522e5602a9a9812cf7a21d098f0798b92d27 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?=
 =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= <xarlamov_2000@list.ru>
Date: Sat, 17 Dec 2022 19:27:39 +0400
Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=BD=D0=B5=D0=B4?=
 =?UTF-8?q?=D0=BE=D1=87=D1=91=D1=82=D0=BE=D0=B2=20=D0=B2=20=D1=84=D0=BE?=
 =?UTF-8?q?=D1=80=D0=BC=D0=B5.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Stormtrooper/Stormtrooper/AbstractMap.cs      | 47 +++++++++++++---
 .../Stormtrooper/DrawningObjectStorm.cs       | 33 +++++-------
 .../FormMapWithSetStormtroopers.Designer.cs   | 54 ++++++++++++++-----
 .../FormMapWithSetStormtroopers.cs            | 37 +++++++++++++
 .../MapWithSetStormtroopersGeneric.cs         | 10 +++-
 .../Stormtrooper/SetStormtroopersGeneric.cs   |  4 +-
 6 files changed, 140 insertions(+), 45 deletions(-)

diff --git a/Stormtrooper/Stormtrooper/AbstractMap.cs b/Stormtrooper/Stormtrooper/AbstractMap.cs
index f00311e..c268f52 100644
--- a/Stormtrooper/Stormtrooper/AbstractMap.cs
+++ b/Stormtrooper/Stormtrooper/AbstractMap.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Stormtrooper
 {
-    internal abstract class AbstractMap
+    internal abstract class AbstractMap : IEquatable<AbstractMap>
     {
         private IDrawningObject _drawningObject = null;
         protected int[,] _map = null;
@@ -43,9 +43,9 @@ namespace Stormtrooper
                 case Direction.Left:
                     for (int i = LefTopX; i >= Math.Abs(LefTopX - Convert.ToInt32(_drawningObject.Step / _size_x)); i--)
                     {
-                        for (int j = LefTopY; j <= objheigh && j<_map.GetLength(1); j++)
+                        for (int j = LefTopY; j <= objheigh && j < _map.GetLength(1); j++)
                         {
-                            
+
                             if (_map[i, j] == _barrier)
                             {
                                 CanStep = false;
@@ -67,7 +67,7 @@ namespace Stormtrooper
                             }
                         }
                     }
-                    
+
                     break;
 
                 case Direction.Down:
@@ -83,12 +83,12 @@ namespace Stormtrooper
                             }
                         }
                     }
-                    
+
                     break;
                 case Direction.Up:
-                    for (int i = LefTopX; i <= objwidth && i<_map.GetLength(0); i++)
+                    for (int i = LefTopX; i <= objwidth && i < _map.GetLength(0); i++)
                     {
-                        for (int j = LefTopY; j >= Math.Abs(LefTopY - Convert.ToInt32(_drawningObject.Step / _size_y)) ; j--)
+                        for (int j = LefTopY; j >= Math.Abs(LefTopY - Convert.ToInt32(_drawningObject.Step / _size_y)); j--)
                         {
                             if (_map[i, j] == _barrier)
                             {
@@ -142,7 +142,7 @@ namespace Stormtrooper
             Graphics gr = Graphics.FromImage(bmp);
             for (int i = 0; i < _map.GetLength(0); ++i)
                 for (int j = 0; j < _map.GetLength(1); ++j)
-                     DrawRoadPart(gr, i, j);
+                    DrawRoadPart(gr, i, j);
 
             for (int i = 0; i < _map.GetLength(0); ++i)
                 for (int j = 0; j < _map.GetLength(1); ++j)
@@ -154,5 +154,36 @@ namespace Stormtrooper
         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)
+            {
+                return false;
+            }
+            if (_width == other._width && _height == other._height && _size_x == other._size_x && _size_y == other._size_y)
+            {
+                if (_map == null && other._map == null)
+                {
+                    return true;
+                }
+                if (_map == null || other._map == null || _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;
+            }
+            return false;
+        }
     }
 }
diff --git a/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs b/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs
index 7ad63c4..79c33f6 100644
--- a/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs
+++ b/Stormtrooper/Stormtrooper/DrawningObjectStorm.cs
@@ -42,52 +42,45 @@ namespace Stormtrooper
             {
                 return false;
             }
-            var otherStorm = other as DrawningObjectStorm;
-            if (otherStorm == null)
+            var otherStormtrooper = other as DrawningObjectStorm;
+            if (otherStormtrooper == null)
             {
                 return false;
             }
-            var storm = _storm.Storm;
-            var otherStormStorm = otherStorm._storm.Storm;
-            if (storm.Speed != otherStormStorm.Speed)
+            var stormtrooper = _storm.Storm;
+            var otherStormtrooperStormtrooper = otherStormtrooper._storm.Storm;
+            if (stormtrooper.Speed != otherStormtrooperStormtrooper.Speed)
             {
                 return false;
             }
-            if (storm.Weight != otherStormStorm.Weight)
+            if (stormtrooper.Weight != otherStormtrooperStormtrooper.Weight)
             {
                 return false;
             }
-            if (storm.BodyColor != otherStormStorm.BodyColor)
+            if (stormtrooper.BodyColor != otherStormtrooperStormtrooper.BodyColor)
             {
                 return false;
             }
-            if (storm is EntityMilitaryStormtrooper milstorm)
+            if (stormtrooper is EntityMilitaryStormtrooper militaryStorm && otherStormtrooperStormtrooper is EntityMilitaryStormtrooper othermilitaryStorm)
             {
-                if (other is not EntityMilitaryStormtrooper othermilstorm)
+                if (militaryStorm.DopColor != othermilitaryStorm.DopColor)
                 {
                     return false;
                 }
-
-                if (milstorm.DopColor != othermilstorm.DopColor)
+                if (militaryStorm.BodyKit != othermilitaryStorm.BodyKit)
                 {
                     return false;
                 }
-
-                if (milstorm.BodyKit != othermilstorm.BodyKit)
+                if (militaryStorm.Rocket != othermilitaryStorm.Rocket)
                 {
                     return false;
                 }
-
-                if (milstorm.Rocket != othermilstorm.Rocket)
-                {
-                    return false;
-                }
-
-                if (milstorm.SportLine != othermilstorm.SportLine)
+                if (militaryStorm.SportLine != othermilitaryStorm.SportLine)
                 {
                     return false;
                 }
             }
+            else if (stormtrooper is EntityMilitaryStormtrooper || otherStormtrooperStormtrooper is EntityMilitaryStormtrooper) return false;
             return true;
         }
     }
diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs
index 28736b6..276c55f 100644
--- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs
+++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs
@@ -29,6 +29,8 @@
         private void InitializeComponent()
         {
             this.groupBoxTools = new System.Windows.Forms.GroupBox();
+            this.buttonSortByColor = new System.Windows.Forms.Button();
+            this.buttonSortByType = new System.Windows.Forms.Button();
             this.groupBoxMaps = new System.Windows.Forms.GroupBox();
             this.listBoxMaps = new System.Windows.Forms.ListBox();
             this.buttonDeleteMap = new System.Windows.Forms.Button();
@@ -59,6 +61,8 @@
             // 
             // groupBoxTools
             // 
+            this.groupBoxTools.Controls.Add(this.buttonSortByColor);
+            this.groupBoxTools.Controls.Add(this.buttonSortByType);
             this.groupBoxTools.Controls.Add(this.groupBoxMaps);
             this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
             this.groupBoxTools.Controls.Add(this.buttonRemoveCar);
@@ -74,11 +78,31 @@
             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, 856);
+            this.groupBoxTools.Size = new System.Drawing.Size(233, 930);
             this.groupBoxTools.TabIndex = 0;
             this.groupBoxTools.TabStop = false;
             this.groupBoxTools.Text = "Инструменты";
             // 
+            // buttonSortByColor
+            // 
+            this.buttonSortByColor.Location = new System.Drawing.Point(20, 446);
+            this.buttonSortByColor.Name = "buttonSortByColor";
+            this.buttonSortByColor.Size = new System.Drawing.Size(198, 50);
+            this.buttonSortByColor.TabIndex = 13;
+            this.buttonSortByColor.Text = "Сортировать по цвету";
+            this.buttonSortByColor.UseVisualStyleBackColor = true;
+            this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
+            // 
+            // buttonSortByType
+            // 
+            this.buttonSortByType.Location = new System.Drawing.Point(21, 395);
+            this.buttonSortByType.Name = "buttonSortByType";
+            this.buttonSortByType.Size = new System.Drawing.Size(198, 45);
+            this.buttonSortByType.TabIndex = 12;
+            this.buttonSortByType.Text = "Сортировать по типу";
+            this.buttonSortByType.UseVisualStyleBackColor = true;
+            this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
+            // 
             // groupBoxMaps
             // 
             this.groupBoxMaps.Controls.Add(this.listBoxMaps);
@@ -148,7 +172,7 @@
             // 
             // maskedTextBoxPosition
             // 
-            this.maskedTextBoxPosition.Location = new System.Drawing.Point(21, 511);
+            this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 571);
             this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
             this.maskedTextBoxPosition.Mask = "00";
             this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
@@ -158,7 +182,7 @@
             // 
             // buttonRemoveCar
             // 
-            this.buttonRemoveCar.Location = new System.Drawing.Point(21, 565);
+            this.buttonRemoveCar.Location = new System.Drawing.Point(20, 625);
             this.buttonRemoveCar.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
             this.buttonRemoveCar.Name = "buttonRemoveCar";
             this.buttonRemoveCar.Size = new System.Drawing.Size(200, 47);
@@ -169,7 +193,7 @@
             // 
             // buttonShowStorage
             // 
-            this.buttonShowStorage.Location = new System.Drawing.Point(21, 633);
+            this.buttonShowStorage.Location = new System.Drawing.Point(20, 693);
             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);
@@ -183,7 +207,7 @@
             this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
             this.buttonDown.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowDown;
             this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
-            this.buttonDown.Location = new System.Drawing.Point(104, 789);
+            this.buttonDown.Location = new System.Drawing.Point(104, 863);
             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);
@@ -196,7 +220,7 @@
             this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
             this.buttonRight.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowRight;
             this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
-            this.buttonRight.Location = new System.Drawing.Point(145, 789);
+            this.buttonRight.Location = new System.Drawing.Point(145, 863);
             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);
@@ -209,7 +233,7 @@
             this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
             this.buttonLeft.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowLeft;
             this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
-            this.buttonLeft.Location = new System.Drawing.Point(63, 789);
+            this.buttonLeft.Location = new System.Drawing.Point(63, 863);
             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);
@@ -222,7 +246,7 @@
             this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
             this.buttonUp.BackgroundImage = global::Stormtrooper.Properties.Resources.arrowUp;
             this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
-            this.buttonUp.Location = new System.Drawing.Point(104, 741);
+            this.buttonUp.Location = new System.Drawing.Point(104, 815);
             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);
@@ -232,7 +256,7 @@
             // 
             // buttonShowOnMap
             // 
-            this.buttonShowOnMap.Location = new System.Drawing.Point(21, 700);
+            this.buttonShowOnMap.Location = new System.Drawing.Point(20, 760);
             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);
@@ -243,7 +267,7 @@
             // 
             // buttonAddCar
             // 
-            this.buttonAddCar.Location = new System.Drawing.Point(21, 443);
+            this.buttonAddCar.Location = new System.Drawing.Point(20, 503);
             this.buttonAddCar.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
             this.buttonAddCar.Name = "buttonAddCar";
             this.buttonAddCar.Size = new System.Drawing.Size(200, 47);
@@ -258,7 +282,7 @@
             this.pictureBox.Location = new System.Drawing.Point(0, 28);
             this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
             this.pictureBox.Name = "pictureBox";
-            this.pictureBox.Size = new System.Drawing.Size(927, 856);
+            this.pictureBox.Size = new System.Drawing.Size(927, 930);
             this.pictureBox.TabIndex = 1;
             this.pictureBox.TabStop = false;
             // 
@@ -284,14 +308,14 @@
             // SaveToolStripMenuItem
             // 
             this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
-            this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
+            this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
             this.SaveToolStripMenuItem.Text = "Сохранение";
             this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
             // 
             // LoadToolStripMenuItem
             // 
             this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
-            this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
+            this.LoadToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
             this.LoadToolStripMenuItem.Text = "Загрузка";
             this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
             // 
@@ -307,7 +331,7 @@
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(1160, 884);
+            this.ClientSize = new System.Drawing.Size(1160, 958);
             this.Controls.Add(this.pictureBox);
             this.Controls.Add(this.groupBoxTools);
             this.Controls.Add(this.menuStrip);
@@ -351,5 +375,7 @@
         private ToolStripMenuItem LoadToolStripMenuItem;
         private OpenFileDialog openFileDialog;
         private SaveFileDialog saveFileDialog;
+        private Button buttonSortByColor;
+        private Button buttonSortByType;
     }
 }
\ No newline at end of file
diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs
index 590c049..fe68ca2 100644
--- a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs
+++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs
@@ -52,6 +52,10 @@ namespace Stormtrooper
         /// <param name="e"></param>
         private void ButtonAddStorm_Click(object sender, EventArgs e)
         {
+            if (listBoxMaps.SelectedIndex == -1)
+            {
+                return;
+            }
             var formStormConfig = new FormStormtrooperConfig();
             formStormConfig.AddEvent(AddStormtrooper);
             formStormConfig.Show();
@@ -75,6 +79,11 @@ namespace Stormtrooper
                 MessageBox.Show($"Не удалось добавить объект: {ex.Message}");
                 _logger.LogWarning($"Ошибка добавления объекта: {ex.Message}");
             }
+            catch (ArgumentException ex)
+            {
+                MessageBox.Show($"Ошибка добавления объекта: {ex.Message}");
+                _logger.LogWarning($"Ошибка добавления объекта: {ex.Message}");
+            }
             catch (Exception ex)
             {
                 MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
@@ -276,5 +285,33 @@ namespace Stormtrooper
                 }
             }
         }
+        /// <summary>
+        /// Сортировка по типу
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void buttonSortByType_Click(object sender, EventArgs e)
+        {
+            if (listBoxMaps.SelectedIndex == -1)
+            {
+                return;
+            }
+            _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new StormtrooperCompareByType());
+            pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
+        }
+        /// <summary>
+        /// Сортировка по цвету
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void buttonSortByColor_Click(object sender, EventArgs e)
+        {
+            if (listBoxMaps.SelectedIndex == -1)
+            {
+                return;
+            }
+            _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? String.Empty].Sort(new StormtrooperCompareByColor());
+            pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
+        }
     }
 }
diff --git a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs
index 75454c3..1aef13c 100644
--- a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs
+++ b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs
@@ -13,7 +13,7 @@ namespace Stormtrooper
     /// <typeparam name="T"></typeparam>
     /// <typeparam name="U"></typeparam>
     internal class MapWithSetStormtroopersGeneric<T, U>
-        where T : class, IDrawningObject
+        where T : class, IDrawningObject, IEquatable<T>
         where U : AbstractMap
     {
         /// <summary>
@@ -204,5 +204,13 @@ namespace Stormtrooper
             }
 
         }
+        /// <summary>
+        /// Сортировка
+        /// </summary>
+        /// <param name="comparer"></param>
+        public void Sort(IComparer<T> comparer)
+        {
+            _setStormtroopers.SortSet(comparer);
+        }
     }
 }
diff --git a/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs
index a746fd6..d8e3423 100644
--- a/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs
+++ b/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs
@@ -49,9 +49,9 @@ namespace Stormtrooper
         /// <returns></returns>
         public int Insert(T stormtrooper, int position)
         {
-            if (_places.Contains(stormtrooper))
+            foreach (var tec_storm in _places)
             {
-                throw new ArgumentException("Такой самолёт уже есть");
+                if ((tec_storm as DrawningObjectStorm).Equals(stormtrooper as DrawningObjectStorm)) throw new ArgumentException("Такой самолёт уже есть");
             }
             if (position < 0 || position >= _maxCount)
                 throw new StorageOverflowException(_maxCount);