diff --git a/AirFighter/AirFighter/AbstractMap.cs b/AirFighter/AirFighter/AbstractMap.cs index 21a9d6c..67f3ca5 100644 --- a/AirFighter/AirFighter/AbstractMap.cs +++ b/AirFighter/AirFighter/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirFighter { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -154,5 +154,24 @@ namespace AirFighter 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 || _height != other._height || _width != other._width || _size_x != other._size_x || _size_y != other._size_y) + { + 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; + } } } diff --git a/AirFighter/AirFighter/AirFighterCompareByColor.cs b/AirFighter/AirFighter/AirFighterCompareByColor.cs new file mode 100644 index 0000000..8300728 --- /dev/null +++ b/AirFighter/AirFighter/AirFighterCompareByColor.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class AirFighterCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAirFighter = x as DrawningObjectAirFighter; + var yAirFighter = y as DrawningObjectAirFighter; + if (xAirFighter == null && yAirFighter == null) + { + return 0; + } + if (xAirFighter == null && yAirFighter != null) + { + return 1; + } + if (xAirFighter != null && yAirFighter == null) + { + return -1; + } + + var xEntityAirFighter = xAirFighter.GetAirFighter.AirFighter; + var yEntityAirFighter = yAirFighter.GetAirFighter.AirFighter; + var colorCompare = xEntityAirFighter.BodyColor.ToArgb().CompareTo(yEntityAirFighter.BodyColor.ToArgb()); + + if (colorCompare != 0) + { + return colorCompare; + } + if(xEntityAirFighter is EntityUpgradeAirFighter xUpgradeAirFighter && yEntityAirFighter is EntityUpgradeAirFighter yUpgradeAirFighter) + { + var dopColorCompare = xUpgradeAirFighter.DopColor.ToArgb().CompareTo(yUpgradeAirFighter.DopColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + + var speedCompare = xAirFighter.GetAirFighter.AirFighter.Speed.CompareTo(yAirFighter.GetAirFighter.AirFighter.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirFighter.GetAirFighter.AirFighter.Weight.CompareTo(yAirFighter.GetAirFighter.AirFighter.Weight); + } + } +} diff --git a/AirFighter/AirFighter/AirFighterCompareByType.cs b/AirFighter/AirFighter/AirFighterCompareByType.cs new file mode 100644 index 0000000..662dd35 --- /dev/null +++ b/AirFighter/AirFighter/AirFighterCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class AirFighterCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAirFighter = x as DrawningObjectAirFighter; + var yAirFighter = y as DrawningObjectAirFighter; + if (xAirFighter == null && yAirFighter == null) + { + return 0; + } + if (xAirFighter == null && yAirFighter != null) + { + return 1; + } + if (xAirFighter != null && yAirFighter == null) + { + return -1; + } + if (xAirFighter.GetAirFighter.GetType().Name != yAirFighter.GetAirFighter.GetType().Name) + { + if (xAirFighter.GetAirFighter.GetType().Name == "DrawningAirFighter") + { + return -1; + } + return 1; + } + var speedCompare = xAirFighter.GetAirFighter.AirFighter.Speed.CompareTo(yAirFighter.GetAirFighter.AirFighter.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirFighter.GetAirFighter.AirFighter.Weight.CompareTo(yAirFighter.GetAirFighter.AirFighter.Weight); + } + } +} diff --git a/AirFighter/AirFighter/DrawningObjectAirFighter.cs b/AirFighter/AirFighter/DrawningObjectAirFighter.cs index 4b73d56..99fe7a6 100644 --- a/AirFighter/AirFighter/DrawningObjectAirFighter.cs +++ b/AirFighter/AirFighter/DrawningObjectAirFighter.cs @@ -10,6 +10,7 @@ namespace AirFighter { private DrawningAirFighter _airFighter = null; public float Step => _airFighter?.AirFighter.Step ?? 0; + public DrawningAirFighter GetAirFighter => _airFighter; public DrawningObjectAirFighter(DrawningAirFighter airFighter) { _airFighter = airFighter; @@ -38,5 +39,52 @@ namespace AirFighter public string GetInfo() => _airFighter?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectAirFighter(data.CreateDrawningCar()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherAirFighter = other as DrawningObjectAirFighter; + if (otherAirFighter == null) + { + return false; + } + var airFighter = _airFighter.AirFighter; + var otherAirFighterAirFighter = otherAirFighter._airFighter.AirFighter; + if (airFighter.Speed != otherAirFighterAirFighter.Speed) + { + return false; + } + if (airFighter.Weight != otherAirFighterAirFighter.Weight) + { + return false; + } + if (airFighter.BodyColor != otherAirFighterAirFighter.BodyColor) + { + return false; + } + if(airFighter is EntityUpgradeAirFighter upgradeAirFighter && otherAirFighterAirFighter is EntityUpgradeAirFighter upgradeOtherAirFighter) + { + if(upgradeAirFighter.DopWing != upgradeOtherAirFighter.DopWing) + { + return false; + } + if(upgradeAirFighter.Rocket != upgradeAirFighter.Rocket) + { + return false; + } + if(upgradeAirFighter.DopColor != upgradeOtherAirFighter.DopColor) + { + return false; + } + }else if(airFighter is EntityUpgradeAirFighter || otherAirFighterAirFighter is EntityUpgradeAirFighter) + { + return false; + } + return true; + + } } } diff --git a/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs b/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs index d286579..7f9243c 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.Designer.cs @@ -51,6 +51,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.groupBox.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBox // + this.groupBox.Controls.Add(this.buttonSortByColor); + this.groupBox.Controls.Add(this.buttonSortByType); this.groupBox.Controls.Add(this.groupBoxMaps); this.groupBox.Controls.Add(this.buttonUp); this.groupBox.Controls.Add(this.buttonDown); @@ -72,7 +76,7 @@ this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox.Location = new System.Drawing.Point(732, 28); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(250, 561); + this.groupBox.Size = new System.Drawing.Size(250, 638); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; @@ -145,7 +149,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirFighter.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(135, 481); + this.buttonUp.Location = new System.Drawing.Point(135, 558); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 2; @@ -157,7 +161,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirFighter.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(135, 519); + this.buttonDown.Location = new System.Drawing.Point(135, 596); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 3; @@ -169,7 +173,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirFighter.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(99, 519); + this.buttonLeft.Location = new System.Drawing.Point(99, 596); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 4; @@ -181,7 +185,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirFighter.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(171, 519); + this.buttonRight.Location = new System.Drawing.Point(171, 596); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 5; @@ -190,7 +194,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(28, 443); + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 522); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(210, 29); this.buttonShowOnMap.TabIndex = 5; @@ -200,7 +204,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(28, 408); + this.buttonShowStorage.Location = new System.Drawing.Point(17, 487); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(210, 29); this.buttonShowStorage.TabIndex = 4; @@ -210,7 +214,7 @@ // // buttonRemoveAirFighter // - this.buttonRemoveAirFighter.Location = new System.Drawing.Point(28, 373); + this.buttonRemoveAirFighter.Location = new System.Drawing.Point(17, 452); this.buttonRemoveAirFighter.Name = "buttonRemoveAirFighter"; this.buttonRemoveAirFighter.Size = new System.Drawing.Size(210, 29); this.buttonRemoveAirFighter.TabIndex = 3; @@ -220,7 +224,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 340); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 419); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(210, 27); @@ -228,7 +232,7 @@ // // buttonAddAirFighter // - this.buttonAddAirFighter.Location = new System.Drawing.Point(28, 296); + this.buttonAddAirFighter.Location = new System.Drawing.Point(17, 375); this.buttonAddAirFighter.Name = "buttonAddAirFighter"; this.buttonAddAirFighter.Size = new System.Drawing.Size(210, 29); this.buttonAddAirFighter.TabIndex = 1; @@ -241,7 +245,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 28); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(732, 561); + this.pictureBox.Size = new System.Drawing.Size(732, 638); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -267,14 +271,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); // @@ -286,11 +290,31 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(17, 296); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(210, 29); + this.buttonSortByType.TabIndex = 7; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(17, 331); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(210, 29); + this.buttonSortByColor.TabIndex = 8; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetAirFighters // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(982, 589); + this.ClientSize = new System.Drawing.Size(982, 666); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Controls.Add(this.menuStrip); @@ -334,5 +358,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/AirFighter/AirFighter/FormMapWithSetAirFighters.cs b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs index fb68958..0b6d067 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs @@ -316,5 +316,37 @@ namespace AirFighter } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new AirFighterCompareByType()); + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? + string.Empty].Sort(new AirFighterCompareByColor()); + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/AirFighter/AirFighter/FormMapWithSetAirFighters.resx b/AirFighter/AirFighter/FormMapWithSetAirFighters.resx index 985a3e4..77f1fe0 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.resx +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.resx @@ -66,4 +66,7 @@ 311, 17 + + 25 + \ No newline at end of file diff --git a/AirFighter/AirFighter/IDrawningObject.cs b/AirFighter/AirFighter/IDrawningObject.cs index 5f09481..5618dcd 100644 --- a/AirFighter/AirFighter/IDrawningObject.cs +++ b/AirFighter/AirFighter/IDrawningObject.cs @@ -9,7 +9,7 @@ namespace AirFighter /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs b/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs index 47afcee..4bba597 100644 --- a/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/MapWithSetAirFightersGeneric.cs @@ -12,7 +12,7 @@ namespace AirFighter /// /// internal class MapWithSetAirFightersGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -139,6 +139,14 @@ namespace AirFighter } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setAirFighters.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/AirFighter/AirFighter/SetAirFightersGeneric.cs b/AirFighter/AirFighter/SetAirFightersGeneric.cs index fbc5f64..1908172 100644 --- a/AirFighter/AirFighter/SetAirFightersGeneric.cs +++ b/AirFighter/AirFighter/SetAirFightersGeneric.cs @@ -12,7 +12,7 @@ namespace AirFighter /// /// internal class SetAirFightersGeneric - where T: class + where T: class, IEquatable { /// /// Список объектов, которые храним @@ -52,6 +52,10 @@ namespace AirFighter /// public int Insert(T airFighter, int position) { + if (_places.Contains(airFighter)) + { + throw new ArgumentException("Элемент с такими характеристиками существует в хранилище"); + } if (Count >= _maxCount) { throw new StorageOverflowException(); @@ -126,5 +130,17 @@ namespace AirFighter } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }