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] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=208?= =?UTF-8?q?=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);