From cd442a400d5c071e3816367155668bf904fea238 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Thu, 15 Dec 2022 13:44:30 +0300 Subject: [PATCH] lab8 full --- AirFighter/AirFighter/AbstractMap.cs | 18 +++++++- .../AirFighter/AircraftCompareByColor.cs | 31 ++++++++++++++ .../AirFighter/AircraftCompareByType.cs | 38 +++++++++++++++++ .../AirFighter/DrawingObjectAirFighter.cs | 33 +++++++++++++++ .../AirFighter/FormMapWithSetCars.Designer.cs | 41 +++++++++++++++---- AirFighter/AirFighter/FormMapWithSetCars.cs | 28 +++++++++++++ AirFighter/AirFighter/FormMapWithSetCars.resx | 3 ++ AirFighter/AirFighter/IDrawingObject.cs | 2 +- .../AirFighter/MapWithSetCarsGeneric.cs | 7 +++- AirFighter/AirFighter/SetCarsGeneric.cs | 10 ++++- 10 files changed, 200 insertions(+), 11 deletions(-) create mode 100644 AirFighter/AirFighter/AircraftCompareByColor.cs create mode 100644 AirFighter/AirFighter/AircraftCompareByType.cs diff --git a/AirFighter/AirFighter/AbstractMap.cs b/AirFighter/AirFighter/AbstractMap.cs index 0dd466d..95a88f6 100644 --- a/AirFighter/AirFighter/AbstractMap.cs +++ b/AirFighter/AirFighter/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirFighter { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawningObject = null; protected int[,] _map = null; @@ -194,5 +194,21 @@ namespace AirFighter 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 (_width != other._width || _height != other._height) return false; + if (_size_x != other._size_x || _size_y != other._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] != other._map[i, j]) return false; + } + } + + return true; + } } } diff --git a/AirFighter/AirFighter/AircraftCompareByColor.cs b/AirFighter/AirFighter/AircraftCompareByColor.cs new file mode 100644 index 0000000..698c961 --- /dev/null +++ b/AirFighter/AirFighter/AircraftCompareByColor.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class AircraftCompareByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xAirFighter = x as DrawingObjectAirFighter; + var yAirFighter = y as DrawingObjectAirFighter; + + if (xAirFighter == null && yAirFighter == null) return 0; + if (xAirFighter == null) return 1; + if (yAirFighter == null) return -1; + + string xColor = xAirFighter.GetAirFighter.AirFighter.BodyColor.Name; + string yColor = yAirFighter.GetAirFighter.AirFighter.BodyColor.Name; + + if (xColor != yColor) return xColor.CompareTo(yColor); + + var speedCompare = xAirFighter.GetAirFighter.AirFighter.Speed.CompareTo(yAirFighter.GetAirFighter.AirFighter.Speed); + if (speedCompare != 0) return speedCompare; + + return xAirFighter.GetAirFighter.AirFighter.Weight.CompareTo(yAirFighter.GetAirFighter.AirFighter.Weight); + } + } +} diff --git a/AirFighter/AirFighter/AircraftCompareByType.cs b/AirFighter/AirFighter/AircraftCompareByType.cs new file mode 100644 index 0000000..b31d407 --- /dev/null +++ b/AirFighter/AirFighter/AircraftCompareByType.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirFighter +{ + internal class AircraftCompareByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xAirFighter = x as DrawingObjectAirFighter; + var yAirFighter = y as DrawingObjectAirFighter; + + if (xAirFighter == null && yAirFighter == null) return 0; + if (xAirFighter == null) return 1; + if (yAirFighter == null) return -1; + + + if (xAirFighter.GetAirFighter.GetType().Name != yAirFighter.GetAirFighter.GetType().Name) + { + if (xAirFighter.GetAirFighter.GetType().Name == "DrawingAirFighter") + { + return -1; + } + return 1; + } + + var speedCompare = xAirFighter.GetAirFighter.AirFighter.Speed.CompareTo(yAirFighter.GetAirFighter.AirFighter.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xAirFighter.GetAirFighter.AirFighter.Weight.CompareTo(yAirFighter.GetAirFighter.AirFighter.Weight); + } + } +} diff --git a/AirFighter/AirFighter/DrawingObjectAirFighter.cs b/AirFighter/AirFighter/DrawingObjectAirFighter.cs index ab78301..09732e3 100644 --- a/AirFighter/AirFighter/DrawingObjectAirFighter.cs +++ b/AirFighter/AirFighter/DrawingObjectAirFighter.cs @@ -13,6 +13,9 @@ namespace AirFighter { _airFighter = airFighter; } + + public DrawingAirFighter GetAirFighter => _airFighter; + public float Step => _airFighter?.AirFighter?.Step ?? 0; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() @@ -35,5 +38,35 @@ namespace AirFighter public string GetInfo() => _airFighter?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectAirFighter(data.CreateDrawingAirFighter()); + public bool Equals(IDrawingObject? other) + { + var otherObj = other as DrawingObjectAirFighter; + if (otherObj == null) return false; + + var airFighter = _airFighter.AirFighter; + var otherAirFighter = otherObj._airFighter.AirFighter; + + if (airFighter.Speed != otherAirFighter.Speed) return false; + if (airFighter.Weight != otherAirFighter.Weight) return false; + if (airFighter.BodyColor != otherAirFighter.BodyColor) return false; + + // TODO доделать проверки в случае продвинутого объекта + + if(airFighter.GetType().Name != otherAirFighter.GetType().Name) + { + return false; + } + + if(airFighter.GetType().Name == "EntityAirFighter") return true; + + var modernEntity1 = (EntityModernAirFighter) airFighter; + var modernEntity2 = (EntityModernAirFighter) otherAirFighter; + + if (modernEntity1.DopColor.Name != modernEntity2.DopColor.Name) return false; + if (modernEntity1.Rockets != modernEntity2.Rockets) return false; + if (modernEntity1.DopWings != modernEntity2.DopWings) return false; + + return true; + } } } diff --git a/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs b/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs index e3b248d..1b0fd5e 100644 --- a/AirFighter/AirFighter/FormMapWithSetCars.Designer.cs +++ b/AirFighter/AirFighter/FormMapWithSetCars.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.tools.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); @@ -59,6 +61,8 @@ // // tools // + this.tools.Controls.Add(this.buttonSortByColor); + this.tools.Controls.Add(this.buttonSortByType); this.tools.Controls.Add(this.groupBox1); this.tools.Controls.Add(this.RightButton); this.tools.Controls.Add(this.LeftButton); @@ -194,15 +198,16 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 392); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 449); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(196, 27); this.maskedTextBoxPosition.TabIndex = 5; + this.maskedTextBoxPosition.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBoxPosition_MaskInputRejected); // // buttonRemove // - this.buttonRemove.Location = new System.Drawing.Point(24, 425); + this.buttonRemove.Location = new System.Drawing.Point(24, 482); this.buttonRemove.Name = "buttonRemove"; this.buttonRemove.Size = new System.Drawing.Size(196, 29); this.buttonRemove.TabIndex = 4; @@ -212,7 +217,7 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(24, 520); + this.ButtonShowOnMap.Location = new System.Drawing.Point(24, 552); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; this.ButtonShowOnMap.Size = new System.Drawing.Size(196, 29); this.ButtonShowOnMap.TabIndex = 3; @@ -222,7 +227,7 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(24, 472); + this.ButtonShowStorage.Location = new System.Drawing.Point(24, 517); this.ButtonShowStorage.Name = "ButtonShowStorage"; this.ButtonShowStorage.Size = new System.Drawing.Size(196, 29); this.ButtonShowStorage.TabIndex = 2; @@ -232,7 +237,7 @@ // // buttonAdd // - this.buttonAdd.Location = new System.Drawing.Point(24, 357); + this.buttonAdd.Location = new System.Drawing.Point(24, 414); this.buttonAdd.Name = "buttonAdd"; this.buttonAdd.Size = new System.Drawing.Size(196, 29); this.buttonAdd.TabIndex = 1; @@ -272,14 +277,14 @@ // saveToolStripMenuItem // this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; - this.saveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.saveToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.saveToolStripMenuItem.Text = "Сохранение"; this.saveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // loadToolStripMenuItem // this.loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - this.loadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.loadToolStripMenuItem.Size = new System.Drawing.Size(177, 26); this.loadToolStripMenuItem.Text = "Загрузка"; this.loadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -291,6 +296,26 @@ // this.saveFileDialog.Filter = "txt file | *.txt"; // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(27, 325); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(196, 29); + this.buttonSortByType.TabIndex = 10; + this.buttonSortByType.Text = "Сортировать по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(27, 360); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(196, 29); + this.buttonSortByColor.TabIndex = 11; + this.buttonSortByColor.Text = "Сортировать по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // FormMapWithSetCars // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -339,5 +364,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/AirFighter/AirFighter/FormMapWithSetCars.cs b/AirFighter/AirFighter/FormMapWithSetCars.cs index 5ef6d3a..163bc10 100644 --- a/AirFighter/AirFighter/FormMapWithSetCars.cs +++ b/AirFighter/AirFighter/FormMapWithSetCars.cs @@ -131,6 +131,9 @@ namespace AirFighter pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); _logger.LogInformation("На карту добавлен новый объект"); + } else + { + MessageBox.Show("Такой объект уже существует"); } } catch (StorageOverflowException ex) { @@ -223,6 +226,27 @@ namespace AirFighter pictureBox.Image =_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); _logger.LogInformation("Просмотр карты"); } + + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) return; + + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AircraftCompareByType()); + 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 AircraftCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// /// Перемещение /// @@ -289,5 +313,9 @@ namespace AirFighter } } + private void maskedTextBoxPosition_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) + { + + } } } diff --git a/AirFighter/AirFighter/FormMapWithSetCars.resx b/AirFighter/AirFighter/FormMapWithSetCars.resx index 79fa9d7..abb54fc 100644 --- a/AirFighter/AirFighter/FormMapWithSetCars.resx +++ b/AirFighter/AirFighter/FormMapWithSetCars.resx @@ -66,4 +66,7 @@ 319, 17 + + 54 + \ No newline at end of file diff --git a/AirFighter/AirFighter/IDrawingObject.cs b/AirFighter/AirFighter/IDrawingObject.cs index e12e2b8..cdd478f 100644 --- a/AirFighter/AirFighter/IDrawingObject.cs +++ b/AirFighter/AirFighter/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirFighter { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/AirFighter/AirFighter/MapWithSetCarsGeneric.cs b/AirFighter/AirFighter/MapWithSetCarsGeneric.cs index f66a707..8a34764 100644 --- a/AirFighter/AirFighter/MapWithSetCarsGeneric.cs +++ b/AirFighter/AirFighter/MapWithSetCarsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirFighter { internal class MapWithSetCarsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// @@ -151,6 +151,11 @@ namespace AirFighter } } } + + public void Sort(IComparer comparer) + { + _setCars.SortSet(comparer); + } /// /// Метод отрисовки фона /// diff --git a/AirFighter/AirFighter/SetCarsGeneric.cs b/AirFighter/AirFighter/SetCarsGeneric.cs index 72f1d1d..47ef7df 100644 --- a/AirFighter/AirFighter/SetCarsGeneric.cs +++ b/AirFighter/AirFighter/SetCarsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace AirFighter { internal class SetCarsGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -29,6 +29,7 @@ namespace AirFighter } public int Insert(T car) { + if (_places.Contains(car)) return -1; // TODO вставка в начало набора if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(0, car); @@ -36,6 +37,7 @@ namespace AirFighter } public int Insert(T car, int position) { + if (_places.Contains(car)) return -1; if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, car); return position; @@ -79,6 +81,12 @@ namespace AirFighter } } + public void SortSet(IComparer comparer) + { + if (comparer == null) return; + _places.Sort(comparer); + } + public IEnumerable GetCars() { foreach (var car in _places) -- 2.25.1