diff --git a/GasolineTanker/GasolineTanker/AbstractMap.cs b/GasolineTanker/GasolineTanker/AbstractMap.cs index 7217b99..34e61a0 100644 --- a/GasolineTanker/GasolineTanker/AbstractMap.cs +++ b/GasolineTanker/GasolineTanker/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace GasolineTanker { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -135,5 +135,45 @@ namespace GasolineTanker 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/GasolineTanker/GasolineTanker/DrawningObjectTanker.cs b/GasolineTanker/GasolineTanker/DrawningObjectTanker.cs index 54c33fe..f7ae70c 100644 --- a/GasolineTanker/GasolineTanker/DrawningObjectTanker.cs +++ b/GasolineTanker/GasolineTanker/DrawningObjectTanker.cs @@ -17,6 +17,8 @@ namespace GasolineTanker public float Step => _Tanker?.Tanker?.Step ?? 0; + public DrawningTanker GetTanker => _Tanker; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _Tanker?.GetCurrentPosition() ?? default; @@ -38,5 +40,58 @@ namespace GasolineTanker } public string GetInfo() => _Tanker?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectTanker(data.CreateDrawningTanker()); + + public bool Equals(IDrawningObject? other) + { + { + if (other == null) + { + return false; + } + var otherTanker = other as DrawningObjectTanker; + if (otherTanker == null) + { + return false; + } + var tanker = _Tanker.Tanker; + var otherTankerTanker = otherTanker._Tanker.Tanker; + if (tanker.GetType().Name != otherTankerTanker.GetType().Name) + { + return false; + } + if (tanker.Speed != otherTankerTanker.Speed) + { + return false; + } + if (tanker.Weight != otherTankerTanker.Weight) + { + return false; + } + if (tanker.BodyColor != otherTankerTanker.BodyColor) + { + return false; + } + if (tanker is EntityGasolineTanker gasolinetanker && otherTankerTanker is EntityGasolineTanker otherGasolineTanker) + { + if (gasolinetanker.DopColor != otherGasolineTanker.DopColor) + { + return false; + } + if (gasolinetanker.Cabin != otherGasolineTanker.Cabin) + { + return false; + } + if (gasolinetanker.Signal != otherGasolineTanker.Signal) + { + return false; + } + if (gasolinetanker.BenzoBack != otherGasolineTanker.BenzoBack) + { + return false; + } + } + return true; + } + } } } diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs index 3bd4b66..268d27d 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSortColor = new System.Windows.Forms.Button(); + this.buttonSortType = 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.buttonSortColor); + this.groupBoxTools.Controls.Add(this.buttonSortType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveTanker); @@ -77,6 +81,26 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortColor + // + this.buttonSortColor.Location = new System.Drawing.Point(17, 308); + this.buttonSortColor.Name = "buttonSortColor"; + this.buttonSortColor.Size = new System.Drawing.Size(175, 29); + this.buttonSortColor.TabIndex = 20; + this.buttonSortColor.Text = "Сортировать по цвету"; + this.buttonSortColor.UseVisualStyleBackColor = true; + this.buttonSortColor.Click += new System.EventHandler(this.ButtonSortColor_Click); + // + // buttonSortType + // + this.buttonSortType.Location = new System.Drawing.Point(17, 273); + this.buttonSortType.Name = "buttonSortType"; + this.buttonSortType.Size = new System.Drawing.Size(175, 29); + this.buttonSortType.TabIndex = 19; + this.buttonSortType.Text = "Сортировать по типу"; + this.buttonSortType.UseVisualStyleBackColor = true; + this.buttonSortType.Click += new System.EventHandler(this.ButtonSortType_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonAddMap); @@ -143,7 +167,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 355); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 384); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -152,7 +176,7 @@ // // buttonRemoveTanker // - this.buttonRemoveTanker.Location = new System.Drawing.Point(17, 384); + this.buttonRemoveTanker.Location = new System.Drawing.Point(17, 413); this.buttonRemoveTanker.Name = "buttonRemoveTanker"; this.buttonRemoveTanker.Size = new System.Drawing.Size(175, 35); this.buttonRemoveTanker.TabIndex = 3; @@ -162,7 +186,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(17, 437); + this.buttonShowStorage.Location = new System.Drawing.Point(17, 454); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 4; @@ -175,7 +199,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 558); + this.buttonDown.Location = new System.Drawing.Point(91, 572); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -187,7 +211,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 558); + this.buttonRight.Location = new System.Drawing.Point(127, 572); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -199,7 +223,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 558); + this.buttonLeft.Location = new System.Drawing.Point(55, 572); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -211,7 +235,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::GasolineTanker.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 522); + this.buttonUp.Location = new System.Drawing.Point(91, 536); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -220,7 +244,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(17, 487); + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 495); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); this.buttonShowOnMap.TabIndex = 5; @@ -230,7 +254,7 @@ // // buttonAddTanker // - this.buttonAddTanker.Location = new System.Drawing.Point(17, 314); + this.buttonAddTanker.Location = new System.Drawing.Point(17, 343); this.buttonAddTanker.Name = "buttonAddTanker"; this.buttonAddTanker.Size = new System.Drawing.Size(175, 35); this.buttonAddTanker.TabIndex = 1; @@ -261,21 +285,21 @@ this.FileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.SaveToolStripMenuItem, this.LoadToolStripMenuItem}); - this.FileToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.FileToolStripMenuItem.Name = "FileToolStripMenuItem"; this.FileToolStripMenuItem.Size = new System.Drawing.Size(48, 20); this.FileToolStripMenuItem.Text = "Файл"; // // 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); // @@ -287,7 +311,7 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // - // FormMapWithSetCars + // FormMapWithSetTankers // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; @@ -296,7 +320,7 @@ this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); this.MainMenuStrip = this.menuStrip; - this.Name = "FormMapWithSetCars"; + this.Name = "FormMapWithSetTankers"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); @@ -335,5 +359,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/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs index 65fdef8..64a556b 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs +++ b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.cs @@ -256,6 +256,25 @@ namespace GasolineTanker } } + private void ButtonSortType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new TankerCompareByType()); + 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 TankerCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { diff --git a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.resx b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.resx index dd050e0..83f683e 100644 --- a/GasolineTanker/GasolineTanker/FormMapWithSetTankers.resx +++ b/GasolineTanker/GasolineTanker/FormMapWithSetTankers.resx @@ -66,7 +66,4 @@ 258, 17 - - 387, 17 - \ No newline at end of file diff --git a/GasolineTanker/GasolineTanker/IDrawningObject.cs b/GasolineTanker/GasolineTanker/IDrawningObject.cs index 569abab..7c0bcc7 100644 --- a/GasolineTanker/GasolineTanker/IDrawningObject.cs +++ b/GasolineTanker/GasolineTanker/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace GasolineTanker { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs b/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs index aa02826..b1d9849 100644 --- a/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs +++ b/GasolineTanker/GasolineTanker/MapWithSetTankersGeneric.cs @@ -1,7 +1,7 @@ namespace GasolineTanker { internal class MapWithSetTankersGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject,IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -130,6 +130,11 @@ } } } + + public void Sort(IComparer comparer) + { + _setTankers.SortSet(comparer); + } } } diff --git a/GasolineTanker/GasolineTanker/SetTankersGeneric.cs b/GasolineTanker/GasolineTanker/SetTankersGeneric.cs index b575010..c37274d 100644 --- a/GasolineTanker/GasolineTanker/SetTankersGeneric.cs +++ b/GasolineTanker/GasolineTanker/SetTankersGeneric.cs @@ -1,7 +1,7 @@ namespace GasolineTanker { internal class SetTankersGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -20,6 +20,10 @@ public int Insert(T tanker, int position) { + if (_places.Contains(tanker)) + { + return -1; + } if (Count == _maxCount) { throw new StorageOverflowException(_maxCount); @@ -76,5 +80,13 @@ } } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } diff --git a/GasolineTanker/GasolineTanker/TankerCompareByColor.cs b/GasolineTanker/GasolineTanker/TankerCompareByColor.cs new file mode 100644 index 0000000..5f7ddc3 --- /dev/null +++ b/GasolineTanker/GasolineTanker/TankerCompareByColor.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GasolineTanker +{ + internal class TankerCompareByColor : 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 xTanker = x as DrawningObjectTanker; + var yTanker = y as DrawningObjectTanker; + if (xTanker == null && yTanker == null) + { + return 0; + } + if (xTanker == null && yTanker != null) + { + return 1; + } + if (xTanker != null && yTanker == null) + { + return -1; + } + string xTankerColor = xTanker.GetTanker.Tanker.BodyColor.Name; + string yTankerColor = yTanker.GetTanker.Tanker.BodyColor.Name; + if (xTankerColor != yTankerColor) + { + return xTankerColor.CompareTo(yTankerColor); + } + if (xTanker.GetTanker.GetType().Name != yTanker.GetTanker.GetType().Name) + { + if (xTanker.GetTanker.GetType().Name == "DrawningTanker") + { + return -1; + } + return 1; + } + if (xTanker.GetTanker.Tanker is EntityGasolineTanker xGasolineTanker && yTanker.GetTanker.Tanker is EntityGasolineTanker yGasolineTanker) + { + string xTankerDopColor = xGasolineTanker.DopColor.Name; + string yTankerDopColor = yGasolineTanker.DopColor.Name; + var dopColorCompare = xTankerDopColor.CompareTo(yTankerDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xTanker.GetTanker.Tanker.Speed.CompareTo(yTanker.GetTanker.Tanker.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xTanker.GetTanker.Tanker.Weight.CompareTo(yTanker.GetTanker.Tanker.Weight); + } + } +} diff --git a/GasolineTanker/GasolineTanker/TankerCompareByType.cs b/GasolineTanker/GasolineTanker/TankerCompareByType.cs new file mode 100644 index 0000000..963f058 --- /dev/null +++ b/GasolineTanker/GasolineTanker/TankerCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GasolineTanker +{ + internal class TankerCompareByType : 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 xTanker = x as DrawningObjectTanker; + var yTanker = y as DrawningObjectTanker; + if (xTanker == null && yTanker == null) + { + return 0; + } + if (xTanker == null && yTanker != null) + { + return 1; + } + if (xTanker != null && yTanker == null) + { + return -1; + } + if (xTanker.GetTanker.GetType().Name != yTanker.GetTanker.GetType().Name) + { + if (xTanker.GetTanker.GetType().Name == "DrawningTanker") + { + return -1; + } + return 1; + } + var speedCompare = xTanker.GetTanker.Tanker.Speed.CompareTo(yTanker.GetTanker.Tanker.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xTanker.GetTanker.Tanker.Weight.CompareTo(yTanker.GetTanker.Tanker.Weight); + } + } +}