diff --git a/WarmlyShip/WarmlyShip/AbstractMap.cs b/WarmlyShip/WarmlyShip/AbstractMap.cs index c8cbac6..44feb16 100644 --- a/WarmlyShip/WarmlyShip/AbstractMap.cs +++ b/WarmlyShip/WarmlyShip/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyShip { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; @@ -117,5 +117,26 @@ namespace WarmlyShip 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 || _map != other._map || _width != other._width || + _size_x != other._size_x || _size_y != other._size_y || _height != other._height || + GetType() != other.GetType() || _map.GetLength(0) != other._map.GetLength(0) || _map.GetLength(1) != other._map.GetLength(1)) + { + 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; + } } } diff --git a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs index f0da2a3..ea458d5 100644 --- a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs +++ b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs @@ -17,6 +17,8 @@ namespace WarmlyShip public float Step => _warmlyShip?.warmlyShip?.Step ?? 0; + public DrawingWarmlyShip GetWarmlyShip => _warmlyShip; + public void DrawningObject(Graphics g) { _warmlyShip?.DrawTransport(g); @@ -40,5 +42,55 @@ namespace WarmlyShip public string GetInfo() => _warmlyShip?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherShip = other as DrawningObjectShip; + if (otherShip == null) + { + return false; + } + var ship = _warmlyShip.warmlyShip; + var otherShipShip = otherShip._warmlyShip.warmlyShip; + if (ship.Speed != otherShipShip.Speed) + { + return false; + } + if (ship.Weight != otherShipShip.Weight) + { + return false; + } + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + + if ((ship is EntityMotorShip) && !(otherShipShip is EntityMotorShip) + || !(ship is EntityMotorShip) && (otherShipShip is EntityMotorShip)) + { + return false; + } + + if (ship is EntityMotorShip motorShip && otherShipShip is EntityMotorShip otherMotorShip) + { + if (motorShip.DopColor != otherMotorShip.DopColor) + { + return false; + } + if (motorShip.Tubes != otherMotorShip.Tubes) + { + return false; + } + if (motorShip.Cistern != otherMotorShip.Cistern) + { + return false; + } + } + return true; + } } } diff --git a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs index 79878fb..40c6473 100644 --- a/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/EntityWarmlyShip.cs @@ -17,8 +17,8 @@ namespace WarmlyShip public EntityWarmlyShip(int speed, float weight, Color bodyColor) { Random random = new Random(); - Speed = speed <= 0 ? random.Next(100, 300) : speed; - Weight = weight <= 0 ? random.Next(1000, 2000) : weight; + Speed = speed <= 0 ? 100 : speed; + Weight = weight <= 0 ? 1000 : weight; BodyColor = bodyColor; } } diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs index 157cb86..21e2d1f 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.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.buttonSortByType = new System.Windows.Forms.Button(); + this.buttonSortByColor = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBox1 // + this.groupBox1.Controls.Add(this.buttonSortByColor); + this.groupBox1.Controls.Add(this.buttonSortByType); this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.buttonDown); @@ -142,7 +146,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 345); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 375); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); @@ -198,7 +202,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 432); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 462); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23); this.buttonShowOnMap.TabIndex = 5; @@ -208,7 +212,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 403); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 433); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(188, 23); this.buttonShowStorage.TabIndex = 4; @@ -218,7 +222,7 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(6, 374); + this.buttonRemoveShip.Location = new System.Drawing.Point(6, 404); this.buttonRemoveShip.Name = "buttonRemoveShip"; this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23); this.buttonRemoveShip.TabIndex = 3; @@ -228,7 +232,7 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(6, 316); + this.buttonAddShip.Location = new System.Drawing.Point(6, 346); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(188, 23); this.buttonAddShip.TabIndex = 1; @@ -266,14 +270,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); // @@ -285,6 +289,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(6, 272); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(188, 23); + 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, 301); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(188, 23); + this.buttonSortByColor.TabIndex = 14; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetShip // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -333,5 +357,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/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index 6261fb6..ef6fed7 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -113,6 +113,7 @@ namespace WarmlyShip pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); _logger.LogInformation($"Добавлен объект: {ship} на карте: {listBoxMaps.SelectedItem}"); } + else MessageBox.Show("Объект не добавлен"); } catch (StorageOverflowException ex) { @@ -250,5 +251,25 @@ namespace WarmlyShip } } } + + private void ButtonSortByType_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 ButtonSortByColor_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/WarmlyShip/WarmlyShip/IDrawningObject.cs b/WarmlyShip/WarmlyShip/IDrawningObject.cs index 94af911..0a41158 100644 --- a/WarmlyShip/WarmlyShip/IDrawningObject.cs +++ b/WarmlyShip/WarmlyShip/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyShip { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 209da81..e19ab5a 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace WarmlyShip { internal class MapWithSetShipGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -83,6 +83,11 @@ namespace WarmlyShip } } + public void Sort(IComparer comparer) + { + _setShips.SortSet(comparer); + } + private void Shaking() { int j = _setShips.Count - 1; diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index 8a37607..fd983b5 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace WarmlyShip { internal class SetShipGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -27,6 +27,10 @@ namespace WarmlyShip public int Insert(T ship, int position) { + if (_places.Contains(ship)) + { + return -1; + } if (position < 0 || position > Count || Count == _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, ship); return position; @@ -69,5 +73,13 @@ namespace WarmlyShip } } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } diff --git a/WarmlyShip/WarmlyShip/ShipCompareByColor.cs b/WarmlyShip/WarmlyShip/ShipCompareByColor.cs new file mode 100644 index 0000000..00337a0 --- /dev/null +++ b/WarmlyShip/WarmlyShip/ShipCompareByColor.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal class ShipCompareByColor : 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 xShip = x as DrawningObjectShip; + var yShip = y as DrawningObjectShip; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + string xAirplaneColor = xShip.GetWarmlyShip.warmlyShip.BodyColor.Name; + string yAirplaneColor = yShip.GetWarmlyShip.warmlyShip.BodyColor.Name; + if (xAirplaneColor != yAirplaneColor) + { + return xAirplaneColor.CompareTo(yAirplaneColor); + } + if (xShip.GetWarmlyShip.warmlyShip is EntityMotorShip xAirbus && yShip.GetWarmlyShip.warmlyShip is EntityMotorShip yAirbus) + { + string xAirplaneDopColor = xAirbus.DopColor.Name; + string yAirplaneDopColor = yAirbus.DopColor.Name; + var dopColorCompare = xAirplaneDopColor.CompareTo(yAirplaneDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xShip.GetWarmlyShip.warmlyShip.Speed.CompareTo(yShip.GetWarmlyShip.warmlyShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetWarmlyShip.warmlyShip.Weight.CompareTo(yShip.GetWarmlyShip.warmlyShip.Weight); + } + } +} diff --git a/WarmlyShip/WarmlyShip/ShipCompareByType.cs b/WarmlyShip/WarmlyShip/ShipCompareByType.cs new file mode 100644 index 0000000..d43aa82 --- /dev/null +++ b/WarmlyShip/WarmlyShip/ShipCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal class ShipCompareByType : 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 xShip = x as DrawningObjectShip; + var yShip = y as DrawningObjectShip; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + if (xShip.GetWarmlyShip.GetType().Name != yShip.GetWarmlyShip.GetType().Name) + { + if (xShip.GetWarmlyShip.GetType().Name == "DrawingWarmlyShip") + { + return -1; + } + return 1; + } + var speedCompare = xShip.GetWarmlyShip.warmlyShip.Speed.CompareTo(yShip.GetWarmlyShip.warmlyShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetWarmlyShip.warmlyShip.Weight.CompareTo(yShip.GetWarmlyShip.warmlyShip.Weight); + } + } +}