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);