Сортировка

This commit is contained in:
Danil Kargin 2022-12-11 20:55:23 +04:00
parent 1f90797c6a
commit d3d92672e1
9 changed files with 236 additions and 15 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -154,5 +154,24 @@ namespace AirFighter
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 || _height != other._height || _width != other._width || _map != other._map || _size_x != other._size_x || _size_y != other._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] != other._map[i, j])
{
return false;
}
}
}
return true;
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AirFighterCompareByColor : 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 xAirFighter = x as DrawningObjectAirFighter;
var yAirFighter = y as DrawningObjectAirFighter;
if (xAirFighter == null && yAirFighter == null)
{
return 0;
}
if (xAirFighter == null && yAirFighter != null)
{
return 1;
}
if (xAirFighter != null && yAirFighter == null)
{
return -1;
}
var xEntityAirFighter = xAirFighter.GetAirFighter.AirFighter;
var yEntityAirFighter = yAirFighter.GetAirFighter.AirFighter;
var colorCompare = xEntityAirFighter.BodyColor.ToArgb().CompareTo(yEntityAirFighter.BodyColor.ToArgb());
if (colorCompare != 0)
{
return colorCompare;
}
if(xEntityAirFighter is EntityUpgradeAirFighter xUpgradeAirFighter && yEntityAirFighter is EntityUpgradeAirFighter yUpgradeAirFighter)
{
var dopColorCompare = xUpgradeAirFighter.DopColor.ToArgb().CompareTo(yUpgradeAirFighter.DopColor.ToArgb());
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xAirFighter.GetAirFighter.AirFighter.Speed.CompareTo(yAirFighter.GetAirFighter.AirFighter.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirFighter.GetAirFighter.AirFighter.Weight.CompareTo(yAirFighter.GetAirFighter.AirFighter.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AirFighterCompareByType : 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 xAirFighter = x as DrawningObjectAirFighter;
var yAirFighter = y as DrawningObjectAirFighter;
if (xAirFighter == null && yAirFighter == null)
{
return 0;
}
if (xAirFighter == null && yAirFighter != null)
{
return 1;
}
if (xAirFighter != null && yAirFighter == null)
{
return -1;
}
if (xAirFighter.GetAirFighter.GetType().Name != yAirFighter.GetAirFighter.GetType().Name)
{
if (xAirFighter.GetAirFighter.GetType().Name == "DrawningAirFighter")
{
return -1;
}
return 1;
}
var speedCompare = xAirFighter.GetAirFighter.AirFighter.Speed.CompareTo(yAirFighter.GetAirFighter.AirFighter.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirFighter.GetAirFighter.AirFighter.Weight.CompareTo(yAirFighter.GetAirFighter.AirFighter.Weight);
}
}
}

View File

@ -10,6 +10,7 @@ namespace AirFighter
{
private DrawningAirFighter _airFighter = null;
public float Step => _airFighter?.AirFighter.Step ?? 0;
public DrawningAirFighter GetAirFighter => _airFighter;
public DrawningObjectAirFighter(DrawningAirFighter airFighter)
{
_airFighter = airFighter;

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.groupBox.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBox
//
this.groupBox.Controls.Add(this.buttonSortByColor);
this.groupBox.Controls.Add(this.buttonSortByType);
this.groupBox.Controls.Add(this.groupBoxMaps);
this.groupBox.Controls.Add(this.buttonUp);
this.groupBox.Controls.Add(this.buttonDown);
@ -72,7 +76,7 @@
this.groupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox.Location = new System.Drawing.Point(732, 28);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(250, 561);
this.groupBox.Size = new System.Drawing.Size(250, 638);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
@ -145,7 +149,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirFighter.Properties.Resources.Up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(135, 481);
this.buttonUp.Location = new System.Drawing.Point(135, 558);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 2;
@ -157,7 +161,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirFighter.Properties.Resources.Down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(135, 519);
this.buttonDown.Location = new System.Drawing.Point(135, 596);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 3;
@ -169,7 +173,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirFighter.Properties.Resources.Left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(99, 519);
this.buttonLeft.Location = new System.Drawing.Point(99, 596);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 4;
@ -181,7 +185,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirFighter.Properties.Resources.Right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(171, 519);
this.buttonRight.Location = new System.Drawing.Point(171, 596);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
@ -190,7 +194,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(28, 443);
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 522);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(210, 29);
this.buttonShowOnMap.TabIndex = 5;
@ -200,7 +204,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(28, 408);
this.buttonShowStorage.Location = new System.Drawing.Point(17, 487);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(210, 29);
this.buttonShowStorage.TabIndex = 4;
@ -210,7 +214,7 @@
//
// buttonRemoveAirFighter
//
this.buttonRemoveAirFighter.Location = new System.Drawing.Point(28, 373);
this.buttonRemoveAirFighter.Location = new System.Drawing.Point(17, 452);
this.buttonRemoveAirFighter.Name = "buttonRemoveAirFighter";
this.buttonRemoveAirFighter.Size = new System.Drawing.Size(210, 29);
this.buttonRemoveAirFighter.TabIndex = 3;
@ -220,7 +224,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 340);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 419);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(210, 27);
@ -228,7 +232,7 @@
//
// buttonAddAirFighter
//
this.buttonAddAirFighter.Location = new System.Drawing.Point(28, 296);
this.buttonAddAirFighter.Location = new System.Drawing.Point(17, 375);
this.buttonAddAirFighter.Name = "buttonAddAirFighter";
this.buttonAddAirFighter.Size = new System.Drawing.Size(210, 29);
this.buttonAddAirFighter.TabIndex = 1;
@ -241,7 +245,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 28);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(732, 561);
this.pictureBox.Size = new System.Drawing.Size(732, 638);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -267,14 +271,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 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(177, 26);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -286,11 +290,31 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(17, 296);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(210, 29);
this.buttonSortByType.TabIndex = 7;
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, 331);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(210, 29);
this.buttonSortByColor.TabIndex = 8;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetAirFighters
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(982, 589);
this.ClientSize = new System.Drawing.Size(982, 666);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.menuStrip);
@ -334,5 +358,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -316,5 +316,37 @@ namespace AirFighter
}
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new AirFighterCompareByType());
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 AirFighterCompareByColor());
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -66,4 +66,7 @@
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>311, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

@ -139,6 +139,14 @@ namespace AirFighter
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setAirFighters.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()

View File

@ -130,5 +130,17 @@ namespace AirFighter
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}