From a8f6a4ed8cabcd764bef3b47e5a31b8c7db8b7f4 Mon Sep 17 00:00:00 2001 From: "Valitov.Damir" Date: Sat, 22 Apr 2023 01:09:06 +0400 Subject: [PATCH] lab8 done --- Sailboat/AbstractMap.cs | 43 +++++++++++++- Sailboat/BoatCompareByColor.cs | 71 ++++++++++++++++++++++++ Sailboat/BoatCompareByType.cs | 56 +++++++++++++++++++ Sailboat/DrawingObjectBoat.cs | 48 ++++++++++++++++ Sailboat/FormMapWithSetBoats.Designer.cs | 58 +++++++++++++------ Sailboat/FormMapWithSetBoats.cs | 21 +++++++ Sailboat/IDrawingObject.cs | 2 +- Sailboat/MapWithSetBoatsGeneric.cs | 6 +- Sailboat/SetBoatsGeneric.cs | 14 ++++- 9 files changed, 298 insertions(+), 21 deletions(-) create mode 100644 Sailboat/BoatCompareByColor.cs create mode 100644 Sailboat/BoatCompareByType.cs diff --git a/Sailboat/AbstractMap.cs b/Sailboat/AbstractMap.cs index a233f7a..ae72a59 100644 --- a/Sailboat/AbstractMap.cs +++ b/Sailboat/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Sailboat { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -145,6 +145,45 @@ namespace Sailboat 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/Sailboat/BoatCompareByColor.cs b/Sailboat/BoatCompareByColor.cs new file mode 100644 index 0000000..1362c1e --- /dev/null +++ b/Sailboat/BoatCompareByColor.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + class BoatCompareByColor : 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 xBoat = x as DrawingObjectBoat; + var yBoat = y as DrawingObjectBoat; + if (xBoat == null && yBoat == null) + { + return 0; + } + if (xBoat == null && yBoat != null) + { + return 1; + } + if (xBoat != null && yBoat == null) + { + return -1; + } + string xBoatColor = xBoat.GetBoat.Boat.BodyColor.Name; + string yBoatColor = yBoat.GetBoat.Boat.BodyColor.Name; + if (xBoatColor != yBoatColor) + { + return xBoatColor.CompareTo(yBoatColor); + } + if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name) + { + if (xBoat.GetBoat.GetType().Name == "DrawingBoat") + { + return -1; + } + return 1; + } + if (xBoat.GetBoat.Boat is Sailboat xSailboat && yBoat.GetBoat.Boat is Sailboat ySailboat) + { + string xBoatDopColor = xSailboat.EdgeColor.Name; + string yBoatDopColor = ySailboat.EdgeColor.Name; + var dopColorCompare = xBoatDopColor.CompareTo(yBoatDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + 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/Sailboat/BoatCompareByType.cs b/Sailboat/BoatCompareByType.cs new file mode 100644 index 0000000..6b53e22 --- /dev/null +++ b/Sailboat/BoatCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + class BoatCompareByType : 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 xBoat = x as DrawingObjectBoat; + var yBoat = y as DrawingObjectBoat; + 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 == "DrawingBoat") + { + 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/Sailboat/DrawingObjectBoat.cs b/Sailboat/DrawingObjectBoat.cs index d03c75e..91bb967 100644 --- a/Sailboat/DrawingObjectBoat.cs +++ b/Sailboat/DrawingObjectBoat.cs @@ -39,6 +39,54 @@ namespace Sailboat public string GetInfo() => _boat?.GetDataForSave(); + public DrawingBoat GetBoat => _boat; public static IDrawingObject Create(string data) => new DrawingObjectBoat(data.CreateDrawningBoat()); + + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherBoat = other as DrawingObjectBoat; + 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 Sailboat sailboat && otherBoatBoat is Sailboat otherSailboat) + { + if (sailboat.EdgeColor != otherSailboat.EdgeColor) + { + return false; + } + if (sailboat.Sail != otherSailboat.Sail) + { + return false; + } + if (sailboat.ExtendedBody != otherSailboat.ExtendedBody) + { + return false; + } + } + return true; + } } } diff --git a/Sailboat/FormMapWithSetBoats.Designer.cs b/Sailboat/FormMapWithSetBoats.Designer.cs index 90150aa..99ece09 100644 --- a/Sailboat/FormMapWithSetBoats.Designer.cs +++ b/Sailboat/FormMapWithSetBoats.Designer.cs @@ -52,6 +52,8 @@ namespace Sailboat 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.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -60,6 +62,8 @@ namespace Sailboat // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.btn_left); this.groupBoxTools.Controls.Add(this.btn_up); @@ -71,9 +75,9 @@ namespace Sailboat this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.btn_add_boat); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(707, 28); + this.groupBoxTools.Location = new System.Drawing.Point(733, 28); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(250, 581); + this.groupBoxTools.Size = new System.Drawing.Size(250, 692); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -145,7 +149,7 @@ namespace Sailboat this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left; this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_left.Location = new System.Drawing.Point(19, 524); + this.btn_left.Location = new System.Drawing.Point(19, 635); this.btn_left.Name = "btn_left"; this.btn_left.Size = new System.Drawing.Size(70, 40); this.btn_left.TabIndex = 11; @@ -157,7 +161,7 @@ namespace Sailboat this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up; this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_up.Location = new System.Drawing.Point(95, 478); + this.btn_up.Location = new System.Drawing.Point(95, 589); this.btn_up.Name = "btn_up"; this.btn_up.Size = new System.Drawing.Size(75, 40); this.btn_up.TabIndex = 10; @@ -169,7 +173,7 @@ namespace Sailboat this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down; this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_down.Location = new System.Drawing.Point(95, 524); + this.btn_down.Location = new System.Drawing.Point(95, 635); this.btn_down.Name = "btn_down"; this.btn_down.Size = new System.Drawing.Size(75, 40); this.btn_down.TabIndex = 9; @@ -181,7 +185,7 @@ namespace Sailboat this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right; this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_right.Location = new System.Drawing.Point(174, 524); + this.btn_right.Location = new System.Drawing.Point(174, 635); this.btn_right.Name = "btn_right"; this.btn_right.Size = new System.Drawing.Size(70, 40); this.btn_right.TabIndex = 8; @@ -190,7 +194,7 @@ namespace Sailboat // // btn_show_map // - this.btn_show_map.Location = new System.Drawing.Point(6, 471); + this.btn_show_map.Location = new System.Drawing.Point(6, 555); this.btn_show_map.Name = "btn_show_map"; this.btn_show_map.Size = new System.Drawing.Size(232, 29); this.btn_show_map.TabIndex = 6; @@ -200,7 +204,7 @@ namespace Sailboat // // btn_show_storage // - this.btn_show_storage.Location = new System.Drawing.Point(6, 436); + this.btn_show_storage.Location = new System.Drawing.Point(6, 520); this.btn_show_storage.Name = "btn_show_storage"; this.btn_show_storage.Size = new System.Drawing.Size(232, 29); this.btn_show_storage.TabIndex = 5; @@ -210,7 +214,7 @@ namespace Sailboat // // btn_remove_boat // - this.btn_remove_boat.Location = new System.Drawing.Point(6, 391); + this.btn_remove_boat.Location = new System.Drawing.Point(6, 475); this.btn_remove_boat.Name = "btn_remove_boat"; this.btn_remove_boat.Size = new System.Drawing.Size(232, 29); this.btn_remove_boat.TabIndex = 4; @@ -220,7 +224,7 @@ namespace Sailboat // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 358); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 442); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(232, 27); @@ -228,7 +232,7 @@ namespace Sailboat // // btn_add_boat // - this.btn_add_boat.Location = new System.Drawing.Point(6, 305); + this.btn_add_boat.Location = new System.Drawing.Point(6, 389); this.btn_add_boat.Name = "btn_add_boat"; this.btn_add_boat.Size = new System.Drawing.Size(232, 29); this.btn_add_boat.TabIndex = 2; @@ -241,7 +245,7 @@ namespace Sailboat 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(707, 581); + this.pictureBox.Size = new System.Drawing.Size(733, 692); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // @@ -252,7 +256,7 @@ namespace Sailboat this.файлToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(957, 28); + this.menuStrip.Size = new System.Drawing.Size(983, 28); this.menuStrip.TabIndex = 1; this.menuStrip.Text = "menuStrip"; // @@ -268,14 +272,14 @@ namespace Sailboat // 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); // @@ -287,11 +291,31 @@ namespace Sailboat // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(6, 299); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(232, 29); + this.buttonSortByType.TabIndex = 13; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(6, 334); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(232, 29); + this.buttonSortByColor.TabIndex = 14; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // // FormMapWithSetBoats // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(957, 609); + this.ClientSize = new System.Drawing.Size(983, 720); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -335,5 +359,7 @@ namespace Sailboat private System.Windows.Forms.ToolStripMenuItem LoadToolStripMenuItem; private System.Windows.Forms.OpenFileDialog openFileDialog; private System.Windows.Forms.SaveFileDialog saveFileDialog; + private System.Windows.Forms.Button buttonSortByColor; + private System.Windows.Forms.Button buttonSortByType; } } \ No newline at end of file diff --git a/Sailboat/FormMapWithSetBoats.cs b/Sailboat/FormMapWithSetBoats.cs index 735b389..fc0fa3f 100644 --- a/Sailboat/FormMapWithSetBoats.cs +++ b/Sailboat/FormMapWithSetBoats.cs @@ -245,5 +245,26 @@ namespace Sailboat } } } + + 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(); + + } } } diff --git a/Sailboat/IDrawingObject.cs b/Sailboat/IDrawingObject.cs index 699cb05..00175c5 100644 --- a/Sailboat/IDrawingObject.cs +++ b/Sailboat/IDrawingObject.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Sailboat { - interface IDrawingObject + interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Sailboat/MapWithSetBoatsGeneric.cs b/Sailboat/MapWithSetBoatsGeneric.cs index 9a9ba2d..677d7c4 100644 --- a/Sailboat/MapWithSetBoatsGeneric.cs +++ b/Sailboat/MapWithSetBoatsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Sailboat { - class MapWithSetBoatsGeneric where T : class, IDrawingObject + class MapWithSetBoatsGeneric where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -149,5 +149,9 @@ namespace Sailboat _setBoats.Insert(DrawingObjectBoat.Create(rec) as T); } } + public void Sort(IComparer comparer) + { + _setBoats.SortSet(comparer); + } } } diff --git a/Sailboat/SetBoatsGeneric.cs b/Sailboat/SetBoatsGeneric.cs index 5f39a19..38fcdab 100644 --- a/Sailboat/SetBoatsGeneric.cs +++ b/Sailboat/SetBoatsGeneric.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Sailboat { - class SetBoatsGeneric where T : class + class SetBoatsGeneric where T : class, IEquatable { private readonly List _places; @@ -28,6 +28,10 @@ namespace Sailboat public int Insert(T boat, int position) { + if (_places.Contains(boat)) + { + return -1; + } if (Count == _maxCount) { throw new StorageOverflowException(_maxCount); @@ -85,5 +89,13 @@ namespace Sailboat } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } \ No newline at end of file