diff --git a/Liner/Liner/AbstractMap.cs b/Liner/Liner/AbstractMap.cs index faa99df..21ceac2 100644 --- a/Liner/Liner/AbstractMap.cs +++ b/Liner/Liner/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Liner { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawningObject = null; protected int[,] _map = null; @@ -139,5 +139,45 @@ namespace Liner 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; + } + var otherMap = other as AbstractMap; + if(otherMap == null) + { + return false; + } + if(_width != otherMap._width) + { + return false; + } + if(_height != otherMap._height) + { + return false; + } + if (_size_x != otherMap._size_x) + { + return false; + } + if (_size_y != otherMap._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] != otherMap._map[i, j]) + { + return false; + } + } + } + return true; + } } } diff --git a/Liner/Liner/DrawingObjectShip.cs b/Liner/Liner/DrawingObjectShip.cs index 8a66da9..ecf33b0 100644 --- a/Liner/Liner/DrawingObjectShip.cs +++ b/Liner/Liner/DrawingObjectShip.cs @@ -18,6 +18,7 @@ namespace Liner { return _ship?.GetCurrentPosition() ?? default; } + public DrawingShip GetShip => _ship; public void MoveObject(Direction direction) { _ship?.MoveTransport(direction); @@ -32,5 +33,54 @@ namespace Liner } public string GetInfo() => _ship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectShip(data.CreateDrawingShip()); + + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherShip = other as DrawingObjectShip; + if (otherShip == null) + { + return false; + } + var ship = _ship.Ship; + var otherShipShip = otherShip._ship.Ship; + if (ship.GetType().Name != otherShipShip.GetType().Name) + { + return false; + } + if (ship.Speed != otherShipShip.Speed) + { + return false; + } + if (ship.Weight != otherShipShip.Weight) + { + return false; + } + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + if (ship is EntityLiner liner && otherShipShip is EntityLiner otherLiner) + { + if (liner.DopColor != otherLiner.DopColor) + { + return false; + } + if (liner.Pool != otherLiner.Pool) + { + return false; + } + if (liner.DopDeck != otherLiner.DopDeck) + { + return false; + } + } + return true; + } } + } + diff --git a/Liner/Liner/FormMapWithSetShips.Designer.cs b/Liner/Liner/FormMapWithSetShips.Designer.cs index 12a5ba7..c9ad1d8 100644 --- a/Liner/Liner/FormMapWithSetShips.Designer.cs +++ b/Liner/Liner/FormMapWithSetShips.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.buttonSortType = new System.Windows.Forms.Button(); + this.buttonSortColor = new System.Windows.Forms.Button(); this.groupBox.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBox // + this.groupBox.Controls.Add(this.buttonSortColor); + this.groupBox.Controls.Add(this.buttonSortType); this.groupBox.Controls.Add(this.groupBox1); this.groupBox.Controls.Add(this.buttonRight); 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(696, 28); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(279, 640); + this.groupBox.Size = new System.Drawing.Size(279, 746); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; @@ -146,7 +150,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Liner.Properties.Resources._3; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(233, 598); + this.buttonRight.Location = new System.Drawing.Point(233, 704); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 17; @@ -158,7 +162,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Liner.Properties.Resources._2; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(197, 598); + this.buttonDown.Location = new System.Drawing.Point(197, 704); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 16; @@ -170,7 +174,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Liner.Properties.Resources._4; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(197, 562); + this.buttonUp.Location = new System.Drawing.Point(197, 668); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 15; @@ -182,7 +186,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Liner.Properties.Resources._1; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(161, 598); + this.buttonLeft.Location = new System.Drawing.Point(161, 704); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 14; @@ -191,7 +195,7 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 527); + this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 633); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; this.ButtonShowOnMap.Size = new System.Drawing.Size(222, 29); this.ButtonShowOnMap.TabIndex = 13; @@ -201,7 +205,7 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(16, 472); + this.ButtonShowStorage.Location = new System.Drawing.Point(16, 578); this.ButtonShowStorage.Name = "ButtonShowStorage"; this.ButtonShowStorage.Size = new System.Drawing.Size(222, 29); this.ButtonShowStorage.TabIndex = 12; @@ -211,7 +215,7 @@ // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 437); + this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 543); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; this.ButtonRemoveShip.Size = new System.Drawing.Size(222, 29); this.ButtonRemoveShip.TabIndex = 11; @@ -221,7 +225,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 404); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 510); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(222, 27); @@ -229,7 +233,7 @@ // // ButtonAddShip // - this.ButtonAddShip.Location = new System.Drawing.Point(16, 356); + this.ButtonAddShip.Location = new System.Drawing.Point(16, 462); this.ButtonAddShip.Name = "ButtonAddShip"; this.ButtonAddShip.Size = new System.Drawing.Size(222, 29); this.ButtonAddShip.TabIndex = 2; @@ -242,7 +246,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(696, 640); + this.pictureBox.Size = new System.Drawing.Size(696, 746); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -283,11 +287,31 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortType + // + this.buttonSortType.Location = new System.Drawing.Point(16, 355); + this.buttonSortType.Name = "buttonSortType"; + this.buttonSortType.Size = new System.Drawing.Size(222, 29); + this.buttonSortType.TabIndex = 19; + this.buttonSortType.Text = "Сортировать по типу"; + this.buttonSortType.UseVisualStyleBackColor = true; + this.buttonSortType.Click += new System.EventHandler(this.ButtonSortType_Click); + // + // buttonSortColor + // + this.buttonSortColor.Location = new System.Drawing.Point(16, 390); + this.buttonSortColor.Name = "buttonSortColor"; + this.buttonSortColor.Size = new System.Drawing.Size(222, 29); + this.buttonSortColor.TabIndex = 20; + this.buttonSortColor.Text = "Сортировать по цвету"; + this.buttonSortColor.UseVisualStyleBackColor = true; + this.buttonSortColor.Click += new System.EventHandler(this.ButtonSortColor_Click); + // // FormMapWithSetShips // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(975, 668); + this.ClientSize = new System.Drawing.Size(975, 774); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Controls.Add(this.menuStrip); @@ -331,5 +355,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button buttonSortColor; + private Button buttonSortType; } } \ No newline at end of file diff --git a/Liner/Liner/FormMapWithSetShips.cs b/Liner/Liner/FormMapWithSetShips.cs index bc0b972..c72d936 100644 --- a/Liner/Liner/FormMapWithSetShips.cs +++ b/Liner/Liner/FormMapWithSetShips.cs @@ -242,5 +242,23 @@ namespace Liner } } } + private void ButtonSortType_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ShipCompareByType()); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + private void ButtonSortColor_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ShipCompareByColor()); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/Liner/Liner/IDrawingObject.cs b/Liner/Liner/IDrawingObject.cs index 7422b43..5525b33 100644 --- a/Liner/Liner/IDrawingObject.cs +++ b/Liner/Liner/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Liner { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/Liner/Liner/MapWithSetShipsGeneric.cs b/Liner/Liner/MapWithSetShipsGeneric.cs index a17ce92..775c0d3 100644 --- a/Liner/Liner/MapWithSetShipsGeneric.cs +++ b/Liner/Liner/MapWithSetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Liner { internal class MapWithSetShipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -74,6 +74,10 @@ namespace Liner _setShips.Insert(DrawingObjectShip.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setShips.SortSet(comparer); + } private void Shaking() { int j = _setShips.Count - 1; diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 13a1be7..761a84b 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Liner { internal class SetShipsGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -23,6 +23,10 @@ namespace Liner } public int Insert(T ship, int position) { + if (_places.Contains(ship)) + { + return -1; + } if (Count == _maxCount) { throw new StorageOverflowException(_maxCount); @@ -75,5 +79,13 @@ namespace Liner } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } \ No newline at end of file diff --git a/Liner/Liner/ShipCompareByColor.cs b/Liner/Liner/ShipCompareByColor.cs new file mode 100644 index 0000000..b522de5 --- /dev/null +++ b/Liner/Liner/ShipCompareByColor.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner +{ + internal class ShipCompareByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xShip= x as DrawingObjectShip; + var yShip = y as DrawingObjectShip; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + string xShipColor = xShip.GetShip.Ship.BodyColor.Name; + string yShipColor = yShip.GetShip.Ship.BodyColor.Name; + if (xShipColor != yShipColor) + { + return xShipColor.CompareTo(yShipColor); + } + if (xShip.GetShip.GetType().Name != yShip.GetShip.GetType().Name) + { + if (xShip.GetShip.GetType().Name == "DrawingShip") + { + return -1; + } + return 1; + } + if (xShip.GetShip.Ship is EntityLiner xLiner && yShip.GetShip.Ship is EntityLiner yLiner) + { + string xShipDopColor = xLiner.DopColor.Name; + string yShipDopColor = yLiner.DopColor.Name; + var dopColorCompare = xShipDopColor.CompareTo(yShipDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight); + } + } +} diff --git a/Liner/Liner/ShipCompareByType.cs b/Liner/Liner/ShipCompareByType.cs new file mode 100644 index 0000000..cc25b69 --- /dev/null +++ b/Liner/Liner/ShipCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner +{ + internal class ShipCompareByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xShip = x as DrawingObjectShip; + var yShip = y as DrawingObjectShip; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + if (xShip.GetShip.GetType().Name != yShip.GetShip.GetType().Name) + { + if (xShip.GetShip.GetType().Name == "DrawingShip") + { + return -1; + } + return 1; + } + var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight); + } + } +}