Emelyanov A.S. LabWork08 #13

Closed
Emelyanov535 wants to merge 4 commits from LabWork08 into LabWork07
9 changed files with 282 additions and 20 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Airbus
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -121,5 +121,32 @@ namespace Airbus
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

@ -14,6 +14,7 @@ namespace Airbus
_plane = plane;
}
public float Step => _plane?.Plane?.Step ?? 0;
public DrawningPlane GetPlane => _plane;
public (float Left, float Top, float Right, float Bottom)
GetCurrentPosition()
{
@ -33,5 +34,59 @@ namespace Airbus
}
public string GetInfo() => _plane?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectPlane(data.CreateDrawningPlane());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherPlane = other as DrawningObjectPlane;
if (otherPlane == null)
{
return false;
}
var plane = _plane.Plane;
var otherPlanePlane = otherPlane._plane.Plane;
if (plane.Speed != otherPlanePlane.Speed)
{
return false;
}
if (plane.Weight != otherPlanePlane.Weight)
{
return false;
}
if (plane.BodyColor != otherPlanePlane.BodyColor)
{
return false;
}
if ((plane is EntityAirbus) && !(otherPlanePlane is EntityAirbus)
|| !(plane is EntityAirbus) && (otherPlanePlane is EntityAirbus))
{
return false;
}
if (plane is EntityAirbus airbus && otherPlanePlane is EntityAirbus otherAirbus)
{
if (airbus.DopColor != otherAirbus.DopColor)
{
return false;
}
if (airbus.BodyKit != otherAirbus.BodyKit)
{
return false;
}
if (airbus.Wing != otherAirbus.BodyKit)
{
return false;
}
if (airbus.SportLine != otherAirbus.SportLine)
{
return false;
}
}
return true;
}
}
}

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonSortByColor);
this.groupBox1.Controls.Add(this.buttonSortByType);
this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.buttonLeft);
this.groupBox1.Controls.Add(this.buttonRight);
@ -72,7 +76,7 @@
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(657, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 616);
this.groupBox1.Size = new System.Drawing.Size(200, 674);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -143,7 +147,7 @@
//
this.buttonLeft.BackgroundImage = global::Airbus.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(56, 572);
this.buttonLeft.Location = new System.Drawing.Point(56, 634);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 9;
@ -154,7 +158,7 @@
//
this.buttonRight.BackgroundImage = global::Airbus.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(128, 572);
this.buttonRight.Location = new System.Drawing.Point(128, 634);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 8;
@ -165,7 +169,7 @@
//
this.buttonDown.BackgroundImage = global::Airbus.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(92, 572);
this.buttonDown.Location = new System.Drawing.Point(92, 634);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 7;
@ -176,7 +180,7 @@
//
this.buttonUp.BackgroundImage = global::Airbus.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(92, 536);
this.buttonUp.Location = new System.Drawing.Point(92, 598);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 6;
@ -185,7 +189,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 499);
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 561);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(171, 31);
this.buttonShowOnMap.TabIndex = 5;
@ -195,7 +199,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(17, 459);
this.buttonShowStorage.Location = new System.Drawing.Point(17, 521);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(171, 34);
this.buttonShowStorage.TabIndex = 4;
@ -205,7 +209,7 @@
//
// buttonRemovePlane
//
this.buttonRemovePlane.Location = new System.Drawing.Point(17, 400);
this.buttonRemovePlane.Location = new System.Drawing.Point(17, 462);
this.buttonRemovePlane.Name = "buttonRemovePlane";
this.buttonRemovePlane.Size = new System.Drawing.Size(171, 31);
this.buttonRemovePlane.TabIndex = 3;
@ -215,7 +219,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 371);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 433);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(171, 23);
@ -223,7 +227,7 @@
//
// buttonAddPlane
//
this.buttonAddPlane.Location = new System.Drawing.Point(17, 315);
this.buttonAddPlane.Location = new System.Drawing.Point(17, 377);
this.buttonAddPlane.Name = "buttonAddPlane";
this.buttonAddPlane.Size = new System.Drawing.Size(171, 31);
this.buttonAddPlane.TabIndex = 1;
@ -236,7 +240,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(657, 616);
this.pictureBox.Size = new System.Drawing.Size(657, 674);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
@ -262,14 +266,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);
//
@ -281,11 +285,31 @@
//
this.saveFileDialog.Filter = "«txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(17, 291);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(171, 26);
this.buttonSortByType.TabIndex = 11;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(17, 323);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(171, 26);
this.buttonSortByColor.TabIndex = 12;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
//
// FormMapWithSetPlanes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(857, 640);
this.ClientSize = new System.Drawing.Size(857, 698);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip1);
@ -330,5 +354,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -295,5 +295,25 @@ namespace Airbus
}
}
}
private void buttonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new PlaneCompareByType());
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 PlaneCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Airbus
{
internal class MapWithSetPlanesGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -212,5 +212,9 @@ namespace Airbus
_setPlanes.Insert(DrawningObjectPlane.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setPlanes.SortSet(comparer);
}
}
}

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
internal class PlaneCompareByColor : 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 xPlane = x as DrawningObjectPlane;
var yPlane = y as DrawningObjectPlane;
if (xPlane == null && yPlane == null)
{
return 0;
}
if (xPlane == null && yPlane != null)
{
return 1;
}
if (xPlane != null && yPlane == null)
{
return -1;
}
string xShipColor = xPlane.GetPlane.Plane.BodyColor.Name;
string yShipColor = yPlane.GetPlane.Plane.BodyColor.Name;
if (xShipColor != yShipColor)
{
return xShipColor.CompareTo(yShipColor);
}
if (xPlane.GetPlane.Plane is EntityAirbus xAirbus && yPlane.GetPlane.Plane is EntityAirbus yAirbus)
{
string xShipDopColor = xAirbus.DopColor.Name;
string yShipDopColor = yAirbus.DopColor.Name;
var dopColorCompare = xShipDopColor.CompareTo(yShipDopColor);
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xPlane.GetPlane.Plane.Speed.CompareTo(yPlane.GetPlane.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xPlane.GetPlane.Plane.Weight.CompareTo(yPlane.GetPlane.Plane.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
internal class PlaneCompareByType : 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 xPlane = x as DrawningObjectPlane;
var yPlane = y as DrawningObjectPlane;
if (xPlane == null && yPlane == null)
{
return 0;
}
if (xPlane == null && yPlane != null)
{
return 1;
}
if (xPlane != null && yPlane == null)
{
return -1;
}
if (xPlane.GetPlane.GetType().Name != yPlane.GetPlane.GetType().Name)
{
if (xPlane.GetPlane.GetType().Name == "DrawningPlane")
{
return -1;
}
return 1;
}
var speedCompare = xPlane.GetPlane.Plane.Speed.CompareTo(yPlane.GetPlane.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xPlane.GetPlane.Plane.Weight.CompareTo(yPlane.GetPlane.Plane.Weight);
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Airbus
{
internal class SetPlanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -35,8 +35,7 @@ namespace Airbus
public int Insert(T plane)
{
if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(0, plane);
return 0;
return Insert(plane, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -48,6 +47,10 @@ namespace Airbus
{
// TODO проверка позиции
// TODO вставка по позиции
if (_places.Contains(plane))
{
return -1;
}
if (_places.Count == _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, plane);
return position;
@ -113,5 +116,14 @@ namespace Airbus
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}