From 94a7aca855f5c8969daaedbbc680b470eb64e4f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Dec 2022 17:12:38 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningObjectBomber.cs | 31 +++++++++++++++++++ AirBomber/AirBomber/IDrawningObject.cs | 2 +- .../AirBomber/MapWithSetAirBomberGeneric.cs | 2 +- AirBomber/AirBomber/SetAirBomberGeneric.cs | 3 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/AirBomber/AirBomber/DrawningObjectBomber.cs b/AirBomber/AirBomber/DrawningObjectBomber.cs index b49ee71..e1d1679 100644 --- a/AirBomber/AirBomber/DrawningObjectBomber.cs +++ b/AirBomber/AirBomber/DrawningObjectBomber.cs @@ -40,5 +40,36 @@ namespace AirBomber public string GetInfo() => _airBomber?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectBomber(data.CreateDrawningAirBomber()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherBomber = other as DrawningObjectBomber; + if (otherBomber == null) + { + return false; + } + var airBomber = _airBomber.AirBomber; + var otherBomberBomber = otherBomber._airBomber.AirBomber; + if (airBomber.Speed != otherBomberBomber.Speed) + { + return false; + } + if (airBomber.Weight != otherBomberBomber.Weight) + { + return false; + } + if (airBomber.BodyColor != otherBomberBomber.BodyColor) + { + return false; + } + + //TODO + + return true; + } } } diff --git a/AirBomber/AirBomber/IDrawningObject.cs b/AirBomber/AirBomber/IDrawningObject.cs index a32cb67..222f745 100644 --- a/AirBomber/AirBomber/IDrawningObject.cs +++ b/AirBomber/AirBomber/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirBomber { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { public float Step { get; } diff --git a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs index 8e9e2ef..33bd31d 100644 --- a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { internal class MapWithSetAirBomberGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; diff --git a/AirBomber/AirBomber/SetAirBomberGeneric.cs b/AirBomber/AirBomber/SetAirBomberGeneric.cs index 4cac74d..283664e 100644 --- a/AirBomber/AirBomber/SetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/SetAirBomberGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { internal class SetAirBomberGeneric - where T : class + where T : class, IEquatable { private readonly List _places; @@ -28,6 +28,7 @@ namespace AirBomber public int Insert(T airBomber, int position) { + //TODO проверка на уникальность if (_places.Count >= _maxCount) { throw new StorageOverflowException(_maxCount); -- 2.25.1 From 04124e04f718ac7385a6c0fbfbbe8040a61dc2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Dec 2022 17:29:46 +0400 Subject: [PATCH 2/3] =?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 --- .../AirBomber/AirBomberCompareByColor.cs | 17 ++++++ AirBomber/AirBomber/AirBomberCompareByType.cs | 54 +++++++++++++++++++ AirBomber/AirBomber/DrawningObjectBomber.cs | 2 + .../FormMapWithSetAirBomber.Designer.cs | 34 ++++++++++-- .../AirBomber/FormMapWithSetAirBomber.cs | 15 ++++++ .../AirBomber/MapWithSetAirBomberGeneric.cs | 5 ++ AirBomber/AirBomber/SetAirBomberGeneric.cs | 9 ++++ 7 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 AirBomber/AirBomber/AirBomberCompareByColor.cs create mode 100644 AirBomber/AirBomber/AirBomberCompareByType.cs diff --git a/AirBomber/AirBomber/AirBomberCompareByColor.cs b/AirBomber/AirBomber/AirBomberCompareByColor.cs new file mode 100644 index 0000000..10d0870 --- /dev/null +++ b/AirBomber/AirBomber/AirBomberCompareByColor.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class AirBomberCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + // TODO реализовать логику сравнения + throw new NotImplementedException(); + } + } +} diff --git a/AirBomber/AirBomber/AirBomberCompareByType.cs b/AirBomber/AirBomber/AirBomberCompareByType.cs new file mode 100644 index 0000000..32d8065 --- /dev/null +++ b/AirBomber/AirBomber/AirBomberCompareByType.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class AirBomberCompareByType : 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 xAirBomber = x as DrawningObjectBomber; + var yAirBomber = y as DrawningObjectBomber; + if (xAirBomber == null && yAirBomber == null) + { + return 0; + } + if (xAirBomber == null && yAirBomber != null) + { + return 1; + } + if (xAirBomber != null && yAirBomber == null) + { + return -1; + } + if (xAirBomber.GetBomber.GetType().Name != yAirBomber.GetBomber.GetType().Name) + { + if (xAirBomber.GetBomber.GetType().Name == "DrawningBomber") + { + return -1; + } + } + var speedCompare = xAirBomber.GetBomber.AirBomber.Speed.CompareTo(yAirBomber.GetBomber.AirBomber.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirBomber.GetBomber.AirBomber.Weight.CompareTo(yAirBomber.GetBomber.AirBomber.Weight); + } + } +} diff --git a/AirBomber/AirBomber/DrawningObjectBomber.cs b/AirBomber/AirBomber/DrawningObjectBomber.cs index e1d1679..7d520cd 100644 --- a/AirBomber/AirBomber/DrawningObjectBomber.cs +++ b/AirBomber/AirBomber/DrawningObjectBomber.cs @@ -17,6 +17,8 @@ namespace AirBomber public float Step => _airBomber?.AirBomber?.Step ?? 0; + public DrawningBomber GetBomber => _airBomber; + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { return _airBomber?.GetCurrentPosition() ?? default; diff --git a/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs index 27df940..df9834b 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.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.buttonRight); this.groupBox1.Controls.Add(this.buttonDown); @@ -86,7 +90,7 @@ this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); this.groupBoxMaps.Location = new System.Drawing.Point(22, 32); this.groupBoxMaps.Name = "groupBoxMaps"; - this.groupBoxMaps.Size = new System.Drawing.Size(150, 326); + this.groupBoxMaps.Size = new System.Drawing.Size(150, 297); this.groupBoxMaps.TabIndex = 11; this.groupBoxMaps.TabStop = false; this.groupBoxMaps.Text = "Карты"; @@ -103,7 +107,7 @@ // // buttonDeleteMap // - this.buttonDeleteMap.Location = new System.Drawing.Point(0, 273); + this.buttonDeleteMap.Location = new System.Drawing.Point(0, 255); this.buttonDeleteMap.Name = "buttonDeleteMap"; this.buttonDeleteMap.Size = new System.Drawing.Size(150, 34); this.buttonDeleteMap.TabIndex = 4; @@ -267,14 +271,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); // @@ -287,6 +291,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(22, 344); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(150, 29); + this.ButtonSortByType.TabIndex = 12; + this.ButtonSortByType.Text = "Сортировать по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(22, 379); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(150, 28); + this.ButtonSortByColor.TabIndex = 13; + this.ButtonSortByColor.Text = "Сортировать по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetAirBomber // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -335,5 +359,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/AirBomber/AirBomber/FormMapWithSetAirBomber.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs index d014f00..9e1e24b 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs @@ -261,5 +261,20 @@ namespace AirBomber } } } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AirBomberCompareByType()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + // TODO прописать логику + } } } diff --git a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs index 33bd31d..0cfbc9a 100644 --- a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs @@ -83,6 +83,11 @@ namespace AirBomber } } + public void Sort(IComparer comparer) + { + _setAirBomber.SortSet(comparer); + } + private void Shaking() { int j = _setAirBomber.Count - 1; diff --git a/AirBomber/AirBomber/SetAirBomberGeneric.cs b/AirBomber/AirBomber/SetAirBomberGeneric.cs index 283664e..edeaed8 100644 --- a/AirBomber/AirBomber/SetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/SetAirBomberGeneric.cs @@ -88,5 +88,14 @@ namespace AirBomber } } } + + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } } -- 2.25.1 From 69e2561f87904adc3fd365abb3a5c7d835840fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Dec 2022 18:56:48 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB?= =?UTF-8?q?=208=20=D0=BB=D0=B0=D0=B1=D1=83=20=D0=B4=D0=BE=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AbstractMap.cs | 24 ++++++++- .../AirBomber/AirBomberCompareByColor.cs | 50 ++++++++++++++++++- AirBomber/AirBomber/AirBomberCompareByType.cs | 2 +- AirBomber/AirBomber/DrawningObjectBomber.cs | 23 ++++++++- .../AirBomber/FormMapWithSetAirBomber.cs | 11 ++-- AirBomber/AirBomber/SetAirBomberGeneric.cs | 5 +- 6 files changed, 104 insertions(+), 11 deletions(-) diff --git a/AirBomber/AirBomber/AbstractMap.cs b/AirBomber/AirBomber/AbstractMap.cs index 09a11d9..222f669 100644 --- a/AirBomber/AirBomber/AbstractMap.cs +++ b/AirBomber/AirBomber/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirBomber { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; @@ -122,5 +122,27 @@ namespace AirBomber 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/AirBomber/AirBomber/AirBomberCompareByColor.cs b/AirBomber/AirBomber/AirBomberCompareByColor.cs index 10d0870..9b118fe 100644 --- a/AirBomber/AirBomber/AirBomberCompareByColor.cs +++ b/AirBomber/AirBomber/AirBomberCompareByColor.cs @@ -10,8 +10,54 @@ namespace AirBomber { public int Compare(IDrawningObject? x, IDrawningObject? y) { - // TODO реализовать логику сравнения - throw new NotImplementedException(); + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAirBomber = x as DrawningObjectBomber; + var yAirBomber = y as DrawningObjectBomber; + if (xAirBomber == null && yAirBomber == null) + { + return 0; + } + if (xAirBomber == null && yAirBomber != null) + { + return 1; + } + if (xAirBomber != null && yAirBomber == null) + { + return -1; + } + string xAirBomberColor = xAirBomber.GetBomber.AirBomber.BodyColor.Name; + string yAirBomberColor = yAirBomber.GetBomber.AirBomber.BodyColor.Name; + if (xAirBomberColor != yAirBomberColor) + { + return xAirBomberColor.CompareTo(yAirBomberColor); + } + if (xAirBomber.GetBomber.AirBomber is EntityWarplane xWarplane && yAirBomber.GetBomber.AirBomber is EntityWarplane yWarplane) + { + string xWarplaneDopColor = xWarplane.DopColor.Name; + string yWarplaneDopColor = yWarplane.DopColor.Name; + var dopColorCompare = xWarplaneDopColor.CompareTo(yWarplaneDopColor); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xAirBomber.GetBomber.AirBomber.Speed.CompareTo(yAirBomber.GetBomber.AirBomber.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirBomber.GetBomber.AirBomber.Weight.CompareTo(yAirBomber.GetBomber.AirBomber.Weight); } } } diff --git a/AirBomber/AirBomber/AirBomberCompareByType.cs b/AirBomber/AirBomber/AirBomberCompareByType.cs index 32d8065..96825ab 100644 --- a/AirBomber/AirBomber/AirBomberCompareByType.cs +++ b/AirBomber/AirBomber/AirBomberCompareByType.cs @@ -11,7 +11,7 @@ namespace AirBomber public int Compare(IDrawningObject? x, IDrawningObject? y) { if (x == null && y == null) - { + { return 0; } if (x == null && y != null) diff --git a/AirBomber/AirBomber/DrawningObjectBomber.cs b/AirBomber/AirBomber/DrawningObjectBomber.cs index 7d520cd..e72e856 100644 --- a/AirBomber/AirBomber/DrawningObjectBomber.cs +++ b/AirBomber/AirBomber/DrawningObjectBomber.cs @@ -65,12 +65,31 @@ namespace AirBomber return false; } if (airBomber.BodyColor != otherBomberBomber.BodyColor) + { + return false; + } + + if ((airBomber is EntityWarplane) && !(otherBomberBomber is EntityWarplane) + || !(airBomber is EntityWarplane) && (otherBomberBomber is EntityWarplane)) { return false; } - //TODO - + if (airBomber is EntityWarplane warplane && otherBomberBomber is EntityWarplane otherWarplane) + { + if (warplane.DopColor != otherWarplane.DopColor) + { + return false; + } + if (warplane.Weapons != otherWarplane.Weapons) + { + return false; + } + if (warplane.Engines != otherWarplane.Engines) + { + return false; + } + } return true; } } diff --git a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs index 9e1e24b..ddb4a3d 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs @@ -12,9 +12,7 @@ using System.Windows.Forms; namespace AirBomber { public partial class FormMapWithSetAirBomber : Form - { - private MapWithSetAirBomberGeneric _mapAirBomberCollectionGeneric; - + { private readonly Dictionary _mapsDict = new() { { "Простая карта", new SimpleMap() }, @@ -274,7 +272,12 @@ namespace AirBomber private void ButtonSortByColor_Click(object sender, EventArgs e) { - // TODO прописать логику + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AirBomberCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } } } diff --git a/AirBomber/AirBomber/SetAirBomberGeneric.cs b/AirBomber/AirBomber/SetAirBomberGeneric.cs index edeaed8..8be340e 100644 --- a/AirBomber/AirBomber/SetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/SetAirBomberGeneric.cs @@ -28,7 +28,10 @@ namespace AirBomber public int Insert(T airBomber, int position) { - //TODO проверка на уникальность + if (_places.Contains(airBomber)) + { + return -1; + } if (_places.Count >= _maxCount) { throw new StorageOverflowException(_maxCount); -- 2.25.1