Merge pull request 'Agliullov D. A. Lab Work 8 Advanced' (#9) from Lab8 into Lab7

Reviewed-on: http://student.git.athene.tech/d.agliullov/ISEbd-21_Agliullov.D.A._AirBomber.Advanced/pulls/9
This commit is contained in:
Евгений Эгов 2022-11-07 11:34:01 +04:00
commit 2ffd010a15
11 changed files with 317 additions and 46 deletions

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace AirBomber
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
private Bitmap? _staticBitMap;
@ -172,5 +172,32 @@ namespace AirBomber
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,33 @@
namespace AirBomber
{
internal class AirplaneCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xAirplane = (DrawningAirplane?)(x as DrawningObject);
var yAirplane = (DrawningAirplane?)(y as DrawningObject);
if (xAirplane == yAirplane)
{
return 0;
}
if (xAirplane == null)
{
return 1;
}
if (yAirplane == null)
{
return -1;
}
var xEntity = xAirplane.Airplane;
var yEntity = yAirplane.Airplane;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 ||
xEntity is not EntityAirBomber xEntityAirBomber ||
yEntity is not EntityAirBomber yEntityAirBomber)
{
return colorWeight;
}
return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,37 @@
namespace AirBomber
{
internal class AirplaneCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xAirplane = (DrawningAirplane?)(x as DrawningObject);
var yAirplane = (DrawningAirplane?)(y as DrawningObject);
if (xAirplane == yAirplane)
{
return 0;
}
if (xAirplane == null)
{
return 1;
}
if (yAirplane == null)
{
return -1;
}
if (xAirplane.GetType().Name != yAirplane.GetType().Name)
{
if (xAirplane.Airplane.GetType() == typeof(DrawningAirplane))
{
return -1;
}
return 1;
}
var speedCompare = xAirplane.Airplane.Speed.CompareTo(yAirplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirplane.Airplane.Weight.CompareTo(yAirplane.Airplane.Weight);
}
}
}

View File

@ -80,5 +80,23 @@ namespace AirBomber
g.DrawLine(pen, baseTail, new PointF(r.Right, r.Top));
g.DrawLine(pen, baseTail, new PointF(r.Right, r.Bottom));
}
/// <summary>
/// Возвращает итератор со свойствами самолета в порядке:
/// Скорость -> Вес -> Цвет корпуса -> Тип двигателя, либо null если их нет -> Количество двигателей -> Дополнительный цвет -> Наличие бомб -> Наличие топливных баков
/// </summary>
public override IEnumerator<object> GetEnumerator()
{
IEnumerator<object> enumBase = base.GetEnumerator();
while (enumBase.MoveNext())
{
yield return enumBase.Current;
}
var entity = (EntityAirBomber)Airplane;
yield return entity.DopColor;
yield return entity.HasBombs;
yield return entity.HasFuelTanks;
yield break;
}
}
}

View File

@ -1,9 +1,12 @@
namespace AirBomber
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections;
namespace AirBomber
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningAirplane
public class DrawningAirplane : IEnumerator<object>, IEnumerable<object>
{
/// <summary>
/// Класс-сущность
@ -11,6 +14,11 @@
public EntityAirplane Airplane { get; protected set; }
public IAirplaneEngines? DrawningEngines { get; protected set; }
private IEnumerator<object>? _machineStateEnum = null;
public object Current => _machineStateEnum?.Current;
/// <summary>
/// Левая координата отрисовки самолета
/// </summary>
@ -241,5 +249,34 @@
{
return new(_startPosX, _startPosY, _airplaneWidth, _airplaneHeight);
}
public bool MoveNext()
{
if (_machineStateEnum == null)
{
_machineStateEnum = GetEnumerator();
}
return _machineStateEnum.MoveNext();
}
public void Reset() => throw new NotSupportedException();
public void Dispose() => GC.SuppressFinalize(this);
/// <summary>
/// Возвращает итератор со свойствами самолета в порядке:
/// Скорость -> Вес -> Цвет корпуса -> Тип двигателя, либо null если их нет -> Количество двигателей
/// </summary>
virtual public IEnumerator<object> GetEnumerator()
{
yield return Airplane.Speed;
yield return Airplane.Weight;
yield return Airplane.BodyColor;
yield return DrawningEngines;
yield return DrawningEngines?.CountEngines ?? 0;
yield break;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}

View File

@ -42,10 +42,38 @@ namespace AirBomber
/// </summary>
/// <param name="drawningObject">The drawning object.</param>
/// <returns></returns>
public static explicit operator DrawningAirplane(DrawningObject drawningObject) => drawningObject._airplane;
public static explicit operator DrawningAirplane?(DrawningObject? drawningObject) => drawningObject?._airplane ?? null;
public string GetInfo() => _airplane?.GetDataForSave();
public string GetInfo() => _airplane?.GetDataForSave() ?? string.Empty;
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
public bool Equals(IDrawningObject? other)
{
if (other is not DrawningObject otherAirplane ||
_airplane.DrawningEngines?.GetType() != otherAirplane._airplane.DrawningEngines?.GetType() ||
_airplane.DrawningEngines?.CountEngines != otherAirplane._airplane.DrawningEngines?.CountEngines)
{
return false;
}
var entity = _airplane.Airplane;
var otherEntity = otherAirplane._airplane.Airplane;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityAirBomber entityAirBomber &&
otherEntity is EntityAirBomber otherEntityAirBomber && (
entityAirBomber.HasBombs != otherEntityAirBomber.HasBombs ||
entityAirBomber.DopColor != otherEntityAirBomber.DopColor ||
entityAirBomber.HasFuelTanks != otherEntityAirBomber.HasFuelTanks))
{
return false;
}
return true;
}
}
}

View File

@ -30,6 +30,8 @@
{
this.comboTypeEngines = new System.Windows.Forms.ComboBox();
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.btnAddDeletedObject = new System.Windows.Forms.Button();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonAddMap = new System.Windows.Forms.Button();
@ -60,10 +62,10 @@
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.SaveMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
this.groupBoxGenerate.SuspendLayout();
@ -89,6 +91,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.btnAddDeletedObject);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
@ -102,16 +106,36 @@
this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonUp);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(811, 24);
this.groupBoxTools.Location = new System.Drawing.Point(840, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(204, 786);
this.groupBoxTools.Size = new System.Drawing.Size(409, 522);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(223, 413);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(175, 35);
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(223, 372);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(175, 35);
this.buttonSortByType.TabIndex = 28;
this.buttonSortByType.Text = "Отсортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// btnAddDeletedObject
//
this.btnAddDeletedObject.Location = new System.Drawing.Point(17, 689);
this.btnAddDeletedObject.Location = new System.Drawing.Point(25, 317);
this.btnAddDeletedObject.Name = "btnAddDeletedObject";
this.btnAddDeletedObject.Size = new System.Drawing.Size(175, 45);
this.btnAddDeletedObject.TabIndex = 27;
@ -126,9 +150,9 @@
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(6, 267);
this.groupBoxMaps.Location = new System.Drawing.Point(212, 14);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(192, 248);
this.groupBoxMaps.Size = new System.Drawing.Size(192, 253);
this.groupBoxMaps.TabIndex = 26;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
@ -137,7 +161,7 @@
//
this.buttonAddMap.Location = new System.Drawing.Point(11, 80);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(175, 35);
this.buttonAddMap.Size = new System.Drawing.Size(175, 47);
this.buttonAddMap.TabIndex = 2;
this.buttonAddMap.Text = "Добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
@ -145,9 +169,9 @@
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(11, 206);
this.buttonDeleteMap.Location = new System.Drawing.Point(11, 218);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(175, 35);
this.buttonDeleteMap.Size = new System.Drawing.Size(175, 29);
this.buttonDeleteMap.TabIndex = 4;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
@ -157,7 +181,7 @@
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(11, 121);
this.listBoxMaps.Location = new System.Drawing.Point(11, 133);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(175, 79);
this.listBoxMaps.TabIndex = 3;
@ -184,7 +208,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 565);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 379);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -193,9 +217,9 @@
//
// buttonRemoveAirplane
//
this.buttonRemoveAirplane.Location = new System.Drawing.Point(17, 594);
this.buttonRemoveAirplane.Location = new System.Drawing.Point(25, 413);
this.buttonRemoveAirplane.Name = "buttonRemoveAirplane";
this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 26);
this.buttonRemoveAirplane.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveAirplane.TabIndex = 23;
this.buttonRemoveAirplane.Text = "Удалить самолет";
this.buttonRemoveAirplane.UseVisualStyleBackColor = true;
@ -203,9 +227,9 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(17, 626);
this.buttonShowStorage.Location = new System.Drawing.Point(223, 273);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 26);
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 24;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
@ -213,9 +237,9 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 658);
this.buttonShowOnMap.Location = new System.Drawing.Point(223, 317);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 25);
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 45);
this.buttonShowOnMap.TabIndex = 25;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
@ -223,7 +247,7 @@
//
// buttonAddAirplane
//
this.buttonAddAirplane.Location = new System.Drawing.Point(17, 521);
this.buttonAddAirplane.Location = new System.Drawing.Point(23, 273);
this.buttonAddAirplane.Name = "buttonAddAirplane";
this.buttonAddAirplane.Size = new System.Drawing.Size(175, 35);
this.buttonAddAirplane.TabIndex = 21;
@ -333,7 +357,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(90, 750);
this.buttonDown.Location = new System.Drawing.Point(57, 486);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -345,7 +369,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(126, 750);
this.buttonRight.Location = new System.Drawing.Point(93, 486);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -357,7 +381,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(54, 750);
this.buttonLeft.Location = new System.Drawing.Point(21, 486);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -369,7 +393,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(90, 714);
this.buttonUp.Location = new System.Drawing.Point(57, 450);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -381,7 +405,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 24);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(811, 786);
this.pictureBox.Size = new System.Drawing.Size(840, 522);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -391,7 +415,7 @@
this.файлToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(1015, 24);
this.menuStrip.Size = new System.Drawing.Size(1249, 24);
this.menuStrip.TabIndex = 2;
//
// файлToolStripMenuItem
@ -411,6 +435,13 @@
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// SaveMapToolStripMenuItem
//
this.SaveMapToolStripMenuItem.Name = "SaveMapToolStripMenuItem";
this.SaveMapToolStripMenuItem.Size = new System.Drawing.Size(280, 22);
this.SaveMapToolStripMenuItem.Text = "Сохранение Выбранного хранилища";
this.SaveMapToolStripMenuItem.Click += new System.EventHandler(this.SaveMapToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
@ -426,18 +457,11 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// SaveMapToolStripMenuItem
//
this.SaveMapToolStripMenuItem.Name = "SaveMapToolStripMenuItem";
this.SaveMapToolStripMenuItem.Size = new System.Drawing.Size(280, 22);
this.SaveMapToolStripMenuItem.Text = "Сохранение Выбранного хранилища";
this.SaveMapToolStripMenuItem.Click += new System.EventHandler(this.SaveMapToolStripMenuItem_Click);
//
// FormMapWithSetAirplanes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1015, 810);
this.ClientSize = new System.Drawing.Size(1249, 546);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -499,5 +523,7 @@
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private ToolStripMenuItem SaveMapToolStripMenuItem;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -143,7 +143,7 @@ namespace AirBomber
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[NameMap].ShowSet();
_logger.LogInformation("Добавлен объект {@Airplane}", (DrawningAirplane)airplane);
_logger.LogInformation("Добавлен объект {@Airplane}", (DrawningAirplane?)airplane);
}
}
catch (StorageOverflowException ex)
@ -151,6 +151,11 @@ namespace AirBomber
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, (DrawningAirplane?)airplane);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Перемещение
@ -401,5 +406,23 @@ namespace AirBomber
}
}
private void maskedTextBoxPosition_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void SortBy(IComparer<IDrawningObject> comparer)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByType_Click (object sender, EventArgs e) => SortBy(new AirplaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new AirplaneCompareByColor());
}
}

View File

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

View File

@ -6,8 +6,8 @@ using System.Threading.Tasks;
namespace AirBomber
{
internal class MapWithSetAirplanesGeneric<T, U>
where T : class, IDrawningObject
internal class MapWithSetAirplanesGeneric<T, U> : IComparable<MapWithSetAirplanesGeneric<T, U>>
where T : class, IEquatable<T>, IDrawningObject
where U : AbstractMap
{
/// <summary>
@ -210,5 +210,27 @@ namespace AirBomber
SetAirplanes.Insert(DrawningObject.Create(rec) as T);
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
SetAirplanes.SortSet(comparer);
}
public int CompareTo(MapWithSetAirplanesGeneric<T, U>? other)
{
if (other == null)
{
return 1;
}
if (this == other)
{
return 0;
}
return SetAirplanes.Count.CompareTo(other.SetAirplanes.Count);
}
}
}

View File

@ -11,7 +11,7 @@ namespace AirBomber
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetAirplanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -51,9 +51,13 @@ namespace AirBomber
/// </summary>
/// <param name="airplane">Добавляемый самолет</param>
/// <param name="position">Позиция</param>
/// <exception cref="ArgumentException"> Исключения генерируется при попытке добавить дубликат в набор</exception>
/// <exception cref="StorageOverflowException"> </exception>
/// <returns>Возвращает позицию вставленого объекта либо -1, если не получилось его добавить</returns>
public int Insert(T airplane, int position)
{
if (_places.Contains(airplane))
throw new ArgumentException($"Объект {airplane} уже есть в наборе");
if (Count == _maxcount)
throw new StorageOverflowException(_maxcount);
if (!isCorrectPosition(position))
@ -111,7 +115,23 @@ namespace AirBomber
yield break;
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
public void Clear()
{
_places.Clear();
}
public void Clear()