From 7a4942a0a50d69d04426b0c985095daf38578381 Mon Sep 17 00:00:00 2001 From: the Date: Mon, 5 Dec 2022 22:22:21 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ship/Ship/DrawingObject.cs | 29 +++++++++++++++++++++++++++++ Ship/Ship/IDrawingObject.cs | 2 +- Ship/Ship/MapWithSetShipsGeneric.cs | 2 +- Ship/Ship/SetShipsGeneric.cs | 7 ++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Ship/Ship/DrawingObject.cs b/Ship/Ship/DrawingObject.cs index 34cd40c..c1d0797 100644 --- a/Ship/Ship/DrawingObject.cs +++ b/Ship/Ship/DrawingObject.cs @@ -40,5 +40,34 @@ namespace Ship public string GetInfo() => _ship?.GetDataForSave(); public static IDrawingObject Create(string data) => new DrawingObject(data.CreateDrawingShip()); + + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + var otherCar = other as DrawingObject; + if (otherCar == null) + { + return false; + } + var car = _ship.Ship; + var otherCarCar = otherCar._ship.Ship; + if (car.Speed != otherCarCar.Speed) + { + return false; + } + if (car.Weight != otherCarCar.Weight) + { + return false; + } + if (car.BodyColor != otherCarCar.BodyColor) + { + return false; + } + // TODO доделать проверки в случае продвинутого объекта + return true; + } } } diff --git a/Ship/Ship/IDrawingObject.cs b/Ship/Ship/IDrawingObject.cs index e1e3d46..b3bc0da 100644 --- a/Ship/Ship/IDrawingObject.cs +++ b/Ship/Ship/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Ship { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/Ship/Ship/MapWithSetShipsGeneric.cs b/Ship/Ship/MapWithSetShipsGeneric.cs index 10254c8..61d6634 100644 --- a/Ship/Ship/MapWithSetShipsGeneric.cs +++ b/Ship/Ship/MapWithSetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Ship { internal class MapWithSetShipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// diff --git a/Ship/Ship/SetShipsGeneric.cs b/Ship/Ship/SetShipsGeneric.cs index 0d11df5..71a4add 100644 --- a/Ship/Ship/SetShipsGeneric.cs +++ b/Ship/Ship/SetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Ship { internal class SetShipsGeneric - where T: class + where T: class, IEquatable { /// /// Массив объектов, которые храним @@ -45,6 +45,11 @@ namespace Ship /// public int Insert(T ship, int position) { + if (_places.Contains(ship)) + { + return -1; + } + if (Count == _maxCount) { throw new StorageOverflowException(_maxCount);