Pyatakov K.M. LabWork8 #13

Closed
ker73rus wants to merge 2 commits from LabWork8 into LabWork7
9 changed files with 306 additions and 7 deletions

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -58,6 +58,46 @@ namespace Stormtrooper
return true;
}
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;
}
private bool CheckCollision(Direction dir)
{
int left = (int)(_drawningObject.GetCurrentPosition().Left / _size_x);

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class AirCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xAir = x as DrawningObject;
var yAir = y as DrawningObject;
if (xAir == null && yAir == null)
{
return 0;
}
if (xAir == null && yAir != null)
{
return 1;
}
if (xAir != null && yAir == null)
{
return -1;
}
var xEntityAir = xAir.GetAirplane.Airplane;
var yEntityAir = yAir.GetAirplane.Airplane;
var baseColorCompare = xEntityAir.Color.ToArgb().CompareTo(yEntityAir.Color.ToArgb());
if (baseColorCompare != 0)
{
return baseColorCompare;
}
if (xEntityAir is EntityStormtrooper xStormtrooper && yEntityAir is EntityStormtrooper yStormtrooper)
{
var dopColorCompare = xStormtrooper.AdvColor.ToArgb().CompareTo(yStormtrooper.AdvColor.ToArgb());
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xAir.GetAirplane.Airplane.Speed.CompareTo(yAir.GetAirplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAir.GetAirplane.Airplane.Weight.CompareTo(yAir.GetAirplane.Airplane.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class AirCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xAir = x as DrawningObject;
var yAir = y as DrawningObject;
if (xAir == null && yAir == null)
{
return 0;
}
if (xAir == null && yAir != null)
{
return 1;
}
if (xAir != null && yAir == null)
{
return -1;
}
if (xAir.GetAirplane.GetType().Name != yAir.GetAirplane.GetType().Name)
{
if (xAir.GetAirplane.GetType().Name == "DrawningMilitaryAirplane")
{
return -1;
}
return 1;
}
var speedCompare = xAir.GetAirplane.Airplane.Speed.CompareTo(yAir.GetAirplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAir.GetAirplane.Airplane.Weight.CompareTo(yAir.GetAirplane.Airplane.Weight);
}
}
}

View File

@ -10,6 +10,8 @@ namespace Stormtrooper
{
private DrawningMilitaryAirplane _airplane;
public DrawningMilitaryAirplane GetAirplane => _airplane;
public DrawningObject(DrawningMilitaryAirplane airplane)
{
_airplane = airplane;
@ -44,5 +46,55 @@ namespace Stormtrooper
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherAir = other as DrawningObject;
if (otherAir == null)
{
return false;
}
var airplane = _airplane.Airplane;
var otherAirplane = otherAir._airplane.Airplane;
if (airplane.Speed != otherAirplane.Speed)
{
return false;
}
if (airplane.Weight != otherAirplane.Weight)
{
return false;
}
if (airplane.Color != otherAirplane.Color)
{
return false;
}
if (airplane is EntityStormtrooper stormtrooper && otherAirplane is EntityStormtrooper otherStormtrooper)
{
if (stormtrooper.Rockets != otherStormtrooper.Rockets)
{
return false;
}
if (stormtrooper.Radar != otherStormtrooper.Radar)
{
return false;
}
if (stormtrooper.Booster != otherStormtrooper.Booster)
{
return false;
}
if (stormtrooper.AdvColor != otherStormtrooper.AdvColor)
{
return false;
}
}
else if (airplane is EntityStormtrooper || otherAirplane is EntityStormtrooper)
{
return false;
}
return true;
}
}
}

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout();
this.groupBoxMap.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.groupBoxMap);
this.groupBoxTools.Controls.Add(this.buttonRight);
this.groupBoxTools.Controls.Add(this.buttonLeft);
@ -86,7 +90,7 @@
this.groupBoxMap.Controls.Add(this.comboBoxMapSelector);
this.groupBoxMap.Location = new System.Drawing.Point(6, 22);
this.groupBoxMap.Name = "groupBoxMap";
this.groupBoxMap.Size = new System.Drawing.Size(182, 259);
this.groupBoxMap.Size = new System.Drawing.Size(182, 240);
this.groupBoxMap.TabIndex = 19;
this.groupBoxMap.TabStop = false;
this.groupBoxMap.Text = "Карты";
@ -265,14 +269,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -280,6 +284,26 @@
//
this.openFileDialog.FileName = "openFileDialog1";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(13, 282);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(75, 23);
this.buttonSortByColor.TabIndex = 20;
this.buttonSortByColor.Text = "по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(95, 282);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(75, 23);
this.buttonSortByType.TabIndex = 21;
this.buttonSortByType.Text = "по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// FormMapWithSetAirplane
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -328,5 +352,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -122,6 +122,11 @@ namespace Stormtrooper
_logger.LogWarning($"Ошибка переполнения хранилища: {ex.Message}");
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning($"Ошибка при добавлении: {ex.Message}");
MessageBox.Show($"Ошибка при добавлении: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
_logger.LogWarning($"Неизвестная ошибка: {ex.Message}");
@ -129,6 +134,35 @@ namespace Stormtrooper
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMap.SelectedIndex == -1)
{
return;
}
_mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].Sort(new AirCompareByType());
pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMap.SelectedIndex == -1)
{
return;
}
_mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].Sort(new AirCompareByColor());
pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Удаление объекта
/// </summary>

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal class MapWithSetAirplaneGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -82,6 +82,15 @@ namespace Stormtrooper
DrawAirs(gr);
return bmp;
}
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setAirs.SortSet(comparer);
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal class SetAirplaneGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// List объектов, которые храним
@ -34,6 +34,8 @@ namespace Stormtrooper
/// <returns></returns>
public int Insert(T airplane)
{
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
Insert(airplane, 0);
@ -47,6 +49,10 @@ namespace Stormtrooper
/// <returns></returns>
public int Insert(T airplane, int position)
{
if (_places.Contains(airplane))
{
throw new ArgumentException($"Объект {airplane} уже присутствует в наборе");
}
if (position < 0 || position >= _maxCount - 1)
throw new StorageOverflowException(_maxCount);
_places.Insert(position, airplane);
@ -66,6 +72,21 @@ namespace Stormtrooper
_places.RemoveAt(position);
return air;
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>