Chernyshov N.I. LabWork 8 #8

Closed
kitossy wants to merge 2 commits from LabWork8 into LabWork7
9 changed files with 214 additions and 18 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AircraftCarrier
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObjectWarship = null;
protected int[,] _map = null;
@ -146,5 +146,26 @@ namespace AircraftCarrier
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

@ -15,6 +15,7 @@ namespace AircraftCarrier
_warship = warship;
}
public float Step => _warship?.Ship?.Step ?? 0;
public DrawingWarship GetWarship => _warship;
public void DrawningObject(Graphics g)
{
_warship.DrawTransport(g);
@ -33,5 +34,32 @@ namespace AircraftCarrier
}
public string GetInfo() => _warship?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship());
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectWarship otherWarship)
{
return false;
}
var warship = _warship.Ship;
var otherEntity = otherWarship._warship.Ship;
if (warship.GetType() != otherEntity.GetType() ||
warship.Speed != otherEntity.Speed ||
warship.Weight != otherEntity.Weight ||
warship.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (warship is EntityPlaneWarship entityPlaneWarship &&
otherEntity is EntityPlaneWarship otherEntityPlaneWarship && (
entityPlaneWarship.BodyKit != otherEntityPlaneWarship.BodyKit ||
entityPlaneWarship.СontrolPlace != otherEntityPlaneWarship.СontrolPlace ||
entityPlaneWarship.RunWay != otherEntityPlaneWarship.RunWay ||
entityPlaneWarship.DopColor != otherEntityPlaneWarship.DopColor))
{
return false;
}
return true;
}
}
}

View File

@ -25,6 +25,8 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonAddMap = new System.Windows.Forms.Button();
this.buttonDeleteMap = new System.Windows.Forms.Button();
@ -55,6 +57,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
@ -68,11 +72,31 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(841, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(217, 650);
this.groupBoxTools.Size = new System.Drawing.Size(217, 683);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(20, 285);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(180, 35);
this.buttonSortByType.TabIndex = 21;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(20, 326);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(180, 35);
this.buttonSortByColor.TabIndex = 20;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
@ -139,7 +163,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(20, 489);
this.buttonShowOnMap.Location = new System.Drawing.Point(20, 531);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(180, 35);
this.buttonShowOnMap.TabIndex = 18;
@ -149,7 +173,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(20, 448);
this.buttonShowStorage.Location = new System.Drawing.Point(20, 490);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(180, 35);
this.buttonShowStorage.TabIndex = 17;
@ -159,7 +183,7 @@
//
// buttonRemoveWarship
//
this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 407);
this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 449);
this.buttonRemoveWarship.Name = "buttonRemoveWarship";
this.buttonRemoveWarship.Size = new System.Drawing.Size(180, 35);
this.buttonRemoveWarship.TabIndex = 16;
@ -169,7 +193,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 378);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 420);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(180, 23);
@ -178,7 +202,7 @@
//
// buttonAddWarship
//
this.buttonAddWarship.Location = new System.Drawing.Point(20, 337);
this.buttonAddWarship.Location = new System.Drawing.Point(20, 379);
this.buttonAddWarship.Name = "buttonAddWarship";
this.buttonAddWarship.Size = new System.Drawing.Size(180, 35);
this.buttonAddWarship.TabIndex = 14;
@ -191,7 +215,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаright;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(126, 610);
this.buttonRight.Location = new System.Drawing.Point(126, 643);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 13;
@ -204,7 +228,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелка;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(54, 610);
this.buttonLeft.Location = new System.Drawing.Point(54, 643);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 12;
@ -217,7 +241,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаtop;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(90, 574);
this.buttonUp.Location = new System.Drawing.Point(90, 607);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 11;
@ -230,7 +254,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаbott;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(90, 610);
this.buttonDown.Location = new System.Drawing.Point(90, 643);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -243,7 +267,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(841, 650);
this.pictureBox.Size = new System.Drawing.Size(841, 683);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -268,14 +292,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(144, 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(144, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -291,7 +315,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1058, 674);
this.ClientSize = new System.Drawing.Size(1058, 707);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -335,5 +359,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -306,7 +306,25 @@ namespace AircraftCarrier
}
}
}
/// <summary>
/// Сортировка по типу и цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Sorting(IComparer<IDrawingObject> comparer)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => Sorting(new WarshipCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => Sorting(new WarshipCompareByColor());
}
}

View File

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

View File

@ -12,7 +12,7 @@ namespace AircraftCarrier
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetWarshipsGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -140,6 +140,14 @@ namespace AircraftCarrier
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setWarships.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()

View File

@ -11,7 +11,7 @@ namespace AircraftCarrier
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetWarshipsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -53,6 +53,8 @@ namespace AircraftCarrier
/// <returns></returns>
public int Insert(T ship, int position)
{
if (_places.Contains(ship))
return -1;
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
@ -107,6 +109,18 @@ namespace AircraftCarrier
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class WarshipCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
var xWarship = x as DrawingObjectWarship;
var yWarship = y as DrawingObjectWarship;
if (xWarship == yWarship)
{
return 0;
}
if (xWarship == null)
{
return 1;
}
if (yWarship == null)
{
return -1;
}
var xEntity = xWarship.GetWarship.Ship;
var yEntity = yWarship.GetWarship.Ship;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 || xEntity is not EntityPlaneWarship xEntityPlaneWarship ||
yEntity is not EntityPlaneWarship yEntityWarmlyShip)
{
return colorWeight;
}
return xEntityPlaneWarship.DopColor.ToArgb().CompareTo(yEntityWarmlyShip.DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class WarshipCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
var xWarship = x as DrawingObjectWarship;
var yWarship = y as DrawingObjectWarship;
if (xWarship == yWarship)
{
return 0;
}
if (xWarship == null)
{
return 1;
}
if (yWarship == null)
{
return -1;
}
if (xWarship.GetWarship.GetType().Name != yWarship.GetWarship.GetType().Name)
{
if (xWarship.GetWarship.GetType() == typeof(DrawingWarship))
{
return -1;
}
return 1;
}
var speedCompare = xWarship.GetWarship.Ship.Speed.CompareTo(yWarship.GetWarship.Ship.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xWarship.GetWarship.Ship.Weight.CompareTo(yWarship.GetWarship.Ship.Weight);
}
}
}