Tsukanova I.V. LabWork8 #10

Closed
Inohara wants to merge 1 commits from LabWork8 into LabWork07
10 changed files with 215 additions and 16 deletions
Showing only changes of commit 296d0951bb - Show all commits

View File

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

@ -16,6 +16,8 @@ namespace AircraftCarrier
}
public float Step => _warship?.Warship?.Step ?? 0;
public DrawingWarship GetWarship => _warship;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _warship?.GetCurrentPosition() ?? default;
@ -39,5 +41,33 @@ namespace AircraftCarrier
void IDrawingObject.DrawningObject(Graphics g) => _warship.DrawTransport(g);
public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawningWarship());
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectWarship otherWarship)
{
return false;
}
var warship = _warship.Warship;
var otherEntity = otherWarship._warship.Warship;
if (warship.GetType() != otherEntity.GetType() ||
warship.Speed != otherEntity.Speed ||
warship.Weight != otherEntity.Weight ||
warship.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (warship is EntityAircraftCarrier entityAircraftCarrier &&
otherEntity is EntityAircraftCarrier otherEntityAircraftCarrier && (
entityAircraftCarrier.BodyKit != otherEntityAircraftCarrier.BodyKit ||
entityAircraftCarrier.Сabin != otherEntityAircraftCarrier.Сabin ||
entityAircraftCarrier.SuperEngine != otherEntityAircraftCarrier.SuperEngine ||
entityAircraftCarrier.DopColor != otherEntityAircraftCarrier.DopColor))
{
return false;
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = 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();
@ -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.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
@ -72,11 +76,31 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(580, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(217, 547);
this.groupBoxTools.Size = new System.Drawing.Size(217, 654);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(20, 338);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(180, 35);
this.buttonSortByColor.TabIndex = 21;
this.buttonSortByColor.Text = "Sort by color";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(20, 297);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(180, 35);
this.buttonSortByType.TabIndex = 20;
this.buttonSortByType.Text = "Sort by type";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
@ -142,7 +166,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(20, 437);
this.buttonShowOnMap.Location = new System.Drawing.Point(20, 544);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(180, 35);
this.buttonShowOnMap.TabIndex = 18;
@ -152,7 +176,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(20, 396);
this.buttonShowStorage.Location = new System.Drawing.Point(20, 503);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(180, 35);
this.buttonShowStorage.TabIndex = 17;
@ -162,7 +186,7 @@
//
// buttonRemoveWarship
//
this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 355);
this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 462);
this.buttonRemoveWarship.Name = "buttonRemoveWarship";
this.buttonRemoveWarship.Size = new System.Drawing.Size(180, 35);
this.buttonRemoveWarship.TabIndex = 16;
@ -172,7 +196,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 326);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 433);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(180, 23);
@ -181,7 +205,7 @@
//
// buttonAddWarship
//
this.buttonAddWarship.Location = new System.Drawing.Point(20, 285);
this.buttonAddWarship.Location = new System.Drawing.Point(20, 392);
this.buttonAddWarship.Name = "buttonAddWarship";
this.buttonAddWarship.Size = new System.Drawing.Size(180, 35);
this.buttonAddWarship.TabIndex = 14;
@ -194,7 +218,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.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(126, 516);
this.buttonRight.Location = new System.Drawing.Point(126, 623);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 13;
@ -207,7 +231,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.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(54, 516);
this.buttonLeft.Location = new System.Drawing.Point(54, 623);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 12;
@ -220,7 +244,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.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(90, 480);
this.buttonUp.Location = new System.Drawing.Point(90, 587);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 11;
@ -233,7 +257,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.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(90, 516);
this.buttonDown.Location = new System.Drawing.Point(90, 623);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -246,7 +270,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(580, 547);
this.pictureBox.Size = new System.Drawing.Size(580, 654);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -294,7 +318,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(797, 571);
this.ClientSize = new System.Drawing.Size(797, 678);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -338,5 +362,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -298,5 +298,19 @@ namespace AircraftCarrier
}
}
}
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

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

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>
@ -138,6 +138,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>
/// Список объектов, которые храним
@ -49,6 +49,9 @@ namespace AircraftCarrier
/// <returns></returns>
public int Insert(T warship, int position)
{
if (_places.Contains(warship))
return -1;
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
@ -111,5 +114,17 @@ 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.Warship;
var yEntity = yWarship.GetWarship.Warship;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 || xEntity is not EntityAircraftCarrier xEntityAircraftCarrier ||
yEntity is not EntityAircraftCarrier yEntityWarmlyShip)
{
return colorWeight;
}
return xEntityAircraftCarrier.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.Warship.Speed.CompareTo(yWarship.GetWarship.Warship.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xWarship.GetWarship.Warship.Weight.CompareTo(yWarship.GetWarship.Warship.Weight);
}
}
}