diff --git a/Liner/Liner/DrawingObjectShip.cs b/Liner/Liner/DrawingObjectShip.cs index 8a66da9..afc03ab 100644 --- a/Liner/Liner/DrawingObjectShip.cs +++ b/Liner/Liner/DrawingObjectShip.cs @@ -32,5 +32,47 @@ namespace Liner } public string GetInfo() => _ship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObjectShip(data.CreateDrawingShip()); + + public bool Equals(IDrawingObject? other) + { + if (other is not DrawingObjectShip otherShip) + { + return false; + } + var ship = _ship.Ship; + var otherShipShip = otherShip._ship.Ship; + if (ship.GetType() != otherShipShip.GetType()) + { + return false; + } + if (ship.Speed != otherShipShip.Speed) + { + return false; + } + if (ship.Weight != otherShipShip.Weight) + { + return false; + } + if (ship.BodyColor != otherShipShip.BodyColor) + { + return false; + } + if (ship is EntityLiner liner && otherShipShip is EntityLiner otherLiner) + { + if (liner.DopColor != otherLiner.DopColor) + { + return false; + } + if (liner.Pool != otherLiner.Pool) + { + return false; + } + if (liner.DopDeck != otherLiner.DopDeck) + { + return false; + } + } + return true; + } } } diff --git a/Liner/Liner/IDrawingObject.cs b/Liner/Liner/IDrawingObject.cs index 7422b43..5525b33 100644 --- a/Liner/Liner/IDrawingObject.cs +++ b/Liner/Liner/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Liner { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/Liner/Liner/MapWithSetShipsGeneric.cs b/Liner/Liner/MapWithSetShipsGeneric.cs index a17ce92..7e338bb 100644 --- a/Liner/Liner/MapWithSetShipsGeneric.cs +++ b/Liner/Liner/MapWithSetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Liner { internal class MapWithSetShipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { private readonly int _pictureWidth; diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 13a1be7..e3904d2 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Liner { internal class SetShipsGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -23,6 +23,10 @@ namespace Liner } public int Insert(T ship, int position) { + if (_places.Contains(ship)) + { + return -1; + } if (Count == _maxCount) { throw new StorageOverflowException(_maxCount);