Сортировка

This commit is contained in:
Nap 2022-12-04 13:36:52 +04:00
parent a8c9d45998
commit a04ab48038
9 changed files with 242 additions and 19 deletions

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
internal class BattleshipCompareByColor : 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 xBattleship = x as DrawningObjectBattleship;
var yBattleship = y as DrawningObjectBattleship;
if (xBattleship == null && yBattleship == null)
return 0;
if (xBattleship == null && yBattleship != null)
return 1;
if (xBattleship != null && yBattleship == null)
return -1;
int xColor = xBattleship.GetBattleship.Battleship.BodyColor.ToArgb();
int yColor = yBattleship.GetBattleship.Battleship.BodyColor.ToArgb();
if (xColor != yColor)
return xColor.CompareTo(yColor);
if (xBattleship.GetBattleship.Battleship is EntityGunBattleship xAdvanced && yBattleship.GetBattleship.Battleship is EntityGunBattleship yAdvanced)
{
xColor = xAdvanced.DopColor.ToArgb();
yColor = yAdvanced.DopColor.ToArgb();
if (xColor != yColor)
return xColor.CompareTo(yColor);
}
var speedCompare = xBattleship.GetBattleship.Battleship.Speed.CompareTo(yBattleship.GetBattleship.Battleship.Speed);
if (speedCompare != 0)
return speedCompare;
return xBattleship.GetBattleship.Battleship.Weight.CompareTo(yBattleship.GetBattleship.Battleship.Weight);
}
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
internal class BattleshipCompareByType : 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 xBattleship = x as DrawningObjectBattleship;
var yBattleship = y as DrawningObjectBattleship;
if (xBattleship == null && yBattleship == null)
return 0;
if (xBattleship == null && yBattleship != null)
return 1;
if (xBattleship != null && yBattleship == null)
return -1;
if (xBattleship.GetBattleship.GetType().Name != yBattleship.GetBattleship.GetType().Name)
{
if (xBattleship.GetBattleship.GetType().Name == "DrawningBattleship")
return -1;
return 1;
}
var speedCompare = xBattleship.GetBattleship.Battleship.Speed.CompareTo(yBattleship.GetBattleship.Battleship.Speed);
if (speedCompare != 0)
return speedCompare;
return xBattleship.GetBattleship.Battleship.Weight.CompareTo(yBattleship.GetBattleship.Battleship.Weight);
}
}
}

View File

@ -17,6 +17,8 @@ namespace Battleship
public float Step => _battleship?.Battleship?.Step ?? 0;
public DrawningBattleship GetBattleship => _battleship;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _battleship?.GetCurrentPosition() ?? default;
@ -40,5 +42,48 @@ namespace Battleship
public string GetInfo() => _battleship?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectBattleship(data.CreateDrawningBattleship());
public bool Equals(IDrawningObject? other)
{
if (other == null)
return false;
var otherBattleship = other as DrawningObjectBattleship;
if (otherBattleship == null)
return false;
var battleship = _battleship.Battleship;
var otherBattleshipBattleship = otherBattleship._battleship.Battleship;
if (battleship.GetType() != otherBattleshipBattleship.GetType())
return false;
if (battleship.Speed != otherBattleshipBattleship.Speed)
return false;
if (battleship.Weight != otherBattleshipBattleship.Weight)
return false;
if (battleship.BodyColor != otherBattleshipBattleship.BodyColor)
return false;
if (battleship is EntityGunBattleship adv && otherBattleshipBattleship is EntityGunBattleship otherAdv)
{
if (adv.DopColor != otherAdv.DopColor)
return false;
if (adv.CompartmentRocket != otherAdv.CompartmentRocket)
return false;
if (adv.GunTower != otherAdv.GunTower)
return false;
if (adv.SternFence != otherAdv.SternFence)
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.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonDown);
this.groupBoxTools.Controls.Add(this.buttonLeft);
@ -70,9 +74,9 @@
this.groupBoxTools.Controls.Add(this.buttonRemoveBattleship);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(744, 24);
this.groupBoxTools.Location = new System.Drawing.Point(748, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 587);
this.groupBoxTools.Size = new System.Drawing.Size(200, 623);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -149,7 +153,7 @@
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Battleship.Properties.Resources.BatteleshipDown;
this.buttonDown.Location = new System.Drawing.Point(77, 543);
this.buttonDown.Location = new System.Drawing.Point(77, 579);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 9;
@ -160,7 +164,7 @@
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Battleship.Properties.Resources.BattleshipLeft;
this.buttonLeft.Location = new System.Drawing.Point(41, 543);
this.buttonLeft.Location = new System.Drawing.Point(41, 579);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -170,7 +174,7 @@
// buttonAddBattleship
//
this.buttonAddBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddBattleship.Location = new System.Drawing.Point(24, 326);
this.buttonAddBattleship.Location = new System.Drawing.Point(24, 362);
this.buttonAddBattleship.Name = "buttonAddBattleship";
this.buttonAddBattleship.Size = new System.Drawing.Size(164, 30);
this.buttonAddBattleship.TabIndex = 1;
@ -182,7 +186,7 @@
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Battleship.Properties.Resources.BattleshipUp;
this.buttonUp.Location = new System.Drawing.Point(77, 507);
this.buttonUp.Location = new System.Drawing.Point(77, 543);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -193,7 +197,7 @@
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Battleship.Properties.Resources.BattleshipRight;
this.buttonRight.Location = new System.Drawing.Point(113, 543);
this.buttonRight.Location = new System.Drawing.Point(113, 579);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 6;
@ -203,7 +207,7 @@
// buttonShowOnMap
//
this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonShowOnMap.Location = new System.Drawing.Point(24, 462);
this.buttonShowOnMap.Location = new System.Drawing.Point(24, 498);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(164, 30);
this.buttonShowOnMap.TabIndex = 5;
@ -214,7 +218,7 @@
// buttonShowStorage
//
this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonShowStorage.Location = new System.Drawing.Point(24, 426);
this.buttonShowStorage.Location = new System.Drawing.Point(24, 462);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(164, 30);
this.buttonShowStorage.TabIndex = 4;
@ -225,7 +229,7 @@
// buttonRemoveBattleship
//
this.buttonRemoveBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRemoveBattleship.Location = new System.Drawing.Point(24, 391);
this.buttonRemoveBattleship.Location = new System.Drawing.Point(24, 427);
this.buttonRemoveBattleship.Name = "buttonRemoveBattleship";
this.buttonRemoveBattleship.Size = new System.Drawing.Size(164, 29);
this.buttonRemoveBattleship.TabIndex = 3;
@ -236,7 +240,7 @@
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 362);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 398);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23);
@ -247,7 +251,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(744, 587);
this.pictureBox.Size = new System.Drawing.Size(748, 623);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
@ -257,7 +261,7 @@
this.файлToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(944, 24);
this.menuStrip.Size = new System.Drawing.Size(948, 24);
this.menuStrip.TabIndex = 1;
//
// файлToolStripMenuItem
@ -272,14 +276,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);
//
@ -291,11 +295,33 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSortByType.Location = new System.Drawing.Point(23, 291);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(164, 29);
this.buttonSortByType.TabIndex = 2;
this.buttonSortByType.Text = "Сортировка по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSortByColor.Location = new System.Drawing.Point(24, 326);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(164, 30);
this.buttonSortByColor.TabIndex = 11;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetBattleship
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(944, 611);
this.ClientSize = new System.Drawing.Size(948, 647);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -339,5 +365,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -266,6 +266,24 @@ namespace Battleship
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
return;
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BattleshipCompareByType());
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 BattleshipCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -57,4 +57,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>125, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>258, 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

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

View File

@ -12,7 +12,7 @@ namespace Battleship
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetBattleshipGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
private readonly int _pictureWidth;
@ -94,6 +94,11 @@ namespace Battleship
}
}
public void Sort(IComparer<T> comparer)
{
_setBattleship.SortSet(comparer);
}
public void Shaking()
{
int j = _setBattleship.Count - 1;

View File

@ -12,7 +12,7 @@ namespace Battleship
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetBattleshipGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
@ -94,5 +94,13 @@ namespace Battleship
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
return;
_places.Sort(comparer);
}
}
}