From b9aefd34339f4abb5943c7959938fb1f57448b61 Mon Sep 17 00:00:00 2001 From: foxkerik6 Date: Tue, 13 Dec 2022 04:27:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Stormtrooper/Stormtrooper/AbstractMap.cs | 42 ++++++++++++- .../Stormtrooper/AirCompareByColor.cs | 62 +++++++++++++++++++ Stormtrooper/Stormtrooper/AirCompareByType.cs | 55 ++++++++++++++++ Stormtrooper/Stormtrooper/DrawningObject.cs | 52 ++++++++++++++++ .../FormMapWithSetAirplane.Designer.cs | 32 +++++++++- .../Stormtrooper/FormMapWithSetAirplane.cs | 34 ++++++++++ Stormtrooper/Stormtrooper/IDrawningObject.cs | 2 +- .../Stormtrooper/MapWithSetAirplaneGeneric.cs | 11 +++- .../Stormtrooper/SetAirplaneGeneric.cs | 23 ++++++- 9 files changed, 306 insertions(+), 7 deletions(-) create mode 100644 Stormtrooper/Stormtrooper/AirCompareByColor.cs create mode 100644 Stormtrooper/Stormtrooper/AirCompareByType.cs diff --git a/Stormtrooper/Stormtrooper/AbstractMap.cs b/Stormtrooper/Stormtrooper/AbstractMap.cs index e998f59..4409536 100644 --- a/Stormtrooper/Stormtrooper/AbstractMap.cs +++ b/Stormtrooper/Stormtrooper/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -58,6 +58,46 @@ namespace Stormtrooper 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 bool CheckCollision(Direction dir) { int left = (int)(_drawningObject.GetCurrentPosition().Left / _size_x); diff --git a/Stormtrooper/Stormtrooper/AirCompareByColor.cs b/Stormtrooper/Stormtrooper/AirCompareByColor.cs new file mode 100644 index 0000000..a20550f --- /dev/null +++ b/Stormtrooper/Stormtrooper/AirCompareByColor.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class AirCompareByColor : 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 xAir = x as DrawningObject; + var yAir = y as DrawningObject; + if (xAir == null && yAir == null) + { + return 0; + } + if (xAir == null && yAir != null) + { + return 1; + } + if (xAir != null && yAir == null) + { + return -1; + } + var xEntityAir = xAir.GetAirplane.Airplane; + var yEntityAir = yAir.GetAirplane.Airplane; + var baseColorCompare = xEntityAir.Color.ToArgb().CompareTo(yEntityAir.Color.ToArgb()); + if (baseColorCompare != 0) + { + return baseColorCompare; + } + if (xEntityAir is EntityStormtrooper xStormtrooper && yEntityAir is EntityStormtrooper yStormtrooper) + { + var dopColorCompare = xStormtrooper.AdvColor.ToArgb().CompareTo(yStormtrooper.AdvColor.ToArgb()); + if (dopColorCompare != 0) + { + return dopColorCompare; + } + } + var speedCompare = xAir.GetAirplane.Airplane.Speed.CompareTo(yAir.GetAirplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAir.GetAirplane.Airplane.Weight.CompareTo(yAir.GetAirplane.Airplane.Weight); + } + } +} diff --git a/Stormtrooper/Stormtrooper/AirCompareByType.cs b/Stormtrooper/Stormtrooper/AirCompareByType.cs new file mode 100644 index 0000000..7c3b345 --- /dev/null +++ b/Stormtrooper/Stormtrooper/AirCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class AirCompareByType : 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 xAir = x as DrawningObject; + var yAir = y as DrawningObject; + if (xAir == null && yAir == null) + { + return 0; + } + if (xAir == null && yAir != null) + { + return 1; + } + if (xAir != null && yAir == null) + { + return -1; + } + if (xAir.GetAirplane.GetType().Name != yAir.GetAirplane.GetType().Name) + { + if (xAir.GetAirplane.GetType().Name == "DrawningLocomotive") + { + return -1; + } + return 1; + } + var speedCompare = xAir.GetAirplane.Airplane.Speed.CompareTo(yAir.GetAirplane.Airplane.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAir.GetAirplane.Airplane.Weight.CompareTo(yAir.GetAirplane.Airplane.Weight); + } + } +} diff --git a/Stormtrooper/Stormtrooper/DrawningObject.cs b/Stormtrooper/Stormtrooper/DrawningObject.cs index bdf9a94..970b3c0 100644 --- a/Stormtrooper/Stormtrooper/DrawningObject.cs +++ b/Stormtrooper/Stormtrooper/DrawningObject.cs @@ -10,6 +10,8 @@ namespace Stormtrooper { private DrawningMilitaryAirplane _airplane; + public DrawningMilitaryAirplane GetAirplane => _airplane; + public DrawningObject(DrawningMilitaryAirplane airplane) { _airplane = airplane; @@ -44,5 +46,55 @@ namespace Stormtrooper public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane()); + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherAir = other as DrawningObject; + if (otherAir == null) + { + return false; + } + var airplane = _airplane.Airplane; + var otherAirplane = otherAir._airplane.Airplane; + if (airplane.Speed != otherAirplane.Speed) + { + return false; + } + if (airplane.Weight != otherAirplane.Weight) + { + return false; + } + if (airplane.Color != otherAirplane.Color) + { + return false; + } + if (airplane is EntityStormtrooper stormtrooper && otherAirplane is EntityStormtrooper otherStormtrooper) + { + if (stormtrooper.Rockets != otherStormtrooper.Rockets) + { + return false; + } + if (stormtrooper.Radar != otherStormtrooper.Radar) + { + return false; + } + if (stormtrooper.Booster != otherStormtrooper.Booster) + { + return false; + } + if (stormtrooper.AdvColor != otherStormtrooper.AdvColor) + { + return false; + } + } + else if (airplane is EntityStormtrooper || otherAirplane is EntityStormtrooper) + { + return false; + } + return true; + } } } diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs index fde0103..ea6af3e 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs @@ -51,6 +51,8 @@ this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBoxTools.SuspendLayout(); this.groupBoxMap.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.buttonSortByType); + this.groupBoxTools.Controls.Add(this.buttonSortByColor); this.groupBoxTools.Controls.Add(this.groupBoxMap); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonLeft); @@ -86,7 +90,7 @@ this.groupBoxMap.Controls.Add(this.comboBoxMapSelector); this.groupBoxMap.Location = new System.Drawing.Point(6, 22); this.groupBoxMap.Name = "groupBoxMap"; - this.groupBoxMap.Size = new System.Drawing.Size(182, 259); + this.groupBoxMap.Size = new System.Drawing.Size(182, 240); this.groupBoxMap.TabIndex = 19; this.groupBoxMap.TabStop = false; this.groupBoxMap.Text = "Карты"; @@ -265,14 +269,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); // @@ -280,6 +284,26 @@ // this.openFileDialog.FileName = "openFileDialog1"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(13, 282); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(75, 23); + this.buttonSortByColor.TabIndex = 20; + this.buttonSortByColor.Text = "по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(95, 282); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(75, 23); + this.buttonSortByType.TabIndex = 21; + this.buttonSortByType.Text = "по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // // FormMapWithSetAirplane // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -328,5 +352,7 @@ private ToolStripMenuItem LoadToolStripMenuItem; private SaveFileDialog saveFileDialog; private OpenFileDialog openFileDialog; + private Button buttonSortByType; + private Button buttonSortByColor; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs index 15524e4..ce43b50 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs @@ -122,6 +122,11 @@ namespace Stormtrooper _logger.LogWarning($"Ошибка переполнения хранилища: {ex.Message}"); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning($"Ошибка при добавлении: {ex.Message}"); + MessageBox.Show($"Ошибка при добавлении: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } catch (Exception ex) { _logger.LogWarning($"Неизвестная ошибка: {ex.Message}"); @@ -129,6 +134,35 @@ namespace Stormtrooper } } + + /// + /// Сортировка по типу + /// + /// + /// + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMap.SelectedIndex == -1) + { + return; + } + _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].Sort(new AirCompareByType()); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Сортировка по цвету + /// + /// + /// + private void ButtonSortByColor_Click(object sender, EventArgs e) + { + if (listBoxMap.SelectedIndex == -1) + { + return; + } + _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].Sort(new AirCompareByColor()); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } /// /// Удаление объекта /// diff --git a/Stormtrooper/Stormtrooper/IDrawningObject.cs b/Stormtrooper/Stormtrooper/IDrawningObject.cs index 082b8ec..09018a8 100644 --- a/Stormtrooper/Stormtrooper/IDrawningObject.cs +++ b/Stormtrooper/Stormtrooper/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs index d563533..996eacc 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Stormtrooper { internal class MapWithSetAirplaneGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -82,6 +82,15 @@ namespace Stormtrooper DrawAirs(gr); return bmp; } + + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setAirs.SortSet(comparer); + } + /// /// Просмотр объекта на карте /// diff --git a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs index 90a54e1..c323e03 100644 --- a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Stormtrooper { internal class SetAirplaneGeneric - where T : class + where T : class, IEquatable { /// /// List объектов, которые храним @@ -34,6 +34,8 @@ namespace Stormtrooper /// public int Insert(T airplane) { + + if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); Insert(airplane, 0); @@ -47,6 +49,10 @@ namespace Stormtrooper /// public int Insert(T airplane, int position) { + if (_places.Contains(airplane)) + { + throw new ArgumentException($"Объект {airplane} уже присутствует в наборе"); + } if (position < 0 || position >= _maxCount - 1) throw new StorageOverflowException(_maxCount); _places.Insert(position, airplane); @@ -66,6 +72,21 @@ namespace Stormtrooper _places.RemoveAt(position); return air; } + + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } + + /// /// Получение объекта из набора по позиции ///