From ae6b62589a2c14c8394c4e9037a6a7f4c8ed66c6 Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Sun, 4 Dec 2022 14:28:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AntiAircraftGun/AbstractMap.cs | 7 +++- .../AntiAircraftGunCompareByColor.cs | 41 ++++++++++++++++++- .../DrawingObjectAntiAircraftGun.cs | 28 ++++++++++--- .../FormMapWithSetAntiAircraftGuns.cs | 9 +++- .../SetAntiAircraftGunsGeneric.cs | 11 +++-- 5 files changed, 81 insertions(+), 15 deletions(-) diff --git a/AntiAircraftGun/AntiAircraftGun/AbstractMap.cs b/AntiAircraftGun/AntiAircraftGun/AbstractMap.cs index 553ef44..750f21e 100644 --- a/AntiAircraftGun/AntiAircraftGun/AbstractMap.cs +++ b/AntiAircraftGun/AntiAircraftGun/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AntiAircraftGun { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -153,5 +153,10 @@ namespace AntiAircraftGun protected abstract void GenerateMap(); protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public bool Equals(AbstractMap? other) + { + return this == other && this._width == other._width && this._height == other._height && this._map == other._map && this._drawingObject == other._drawingObject; + } } } diff --git a/AntiAircraftGun/AntiAircraftGun/AntiAircraftGunCompareByColor.cs b/AntiAircraftGun/AntiAircraftGun/AntiAircraftGunCompareByColor.cs index dc4df92..1ea7e52 100644 --- a/AntiAircraftGun/AntiAircraftGun/AntiAircraftGunCompareByColor.cs +++ b/AntiAircraftGun/AntiAircraftGun/AntiAircraftGunCompareByColor.cs @@ -10,8 +10,45 @@ namespace AntiAircraftGun { public int Compare(IDrawingObject? x, IDrawingObject? y) { - // TODO реализовать логику сравнения - throw new NotImplementedException(); + if (x == null && y == null) + { + return 0; + } + if (x == null && y != null) + { + return 1; + } + if (x != null && y == null) + { + return -1; + } + var xAntiAircraftGun = x as DrawingObjectAntiAircraftGun; + var yAntiAircraftGun = y as DrawingObjectAntiAircraftGun; + if (xAntiAircraftGun == null && yAntiAircraftGun == null) + { + return 0; + } + if (xAntiAircraftGun == null && yAntiAircraftGun != null) + { + return 1; + } + if (xAntiAircraftGun != null && yAntiAircraftGun == null) + { + return -1; + } + if (xAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor == yAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor) + { + return 0; + } + if (xAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.R.CompareTo(yAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.R) == 0) + { + if (xAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.G.CompareTo(yAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.G) == 0) + { + return xAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.B.CompareTo(yAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.B); + } + else return xAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.G.CompareTo(yAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.G); + } + else return xAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.R.CompareTo(yAntiAircraftGun.GetAntiAircraftGun.AntiAircraftGun.BodyColor.R); } } } diff --git a/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs b/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs index 17a89ac..a9894a7 100644 --- a/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs +++ b/AntiAircraftGun/AntiAircraftGun/DrawingObjectAntiAircraftGun.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.PortableExecutable; using System.Text; using System.Threading.Tasks; @@ -48,21 +49,36 @@ namespace AntiAircraftGun { return false; } - var car = _antiAircraftGun.AntiAircraftGun; - var otherCarCar = otherAntiAircraftGun._antiAircraftGun.AntiAircraftGun; - if (car.Speed != otherCarCar.Speed) + var antiAircraftGun = _antiAircraftGun.AntiAircraftGun; + var otherAntiAircraftGunAntiAircraftGun = otherAntiAircraftGun._antiAircraftGun.AntiAircraftGun; + if (antiAircraftGun.Speed != otherAntiAircraftGunAntiAircraftGun.Speed) { return false; } - if (car.Weight != otherCarCar.Weight) + if (antiAircraftGun.Weight != otherAntiAircraftGunAntiAircraftGun.Weight) { return false; } - if (car.BodyColor != otherCarCar.BodyColor) + if (antiAircraftGun.BodyColor != otherAntiAircraftGunAntiAircraftGun.BodyColor) { return false; } - // TODO доделать проверки в случае продвинутого объекта + + if (antiAircraftGun is EntityUpdateAntiAircraftGun && otherAntiAircraftGunAntiAircraftGun is EntityUpdateAntiAircraftGun) + { + var updateAntiAircraftGun = antiAircraftGun as EntityUpdateAntiAircraftGun; + var otherUpdateAntiAircraftGun = otherAntiAircraftGunAntiAircraftGun as EntityUpdateAntiAircraftGun; + if (updateAntiAircraftGun.DopColor != otherUpdateAntiAircraftGun.DopColor || updateAntiAircraftGun.Gun != otherUpdateAntiAircraftGun.Gun || + updateAntiAircraftGun.Radar != otherUpdateAntiAircraftGun.Radar) + { + return false; + } + } + else if (antiAircraftGun is EntityUpdateAntiAircraftGun || otherAntiAircraftGunAntiAircraftGun is EntityUpdateAntiAircraftGun) + { + return false; + } + return true; } } diff --git a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs index 19b698b..9c3a855 100644 --- a/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs +++ b/AntiAircraftGun/AntiAircraftGun/FormMapWithSetAntiAircraftGuns.cs @@ -145,8 +145,8 @@ namespace AntiAircraftGun DrawingObjectAntiAircraftGun antiAircraftGun = new DrawingObjectAntiAircraftGun(drawingAntiAircraftGuns); if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + antiAircraftGun != -1) { - MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + MessageBox.Show("Объект добавлен"); _logger.LogInformation($"Добавлен объект {drawingAntiAircraftGuns} на карту "); } else @@ -329,7 +329,12 @@ namespace AntiAircraftGun /// private void ButtonSortByColor_Click(object sender, EventArgs e) { - // TODO прописать логику + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AntiAircraftGunCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } } } diff --git a/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs b/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs index fbb5cc4..ca7294c 100644 --- a/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs +++ b/AntiAircraftGun/AntiAircraftGun/SetAntiAircraftGunsGeneric.cs @@ -40,7 +40,6 @@ namespace AntiAircraftGun /// public int Insert(T antiAircraftGun) { - //TODO проверка на уникальность if (_places.Count < _maxCount) return Insert(antiAircraftGun, 0); else throw new StorageOverflowException(_maxCount); } @@ -52,9 +51,13 @@ namespace AntiAircraftGun /// public int Insert(T antiAircraftGun, int position) { - if (position >= _maxCount) throw new StorageOverflowException(_maxCount); - _places.Insert(position, antiAircraftGun); - return position; + if (_places.All(p => p.Equals(antiAircraftGun) == false)) + { + if (position >= _maxCount) throw new StorageOverflowException(_maxCount); + _places.Insert(position, antiAircraftGun); + return position; + } + return - 1; } /// /// Удаление объекта из набора с конкретной позиции