From c67d41e1ab0feb80428ea84972e5d98487f8aa17 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Fri, 2 Dec 2022 11:11:24 +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 --- Liner/Liner/DrawingObjectShip.cs | 42 +++++++++++++++++++++++++++ Liner/Liner/IDrawingObject.cs | 2 +- Liner/Liner/MapWithSetShipsGeneric.cs | 2 +- Liner/Liner/SetShipsGeneric.cs | 6 +++- 4 files changed, 49 insertions(+), 3 deletions(-) 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);