This commit is contained in:
Дамир Нугаев 2022-12-05 23:02:12 +04:00
parent 75aeb9e0ec
commit f73284338d
10 changed files with 146 additions and 242 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
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;
}
}
}

View File

@ -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<IDrawingObject>
{
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());
}
}
}

View File

@ -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<IDrawingObject>
{
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);
}
}
}

View File

@ -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<IDrawingObject>
{
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);
}
}
}

View File

@ -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<IDrawingObject>
{
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);
}
}
}

View File

@ -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)

View File

@ -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
//

View File

@ -13,8 +13,6 @@ namespace Bus
{
public partial class FormMapWithSetDoubleDeckerBus : Form
{
private MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap> _mapBusCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _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<DrawingObjectBus, AbstractMap>(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<IDrawingObject> 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();
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal class MapWithSetDoubleDeckerBusGeneric<T, U>
where T : class, IDrawingObject, IEquatable<T>
where T : class, IEquatable<T>, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;

View File

@ -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<T> _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);
}
}