From 9da6b57bd9f1687b7d68af0a04342b1a9bee18a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 1 Nov 2022 20:24:33 +0400 Subject: [PATCH 1/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=D0=B7=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=87=D0=B0=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/AbstractMap.cs | 29 +++++- AirBomber/AirBomber/AirplaneCompareByColor.cs | 33 +++++++ AirBomber/AirBomber/AirplaneCompareByType.cs | 37 ++++++++ AirBomber/AirBomber/DrawningObject.cs | 30 ++++++- .../FormMapWithSetAirplanes.Designer.cs | 90 ++++++++++++------- .../AirBomber/FormMapWithSetAirplanes.cs | 25 +++++- AirBomber/AirBomber/IDrawningObject.cs | 2 +- .../AirBomber/MapWithSetAirplanesGeneric.cs | 11 ++- AirBomber/AirBomber/SetAirplanesGeneric.cs | 19 +++- 9 files changed, 236 insertions(+), 40 deletions(-) create mode 100644 AirBomber/AirBomber/AirplaneCompareByColor.cs create mode 100644 AirBomber/AirBomber/AirplaneCompareByType.cs diff --git a/AirBomber/AirBomber/AbstractMap.cs b/AirBomber/AirBomber/AbstractMap.cs index 8cc655f..e11d0ee 100644 --- a/AirBomber/AirBomber/AbstractMap.cs +++ b/AirBomber/AirBomber/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; private Bitmap? _staticBitMap; @@ -172,5 +172,32 @@ 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; + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/AirplaneCompareByColor.cs b/AirBomber/AirBomber/AirplaneCompareByColor.cs new file mode 100644 index 0000000..d9915bf --- /dev/null +++ b/AirBomber/AirBomber/AirplaneCompareByColor.cs @@ -0,0 +1,33 @@ +namespace AirBomber +{ + internal class AirplaneCompareByColor : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + var xAirplane = (DrawningAirplane?)(x as DrawningObject); + var yAirplane = (DrawningAirplane?)(y as DrawningObject); + if (xAirplane == yAirplane) + { + return 0; + } + if (xAirplane == null) + { + return 1; + } + if (yAirplane == null) + { + return -1; + } + var xEntity = xAirplane.Airplane; + var yEntity = yAirplane.Airplane; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || + xEntity is not EntityAirBomber xEntityAirBomber || + yEntity is not EntityAirBomber yEntityAirBomber) + { + return colorWeight; + } + return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb()); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/AirplaneCompareByType.cs b/AirBomber/AirBomber/AirplaneCompareByType.cs new file mode 100644 index 0000000..f237f9a --- /dev/null +++ b/AirBomber/AirBomber/AirplaneCompareByType.cs @@ -0,0 +1,37 @@ +namespace AirBomber +{ + internal class AirplaneCompareByType : IComparer + { + public int Compare(IDrawningObject? x, IDrawningObject? y) + { + var xAirplane = (DrawningAirplane?)(x as DrawningObject); + var yAirplane = (DrawningAirplane?)(y as DrawningObject); + if (xAirplane == yAirplane) + { + return 0; + } + if (xAirplane == null) + { + return 1; + } + if (yAirplane == null) + { + return -1; + } + if (xAirplane.GetType().Name != yAirplane.GetType().Name) + { + if (xAirplane.Airplane.GetType() == typeof(DrawningAirplane)) + { + return -1; + } + return 1; + } + var speedCompare = xAirplane.Airplane.Speed.CompareTo(yAirplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirplane.Airplane.Weight.CompareTo(yAirplane.Airplane.Weight); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/DrawningObject.cs b/AirBomber/AirBomber/DrawningObject.cs index f2d1a16..ba53095 100644 --- a/AirBomber/AirBomber/DrawningObject.cs +++ b/AirBomber/AirBomber/DrawningObject.cs @@ -42,10 +42,36 @@ namespace AirBomber /// /// The drawning object. /// - public static explicit operator DrawningAirplane(DrawningObject drawningObject) => drawningObject._airplane; + public static explicit operator DrawningAirplane?(DrawningObject? drawningObject) => drawningObject?._airplane ?? null; - public string GetInfo() => _airplane?.GetDataForSave(); + public string GetInfo() => _airplane?.GetDataForSave() ?? string.Empty; public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane()); + + public bool Equals(IDrawningObject? other) + { + if (other is not DrawningObject otherAirplane) + { + return false; + } + var entity = _airplane.Airplane; + var otherEntity = otherAirplane._airplane.Airplane; + if (entity.GetType() != otherEntity.GetType() || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || + entity.BodyColor != otherEntity.BodyColor) + { + return false; + } + if (entity is EntityAirBomber entityAirBomber && + otherEntity is EntityAirBomber otherEntityAirBomber && ( + entityAirBomber.HasBombs != otherEntityAirBomber.HasBombs || + entityAirBomber.DopColor != otherEntityAirBomber.DopColor || + entityAirBomber.HasFuelTanks != otherEntityAirBomber.HasFuelTanks)) + { + return false; + } + return true; + } } } diff --git a/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs b/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs index 073c34e..1a5d698 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.Designer.cs @@ -30,6 +30,8 @@ { this.comboTypeEngines = new System.Windows.Forms.ComboBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.btnAddDeletedObject = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonAddMap = new System.Windows.Forms.Button(); @@ -60,10 +62,10 @@ this.menuStrip = new System.Windows.Forms.MenuStrip(); this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); - this.SaveMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.groupBoxTools.SuspendLayout(); this.groupBoxMaps.SuspendLayout(); this.groupBoxGenerate.SuspendLayout(); @@ -89,6 +91,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByColor); + this.groupBoxTools.Controls.Add(this.buttonSortByType); this.groupBoxTools.Controls.Add(this.btnAddDeletedObject); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); @@ -102,16 +106,36 @@ this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(811, 24); + this.groupBoxTools.Location = new System.Drawing.Point(840, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 786); + this.groupBoxTools.Size = new System.Drawing.Size(409, 522); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(223, 413); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(175, 35); + this.buttonSortByColor.TabIndex = 12; + this.buttonSortByColor.Text = "Отсортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(223, 372); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(175, 35); + this.buttonSortByType.TabIndex = 28; + this.buttonSortByType.Text = "Отсортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // btnAddDeletedObject // - this.btnAddDeletedObject.Location = new System.Drawing.Point(17, 689); + this.btnAddDeletedObject.Location = new System.Drawing.Point(25, 317); this.btnAddDeletedObject.Name = "btnAddDeletedObject"; this.btnAddDeletedObject.Size = new System.Drawing.Size(175, 45); this.btnAddDeletedObject.TabIndex = 27; @@ -126,9 +150,9 @@ this.groupBoxMaps.Controls.Add(this.listBoxMaps); this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); - this.groupBoxMaps.Location = new System.Drawing.Point(6, 267); + this.groupBoxMaps.Location = new System.Drawing.Point(212, 14); this.groupBoxMaps.Name = "groupBoxMaps"; - this.groupBoxMaps.Size = new System.Drawing.Size(192, 248); + this.groupBoxMaps.Size = new System.Drawing.Size(192, 253); this.groupBoxMaps.TabIndex = 26; this.groupBoxMaps.TabStop = false; this.groupBoxMaps.Text = "Карты"; @@ -137,7 +161,7 @@ // this.buttonAddMap.Location = new System.Drawing.Point(11, 80); this.buttonAddMap.Name = "buttonAddMap"; - this.buttonAddMap.Size = new System.Drawing.Size(175, 35); + this.buttonAddMap.Size = new System.Drawing.Size(175, 47); this.buttonAddMap.TabIndex = 2; this.buttonAddMap.Text = "Добавить карту"; this.buttonAddMap.UseVisualStyleBackColor = true; @@ -145,9 +169,9 @@ // // buttonDeleteMap // - this.buttonDeleteMap.Location = new System.Drawing.Point(11, 206); + this.buttonDeleteMap.Location = new System.Drawing.Point(11, 218); this.buttonDeleteMap.Name = "buttonDeleteMap"; - this.buttonDeleteMap.Size = new System.Drawing.Size(175, 35); + this.buttonDeleteMap.Size = new System.Drawing.Size(175, 29); this.buttonDeleteMap.TabIndex = 4; this.buttonDeleteMap.Text = "Удалить карту"; this.buttonDeleteMap.UseVisualStyleBackColor = true; @@ -157,7 +181,7 @@ // this.listBoxMaps.FormattingEnabled = true; this.listBoxMaps.ItemHeight = 15; - this.listBoxMaps.Location = new System.Drawing.Point(11, 121); + this.listBoxMaps.Location = new System.Drawing.Point(11, 133); this.listBoxMaps.Name = "listBoxMaps"; this.listBoxMaps.Size = new System.Drawing.Size(175, 79); this.listBoxMaps.TabIndex = 3; @@ -184,7 +208,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 565); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 379); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -193,9 +217,9 @@ // // buttonRemoveAirplane // - this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 594); + this.buttonRemoveAirplane.Location = new System.Drawing.Point(25, 413); this.buttonRemoveAirplane.Name = "buttonRemoveAirplane"; - this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 26); + this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 35); this.buttonRemoveAirplane.TabIndex = 23; this.buttonRemoveAirplane.Text = "Удалить самолет"; this.buttonRemoveAirplane.UseVisualStyleBackColor = true; @@ -203,9 +227,9 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(17, 626); + this.buttonShowStorage.Location = new System.Drawing.Point(223, 273); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(175, 26); + this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 24; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -213,9 +237,9 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(17, 658); + this.buttonShowOnMap.Location = new System.Drawing.Point(223, 317); this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(175, 25); + this.buttonShowOnMap.Size = new System.Drawing.Size(175, 45); this.buttonShowOnMap.TabIndex = 25; this.buttonShowOnMap.Text = "Посмотреть карту"; this.buttonShowOnMap.UseVisualStyleBackColor = true; @@ -223,7 +247,7 @@ // // buttonAddAirplane // - this.buttonAddAirplane.Location = new System.Drawing.Point(17, 521); + this.buttonAddAirplane.Location = new System.Drawing.Point(23, 273); this.buttonAddAirplane.Name = "buttonAddAirplane"; this.buttonAddAirplane.Size = new System.Drawing.Size(175, 35); this.buttonAddAirplane.TabIndex = 21; @@ -333,7 +357,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(90, 750); + this.buttonDown.Location = new System.Drawing.Point(57, 486); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -345,7 +369,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(126, 750); + this.buttonRight.Location = new System.Drawing.Point(93, 486); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -357,7 +381,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(54, 750); + this.buttonLeft.Location = new System.Drawing.Point(21, 486); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -369,7 +393,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(90, 714); + this.buttonUp.Location = new System.Drawing.Point(57, 450); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -381,7 +405,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(811, 786); + this.pictureBox.Size = new System.Drawing.Size(840, 522); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -391,7 +415,7 @@ this.файлToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(1015, 24); + this.menuStrip.Size = new System.Drawing.Size(1249, 24); this.menuStrip.TabIndex = 2; // // файлToolStripMenuItem @@ -411,6 +435,13 @@ this.SaveToolStripMenuItem.Text = "Сохранение"; this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // + // SaveMapToolStripMenuItem + // + this.SaveMapToolStripMenuItem.Name = "SaveMapToolStripMenuItem"; + this.SaveMapToolStripMenuItem.Size = new System.Drawing.Size(280, 22); + this.SaveMapToolStripMenuItem.Text = "Сохранение Выбранного хранилища"; + this.SaveMapToolStripMenuItem.Click += new System.EventHandler(this.SaveMapToolStripMenuItem_Click); + // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; @@ -426,18 +457,11 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // - // SaveMapToolStripMenuItem - // - this.SaveMapToolStripMenuItem.Name = "SaveMapToolStripMenuItem"; - this.SaveMapToolStripMenuItem.Size = new System.Drawing.Size(280, 22); - this.SaveMapToolStripMenuItem.Text = "Сохранение Выбранного хранилища"; - this.SaveMapToolStripMenuItem.Click += new System.EventHandler(this.SaveMapToolStripMenuItem_Click); - // // FormMapWithSetAirplanes // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1015, 810); + this.ClientSize = new System.Drawing.Size(1249, 546); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip); @@ -499,5 +523,7 @@ private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; private ToolStripMenuItem SaveMapToolStripMenuItem; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs index 66ebb84..09867fb 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirplanes.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirplanes.cs @@ -143,7 +143,7 @@ namespace AirBomber { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[NameMap].ShowSet(); - _logger.LogInformation("Добавлен объект {@Airplane}", (DrawningAirplane)airplane); + _logger.LogInformation("Добавлен объект {@Airplane}", (DrawningAirplane?)airplane); } } catch (StorageOverflowException ex) @@ -151,6 +151,11 @@ namespace AirBomber _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, (DrawningAirplane?)airplane); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// /// Перемещение @@ -401,5 +406,23 @@ namespace AirBomber } } + + private void maskedTextBoxPosition_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) + { + + } + + private void SortBy(IComparer comparer) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonSortByType_Click (object sender, EventArgs e) => SortBy(new AirplaneCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new AirplaneCompareByColor()); } } \ No newline at end of file diff --git a/AirBomber/AirBomber/IDrawningObject.cs b/AirBomber/AirBomber/IDrawningObject.cs index 2394173..ef71303 100644 --- a/AirBomber/AirBomber/IDrawningObject.cs +++ b/AirBomber/AirBomber/IDrawningObject.cs @@ -9,7 +9,7 @@ namespace AirBomber /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - public interface IDrawningObject + public interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index 6c98db3..a25c924 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirBomber { internal class MapWithSetAirplanesGeneric - where T : class, IDrawningObject + where T : class, IEquatable, IDrawningObject where U : AbstractMap { /// @@ -205,5 +205,14 @@ namespace AirBomber SetAirplanes.Insert(DrawningObject.Create(rec) as T); } } + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + SetAirplanes.SortSet(comparer); + } } } diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index 2830350..d1a41d4 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -11,7 +11,7 @@ namespace AirBomber /// /// internal class SetAirplanesGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -51,9 +51,13 @@ namespace AirBomber /// /// Добавляемый самолет /// Позиция + /// Исключения генерируется при попытке добавить дубликат в набор + /// /// Возвращает позицию вставленого объекта либо -1, если не получилось его добавить public int Insert(T airplane, int position) { + if (_places.Contains(airplane)) + throw new ArgumentException($"Объект {airplane} уже есть в наборе"); if (Count == _maxcount) throw new StorageOverflowException(_maxcount); if (!isCorrectPosition(position)) @@ -111,7 +115,18 @@ namespace AirBomber yield break; } } - + } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); } } } -- 2.25.1 From 5e1f6097414c4828a60d5062ac3bcdca9b6ae1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 1 Nov 2022 21:30:26 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=D0=92=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D0=BA=D0=B5=20=D0=BD=D0=B0=20=D1=8D=D0=BA=D0=B2=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B5=D0=BD=D1=82=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D0=B0=D0=BC=D0=BE=D0=BB=D0=B5=D1=82=D0=BE=D0=B2?= =?UTF-8?q?,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D1=82=D0=B8=D0=BF=20=D0=B8=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B2=D0=BE=20=D0=B4=D0=B2=D0=B8=D0=B3=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningObject.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AirBomber/AirBomber/DrawningObject.cs b/AirBomber/AirBomber/DrawningObject.cs index ba53095..251f49a 100644 --- a/AirBomber/AirBomber/DrawningObject.cs +++ b/AirBomber/AirBomber/DrawningObject.cs @@ -50,7 +50,9 @@ namespace AirBomber public bool Equals(IDrawningObject? other) { - if (other is not DrawningObject otherAirplane) + if (other is not DrawningObject otherAirplane || + _airplane.DrawningEngines?.GetType() != otherAirplane._airplane.DrawningEngines?.GetType() || + _airplane.DrawningEngines?.CountEngines != otherAirplane._airplane.DrawningEngines?.CountEngines) { return false; } -- 2.25.1 From 8c6ce67917e3a038b61840c9c1ccbb4ef8dc48ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 1 Nov 2022 22:07:42 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index a25c924..537b76f 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirBomber { - internal class MapWithSetAirplanesGeneric + internal class MapWithSetAirplanesGeneric : IComparable> where T : class, IEquatable, IDrawningObject where U : AbstractMap { @@ -214,5 +214,18 @@ namespace AirBomber { SetAirplanes.SortSet(comparer); } + + public int CompareTo(MapWithSetAirplanesGeneric? other) + { + if (other == null) + { + return 1; + } + if (this == other) + { + return 0; + } + return SetAirplanes.Count.CompareTo(other.SetAirplanes.Count); + } } } -- 2.25.1 From 0f9caec5c3e129070c3c0ab6af0e0ddd1c7a18e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 2 Nov 2022 00:55:25 +0400 Subject: [PATCH 4/7] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5?= =?UTF-8?q?=D0=B9=D1=81=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20Airplane=20=D0=B8=20AirBomb?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningAirBomber.cs | 18 +++++++++ AirBomber/AirBomber/DrawningAirplane.cs | 49 +++++++++++++++++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/AirBomber/AirBomber/DrawningAirBomber.cs b/AirBomber/AirBomber/DrawningAirBomber.cs index f4af376..b240427 100644 --- a/AirBomber/AirBomber/DrawningAirBomber.cs +++ b/AirBomber/AirBomber/DrawningAirBomber.cs @@ -80,5 +80,23 @@ namespace AirBomber g.DrawLine(pen, baseTail, new PointF(r.Right, r.Top)); g.DrawLine(pen, baseTail, new PointF(r.Right, r.Bottom)); } + + /// + /// Возвращает итератор со свойствами самолета в порядке: + /// Скорость -> Вес -> Цвет корпуса -> Тип двигателя, либо null если их нет -> Количество двигателей -> Дополнительный цвет -> Наличие бомб -> Наличие топливных баков + /// + public override IEnumerator GetEnumerator() + { + IEnumerator enumBase = base.GetEnumerator(); + while (enumBase.MoveNext()) + { + yield return enumBase.Current; + } + var entity = (EntityAirBomber)Airplane; + yield return entity.DopColor; + yield return entity.HasBombs; + yield return entity.HasFuelTanks; + yield break; + } } } diff --git a/AirBomber/AirBomber/DrawningAirplane.cs b/AirBomber/AirBomber/DrawningAirplane.cs index 4c3c0c4..abeabfc 100644 --- a/AirBomber/AirBomber/DrawningAirplane.cs +++ b/AirBomber/AirBomber/DrawningAirplane.cs @@ -1,16 +1,24 @@ -namespace AirBomber +using Microsoft.CodeAnalysis.CSharp.Syntax; +using System.Collections; + +namespace AirBomber { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - public class DrawningAirplane + public class DrawningAirplane : IEnumerator, IEnumerable { /// /// Класс-сущность /// public EntityAirplane Airplane { get; protected set; } - + public IAirplaneEngines? DrawningEngines { get; protected set; } + + private IEnumerator? _machineStateEnum = null; + + public object Current => _machineStateEnum?.Current; + /// /// Левая координата отрисовки самолета /// @@ -97,7 +105,7 @@ _pictureHeight = height; // Проверка на нахождение предмета полностью в границах окна if (width <= _airplaneWidth || height <= _airplaneHeight - || _startPosX < 0 || _startPosX + _airplaneWidth > width || + || _startPosX < 0 || _startPosX + _airplaneWidth > width || _startPosY < 0 || _startPosY + _airplaneHeight > height) { _pictureWidth = null; @@ -163,14 +171,14 @@ var h = _airplaneHeight; SolidBrush BodyColorBrush = new(Airplane.BodyColor); // Треугольник самолета - g.FillPolygon(BodyColorBrush, new PointF[] { + g.FillPolygon(BodyColorBrush, new PointF[] { new PointF(x , y + h / 2), new PointF(x + 15, y + h / 2 - 7), new PointF(x + 15, y + h / 2 + 8), }); // Тело самолета g.FillRectangle(BodyColorBrush, x + 15, y + h / 2 - 7, w - 35, 15); - DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 - 7), new PointF(x + w / 2, y + h / 2 - 7), true , h / 2 - 7); + DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 - 7), new PointF(x + w / 2, y + h / 2 - 7), true, h / 2 - 7); DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 + 7), new PointF(x + w / 2, y + h / 2 + 7), false, h / 2 - 7); // Линия примыкающая к хвосту слева @@ -241,5 +249,34 @@ { return new(_startPosX, _startPosY, _airplaneWidth, _airplaneHeight); } + + public bool MoveNext() + { + if (_machineStateEnum == null) + { + _machineStateEnum = GetEnumerator(); + } + return _machineStateEnum.MoveNext(); + } + + public void Reset() => throw new NotSupportedException(); + + public void Dispose() => GC.SuppressFinalize(this); + + /// + /// Возвращает итератор со свойствами самолета в порядке: + /// Скорость -> Вес -> Цвет корпуса -> Тип двигателя, либо null если их нет -> Количество двигателей + /// + virtual public IEnumerator GetEnumerator() + { + yield return Airplane.Speed; + yield return Airplane.Weight; + yield return Airplane.BodyColor; + yield return DrawningEngines; + yield return DrawningEngines?.CountEngines ?? 0; + yield break; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } \ No newline at end of file -- 2.25.1 From 844514fc0d5a991e6bf659a30e8b8eb48abc2127 Mon Sep 17 00:00:00 2001 From: "d.agil" Date: Wed, 2 Nov 2022 13:12:01 +0400 Subject: [PATCH 5/7] fix --- AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs | 4 ++-- AirBomber/AirBomber/Program.cs | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index 537b76f..a828f0a 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -188,9 +188,9 @@ namespace AirBomber public string GetData(char separatorType, char separatorData) { string data = $"{_map.GetType().Name}{separatorType}"; - foreach (var car in SetAirplanes.GetAirplanes()) + foreach (var airplane in SetAirplanes.GetAirplanes()) { - data += $"{car.GetInfo()}{separatorData}"; + data += $"{airplane.GetInfo()}{separatorData}"; } return data; } diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index 043ac59..70ce32b 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -42,10 +42,6 @@ namespace AirBomber var logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) - .WriteTo.Logger(lc => lc - .Filter.ByIncludingOnly(f => - f.Level == Serilog.Events.LogEventLevel.Information) - .WriteTo.File("action_user.txt")) .CreateLogger(); return logger; } -- 2.25.1 From 44b8fefd9e31ae1d83af7563bb00a2482f846f77 Mon Sep 17 00:00:00 2001 From: "d.agil" Date: Wed, 2 Nov 2022 13:02:42 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirBomber/MapWithSetAirplanesGeneric.cs | 5 ++ AirBomber/AirBomber/MapsCollection.cs | 46 ++++++++++--------- AirBomber/AirBomber/SetAirplanesGeneric.cs | 5 ++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs index a828f0a..b116807 100644 --- a/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirplanesGeneric.cs @@ -180,6 +180,11 @@ namespace AirBomber } } + public void Clear() + { + SetAirplanes.Clear(); + } + /// /// Получение данных в виде строки /// diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs index 5b23807..acc92a8 100644 --- a/AirBomber/AirBomber/MapsCollection.cs +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -140,7 +140,14 @@ namespace AirBomber map = new WallMap(); break; } - _mapStorages[nameMapStorage] = new(_pictureWidth, _pictureHeight, map); + if (_mapStorages.ContainsKey(nameMapStorage)) + { + _mapStorages[nameMapStorage].Clear(); + } + else + { + _mapStorages[nameMapStorage] = new(_pictureWidth, _pictureHeight, map); + } _mapStorages[nameMapStorage].LoadData(lineData.Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); } @@ -154,32 +161,29 @@ namespace AirBomber { throw new FileNotFoundException("Файл не найден"); } - List strs = new List(); using (StreamReader fs = new(filename)) { + var formatData = fs.ReadLine() ?? string.Empty; + bool isNotMapsCollection = !formatData.Contains("MapsCollection"); + if (formatData.Contains("Map") && isNotMapsCollection) + { + var nameMap = fs.ReadLine().Replace("Name: ", ""); + var elem = fs.ReadLine().Split(separatorDict); + LoadMap(nameMap, elem[0], elem[1]); + } + if (isNotMapsCollection) + { + //если нет такой записи, то это не те данные + throw new FileFormatException("Формат данных в файле не правильный"); + } + //очищаем записи + _mapStorages.Clear(); while (!fs.EndOfStream) { - strs.Add(fs.ReadLine()); + var elem = fs.ReadLine().Split(separatorDict); + LoadMap(elem[0], elem[1], elem[2]); } } - bool isNotMapsCollection = !strs[0].Contains("MapsCollection"); - if (strs[0].Contains("Map") && isNotMapsCollection) - { - var elem = strs[2].Split(separatorDict); - LoadMap(strs[1].Replace("Name: ", ""), elem[0], elem[1]); - } - else if (isNotMapsCollection) - { - //если нет такой записи, то это не те данные - throw new FileFormatException("Формат данных в файле не правильный"); - } - //очищаем записи - _mapStorages.Clear(); - for (int i = 1; i < strs.Count; ++i) - { - var elem = strs[i].Split(separatorDict); - LoadMap(elem[0], elem[1], elem[2]); - } } } } diff --git a/AirBomber/AirBomber/SetAirplanesGeneric.cs b/AirBomber/AirBomber/SetAirplanesGeneric.cs index d1a41d4..40b5a2a 100644 --- a/AirBomber/AirBomber/SetAirplanesGeneric.cs +++ b/AirBomber/AirBomber/SetAirplanesGeneric.cs @@ -128,5 +128,10 @@ namespace AirBomber } _places.Sort(comparer); } + + public void Clear() + { + _places.Clear(); + } } } -- 2.25.1 From 75695fec70f77dc8cfec84973aa99e6d850b9dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 2 Nov 2022 23:32:34 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D0=9E=D1=82=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D1=83=D1=81?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawningObject.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AirBomber/AirBomber/DrawningObject.cs b/AirBomber/AirBomber/DrawningObject.cs index 251f49a..36a618a 100644 --- a/AirBomber/AirBomber/DrawningObject.cs +++ b/AirBomber/AirBomber/DrawningObject.cs @@ -59,8 +59,8 @@ namespace AirBomber var entity = _airplane.Airplane; var otherEntity = otherAirplane._airplane.Airplane; if (entity.GetType() != otherEntity.GetType() || - entity.Speed != otherEntity.Speed || - entity.Weight != otherEntity.Weight || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || entity.BodyColor != otherEntity.BodyColor) { return false; -- 2.25.1