From a2d2543486b8f696ff42f78458c452ecdb419014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Mon, 12 Dec 2022 01:35:20 +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=D1=8F=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/DrawningObjectShip.cs | 44 +++++++++++++++++++ WarmlyShip/WarmlyShip/IDrawningObject.cs | 2 +- .../WarmlyShip/MapWithSetShipGeneric.cs | 2 +- WarmlyShip/WarmlyShip/SetShipGeneric.cs | 6 ++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs index f0da2a3..1a2ddb2 100644 --- a/WarmlyShip/WarmlyShip/DrawningObjectShip.cs +++ b/WarmlyShip/WarmlyShip/DrawningObjectShip.cs @@ -40,5 +40,49 @@ namespace WarmlyShip public string GetInfo() => _warmlyShip?.GetDataForSave(); public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherShip = other as DrawningObjectShip; + if (otherShip == null) + { + return false; + } + var ship = _warmlyShip.warmlyShip; + var otherShipShip = otherShip._warmlyShip.warmlyShip; + if (ship.Speed != otherShipShip.Speed) + { + return false; + } + if (ship.Weight != otherShipShip.Weight) + { + return false; + } + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + + if (ship is EntityMotorShip motorShip && otherShipShip is EntityMotorShip otherMotorShip) + { + if (motorShip.DopColor != otherMotorShip.DopColor) + { + return false; + } + if (motorShip.Tubes != otherMotorShip.Tubes) + { + return false; + } + if (motorShip.Cistern != otherMotorShip.Cistern) + { + return false; + } + } + return true; + } } } diff --git a/WarmlyShip/WarmlyShip/IDrawningObject.cs b/WarmlyShip/WarmlyShip/IDrawningObject.cs index 94af911..0a41158 100644 --- a/WarmlyShip/WarmlyShip/IDrawningObject.cs +++ b/WarmlyShip/WarmlyShip/IDrawningObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace WarmlyShip { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 209da81..1b0ad57 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace WarmlyShip { internal class MapWithSetShipGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index 8a37607..9eb0df0 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace WarmlyShip { internal class SetShipGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -27,6 +27,10 @@ namespace WarmlyShip public int Insert(T ship, int position) { + if (_places.Contains(ship)) + { + return -1; + } if (position < 0 || position > Count || Count == _maxCount) throw new StorageOverflowException(_maxCount); _places.Insert(position, ship); return position;