From 3f1df7742f0259fd4aeafbfcd5600eb21826e2de Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Tue, 29 Nov 2022 19:29:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Warship/Warship/AbstractMap.cs | 27 ++++++++++++++++++++++- Warship/Warship/FormMapWithSetWarships.cs | 3 +-- Warship/Warship/SetWarshipsGeneric.cs | 9 +++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Warship/Warship/AbstractMap.cs b/Warship/Warship/AbstractMap.cs index d9a28b3..ecaef89 100644 --- a/Warship/Warship/AbstractMap.cs +++ b/Warship/Warship/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Warship { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -148,5 +148,30 @@ namespace Warship protected abstract void GenerateMap(); protected abstract void DrawWaterPart(Graphics gr, int i, int j); protected abstract void DrawLandPart(Graphics gr, int i, int j); + + public bool Equals(AbstractMap? other) + { + if (other == null) + return false; + + if (_width != other._width) + return false; + + if (_height != other._height) + return false; + + if (_size_x != other._size_x) + return false; + + if (_size_y != other._size_y) + return false; + + for (int i = 0; i < _map.GetLength(0); i++) + for (int j = 0; j < _map.GetLength(1); j++) + if (_map[i, j] != other._map[i, j]) + return false; + + return true; + } } } diff --git a/Warship/Warship/FormMapWithSetWarships.cs b/Warship/Warship/FormMapWithSetWarships.cs index 330340f..89820ca 100644 --- a/Warship/Warship/FormMapWithSetWarships.cs +++ b/Warship/Warship/FormMapWithSetWarships.cs @@ -64,10 +64,9 @@ namespace Warship { return; } - DrawingObjectWarship warship = new(drawingWarship); try { - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectWarship(drawingWarship) != -1) { _logger.LogInformation("Новый объект добавлен"); MessageBox.Show("Объект добавлен"); diff --git a/Warship/Warship/SetWarshipsGeneric.cs b/Warship/Warship/SetWarshipsGeneric.cs index e5be7a7..1b66385 100644 --- a/Warship/Warship/SetWarshipsGeneric.cs +++ b/Warship/Warship/SetWarshipsGeneric.cs @@ -33,11 +33,14 @@ namespace Warship public int Insert(T warship, int position) { - if (_places.Contains(warship)) + if (Count == _maxCount) + throw new StorageOverflowException(_maxCount); + + if (position < 0 || position > Count) return -1; - if (position >= _maxCount || position < 0) - throw new StorageOverflowException(_maxCount); + if (_places.Contains(warship)) + return -1; _places.Insert(position, warship);