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;