Сортировка

This commit is contained in:
Arklightning 2022-12-19 23:15:34 +04:00
parent 28682f5779
commit 9e07faa1b4
11 changed files with 275 additions and 32 deletions

@ -7,7 +7,7 @@ using System.Drawing;
namespace Trolleybus
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -136,5 +136,45 @@ namespace Trolleybus
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)
{
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;
}
}
}

@ -8,9 +8,9 @@ using static System.Windows.Forms.AxHost;
namespace Trolleybus
{
internal class DrawningObject : IDrawningObject
internal class DrawningObject
{
private DrawingTrolleybus _trolleybus = null;
public DrawingTrolleybus _trolleybus = null;
public DrawningObject(DrawingTrolleybus car)
{
@ -38,12 +38,5 @@ namespace Trolleybus
{
_trolleybus.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
_trolleybus.DrawTransport(g);
}
public string GetInfo() => _trolleybus?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectTrolleybus(data.CreateDrawingTrolleybus());
}
}

@ -9,7 +9,7 @@ namespace Trolleybus
{
internal class DrawningObjectTrolleybus : IDrawningObject
{
private DrawingTrolleybus _trolleybus = null;
public DrawingTrolleybus _trolleybus { get; private set; }
public DrawningObjectTrolleybus(DrawingTrolleybus trolleybus)
{
@ -18,6 +18,8 @@ namespace Trolleybus
public float Step => _trolleybus?.Trolleybus?.Step ?? 0;
public DrawingTrolleybus GetTrolleybus => _trolleybus;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _trolleybus?.GetCurrentPosition() ?? default;
@ -43,11 +45,37 @@ namespace Trolleybus
_trolleybus.DrawTransport(g);
}
public string GetInfo() => _trolleybus?.GetDataForSave();
public string GetInfo() => _trolleybus?.GetDataForSave() ?? string.Empty;
public static IDrawningObject Create(string data)
{
return new DrawningObjectTrolleybus(data.CreateDrawingTrolleybus());
}
public bool Equals(IDrawningObject? other)
{
if (other is not DrawningObjectTrolleybus otherAirplane)
{
return false;
}
var entity = _trolleybus.Trolleybus;
var otherEntity = otherAirplane._trolleybus.Trolleybus;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntitySmallTrolleybus entitySmallTrolleybus &&
otherEntity is EntitySmallTrolleybus otherEntityAirplaneWithRadar && (
entitySmallTrolleybus.BodyKit != otherEntityAirplaneWithRadar.BodyKit ||
entitySmallTrolleybus.DopColor != otherEntityAirplaneWithRadar.DopColor ||
entitySmallTrolleybus.Horns != otherEntityAirplaneWithRadar.Horns ||
entitySmallTrolleybus.Battary != otherEntityAirplaneWithRadar.Battary))
{
return false;
}
return true;
}
}
}

@ -51,6 +51,8 @@
this.downloadToolStripMenuItem = 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.groupBox2.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.groupBox2);
this.groupBox1.Controls.Add(this.buttonLeft);
this.groupBox1.Controls.Add(this.buttonRight);
@ -70,11 +74,11 @@
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddTrolleybus);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(900, 33);
this.groupBox1.Location = new System.Drawing.Point(900, 36);
this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.groupBox1.Size = new System.Drawing.Size(300, 964);
this.groupBox1.Size = new System.Drawing.Size(300, 1080);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -153,7 +157,7 @@
//
this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(120, 931);
this.buttonLeft.Location = new System.Drawing.Point(124, 1021);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(48, 48);
@ -165,7 +169,7 @@
//
this.buttonRight.BackgroundImage = global::Trolleybus.Properties.Resources.right30;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(234, 931);
this.buttonRight.Location = new System.Drawing.Point(238, 1021);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(48, 48);
@ -177,7 +181,7 @@
//
this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(177, 931);
this.buttonDown.Location = new System.Drawing.Point(181, 1021);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(48, 48);
@ -189,7 +193,7 @@
//
this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(177, 874);
this.buttonUp.Location = new System.Drawing.Point(181, 964);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(48, 48);
@ -199,7 +203,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(10, 786);
this.buttonShowOnMap.Location = new System.Drawing.Point(14, 876);
this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(272, 35);
@ -210,7 +214,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(10, 725);
this.buttonShowStorage.Location = new System.Drawing.Point(14, 815);
this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(272, 35);
@ -221,7 +225,7 @@
//
// buttonRemoveTrolleybus
//
this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(9, 654);
this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(13, 744);
this.buttonRemoveTrolleybus.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus";
this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(273, 35);
@ -232,7 +236,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(10, 589);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(14, 679);
this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(270, 26);
@ -240,7 +244,7 @@
//
// buttonAddTrolleybus
//
this.buttonAddTrolleybus.Location = new System.Drawing.Point(10, 523);
this.buttonAddTrolleybus.Location = new System.Drawing.Point(14, 613);
this.buttonAddTrolleybus.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonAddTrolleybus.Name = "buttonAddTrolleybus";
this.buttonAddTrolleybus.Size = new System.Drawing.Size(272, 35);
@ -252,10 +256,10 @@
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 33);
this.pictureBox.Location = new System.Drawing.Point(0, 36);
this.pictureBox.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(900, 964);
this.pictureBox.Size = new System.Drawing.Size(900, 1080);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -267,7 +271,7 @@
this.FiletoolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1200, 33);
this.menuStrip1.Size = new System.Drawing.Size(1200, 36);
this.menuStrip1.TabIndex = 2;
this.menuStrip1.Text = "menuStrip1";
//
@ -277,20 +281,20 @@
this.saveToolStripMenuItem,
this.downloadToolStripMenuItem});
this.FiletoolStripMenuItem.Name = "FiletoolStripMenuItem";
this.FiletoolStripMenuItem.Size = new System.Drawing.Size(54, 29);
this.FiletoolStripMenuItem.Size = new System.Drawing.Size(54, 32);
this.FiletoolStripMenuItem.Text = "File";
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.saveToolStripMenuItem.Size = new System.Drawing.Size(196, 34);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// downloadToolStripMenuItem
//
this.downloadToolStripMenuItem.Name = "downloadToolStripMenuItem";
this.downloadToolStripMenuItem.Size = new System.Drawing.Size(270, 34);
this.downloadToolStripMenuItem.Size = new System.Drawing.Size(196, 34);
this.downloadToolStripMenuItem.Text = "Download";
this.downloadToolStripMenuItem.Click += new System.EventHandler(this.downloadToolStripMenuItem_Click);
//
@ -302,11 +306,31 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(14, 506);
this.buttonSortByType.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(272, 35);
this.buttonSortByType.TabIndex = 10;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(14, 551);
this.buttonSortByColor.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(272, 35);
this.buttonSortByColor.TabIndex = 11;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
//
// FormMapWithSetTrolleybus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1200, 997);
this.ClientSize = new System.Drawing.Size(1200, 1116);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip1);
@ -351,5 +375,7 @@
private System.Windows.Forms.ToolStripMenuItem downloadToolStripMenuItem;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
private System.Windows.Forms.Button buttonSortByColor;
private System.Windows.Forms.Button buttonSortByType;
}
}

@ -254,7 +254,29 @@ namespace Trolleybus
MessageBox.Show("Не получилось загрузить файл", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new TrolleybusCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new TrolleybusCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

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

@ -130,6 +130,12 @@ namespace Trolleybus
_setTrolleybus.Insert(DrawningObjectTrolleybus.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setTrolleybus.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>

@ -86,5 +86,14 @@ namespace Trolleybus
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

@ -145,6 +145,8 @@
<Compile Include="SetTrolleybusGeneric.cs" />
<Compile Include="SimpleMap.cs" />
<Compile Include="StorageOverflowException.cs" />
<Compile Include="TrolleybusCompareByColor.cs" />
<Compile Include="TrolleybusCompareByType.cs" />
<Compile Include="TrolleybusDelegate.cs" />
<Compile Include="TrolleybusNotFoundException.cs" />
<EmbeddedResource Include="Form1.resx">

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
{
internal class TrolleybusCompareByColor : 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 xTrolleybus = x as DrawningObjectTrolleybus;
var yTrolleybus = y as DrawningObjectTrolleybus;
if (xTrolleybus == null && yTrolleybus == null)
{
return 0;
}
if (xTrolleybus == null && yTrolleybus != null)
{
return 1;
}
if (xTrolleybus != null && yTrolleybus == null)
{
return -1;
}
var xEntityTrolleybus = xTrolleybus._trolleybus.Trolleybus;
var yEntityTrolleybus = yTrolleybus._trolleybus.Trolleybus;
var baseColorCompare = xEntityTrolleybus.BodyColor.ToArgb().CompareTo(yEntityTrolleybus.BodyColor.ToArgb());
if (baseColorCompare != 0)
{
return baseColorCompare;
}
if (xEntityTrolleybus is EntitySmallTrolleybus xSmallTrolleybus && yEntityTrolleybus is EntitySmallTrolleybus ySmallTrolleybus)
{
var dopColorCompare = xSmallTrolleybus.DopColor.ToArgb().CompareTo(ySmallTrolleybus.DopColor.ToArgb());
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xTrolleybus._trolleybus.Trolleybus.Speed.CompareTo(yTrolleybus._trolleybus.Trolleybus.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xTrolleybus._trolleybus.Trolleybus.Weight.CompareTo(yTrolleybus._trolleybus.Trolleybus.Weight);
}
}
}

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
{
internal class TrolleybusCompareByType : 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 xTrolleybus = x as DrawningObjectTrolleybus;
var yTrolleybus = y as DrawningObjectTrolleybus;
if (xTrolleybus == null && yTrolleybus == null)
{
return 0;
}
if (xTrolleybus == null && yTrolleybus != null)
{
return 1;
}
if (xTrolleybus != null && yTrolleybus == null)
{
return -1;
}
if (xTrolleybus.GetTrolleybus.GetType().Name != yTrolleybus.GetTrolleybus.GetType().Name)
{
if (xTrolleybus.GetTrolleybus.GetType().Name == "DrawningAirplane")
{
return -1;
}
return 1;
}
var speedCompare = xTrolleybus.GetTrolleybus.Trolleybus.Speed.CompareTo(yTrolleybus.GetTrolleybus.Trolleybus.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xTrolleybus.GetTrolleybus.Trolleybus.Weight.CompareTo(yTrolleybus.GetTrolleybus.Trolleybus.Weight);
}
}
}