From 4cd90e298f965b322befbecf2e9051f2128c7c73 Mon Sep 17 00:00:00 2001 From: m1aksim1 Date: Tue, 22 Nov 2022 17:55:27 +0400 Subject: [PATCH] fix --- WarPlanes/WarPlanes/AbstractMap.cs | 29 +++++++++- WarPlanes/WarPlanes/DrawningObjectWarPlane.cs | 57 +++++++++---------- .../WarPlanes/FormMapWithSetWarPlanes.cs | 5 ++ WarPlanes/WarPlanes/WarPlaneCompareByColor.cs | 26 ++++++++- 4 files changed, 84 insertions(+), 33 deletions(-) diff --git a/WarPlanes/WarPlanes/AbstractMap.cs b/WarPlanes/WarPlanes/AbstractMap.cs index 90f92b0..81802ed 100644 --- a/WarPlanes/WarPlanes/AbstractMap.cs +++ b/WarPlanes/WarPlanes/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AirFighter { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -103,5 +103,32 @@ 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 (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/WarPlanes/WarPlanes/DrawningObjectWarPlane.cs b/WarPlanes/WarPlanes/DrawningObjectWarPlane.cs index ced9ded..157d248 100644 --- a/WarPlanes/WarPlanes/DrawningObjectWarPlane.cs +++ b/WarPlanes/WarPlanes/DrawningObjectWarPlane.cs @@ -10,34 +10,6 @@ } public DrawningWarPlane GetWarPlane => _warplane; - public bool Equals(IDrawningObject? other) - { - if (other == null) - { - return false; - } - var otherWarPlane = other as DrawningObjectWarPlane; - if (otherWarPlane == null) - { - return false; - } - var warplane = _warplane.WarPlane; - var otherWarPlaneWarPlane = otherWarPlane._warplane.WarPlane; - if (warplane.Speed != otherWarPlaneWarPlane.Speed) - { - return false; - } - if (warplane.Weight != otherWarPlaneWarPlane.Weight) - { - return false; - } - if (warplane.BodyColor != otherWarPlaneWarPlane.BodyColor) - { - return false; - } - // TODO доделать проверки в случае продвинутого объекта - return true; - } public float Step => _warplane?.WarPlane?.Step ?? 0; public RectangleF GetCurrentPosition() @@ -60,9 +32,34 @@ _warplane?.DrawTransport(g); } - public string GetInfo() => _warplane?.GetDataForSave(); + public string GetInfo() => _warplane?.GetDataForSave() ?? string.Empty; public static IDrawningObject Create(string data) => new DrawningObjectWarPlane(data.CreateDrawningWarPlane()); - + + public bool Equals(IDrawningObject? other) + { + if (other is not DrawningObjectWarPlane otherWarPlane) + { + return false; + } + var entity = _warplane.WarPlane; + var otherEntity = otherWarPlane._warplane.WarPlane; + if (entity.GetType() != otherEntity.GetType() || + entity.Speed != otherEntity.Speed || + entity.Weight != otherEntity.Weight || + entity.BodyColor != otherEntity.BodyColor) + { + return false; + } + if (entity is EntityFighter entityFighter && + otherEntity is EntityFighter otherEntityAirBomber && ( + entityFighter.Rocket != otherEntityAirBomber.Rocket || + entityFighter.DopColor != otherEntityAirBomber.DopColor || + entityFighter.Wing != otherEntityAirBomber.Wing)) + { + return false; + } + return true; + } } } \ No newline at end of file diff --git a/WarPlanes/WarPlanes/FormMapWithSetWarPlanes.cs b/WarPlanes/WarPlanes/FormMapWithSetWarPlanes.cs index cac0de4..d9ea3f3 100644 --- a/WarPlanes/WarPlanes/FormMapWithSetWarPlanes.cs +++ b/WarPlanes/WarPlanes/FormMapWithSetWarPlanes.cs @@ -149,6 +149,11 @@ namespace AirFighter _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } + catch (ArgumentException ex) + { + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, warplane); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } /// /// Удаление объекта diff --git a/WarPlanes/WarPlanes/WarPlaneCompareByColor.cs b/WarPlanes/WarPlanes/WarPlaneCompareByColor.cs index c0765fd..1002eba 100644 --- a/WarPlanes/WarPlanes/WarPlaneCompareByColor.cs +++ b/WarPlanes/WarPlanes/WarPlaneCompareByColor.cs @@ -4,8 +4,30 @@ { public int Compare(IDrawningObject? x, IDrawningObject? y) { - // TODO реализовать логику сравнения - throw new NotImplementedException(); + var xWarPlane = x as DrawningObjectWarPlane; + var yWarPlane = y as DrawningObjectWarPlane; + if (xWarPlane == yWarPlane) + { + return 0; + } + if (xWarPlane == null) + { + return 1; + } + if (yWarPlane == null) + { + return -1; + } + var xEntity = xWarPlane.GetWarPlane.WarPlane; + var yEntity = yWarPlane.GetWarPlane.WarPlane; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || + xEntity is not EntityFighter xEntityAirBomber || + yEntity is not EntityFighter yEntityAirBomber) + { + return colorWeight; + } + return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb()); } } } \ No newline at end of file