diff --git a/RoadTrain/RoadTrain/AbstractMap.cs b/RoadTrain/RoadTrain/AbstractMap.cs index 1eae74d..9989edc 100644 --- a/RoadTrain/RoadTrain/AbstractMap.cs +++ b/RoadTrain/RoadTrain/AbstractMap.cs @@ -1,6 +1,6 @@ namespace RoadTrain { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -110,6 +110,32 @@ _drawningObject.DrawningObject(gr); return bmp; } + + public bool Equals(AbstractMap? other) + { + if (other == null) + return false; + + if (_width != other._width) + return false; + + if (_height != other._height) + return false; + + if (_size_x != other._size_x) + return false; + + if (_size_y != other._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] != other._map[i, j]) + return false; + + return true; + } + protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); diff --git a/RoadTrain/RoadTrain/DrawningObjectRoadTrain.cs b/RoadTrain/RoadTrain/DrawningObjectRoadTrain.cs index 0c47a96..06c095b 100644 --- a/RoadTrain/RoadTrain/DrawningObjectRoadTrain.cs +++ b/RoadTrain/RoadTrain/DrawningObjectRoadTrain.cs @@ -1,9 +1,13 @@ -namespace RoadTrain +using System.Net.Sockets; + +namespace RoadTrain { internal class DrawningObjectRoadTrain : IDrawningObject { private DrawningRoadTrain _roadTrain = null; + public DrawningRoadTrain GetRoadTrain => _roadTrain; + public DrawningObjectRoadTrain(DrawningRoadTrain roadTrain) { _roadTrain = roadTrain; @@ -34,5 +38,52 @@ public string GetInfo() => _roadTrain?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectRoadTrain(data.CreateDrawningRoadTrain()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherRoadTrain = other as DrawningObjectRoadTrain; + if (otherRoadTrain == null) + { + return false; + } + var roadTrain = _roadTrain.RoadTrain; + var otherRoadTrainRoadTrain = otherRoadTrain._roadTrain.RoadTrain; + if (roadTrain.GetType() != otherRoadTrainRoadTrain.GetType()) + { + return false; + } + if (roadTrain.Speed != otherRoadTrainRoadTrain.Speed) + { + return false; + } + if (roadTrain.Weight != otherRoadTrainRoadTrain.Weight) + { + return false; + } + if (roadTrain.BodyColor != otherRoadTrainRoadTrain.BodyColor) + { + return false; + } + if (roadTrain is EntitySweeperRoadTrain srt && otherRoadTrainRoadTrain is EntitySweeperRoadTrain otherSrt) + { + if (srt.DopColor != otherSrt.DopColor) + { + return false; + } + if (srt.WaterTank!= otherSrt.WaterTank) + { + return false; + } + if (srt.SweepingBush != otherSrt.SweepingBush) + { + return false; + } + } + return true; + } } } diff --git a/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.Designer.cs b/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.Designer.cs index 6f8efce..fd6fb84 100644 --- a/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.Designer.cs +++ b/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.Designer.cs @@ -30,6 +30,8 @@ { this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox = new System.Windows.Forms.GroupBox(); + this.ButtonSortByType = new System.Windows.Forms.Button(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.textBoxNewMapName = new System.Windows.Forms.TextBox(); this.buttonAddMap = new System.Windows.Forms.Button(); @@ -60,12 +62,14 @@ // this.pictureBox.Location = new System.Drawing.Point(3, 27); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(644, 421); + this.pictureBox.Size = new System.Drawing.Size(644, 480); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // // groupBox // + this.groupBox.Controls.Add(this.ButtonSortByType); + this.groupBox.Controls.Add(this.ButtonSortByColor); this.groupBox.Controls.Add(this.groupBoxMaps); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonShowOnMap); @@ -78,11 +82,31 @@ this.groupBox.Controls.Add(this.buttonAddRoadTrain); this.groupBox.Location = new System.Drawing.Point(653, 3); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(175, 445); + this.groupBox.Size = new System.Drawing.Size(175, 504); this.groupBox.TabIndex = 1; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(6, 230); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(160, 25); + this.ButtonSortByType.TabIndex = 16; + 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, 258); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(160, 25); + this.ButtonSortByColor.TabIndex = 15; + this.ButtonSortByColor.Text = "Сортировать по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); @@ -92,7 +116,7 @@ this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); this.groupBoxMaps.Location = new System.Drawing.Point(6, 22); this.groupBoxMaps.Name = "groupBoxMaps"; - this.groupBoxMaps.Size = new System.Drawing.Size(163, 211); + this.groupBoxMaps.Size = new System.Drawing.Size(163, 200); this.groupBoxMaps.TabIndex = 14; this.groupBoxMaps.TabStop = false; this.groupBoxMaps.Text = "Карты"; @@ -116,7 +140,7 @@ // // buttonRemoveMap // - this.buttonRemoveMap.Location = new System.Drawing.Point(6, 180); + this.buttonRemoveMap.Location = new System.Drawing.Point(6, 165); this.buttonRemoveMap.Name = "buttonRemoveMap"; this.buttonRemoveMap.Size = new System.Drawing.Size(151, 25); this.buttonRemoveMap.TabIndex = 15; @@ -130,7 +154,7 @@ this.listBoxMaps.ItemHeight = 15; this.listBoxMaps.Location = new System.Drawing.Point(6, 110); this.listBoxMaps.Name = "listBoxMaps"; - this.listBoxMaps.Size = new System.Drawing.Size(151, 64); + this.listBoxMaps.Size = new System.Drawing.Size(151, 49); this.listBoxMaps.TabIndex = 1; this.listBoxMaps.Click += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); // @@ -152,7 +176,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::RoadTrain.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(103, 418); + this.buttonRight.Location = new System.Drawing.Point(103, 477); this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(26, 22); @@ -162,7 +186,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 357); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 420); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(160, 25); this.buttonShowOnMap.TabIndex = 4; @@ -175,7 +199,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::RoadTrain.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(74, 390); + this.buttonUp.Location = new System.Drawing.Point(74, 449); this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(26, 22); @@ -188,7 +212,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::RoadTrain.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(74, 417); + this.buttonDown.Location = new System.Drawing.Point(74, 476); this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(26, 22); @@ -201,7 +225,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::RoadTrain.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(43, 417); + this.buttonLeft.Location = new System.Drawing.Point(43, 476); this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(26, 22); @@ -211,14 +235,14 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 268); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 328); this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(160, 23); this.maskedTextBoxPosition.TabIndex = 5; // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 330); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 393); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(160, 25); this.buttonShowStorage.TabIndex = 3; @@ -228,7 +252,7 @@ // // buttonRemoveRoadTrain // - this.buttonRemoveRoadTrain.Location = new System.Drawing.Point(6, 295); + this.buttonRemoveRoadTrain.Location = new System.Drawing.Point(6, 355); this.buttonRemoveRoadTrain.Name = "buttonRemoveRoadTrain"; this.buttonRemoveRoadTrain.Size = new System.Drawing.Size(160, 27); this.buttonRemoveRoadTrain.TabIndex = 2; @@ -238,7 +262,7 @@ // // buttonAddRoadTrain // - this.buttonAddRoadTrain.Location = new System.Drawing.Point(6, 239); + this.buttonAddRoadTrain.Location = new System.Drawing.Point(6, 299); this.buttonAddRoadTrain.Name = "buttonAddRoadTrain"; this.buttonAddRoadTrain.Size = new System.Drawing.Size(160, 25); this.buttonAddRoadTrain.TabIndex = 1; @@ -284,7 +308,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(831, 450); + this.ClientSize = new System.Drawing.Size(831, 519); this.Controls.Add(this.groupBox); this.Controls.Add(this.pictureBox); this.Controls.Add(this.menuStrip1); @@ -327,5 +351,7 @@ private MenuStrip menuStrip1; private ToolStripMenuItem SaveToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs b/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs index c2b9ca7..017c8eb 100644 --- a/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs +++ b/RoadTrain/RoadTrain/FormMapWithSetRoadTrains.cs @@ -321,5 +321,37 @@ namespace RoadTrain } ReloadMaps(); } + + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new RoadTrainCompareByType()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + 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 RoadTrainCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/RoadTrain/RoadTrain/IDrawningObject.cs b/RoadTrain/RoadTrain/IDrawningObject.cs index 57e5aa6..3652bb0 100644 --- a/RoadTrain/RoadTrain/IDrawningObject.cs +++ b/RoadTrain/RoadTrain/IDrawningObject.cs @@ -1,6 +1,9 @@ namespace RoadTrain { - internal interface IDrawningObject + /// + /// Интерфейс для работы с объектом, прорисовываемым на форме + /// + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs b/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs index 78197e0..2afe6f9 100644 --- a/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs +++ b/RoadTrain/RoadTrain/MapWithSetRoadTrainsGeneric.cs @@ -6,7 +6,7 @@ /// /// internal class MapWithSetRoadTrainsGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -146,6 +146,15 @@ } } + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setRoadTrains.SortSet(comparer); + } + /// /// Метод отрисовки фона /// diff --git a/RoadTrain/RoadTrain/MapsCollection.cs b/RoadTrain/RoadTrain/MapsCollection.cs index 2b4631a..9383209 100644 --- a/RoadTrain/RoadTrain/MapsCollection.cs +++ b/RoadTrain/RoadTrain/MapsCollection.cs @@ -90,17 +90,6 @@ namespace RoadTrain return null; } } - - /// - /// Метод записи информации в файл - /// - /// Строка, которую следует записать - /// Поток для записи - private static void WriteToFile(string text, FileStream stream) - { - byte[] info = new UTF8Encoding(true).GetBytes(text); - stream.Write(info, 0, info.Length); - } /// /// Сохранение информации по грузовикам хранилища в файл @@ -134,7 +123,6 @@ namespace RoadTrain { throw new Exception("Файл не найден"); } - string bufferTextFromFile = ""; using (StreamReader sr = new(filename)) { string str = ""; diff --git a/RoadTrain/RoadTrain/RoadTrainCompareByColor.cs b/RoadTrain/RoadTrain/RoadTrainCompareByColor.cs new file mode 100644 index 0000000..340c16d --- /dev/null +++ b/RoadTrain/RoadTrain/RoadTrainCompareByColor.cs @@ -0,0 +1,58 @@ +namespace RoadTrain +{ + internal class RoadTrainCompareByColor : 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 xRoadTrain = x as DrawningObjectRoadTrain; + var yRoadTrain = y as DrawningObjectRoadTrain; + if (xRoadTrain == null && yRoadTrain == null) + { + return 0; + } + if (xRoadTrain == null && yRoadTrain != null) + { + return 1; + } + if (xRoadTrain != null && yRoadTrain == null) + { + return -1; + } + + int xColor = xRoadTrain.GetRoadTrain.RoadTrain.BodyColor.ToArgb(); + int yColor = yRoadTrain.GetRoadTrain.RoadTrain.BodyColor.ToArgb(); + + if (xColor != yColor) + return xColor.CompareTo(yColor); + + if (xRoadTrain.GetRoadTrain.RoadTrain is EntitySweeperRoadTrain xSweeper && yRoadTrain.GetRoadTrain.RoadTrain is EntitySweeperRoadTrain ySweeper) + { + xColor = xSweeper.DopColor.ToArgb(); + yColor = ySweeper.DopColor.ToArgb(); + + if (xColor != yColor) + return xColor.CompareTo(yColor); + + } + + var speedCompare = xRoadTrain.GetRoadTrain.RoadTrain.Speed.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xRoadTrain.GetRoadTrain.RoadTrain.Weight.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Weight); + } + } +} diff --git a/RoadTrain/RoadTrain/RoadTrainCompareByType.cs b/RoadTrain/RoadTrain/RoadTrainCompareByType.cs new file mode 100644 index 0000000..d50ae60 --- /dev/null +++ b/RoadTrain/RoadTrain/RoadTrainCompareByType.cs @@ -0,0 +1,49 @@ +namespace RoadTrain +{ + internal class RoadTrainCompareByType : 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 xRoadTrain = x as DrawningObjectRoadTrain; + var yRoadTrain = y as DrawningObjectRoadTrain; + if (xRoadTrain == null && yRoadTrain == null) + { + return 0; + } + if (xRoadTrain == null && yRoadTrain != null) + { + return 1; + } + if (xRoadTrain != null && yRoadTrain == null) + { + return -1; + } + if (xRoadTrain.GetRoadTrain.GetType().Name != yRoadTrain.GetRoadTrain.GetType().Name) + { + if (xRoadTrain.GetRoadTrain.GetType().Name == "DrawningRoadTrain") + { + return -1; + } + return 1; + } + var speedCompare = xRoadTrain.GetRoadTrain.RoadTrain.Speed.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xRoadTrain.GetRoadTrain.RoadTrain.Weight.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Weight); + } + } +} diff --git a/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs b/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs index 13d3946..3c54ef3 100644 --- a/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs +++ b/RoadTrain/RoadTrain/SetRoadTrainsGeneric.cs @@ -5,7 +5,7 @@ /// /// public class SetRoadTrainsGeneric - where T : class + where T : class, IEquatable { /// /// Массив объектов, которые храним @@ -39,7 +39,7 @@ // проверка на _maxCount if (_places.Count + 1 >= _maxCount) throw new StorageOverflowException(_maxCount); - _places.Insert(0, roadTrain); + Insert(roadTrain, 0); return 0; } @@ -51,6 +51,10 @@ /// public int Insert(T roadTrain, int position) { + // проверка на уникальность + if (_places.Contains(roadTrain)) + return -1; + // проверка позиции if (position < 0 || position >= _maxCount) return -1; @@ -60,7 +64,7 @@ throw new StorageOverflowException(_maxCount); // вставка по позиции - _places[position] = roadTrain; + _places.Insert(position, roadTrain); return position; } @@ -133,5 +137,18 @@ } } } + + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }