diff --git a/AircraftCarrier/AircraftCarrier/AbstractMap.cs b/AircraftCarrier/AircraftCarrier/AbstractMap.cs index 2193c03..1b49cdc 100644 --- a/AircraftCarrier/AircraftCarrier/AbstractMap.cs +++ b/AircraftCarrier/AircraftCarrier/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AircraftCarrier { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -156,5 +156,27 @@ 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; + } } } diff --git a/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs b/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs index 2c74cc8..4d95da8 100644 --- a/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs +++ b/AircraftCarrier/AircraftCarrier/DrawingObjectWarship.cs @@ -16,6 +16,8 @@ namespace AircraftCarrier } public float Step => _warship?.Warship?.Step ?? 0; + public DrawingWarship GetWarship => _warship; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _warship?.GetCurrentPosition() ?? default; @@ -39,5 +41,33 @@ namespace AircraftCarrier void IDrawingObject.DrawningObject(Graphics g) => _warship.DrawTransport(g); public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawningWarship()); + + public bool Equals(IDrawingObject? other) + { + if (other is not DrawingObjectWarship otherWarship) + { + return false; + } + var warship = _warship.Warship; + var otherEntity = otherWarship._warship.Warship; + if (warship.GetType() != otherEntity.GetType() || + warship.Speed != otherEntity.Speed || + warship.Weight != otherEntity.Weight || + warship.BodyColor != otherEntity.BodyColor) + { + return false; + } + + if (warship is EntityAircraftCarrier entityAircraftCarrier && + otherEntity is EntityAircraftCarrier otherEntityAircraftCarrier && ( + entityAircraftCarrier.BodyKit != otherEntityAircraftCarrier.BodyKit || + entityAircraftCarrier.Сabin != otherEntityAircraftCarrier.Сabin || + entityAircraftCarrier.SuperEngine != otherEntityAircraftCarrier.SuperEngine || + entityAircraftCarrier.DopColor != otherEntityAircraftCarrier.DopColor)) + { + return false; + } + return true; + } } } diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs index bac9473..ec2b765 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.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.buttonAddMap = new System.Windows.Forms.Button(); 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.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowStorage); @@ -72,11 +76,31 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(580, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(217, 547); + this.groupBoxTools.Size = new System.Drawing.Size(217, 654); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Tools"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(20, 338); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(180, 35); + this.buttonSortByColor.TabIndex = 21; + this.buttonSortByColor.Text = "Sort by color"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(20, 297); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(180, 35); + this.buttonSortByType.TabIndex = 20; + this.buttonSortByType.Text = "Sort by type"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonAddMap); @@ -142,7 +166,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(20, 437); + this.buttonShowOnMap.Location = new System.Drawing.Point(20, 544); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(180, 35); this.buttonShowOnMap.TabIndex = 18; @@ -152,7 +176,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(20, 396); + this.buttonShowStorage.Location = new System.Drawing.Point(20, 503); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(180, 35); this.buttonShowStorage.TabIndex = 17; @@ -162,7 +186,7 @@ // // buttonRemoveWarship // - this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 355); + this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 462); this.buttonRemoveWarship.Name = "buttonRemoveWarship"; this.buttonRemoveWarship.Size = new System.Drawing.Size(180, 35); this.buttonRemoveWarship.TabIndex = 16; @@ -172,7 +196,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 326); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 433); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(180, 23); @@ -181,7 +205,7 @@ // // buttonAddWarship // - this.buttonAddWarship.Location = new System.Drawing.Point(20, 285); + this.buttonAddWarship.Location = new System.Drawing.Point(20, 392); this.buttonAddWarship.Name = "buttonAddWarship"; this.buttonAddWarship.Size = new System.Drawing.Size(180, 35); this.buttonAddWarship.TabIndex = 14; @@ -194,7 +218,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.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(126, 516); + this.buttonRight.Location = new System.Drawing.Point(126, 623); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 13; @@ -207,7 +231,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.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(54, 516); + this.buttonLeft.Location = new System.Drawing.Point(54, 623); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 12; @@ -220,7 +244,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.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(90, 480); + this.buttonUp.Location = new System.Drawing.Point(90, 587); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 11; @@ -233,7 +257,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.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(90, 516); + this.buttonDown.Location = new System.Drawing.Point(90, 623); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -246,7 +270,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(580, 547); + this.pictureBox.Size = new System.Drawing.Size(580, 654); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -294,7 +318,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(797, 571); + this.ClientSize = new System.Drawing.Size(797, 678); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -338,5 +362,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/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs index 9b0dcc5..d193266 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -298,5 +298,19 @@ 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/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.resx b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.resx index 934ed35..706a0f1 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.resx +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.resx @@ -66,4 +66,7 @@ 265, 17 + + 44 + \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/IDrawingObject.cs b/AircraftCarrier/AircraftCarrier/IDrawingObject.cs index 1196b5d..1d26700 100644 --- a/AircraftCarrier/AircraftCarrier/IDrawingObject.cs +++ b/AircraftCarrier/AircraftCarrier/IDrawingObject.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs index 45b41d5..8c43a10 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -12,7 +12,7 @@ namespace AircraftCarrier /// /// internal class MapWithSetWarshipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// @@ -138,6 +138,14 @@ namespace AircraftCarrier } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setWarships.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs index b896fc5..8cc5e9e 100644 --- a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs @@ -11,7 +11,7 @@ namespace AircraftCarrier /// /// internal class SetWarshipsGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -49,6 +49,9 @@ namespace AircraftCarrier /// public int Insert(T warship, int position) { + if (_places.Contains(warship)) + return -1; + if (Count == _maxCount) throw new StorageOverflowException(_maxCount); @@ -111,5 +114,17 @@ namespace AircraftCarrier } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } diff --git a/AircraftCarrier/AircraftCarrier/WarshipCompareByColor.cs b/AircraftCarrier/AircraftCarrier/WarshipCompareByColor.cs new file mode 100644 index 0000000..d252180 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/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.Warship; + var yEntity = yWarship.GetWarship.Warship; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || xEntity is not EntityAircraftCarrier xEntityAircraftCarrier || + yEntity is not EntityAircraftCarrier yEntityWarmlyShip) + { + return colorWeight; + } + return xEntityAircraftCarrier.DopColor.ToArgb().CompareTo(yEntityWarmlyShip.DopColor.ToArgb()); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/WarshipCompareByType.cs b/AircraftCarrier/AircraftCarrier/WarshipCompareByType.cs new file mode 100644 index 0000000..449f77f --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/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.Warship.Speed.CompareTo(yWarship.GetWarship.Warship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xWarship.GetWarship.Warship.Weight.CompareTo(yWarship.GetWarship.Warship.Weight); + } + } +}