From 9d3083ae94636a29974e4f263b1cf4f33cd16b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=A7=D0=B5=D1=80?= =?UTF-8?q?=D0=BD=D1=8B=D1=88=D0=BE=D0=B2?= Date: Thu, 22 Dec 2022 21:51:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Warship/Warship/DrawingObjectWarship.cs | 27 +++++++++++++++++++++ Warship/Warship/IDrawingObject.cs | 2 +- Warship/Warship/MapWithSetWarshipGeneric.cs | 2 +- Warship/Warship/SetWarshipGeneric.cs | 16 +++++++++++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Warship/Warship/DrawingObjectWarship.cs b/Warship/Warship/DrawingObjectWarship.cs index 05fedb7..c9e162c 100644 --- a/Warship/Warship/DrawingObjectWarship.cs +++ b/Warship/Warship/DrawingObjectWarship.cs @@ -33,5 +33,32 @@ namespace AircraftCarrier } public string GetInfo() => _warship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship()); + public bool Equals(IDrawingObject? other) + { + if (other is not DrawingObjectWarship otherWarship) + { + return false; + } + var warship = _warship.Ship; + var otherEntity = otherWarship._warship.Ship; + if (warship.GetType() != otherEntity.GetType() || + warship.Speed != otherEntity.Speed || + warship.Weight != otherEntity.Weight || + warship.BodyColor != otherEntity.BodyColor) + { + return false; + } + + if (warship is EntityPlaneWarship entityPlaneWarship && + otherEntity is EntityPlaneWarship otherEntityPlaneWarship && ( + entityPlaneWarship.BodyKit != otherEntityPlaneWarship.BodyKit || + entityPlaneWarship.СontrolPlace != otherEntityPlaneWarship.СontrolPlace || + entityPlaneWarship.RunWay != otherEntityPlaneWarship.RunWay || + entityPlaneWarship.DopColor != otherEntityPlaneWarship.DopColor)) + { + return false; + } + return true; + } } } diff --git a/Warship/Warship/IDrawingObject.cs b/Warship/Warship/IDrawingObject.cs index 71b9ea5..2ea3196 100644 --- a/Warship/Warship/IDrawingObject.cs +++ b/Warship/Warship/IDrawingObject.cs @@ -9,7 +9,7 @@ namespace AircraftCarrier /// /// Интерфейс для работы с объектом, прорисовываемым на форме /// - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Warship/Warship/MapWithSetWarshipGeneric.cs b/Warship/Warship/MapWithSetWarshipGeneric.cs index 0c9b274..de34607 100644 --- a/Warship/Warship/MapWithSetWarshipGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipGeneric.cs @@ -12,7 +12,7 @@ namespace AircraftCarrier /// /// internal class MapWithSetWarshipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// diff --git a/Warship/Warship/SetWarshipGeneric.cs b/Warship/Warship/SetWarshipGeneric.cs index e52c7d7..8c5ca5a 100644 --- a/Warship/Warship/SetWarshipGeneric.cs +++ b/Warship/Warship/SetWarshipGeneric.cs @@ -11,7 +11,7 @@ namespace AircraftCarrier /// /// internal class SetWarshipsGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -53,6 +53,8 @@ namespace AircraftCarrier /// public int Insert(T ship, int position) { + if (_places.Contains(ship)) + return -1; if (Count == _maxCount) throw new StorageOverflowException(_maxCount); if (!isCorrectPosition(position)) @@ -107,6 +109,18 @@ namespace AircraftCarrier } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }