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 05fedb7..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); @@ -33,5 +34,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/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/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..d1d056c 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 { /// @@ -140,6 +140,14 @@ namespace AircraftCarrier } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setWarships.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() 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); + } } } 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); + } + } +}