лаб 8 добавлена

This commit is contained in:
m1aksim1 2022-11-11 22:02:47 +04:00
parent 3e990f3e45
commit b599b62e09
9 changed files with 181 additions and 17 deletions

View File

@ -8,7 +8,36 @@
{
_warplane = warplane;
}
public DrawningWarPlane GetWarPlane => _warplane;
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherWarPlane = other as DrawningObjectWarPlane;
if (otherWarPlane == null)
{
return false;
}
var warplane = _warplane.WarPlane;
var otherWarPlaneWarPlane = otherWarPlane._warplane.WarPlane;
if (warplane.Speed != otherWarPlaneWarPlane.Speed)
{
return false;
}
if (warplane.Weight != otherWarPlaneWarPlane.Weight)
{
return false;
}
if (warplane.BodyColor != otherWarPlaneWarPlane.BodyColor)
{
return false;
}
// TODO доделать проверки в случае продвинутого объекта
return true;
}
public float Step => _warplane?.WarPlane?.Step ?? 0;
public RectangleF GetCurrentPosition()

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.buttonSortByType);
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRemoveWarPlane);
@ -72,7 +76,7 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(811, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(204, 600);
this.groupBoxTools.Size = new System.Drawing.Size(204, 667);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -141,7 +145,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 349);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 429);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -150,7 +154,7 @@
//
// buttonRemoveWarPlane
//
this.buttonRemoveWarPlane.Location = new System.Drawing.Point(17, 378);
this.buttonRemoveWarPlane.Location = new System.Drawing.Point(15, 458);
this.buttonRemoveWarPlane.Name = "buttonRemoveWarPlane";
this.buttonRemoveWarPlane.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveWarPlane.TabIndex = 3;
@ -160,7 +164,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(17, 419);
this.buttonShowStorage.Location = new System.Drawing.Point(15, 499);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 4;
@ -173,7 +177,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.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(91, 550);
this.buttonDown.Location = new System.Drawing.Point(91, 617);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -185,7 +189,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.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(127, 550);
this.buttonRight.Location = new System.Drawing.Point(127, 617);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -197,7 +201,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.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(55, 550);
this.buttonLeft.Location = new System.Drawing.Point(55, 617);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -209,7 +213,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.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(91, 514);
this.buttonUp.Location = new System.Drawing.Point(91, 581);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -218,7 +222,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 460);
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 540);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 5;
@ -228,7 +232,7 @@
//
// buttonAddWarPlane
//
this.buttonAddWarPlane.Location = new System.Drawing.Point(17, 308);
this.buttonAddWarPlane.Location = new System.Drawing.Point(15, 388);
this.buttonAddWarPlane.Name = "buttonAddWarPlane";
this.buttonAddWarPlane.Size = new System.Drawing.Size(175, 35);
this.buttonAddWarPlane.TabIndex = 1;
@ -241,7 +245,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(811, 600);
this.pictureBox.Size = new System.Drawing.Size(811, 667);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -266,14 +270,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);
//
@ -285,11 +289,31 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(15, 293);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(175, 35);
this.buttonSortByType.TabIndex = 14;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(15, 334);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(175, 35);
this.buttonSortByColor.TabIndex = 13;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click);
//
// FormMapWithSetWarPlanes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1015, 600);
this.ClientSize = new System.Drawing.Size(1015, 667);
this.Controls.Add(this.menuStrip);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
@ -332,5 +356,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByType;
private Button buttonSortByColor;
}
}

View File

@ -290,5 +290,16 @@ namespace AirFighter
}
}
}
private void SortBy(IComparer<IDrawningObject> 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) => SortBy(new WarPlaneCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new WarPlaneCompareByColor());
}
}

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>43</value>
</metadata>
</root>

View File

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

View File

@ -6,8 +6,9 @@
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetWarPlanesGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
/// Ширина окна отрисовки
@ -39,6 +40,15 @@
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setWarPlanes.SortSet(comparer);
}
public MapWithSetWarPlanesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;

View File

@ -5,7 +5,7 @@
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetWarPlanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Массив объектов, которые храним
@ -26,6 +26,18 @@
_maxcount = count;
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="warplane">Добавляемый самолёт</param>
@ -46,6 +58,8 @@
/// <returns></returns>
public int Insert(T warplane, int position)
{
if (_places.Contains(warplane))
throw new ArgumentException($"Объект {warplane} уже есть в наборе");
if (Count == _maxcount)
throw new StorageOverflowException(_maxcount);
if (!isCorrectPosition(position))

View File

@ -0,0 +1,11 @@
namespace AirFighter
{
internal class WarPlaneCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
// TODO реализовать логику сравнения
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class WarPlaneCompareByType : 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 xWarPlane = x as DrawningObjectWarPlane;
var yWarPlane = y as DrawningObjectWarPlane;
if (xWarPlane == null && yWarPlane == null)
{
return 0;
}
if (xWarPlane == null && yWarPlane != null)
{
return 1;
}
if (xWarPlane != null && yWarPlane == null)
{
return -1;
}
if (xWarPlane.GetWarPlane.GetType().Name != yWarPlane.GetWarPlane.GetType().Name)
{
if (xWarPlane.GetWarPlane.GetType().Name == "DrawningCar")
{
return -1;
}
return 1;
}
var speedCompare = xWarPlane.GetWarPlane.WarPlane.Speed.CompareTo(yWarPlane.GetWarPlane.WarPlane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xWarPlane.GetWarPlane.WarPlane.Weight.CompareTo(yWarPlane.GetWarPlane.WarPlane.Weight);
}
}
}