diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/AbstractMap.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/AbstractMap.cs index 680ffb5..a9ee26a 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/AbstractMap.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -131,5 +131,45 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base 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/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/BoatCompareByColor.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/BoatCompareByColor.cs new file mode 100644 index 0000000..1b79e65 --- /dev/null +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/BoatCompareByColor.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base +{ + internal class BoatCompareByColor : 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 xBoat = x as DrawningObjectBoat; + var yBoat = y as DrawningObjectBoat; + if (xBoat == null && yBoat == null) + { + return 0; + } + if (xBoat == null && yBoat != null) + { + return 1; + } + if (xBoat != null && yBoat == null) + { + return -1; + } + var colorCompare = xBoat.GetBoat.Boat.BodyColor.Name.CompareTo(yBoat.GetBoat.Boat.BodyColor.Name); + if (colorCompare != 0) + { + return colorCompare; + } + var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight); + } + } +} diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/BoatCompareByType.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/BoatCompareByType.cs new file mode 100644 index 0000000..629a596 --- /dev/null +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/BoatCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base +{ + internal class BoatCompareByType : 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 xBoat = x as DrawningObjectBoat; + var yBoat = y as DrawningObjectBoat; + if (xBoat == null && yBoat == null) + { + return 0; + } + if (xBoat == null && yBoat != null) + { + return 1; + } + if (xBoat != null && yBoat == null) + { + return -1; + } + if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name) + { + if (xBoat.GetBoat.GetType().Name == "DrawningBoat") + { + return -1; + } + return 1; + } + var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight); + } + } +} diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs index 6dc06c4..a3aed9f 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/DrawningObjectBoat.cs @@ -14,6 +14,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base _boat = boat; } public float Step => _boat?.Boat?.Step ?? 0; + public DrawningBoat GetBoat => _boat; public void DrawningObject(Graphics g) { _boat?.DrawTransport(g); @@ -32,5 +33,55 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base _boat.SetPosition(x, y, width, height); } public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat()); + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherBoat = other as DrawningObjectBoat; + if (otherBoat == null) + { + return false; + } + var boat = _boat.Boat; + var otherBoatBoat = otherBoat._boat.Boat; + if (boat.GetType().Name != otherBoatBoat.GetType().Name) + { + return false; + } + if (boat.Speed != otherBoatBoat.Speed) + { + return false; + } + if (boat.Weight != otherBoatBoat.Weight) + { + return false; + } + if (boat.BodyColor != otherBoatBoat.BodyColor) + { + return false; + } + if (boat is EntitySpeedboat speedboat && otherBoatBoat is EntitySpeedboat otherSpeedboatSpeedboat) + { + if (speedboat.DopColor != otherSpeedboatSpeedboat.DopColor) + { + return false; + } + if (speedboat.BodyKit != otherSpeedboatSpeedboat.BodyKit) + { + return false; + } + if (speedboat.Wing != otherSpeedboatSpeedboat.Wing) + { + return false; + } + if (speedboat.SportLine != otherSpeedboatSpeedboat.SportLine) + { + return false; + } + } + return true; + } } } diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs index c4ab4c3..bdd7102 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.Designer.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.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.groupBoxMap = new System.Windows.Forms.GroupBox(); this.buttonDeleteMap = new System.Windows.Forms.Button(); this.listBoxMaps = new System.Windows.Forms.ListBox(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMap); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveBoat); @@ -72,11 +76,31 @@ this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(690, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 563); + this.groupBoxTools.Size = new System.Drawing.Size(204, 626); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(17, 308); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(175, 35); + 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(17, 267); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(175, 35); + this.buttonSortByType.TabIndex = 12; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // groupBoxMap // this.groupBoxMap.Controls.Add(this.buttonDeleteMap); @@ -142,7 +166,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 335); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 390); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -151,7 +175,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(17, 364); + this.buttonRemoveBoat.Location = new System.Drawing.Point(17, 419); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(175, 35); this.buttonRemoveBoat.TabIndex = 3; @@ -161,7 +185,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(17, 405); + this.buttonShowStorage.Location = new System.Drawing.Point(17, 460); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 4; @@ -174,7 +198,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 513); + this.buttonDown.Location = new System.Drawing.Point(91, 576); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -186,7 +210,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 513); + this.buttonRight.Location = new System.Drawing.Point(127, 576); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -198,7 +222,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 513); + this.buttonLeft.Location = new System.Drawing.Point(55, 576); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -210,7 +234,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 477); + this.buttonUp.Location = new System.Drawing.Point(91, 540); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -219,7 +243,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(17, 446); + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 501); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); this.buttonShowOnMap.TabIndex = 5; @@ -229,7 +253,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(17, 294); + this.buttonAddBoat.Location = new System.Drawing.Point(17, 349); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(175, 35); this.buttonAddBoat.TabIndex = 1; @@ -242,7 +266,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(690, 563); + this.pictureBox.Size = new System.Drawing.Size(690, 626); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -267,14 +291,14 @@ // SaveToolStripMenuItem // this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 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(141, 22); this.LoadToolStripMenuItem.Text = "Загрузка"; this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -290,7 +314,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(894, 587); + this.ClientSize = new System.Drawing.Size(894, 650); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); 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/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs index 34fcc89..52d86e0 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/FormMapWithSetBoats.cs @@ -294,5 +294,33 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base } } } + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByType()); + 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 BoatCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } \ No newline at end of file diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs index e2ce4e7..a1777cc 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs index 0f6e139..dc4cdea 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/MapWithSetBoatsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { internal class MapWithSetBoatsGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -134,6 +134,14 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base } } /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setBoats.SortSet(comparer); + } + /// /// "Взбалтываем" набор, чтобы все элементы оказались в начале /// private void Shaking() diff --git a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/SetBoatsGeneric.cs b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/SetBoatsGeneric.cs index 1c0f397..1d06735 100644 --- a/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/SetBoatsGeneric.cs +++ b/PIbd-22_Kalyshev_Y_V_MotorBoat_Base/SetBoatsGeneric.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base { internal class SetBoatsGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -36,6 +36,10 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base /// public int Insert(T boat) { + if (_places.Contains(boat)) + { + return -1; + } if (_places.Count < _maxCount) { _places.Add(boat); @@ -128,5 +132,17 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }