From f73284338db2f303e61bf3ad5a5295eb3cb186c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D0=B8=D1=80=20=D0=9D=D1=83=D0=B3=D0=B0?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Mon, 5 Dec 2022 23:02:12 +0400 Subject: [PATCH] fix --- Bus/Bus/AbstractMap.cs | 50 ++++++------ Bus/Bus/BusCompareByColor.cs | 39 ++++++++++ Bus/Bus/BusCompareByType.cs | 43 ++++++++++ Bus/Bus/BusComperyByColor.cs | 78 ------------------- Bus/Bus/BusComperyByType.cs | 63 --------------- Bus/Bus/DrawingObjectBus.cs | 3 +- .../FormMapWithSetDoubleDeckerBus.Designer.cs | 2 +- Bus/Bus/FormMapWithSetDoubleDeckerBus.cs | 72 ++++++----------- Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs | 2 +- Bus/Bus/SetDoubleDeckerBusGeneric.cs | 36 +++------ 10 files changed, 146 insertions(+), 242 deletions(-) create mode 100644 Bus/Bus/BusCompareByColor.cs create mode 100644 Bus/Bus/BusCompareByType.cs delete mode 100644 Bus/Bus/BusComperyByColor.cs delete mode 100644 Bus/Bus/BusComperyByType.cs diff --git a/Bus/Bus/AbstractMap.cs b/Bus/Bus/AbstractMap.cs index 17d77f0..a2eb582 100644 --- a/Bus/Bus/AbstractMap.cs +++ b/Bus/Bus/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Bus { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawingObject _drawingObject = null; protected int[,] _map = null; @@ -54,7 +54,6 @@ namespace Bus public Bitmap MoveObject(Direction direction) { - _drawingObject.MoveObject(direction); bool collision = CheckCollision(); @@ -80,23 +79,6 @@ namespace Bus return DrawMapWithObject(); } - private Direction MoveObjectBack(Direction direction) - { - switch (direction) - { - case Direction.Up: - return Direction.Down; - case Direction.Down: - return Direction.Up; - case Direction.Left: - return Direction.Right; - case Direction.Right: - return Direction.Left; - } - return Direction.None; - } - - private bool SetObjectOnMap() { if (_drawingObject == null || _map == null) @@ -135,8 +117,6 @@ namespace Bus return bmp; } - - private bool CheckCollision() { var pos = _drawingObject.GetCurrentPosition(); @@ -157,12 +137,38 @@ namespace Bus } } } - return false; } 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) + { + if (other == null || + _map != other._map || + _width != other._width || + _size_x != other._size_x || + _size_y != other._size_y || + _height != other._height || + GetType() != other.GetType() || + _map.GetLength(0) != other._map.GetLength(0) || + _map.GetLength(1) != other._map.GetLength(1)) + { + 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/Bus/Bus/BusCompareByColor.cs b/Bus/Bus/BusCompareByColor.cs new file mode 100644 index 0000000..f2c7492 --- /dev/null +++ b/Bus/Bus/BusCompareByColor.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bus +{ + internal class BusCompareByColor : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xBus = x as DrawingObjectBus; + var yBus = y as DrawingObjectBus; + if (xBus == yBus) + { + return 0; + } + if (xBus == null) + { + return 1; + } + if (yBus == null) + { + return -1; + } + var xEntity = xBus._bus.Bus; + var yEntity = yBus._bus.Bus; + var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb()); + if (colorWeight != 0 || + xEntity is not EntitySportBus xEntitySportBus || + yEntity is not EntitySportBus yEntitySportBus) + { + return colorWeight; + } + return xEntitySportBus.DopColor.ToArgb().CompareTo(yEntitySportBus.DopColor.ToArgb()); + } + } +} diff --git a/Bus/Bus/BusCompareByType.cs b/Bus/Bus/BusCompareByType.cs new file mode 100644 index 0000000..a70003e --- /dev/null +++ b/Bus/Bus/BusCompareByType.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bus +{ + internal class BusCompareByType : IComparer + { + public int Compare(IDrawingObject? x, IDrawingObject? y) + { + var xBus = x as DrawingObjectBus; + var yBus = y as DrawingObjectBus; + if (xBus == yBus) + { + return 0; + } + if (xBus == null) + { + return 1; + } + if (yBus == null) + { + return -1; + } + if (xBus._bus.GetType().Name != yBus._bus.GetType().Name) + { + if (xBus._bus.GetType() == typeof(DrawingBus)) + { + return -1; + } + return 1; + } + var speedCompare = xBus._bus.Bus.Speed.CompareTo(yBus._bus.Bus.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xBus._bus.Bus.Weight.CompareTo(yBus._bus.Bus.Weight); + } + } +} diff --git a/Bus/Bus/BusComperyByColor.cs b/Bus/Bus/BusComperyByColor.cs deleted file mode 100644 index 139651f..0000000 --- a/Bus/Bus/BusComperyByColor.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Bus -{ - internal class BusComperyByColor : IComparer - { - public int Compare(IDrawingObject? x, IDrawingObject? y) - { - if (x == null && y == null) - { - return 0; - } - - if (x == null && y != null) - { - return 1; - } - - if (x != null && y != null) - { - return -1; - } - - var xBus = x as DrawingObjectBus; - var yBus = y as DrawingObjectBus; - - if (xBus == null && yBus == null) - { - return 0; - } - - if (xBus == null && yBus != null) - { - return 1; - } - - if (xBus != null && yBus == null) - { - return 1; - } - - var xEntity = xBus.GetBus.Bus; - var yEntity = yBus.GetBus.Bus; - //var colorCompare = xEntity.DopColor.ToArgb().CompareTo(yEntity.DopColor.ToArgb()); - - //int i = xEntity.DopColor.ToArgb(); - //int j = yEntity.DopColor.ToArgb(); - - /*if (colorCompare != 0) - { - return colorCompare; - }*/ - - if (xEntity is EntitySportBus xEntityAirbus && yEntity is EntitySportBus yEntityAirbus) - { - var addColorCompare = xEntityAirbus.DopColor.ToArgb().CompareTo(yEntityAirbus.DopColor.ToArgb()); - - if (addColorCompare != 0) - { - return addColorCompare; - } - } - - var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); - - if (speedCompare != 0) - { - return speedCompare; - } - - return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); - } - } -} diff --git a/Bus/Bus/BusComperyByType.cs b/Bus/Bus/BusComperyByType.cs deleted file mode 100644 index 9027a5a..0000000 --- a/Bus/Bus/BusComperyByType.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Bus -{ - internal class BusComperyByType : IComparer - { - public int Compare(IDrawingObject? x, IDrawingObject? y) - { - if (x == null && y == null) - { - return 0; - } - if (x == null && y != null) - { - return 1; - } - if (x != null && y != null) - { - return -1; - } - var xBus = x as DrawingObjectBus; - var yBus = y as DrawingObjectBus; - - if (xBus == null && yBus == null) - { - return 0; - } - - if (xBus == null && yBus != null) - { - return 1; - } - - if (xBus != null && yBus == null) - { - return 1; - } - - if (xBus.GetBus.GetType().Name != yBus.GetBus.GetType().Name) - { - if (xBus.GetBus.GetType().Name == "DrawningAirbus") - { - return -1; - } - - return -1; - } - - var speedCompare = xBus.GetBus.Bus.Speed.CompareTo(yBus.GetBus.Bus.Speed); - - if (speedCompare != 0) - { - return speedCompare; - } - - return xBus.GetBus.Bus.Weight.CompareTo(yBus.GetBus.Bus.Weight); - } - } -} diff --git a/Bus/Bus/DrawingObjectBus.cs b/Bus/Bus/DrawingObjectBus.cs index 09f8c8d..696a203 100644 --- a/Bus/Bus/DrawingObjectBus.cs +++ b/Bus/Bus/DrawingObjectBus.cs @@ -8,9 +8,8 @@ namespace Bus { internal class DrawingObjectBus : IDrawingObject { - private DrawingBus _bus = null; + public DrawingBus _bus = null; public float Step => _bus?.Bus?.Step ?? 0; - public DrawingBus GetBus => _bus; public DrawingObjectBus(DrawingBus bus) diff --git a/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs b/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs index 6d616f8..00eaa34 100644 --- a/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs +++ b/Bus/Bus/FormMapWithSetDoubleDeckerBus.Designer.cs @@ -88,7 +88,7 @@ this.buttonSortByColor.TabIndex = 13; this.buttonSortByColor.Text = "Сохранить по цвету"; this.buttonSortByColor.UseVisualStyleBackColor = true; - this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); // // buttonSortByType // diff --git a/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs b/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs index 7fcdbdd..53ab154 100644 --- a/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs +++ b/Bus/Bus/FormMapWithSetDoubleDeckerBus.cs @@ -13,8 +13,6 @@ namespace Bus { public partial class FormMapWithSetDoubleDeckerBus : Form { - private MapWithSetDoubleDeckerBusGeneric _mapBusCollectionGeneric; - private readonly Dictionary _mapsDict = new() { {"Простая карта", new SimpleMap() }, @@ -31,9 +29,8 @@ namespace Bus comboBoxSelectorMap.Items.Clear(); foreach (var item in _mapsDict) { - comboBoxSelectorMap.Items.Add(item.Key);// + comboBoxSelectorMap.Items.Add(item.Key); } - } private void ReloadMaps() @@ -65,21 +62,13 @@ namespace Bus case "Водная карта": map = new MyMap(); break; - } - if (map != null) - { - _mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric(pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapBusCollectionGeneric = null; - } + } } private void ButtonAddBus_Click(object sender, EventArgs e) { var formBusConfig = new FormBusConfig(); - formBusConfig.AddEvent(AddBus); + formBusConfig.AddEvent(new(AddBus)); formBusConfig.Show(); } @@ -89,15 +78,14 @@ namespace Bus { if (listBoxMaps.SelectedIndex == -1) { - return; + MessageBox.Show("Перед добавлением объекта необходимо создать карту"); } - DrawingObjectBus boat = new(bus); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0) + else if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectBus(bus) != -1) { MessageBox.Show("Объект добавлен"); - _logger.LogInformation("Добавлен объект {@Airbus}", bus); + _logger.LogInformation("Добавлен объект {@bus}", bus); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } + } else { MessageBox.Show("Не удалось добавить объект"); @@ -106,39 +94,31 @@ namespace Bus } catch (StorageOverflowException ex) { - _logger.LogWarning("Ошибка, переполнение хранилища: {0}", ex.Message); - MessageBox.Show($"Ошибка, хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + MessageBox.Show("Не удалось добавить объект"); + _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); + MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } catch (ArgumentException ex) { - _logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airbus}", ex.Message, bus); + _logger.LogWarning("Ошибка добавления: {0}. Объект: {@bus}", ex.Message, bus); MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonRemoveBus_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) - { - return; - } - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) - { - return; - } - int pos = Convert.ToInt32(maskedTextBoxPosition.Text); try { - if (_mapBusCollectionGeneric - pos != null) + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBusCollectionGeneric.ShowSet(); + return; } - else + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { - MessageBox.Show("Не удалось удалить объект"); + return; } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); } catch (BusNotFoundException ex) { @@ -273,26 +253,18 @@ namespace Bus } } - private void ButtonSortByType_Click(object sender, EventArgs e) + private void SortBy(IComparer comparer) { if (listBoxMaps.SelectedIndex == -1) { return; } - - _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusComperyByType()); + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } - private void buttonSortByColor_Click(object sender, EventArgs e) - { - if (listBoxMaps.SelectedIndex == -1) - { - return; - } + private void ButtonSortByType_Click(object sender, EventArgs e) => SortBy(new BusCompareByType()); + private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new BusCompareByColor()); - _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BusComperyByColor()); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } } } diff --git a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs index a22b684..9954710 100644 --- a/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/MapWithSetDoubleDeckerBusGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Bus { internal class MapWithSetDoubleDeckerBusGeneric - where T : class, IDrawingObject, IEquatable + where T : class, IEquatable, IDrawingObject where U : AbstractMap { private readonly int _pictureWidth; diff --git a/Bus/Bus/SetDoubleDeckerBusGeneric.cs b/Bus/Bus/SetDoubleDeckerBusGeneric.cs index 2cc8965..861e5f3 100644 --- a/Bus/Bus/SetDoubleDeckerBusGeneric.cs +++ b/Bus/Bus/SetDoubleDeckerBusGeneric.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; @@ -11,8 +12,6 @@ namespace Bus { private readonly List _places; public int Count => _places.Count; - private int BusPlaces = 0; - private readonly int _maxCount; public SetDoubleDeckerBusGeneric(int count) @@ -33,30 +32,25 @@ namespace Bus public int Insert(T bus, int position) { - - if (_places.Contains(bus)) - throw new ArgumentException($"Объект {bus} уже есть в наборе"); - if (Count == _maxCount) - throw new StorageOverflowException(_maxCount); - if (!isCorrectPosition(position)) + if (_places.Contains(bus)) return 0; + if (position < 0) return -1; + if (Count >= _maxCount) { - return -1; + + throw new StorageOverflowException(_maxCount); } _places.Insert(position, bus); + return position; } public T Remove(int position) { - if (position < 0 || position >= _maxCount) - { + if (!isCorrectPosition(position)) return null; - } - if (position >= Count || position < 0) - { + var result = this[position]; + if (result == null) throw new BusNotFoundException(position); - } - var result = _places[position]; _places.RemoveAt(position); return result; } @@ -65,14 +59,7 @@ namespace Bus { get { - if (position >= 0 && position < _maxCount && position < Count) - { - return _places[position]; - } - else - { - return null; - } + return isCorrectPosition(position) && position < Count ? _places[position] : null; } set { @@ -101,7 +88,6 @@ namespace Bus { return; } - _places.Sort(comparer); } }