From 99871550b4c27f9681283cfad5d18c4ea4b8d38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Fri, 16 Dec 2022 20:46:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/AbstractMap.cs | 43 ++++++++++++- MotorBoat/MotorBoat/BoatCompareByColor.cs | 62 +++++++++++++++++++ MotorBoat/MotorBoat/BoatCompareByType.cs | 56 +++++++++++++++++ MotorBoat/MotorBoat/DrawningObjectBoat.cs | 50 +++++++++++++++ .../MotorBoat/FormMapWithSetBoats.Designer.cs | 49 +++++++++++---- MotorBoat/MotorBoat/FormMapWithSetBoats.cs | 48 +++++++------- MotorBoat/MotorBoat/IDrawningObject.cs | 2 +- MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs | 9 ++- MotorBoat/MotorBoat/SetBoatsGeneric.cs | 20 +++++- 9 files changed, 294 insertions(+), 45 deletions(-) create mode 100644 MotorBoat/MotorBoat/BoatCompareByColor.cs create mode 100644 MotorBoat/MotorBoat/BoatCompareByType.cs diff --git a/MotorBoat/MotorBoat/AbstractMap.cs b/MotorBoat/MotorBoat/AbstractMap.cs index e6cd207..93f1f70 100644 --- a/MotorBoat/MotorBoat/AbstractMap.cs +++ b/MotorBoat/MotorBoat/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace MotorBoat { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -170,6 +170,47 @@ namespace MotorBoat return true; } + + 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; + } + private Bitmap DrawMapWithObject() { Bitmap bmp = new(_width, _height); diff --git a/MotorBoat/MotorBoat/BoatCompareByColor.cs b/MotorBoat/MotorBoat/BoatCompareByColor.cs new file mode 100644 index 0000000..84ceabb --- /dev/null +++ b/MotorBoat/MotorBoat/BoatCompareByColor.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorBoat +{ + 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 xShip = x as DrawningObjectBoat; + var yShip = y as DrawningObjectBoat; + if (xShip == null && yShip == null) + { + return 0; + } + if (xShip == null && yShip != null) + { + return 1; + } + if (xShip != null && yShip == null) + { + return -1; + } + var xEntityShip = xShip.GetShip.Boat; + var yEntityShip = yShip.GetShip.Boat; + var baseColorCompare = xEntityShip.BodyColor.ToArgb().CompareTo(yEntityShip.BodyColor.ToArgb()); + if (baseColorCompare != 0) + { + return baseColorCompare; + } + if (xEntityShip is EntityMotorBoat xWarmlyShip && yEntityShip is EntityMotorBoat yWarmlyShip) + { + var dopColorCompare = xWarmlyShip.DopColor.ToArgb().CompareTo(yWarmlyShip.DopColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xShip.GetShip.Boat.Speed.CompareTo(yShip.GetShip.Boat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xShip.GetShip.Boat.Weight.CompareTo(yShip.GetShip.Boat.Weight); + } + } +} diff --git a/MotorBoat/MotorBoat/BoatCompareByType.cs b/MotorBoat/MotorBoat/BoatCompareByType.cs new file mode 100644 index 0000000..d769cd3 --- /dev/null +++ b/MotorBoat/MotorBoat/BoatCompareByType.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorBoat +{ + 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 xCar = x as DrawningObjectBoat; + var yCar = y as DrawningObjectBoat; + if (xCar == null && yCar == null) + { + return 0; + } + if (xCar == null && yCar != null) + { + return 1; + } + if (xCar != null && yCar == null) + { + return -1; + } + if (xCar.GetShip.GetType().Name != yCar.GetShip.GetType().Name) + { + if (xCar.GetShip.GetType().Name == "DrawningBoat") + { + return -1; + } + return 1; + } + var speedCompare = + xCar.GetShip.Boat.Speed.CompareTo(yCar.GetShip.Boat.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xCar.GetShip.Boat.Weight.CompareTo(yCar.GetShip.Boat.Weight); + } + } +} diff --git a/MotorBoat/MotorBoat/DrawningObjectBoat.cs b/MotorBoat/MotorBoat/DrawningObjectBoat.cs index 16b4fe2..f55e8e8 100644 --- a/MotorBoat/MotorBoat/DrawningObjectBoat.cs +++ b/MotorBoat/MotorBoat/DrawningObjectBoat.cs @@ -19,6 +19,9 @@ namespace MotorBoat { return _boat?.GetCurrentPosition() ?? default; } + + public DrawningBoat GetShip => _boat; + public void MoveObject(Direction direction) { _boat?.MoveTransport(direction); @@ -34,5 +37,52 @@ namespace MotorBoat } public string GetInfo() => _boat?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherShip = other as DrawningObjectBoat; + if (otherShip == null) + { + return false; + } + var ship = _boat.Boat; + var otherShipShip = otherShip._boat.Boat; + if (ship.GetType().Name != otherShipShip.GetType().Name) + { + return false; + } + if (ship.Speed != otherShipShip.Speed) + { + return false; + } + if (ship.Weight != otherShipShip.Weight) + { + return false; + } + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + if (ship is EntityMotorBoat liner && otherShipShip is EntityMotorBoat otherLiner) + { + if (liner.DopColor != otherLiner.DopColor) + { + return false; + } + if (liner.SportLine != otherLiner.SportLine) + { + return false; + } + if (liner.Wing != otherLiner.Wing) + { + return false; + } + } + return true; + } } } diff --git a/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs index 1894cbd..0de2f59 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.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.ButtonDeleteMap = new System.Windows.Forms.Button(); this.ListBoxMaps = new System.Windows.Forms.ListBox(); this.ButtonAddMap = new System.Windows.Forms.Button(); @@ -57,6 +59,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.ButtonSortColor); + this.groupBoxTools.Controls.Add(this.ButtonSortType); this.groupBoxTools.Controls.Add(this.ButtonDeleteMap); this.groupBoxTools.Controls.Add(this.ListBoxMaps); this.groupBoxTools.Controls.Add(this.ButtonAddMap); @@ -79,6 +83,26 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "v"; // + // ButtonSortColor + // + this.ButtonSortColor.Location = new System.Drawing.Point(130, 268); + this.ButtonSortColor.Name = "ButtonSortColor"; + this.ButtonSortColor.Size = new System.Drawing.Size(83, 23); + this.ButtonSortColor.TabIndex = 36; + this.ButtonSortColor.Text = "По цвету"; + this.ButtonSortColor.UseVisualStyleBackColor = true; + this.ButtonSortColor.Click += new System.EventHandler(this.ButtonSortColor_Click); + // + // ButtonSortType + // + this.ButtonSortType.Location = new System.Drawing.Point(38, 268); + this.ButtonSortType.Name = "ButtonSortType"; + this.ButtonSortType.Size = new System.Drawing.Size(86, 23); + this.ButtonSortType.TabIndex = 35; + this.ButtonSortType.Text = "По типу"; + this.ButtonSortType.UseVisualStyleBackColor = true; + this.ButtonSortType.Click += new System.EventHandler(this.ButtonSortType_Click); + // // ButtonDeleteMap // this.ButtonDeleteMap.Location = new System.Drawing.Point(38, 209); @@ -118,7 +142,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 328); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 359); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -127,7 +151,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 357); + this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 388); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(175, 35); this.buttonRemoveBoat.TabIndex = 14; @@ -137,7 +161,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(38, 412); + this.buttonShowStorage.Location = new System.Drawing.Point(38, 443); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 15; @@ -150,7 +174,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::MotorBoat.Properties.Resources.d; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(112, 529); + this.buttonDown.Location = new System.Drawing.Point(112, 560); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 20; @@ -162,7 +186,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::MotorBoat.Properties.Resources.up; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(148, 529); + this.buttonRight.Location = new System.Drawing.Point(148, 560); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 19; @@ -174,7 +198,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::MotorBoat.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(76, 529); + this.buttonLeft.Location = new System.Drawing.Point(76, 560); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 18; @@ -186,7 +210,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::MotorBoat.Properties.Resources.r; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(112, 493); + this.buttonUp.Location = new System.Drawing.Point(112, 524); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 17; @@ -195,7 +219,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(38, 453); + this.buttonShowOnMap.Location = new System.Drawing.Point(38, 484); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); this.buttonShowOnMap.TabIndex = 16; @@ -205,7 +229,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(38, 287); + this.buttonAddBoat.Location = new System.Drawing.Point(38, 318); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(175, 35); this.buttonAddBoat.TabIndex = 12; @@ -225,7 +249,6 @@ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); this.comboBoxSelectorMap.TabIndex = 11; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // pictureBox // @@ -258,14 +281,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); // @@ -322,5 +345,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/MotorBoat/MotorBoat/FormMapWithSetBoats.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs index 43324ed..b14da86 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs @@ -16,7 +16,6 @@ namespace MotorBoat /// /// Объект от класса карты с набором объектов /// - private MapWithSetBoatsGeneric _mapShipCollectionGeneric; private readonly Dictionary _mapDict = new() { {"Простая карта", new SimpleMap() }, @@ -60,32 +59,6 @@ namespace MotorBoat } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, - EventArgs e) - { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Розовая карта": - map = new PinkMap(); - break; - case "Морская карта": - map = new SeaMap(); - break; - } - if (map != null) - { - _mapShipCollectionGeneric = new MapWithSetBoatsGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapShipCollectionGeneric = null; - } - } /// /// Выбор карты /// @@ -314,6 +287,27 @@ namespace MotorBoat } } } + + private void ButtonSortType_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 ButtonSortColor_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/MotorBoat/MotorBoat/IDrawningObject.cs b/MotorBoat/MotorBoat/IDrawningObject.cs index dea6b07..da0f999 100644 --- a/MotorBoat/MotorBoat/IDrawningObject.cs +++ b/MotorBoat/MotorBoat/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace MotorBoat { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs b/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs index aa3ceb9..2a85d85 100644 --- a/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs +++ b/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs @@ -2,7 +2,7 @@ { /// Карта с набром объектов под нее internal class MapWithSetBoatsGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// Ширина окна отрисовки @@ -67,6 +67,13 @@ return new(_pictureWidth, _pictureHeight); } /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + + public void Sort(IComparer comparer) + { + _setBoats.SortSet(comparer); + } + private void Shaking() { int j = _setBoats.Count - 1; diff --git a/MotorBoat/MotorBoat/SetBoatsGeneric.cs b/MotorBoat/MotorBoat/SetBoatsGeneric.cs index 19967d9..56ca73a 100644 --- a/MotorBoat/MotorBoat/SetBoatsGeneric.cs +++ b/MotorBoat/MotorBoat/SetBoatsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace MotorBoat { internal class SetBoatsGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -19,7 +19,12 @@ namespace MotorBoat } public int Insert(T boat) { - if (_places.Count > _maxCount) + if (_places.Contains(boat)) + { + return -1; + } + + if (_places.Count > _maxCount) { return -1; } @@ -75,5 +80,14 @@ namespace MotorBoat } } } - } + + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } + } } \ No newline at end of file