4 Commits

Author SHA1 Message Date
Nikita Potapov
cf3fbd9e96 fixed bug with similar objects checking 2023-01-16 19:11:29 +04:00
Nikita Potapov
e603c06444 AbstractMap IEquatable 2022-12-27 10:32:17 +04:00
Nikita Potapov
30e5121c94 Сортировка 2022-12-27 10:27:47 +04:00
Nikita Potapov
9387eb7e2f Сравнение объектов 2022-12-25 10:42:59 +04:00
9 changed files with 293 additions and 32 deletions

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Boats
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@@ -196,5 +196,43 @@ namespace Boats
/// <param name="i"></param>
/// <param name="j"></param>
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null)
{
return false;
}
var otherMap = other as AbstractMap;
if (otherMap == null)
{
return false;
}
if (_width != otherMap._width)
{
return false;
}
if (_height != otherMap._height)
{
return false;
}
if (_size_x != otherMap._size_x)
{
return false;
}
if (_size_y != otherMap._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] != otherMap._map[i, j])
return false;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Boats
{
internal class BoatCompareByColor : 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 xBoat = x as DrawingObjectBoat;
var yBoat = y as DrawingObjectBoat;
if (xBoat == null && yBoat == null)
{
return 0;
}
if (xBoat == null && yBoat != null)
{
return 1;
}
if (xBoat != null && yBoat == null)
{
return -1;
}
string xBoatColor = xBoat.GetBoat.Boat.BodyColor.Name;
string yBoatColor = yBoat.GetBoat.Boat.BodyColor.Name;
if (xBoatColor != yBoatColor)
{
return xBoatColor.CompareTo(yBoatColor);
}
if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name)
{
if (xBoat.GetBoat.GetType().Name == "DrawingBoat")
{
return -1;
}
return 1;
}
if (xBoat.GetBoat.Boat is EntityCatamaran xCatamaran &&
yBoat.GetBoat.Boat is EntityCatamaran yCatamaran)
{
string xBoatDopColor = xCatamaran.DopColor.Name;
string yBoatDopColor = yCatamaran.DopColor.Name;
var dopColorCompare = xBoatDopColor.CompareTo(yBoatDopColor);
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight);
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Boats
{
internal class BoatCompareByType : 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 xBoat = x as DrawingObjectBoat;
var yBoat = y as DrawingObjectBoat;
if (xBoat == null && yBoat == null)
{
return 0;
}
if (xBoat == null && yBoat != null)
{
return 1;
}
if (xBoat != null && yBoat == null)
{
return -1;
}
if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name)
{
if (xBoat.GetBoat.GetType().Name == "DrawingBoat")
{
return -1;
}
return 1;
}
var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight);
}
}
}

View File

@@ -9,6 +9,7 @@ namespace Boats
internal class DrawingObjectBoat : IDrawingObject
{
private DrawingBoat _boat = null;
public DrawingBoat GetBoat => _boat;
public DrawingObjectBoat(DrawingBoat boat)
{
_boat = boat;
@@ -32,5 +33,55 @@ namespace Boats
}
public string GetInfo() => _boat?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBoat(data.CreateDrawingBoat());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherBoat = other as DrawingObjectBoat;
if (otherBoat == null)
{
return false;
}
var boat = _boat.Boat;
var otherBoatBoat = otherBoat._boat.Boat;
if (boat.Speed != otherBoatBoat.Speed)
{
return false;
}
if (boat.Weight != otherBoatBoat.Weight)
{
return false;
}
if (boat.BodyColor != otherBoatBoat.BodyColor)
{
return false;
}
if (boat is EntityCatamaran catamaran)
{
if (otherBoatBoat is EntityCatamaran otherCatamaran)
{
if (catamaran.Sail != otherCatamaran.Sail)
{
return false;
}
if (catamaran.Bobbers != otherCatamaran.Bobbers)
{
return false;
}
if (catamaran.DopColor != otherCatamaran.DopColor)
{
return false;
}
}
else
{
return false;
}
}
return true;
}
}
}

View File

@@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBoxInstruments = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.ButtonDown = new System.Windows.Forms.Button();
this.ButtonRight = new System.Windows.Forms.Button();
this.ButtonLeft = new System.Windows.Forms.Button();
@@ -59,6 +61,8 @@
//
// groupBoxInstruments
//
this.groupBoxInstruments.Controls.Add(this.buttonSortByColor);
this.groupBoxInstruments.Controls.Add(this.buttonSortByType);
this.groupBoxInstruments.Controls.Add(this.ButtonDown);
this.groupBoxInstruments.Controls.Add(this.ButtonRight);
this.groupBoxInstruments.Controls.Add(this.ButtonLeft);
@@ -76,6 +80,26 @@
this.groupBoxInstruments.TabStop = false;
this.groupBoxInstruments.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(9, 354);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(232, 29);
this.buttonSortByColor.TabIndex = 12;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(9, 321);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(232, 29);
this.buttonSortByType.TabIndex = 11;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// ButtonDown
//
this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
@@ -270,14 +294,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(166, 26);
this.SaveToolStripMenuItem.Text = "Сохранить";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(166, 26);
this.LoadToolStripMenuItem.Text = "Загрузить";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@@ -339,5 +363,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@@ -28,10 +28,6 @@ namespace Boats
private readonly MapsCollection _mapsCollection;
private readonly ILogger _logger;
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetBoats(ILogger<FormMapWithSetBoats> logger)
@@ -72,28 +68,8 @@ namespace Boats
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (ComboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Линии карта":
map = new LineMap();
break;
case "Океан карта":
map = new OceanMap();
break;
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBoatsCollectionGeneric = null;
}
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty]?.ShowSet();
_logger.LogInformation($"Переход на карту: {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}");
}
/// <summary>
/// Добавление объекта
@@ -291,6 +267,12 @@ namespace Boats
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning($"Ошибка добавления объекта: {ex.Message}");
MessageBox.Show($"Ошибка добавления объекта: {ex.Message}",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Обработка нажатия "Сохранить"
@@ -338,5 +320,25 @@ namespace Boats
}
}
}
private void buttonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void buttonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@@ -9,7 +9,7 @@ namespace Boats
/// <summary>
/// Интерфейс для работы с объектом, прорисовываемым на форме
/// </summary>
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@@ -12,7 +12,7 @@ namespace Boats
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetBoatsGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@@ -264,5 +264,9 @@ namespace Boats
_setBoats.Insert(DrawingObjectBoat.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setBoats.SortSet(comparer);
}
}
}

View File

@@ -11,7 +11,7 @@ namespace Boats
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetBoatsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Массив объектов, которые храним
@@ -56,6 +56,11 @@ namespace Boats
/// <returns></returns>
public int Insert(T boat, int position)
{
// Проверка на уникальность
if (_places.Contains(boat))
{
throw new ArgumentException($"Объект {boat} уже есть");
}
// Проверка позиции
if (position < 0 || position >= _maxCount - 1)
return -1;
@@ -114,5 +119,13 @@ namespace Boats
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}