From a04ab480388aded6ea8a0bfdba8280f48709c4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B9=D0=B4=D0=B0=D1=80?= Date: Sun, 4 Dec 2022 13:36:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/BattleshipCompareByColor.cs | 57 ++++++++++++++++++ .../Battleship/BattleshipCompareByType.cs | 50 ++++++++++++++++ .../Battleship/DrawningObjectBattleship.cs | 45 ++++++++++++++ .../FormMapWithSetBattleship.Designer.cs | 60 ++++++++++++++----- .../Battleship/FormMapWithSetBattleship.cs | 18 ++++++ .../Battleship/FormMapWithSetBattleship.resx | 12 ++++ Battleship/Battleship/IDrawningObject.cs | 2 +- .../Battleship/MapWithSetBattleshipGeneric.cs | 7 ++- Battleship/Battleship/SetBattleshipGeneric.cs | 10 +++- 9 files changed, 242 insertions(+), 19 deletions(-) create mode 100644 Battleship/Battleship/BattleshipCompareByColor.cs create mode 100644 Battleship/Battleship/BattleshipCompareByType.cs diff --git a/Battleship/Battleship/BattleshipCompareByColor.cs b/Battleship/Battleship/BattleshipCompareByColor.cs new file mode 100644 index 0000000..2ce555d --- /dev/null +++ b/Battleship/Battleship/BattleshipCompareByColor.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + internal class BattleshipCompareByColor : 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 xBattleship = x as DrawningObjectBattleship; + var yBattleship = y as DrawningObjectBattleship; + + if (xBattleship == null && yBattleship == null) + return 0; + + if (xBattleship == null && yBattleship != null) + return 1; + + if (xBattleship != null && yBattleship == null) + return -1; + + int xColor = xBattleship.GetBattleship.Battleship.BodyColor.ToArgb(); + int yColor = yBattleship.GetBattleship.Battleship.BodyColor.ToArgb(); + + if (xColor != yColor) + return xColor.CompareTo(yColor); + + if (xBattleship.GetBattleship.Battleship is EntityGunBattleship xAdvanced && yBattleship.GetBattleship.Battleship is EntityGunBattleship yAdvanced) + { + xColor = xAdvanced.DopColor.ToArgb(); + yColor = yAdvanced.DopColor.ToArgb(); + + if (xColor != yColor) + return xColor.CompareTo(yColor); + + } + + var speedCompare = xBattleship.GetBattleship.Battleship.Speed.CompareTo(yBattleship.GetBattleship.Battleship.Speed); + if (speedCompare != 0) + return speedCompare; + + return xBattleship.GetBattleship.Battleship.Weight.CompareTo(yBattleship.GetBattleship.Battleship.Weight); + } + } +} diff --git a/Battleship/Battleship/BattleshipCompareByType.cs b/Battleship/Battleship/BattleshipCompareByType.cs new file mode 100644 index 0000000..47ce496 --- /dev/null +++ b/Battleship/Battleship/BattleshipCompareByType.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + internal class BattleshipCompareByType : 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 xBattleship = x as DrawningObjectBattleship; + var yBattleship = y as DrawningObjectBattleship; + + if (xBattleship == null && yBattleship == null) + return 0; + + if (xBattleship == null && yBattleship != null) + return 1; + + if (xBattleship != null && yBattleship == null) + return -1; + + if (xBattleship.GetBattleship.GetType().Name != yBattleship.GetBattleship.GetType().Name) + { + if (xBattleship.GetBattleship.GetType().Name == "DrawningBattleship") + return -1; + + return 1; + } + + var speedCompare = xBattleship.GetBattleship.Battleship.Speed.CompareTo(yBattleship.GetBattleship.Battleship.Speed); + + if (speedCompare != 0) + return speedCompare; + + return xBattleship.GetBattleship.Battleship.Weight.CompareTo(yBattleship.GetBattleship.Battleship.Weight); + } + } +} diff --git a/Battleship/Battleship/DrawningObjectBattleship.cs b/Battleship/Battleship/DrawningObjectBattleship.cs index 5a0917f..06f56b4 100644 --- a/Battleship/Battleship/DrawningObjectBattleship.cs +++ b/Battleship/Battleship/DrawningObjectBattleship.cs @@ -17,6 +17,8 @@ namespace Battleship public float Step => _battleship?.Battleship?.Step ?? 0; + public DrawningBattleship GetBattleship => _battleship; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _battleship?.GetCurrentPosition() ?? default; @@ -40,5 +42,48 @@ namespace Battleship public string GetInfo() => _battleship?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectBattleship(data.CreateDrawningBattleship()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + return false; + + var otherBattleship = other as DrawningObjectBattleship; + + if (otherBattleship == null) + return false; + + var battleship = _battleship.Battleship; + var otherBattleshipBattleship = otherBattleship._battleship.Battleship; + + if (battleship.GetType() != otherBattleshipBattleship.GetType()) + return false; + + if (battleship.Speed != otherBattleshipBattleship.Speed) + return false; + + if (battleship.Weight != otherBattleshipBattleship.Weight) + return false; + + if (battleship.BodyColor != otherBattleshipBattleship.BodyColor) + return false; + + if (battleship is EntityGunBattleship adv && otherBattleshipBattleship is EntityGunBattleship otherAdv) + { + if (adv.DopColor != otherAdv.DopColor) + return false; + + if (adv.CompartmentRocket != otherAdv.CompartmentRocket) + return false; + + if (adv.GunTower != otherAdv.GunTower) + return false; + + if (adv.SternFence != otherAdv.SternFence) + return false; + } + + return true; + } } } diff --git a/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs b/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs index db68d7e..e5762bc 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs +++ b/Battleship/Battleship/FormMapWithSetBattleship.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.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Controls.Add(this.buttonLeft); @@ -70,9 +74,9 @@ this.groupBoxTools.Controls.Add(this.buttonRemoveBattleship); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(744, 24); + this.groupBoxTools.Location = new System.Drawing.Point(748, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 587); + this.groupBoxTools.Size = new System.Drawing.Size(200, 623); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; @@ -149,7 +153,7 @@ // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Battleship.Properties.Resources.BatteleshipDown; - this.buttonDown.Location = new System.Drawing.Point(77, 543); + this.buttonDown.Location = new System.Drawing.Point(77, 579); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -160,7 +164,7 @@ // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Battleship.Properties.Resources.BattleshipLeft; - this.buttonLeft.Location = new System.Drawing.Point(41, 543); + this.buttonLeft.Location = new System.Drawing.Point(41, 579); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -170,7 +174,7 @@ // buttonAddBattleship // this.buttonAddBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddBattleship.Location = new System.Drawing.Point(24, 326); + this.buttonAddBattleship.Location = new System.Drawing.Point(24, 362); this.buttonAddBattleship.Name = "buttonAddBattleship"; this.buttonAddBattleship.Size = new System.Drawing.Size(164, 30); this.buttonAddBattleship.TabIndex = 1; @@ -182,7 +186,7 @@ // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Battleship.Properties.Resources.BattleshipUp; - this.buttonUp.Location = new System.Drawing.Point(77, 507); + this.buttonUp.Location = new System.Drawing.Point(77, 543); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -193,7 +197,7 @@ // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Battleship.Properties.Resources.BattleshipRight; - this.buttonRight.Location = new System.Drawing.Point(113, 543); + this.buttonRight.Location = new System.Drawing.Point(113, 579); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 6; @@ -203,7 +207,7 @@ // buttonShowOnMap // this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowOnMap.Location = new System.Drawing.Point(24, 462); + this.buttonShowOnMap.Location = new System.Drawing.Point(24, 498); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(164, 30); this.buttonShowOnMap.TabIndex = 5; @@ -214,7 +218,7 @@ // buttonShowStorage // this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowStorage.Location = new System.Drawing.Point(24, 426); + this.buttonShowStorage.Location = new System.Drawing.Point(24, 462); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(164, 30); this.buttonShowStorage.TabIndex = 4; @@ -225,7 +229,7 @@ // buttonRemoveBattleship // this.buttonRemoveBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRemoveBattleship.Location = new System.Drawing.Point(24, 391); + this.buttonRemoveBattleship.Location = new System.Drawing.Point(24, 427); this.buttonRemoveBattleship.Name = "buttonRemoveBattleship"; this.buttonRemoveBattleship.Size = new System.Drawing.Size(164, 29); this.buttonRemoveBattleship.TabIndex = 3; @@ -236,7 +240,7 @@ // maskedTextBoxPosition // this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 362); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 398); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23); @@ -247,7 +251,7 @@ this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 24); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(744, 587); + this.pictureBox.Size = new System.Drawing.Size(748, 623); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // @@ -257,7 +261,7 @@ this.файлToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(944, 24); + this.menuStrip.Size = new System.Drawing.Size(948, 24); this.menuStrip.TabIndex = 1; // // файлToolStripMenuItem @@ -272,14 +276,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); // @@ -291,11 +295,33 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSortByType.Location = new System.Drawing.Point(23, 291); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(164, 29); + this.buttonSortByType.TabIndex = 2; + this.buttonSortByType.Text = "Сортировка по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSortByColor.Location = new System.Drawing.Point(24, 326); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(164, 30); + this.buttonSortByColor.TabIndex = 11; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetBattleship // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(944, 611); + this.ClientSize = new System.Drawing.Size(948, 647); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -339,5 +365,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/Battleship/Battleship/FormMapWithSetBattleship.cs b/Battleship/Battleship/FormMapWithSetBattleship.cs index 5a244f9..326216a 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.cs +++ b/Battleship/Battleship/FormMapWithSetBattleship.cs @@ -266,6 +266,24 @@ namespace Battleship } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + return; + + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BattleshipCompareByType()); + 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 BattleshipCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } diff --git a/Battleship/Battleship/FormMapWithSetBattleship.resx b/Battleship/Battleship/FormMapWithSetBattleship.resx index f298a7b..18ac157 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.resx +++ b/Battleship/Battleship/FormMapWithSetBattleship.resx @@ -57,4 +57,16 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 125, 17 + + + 258, 17 + + + 25 + \ No newline at end of file diff --git a/Battleship/Battleship/IDrawningObject.cs b/Battleship/Battleship/IDrawningObject.cs index 16fb2de..e9b970b 100644 --- a/Battleship/Battleship/IDrawningObject.cs +++ b/Battleship/Battleship/IDrawningObject.cs @@ -9,7 +9,7 @@ namespace Battleship /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs index 6c95100..0dd13fd 100644 --- a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -12,7 +12,7 @@ namespace Battleship /// /// internal class MapWithSetBattleshipGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; @@ -94,6 +94,11 @@ namespace Battleship } } + public void Sort(IComparer comparer) + { + _setBattleship.SortSet(comparer); + } + public void Shaking() { int j = _setBattleship.Count - 1; diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs index 2e15199..0517274 100644 --- a/Battleship/Battleship/SetBattleshipGeneric.cs +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -12,7 +12,7 @@ namespace Battleship /// /// internal class SetBattleshipGeneric - where T : class + where T : class, IEquatable { private readonly List _places; @@ -94,5 +94,13 @@ namespace Battleship } } } + + public void SortSet(IComparer comparer) + { + if (comparer == null) + return; + + _places.Sort(comparer); + } } }