Compare commits

...

4 Commits

10 changed files with 301 additions and 17 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -130,5 +130,43 @@ namespace AirplaneWithRadar
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)
{
return false;
}
var otherMap = other as AbstractMap;
if (otherMap == null)
{
return false;
}
if (_width != otherMap._width)
{
return false;
}
if (_height != otherMap._height)
{
return false;
}
if (_size_x != otherMap._size_x)
{
return false;
}
if (_size_y != otherMap._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] != otherMap._map[i, j])
return false;
}
}
return true;
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal class AirplaneCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xAirplane = x as DrawingObjectAirplane;
var yAirplane = y as DrawingObjectAirplane;
if (xAirplane == null && yAirplane == null)
{
return 0;
}
if (xAirplane == null && yAirplane != null)
{
return 1;
}
if (xAirplane != null && yAirplane == null)
{
return -1;
}
var baseColorCompare = xAirplane.GetAirplane.Airplane.BodyColor.ToString().CompareTo(yAirplane.GetAirplane.Airplane.BodyColor.ToString());
if (baseColorCompare != 0)
{
return baseColorCompare;
}
if (xAirplane.GetAirplane.Airplane is EntityAirplaneWithRadar xAirpl && yAirplane.GetAirplane.Airplane is EntityAirplaneWithRadar yAirpl)
{
var dopColorCompare = xAirpl.DopColor.ToString().CompareTo(yAirpl.DopColor.ToString());
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xAirplane.GetAirplane.Airplane.Speed.CompareTo(yAirplane.GetAirplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirplane.GetAirplane.Airplane.Weight.CompareTo(yAirplane.GetAirplane.Airplane.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar
{
internal class AirplaneCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xAirplane = x as DrawingObjectAirplane;
var yAirplane = y as DrawingObjectAirplane;
if (xAirplane == null && yAirplane == null)
{
return 0;
}
if (xAirplane == null && yAirplane != null)
{
return 1;
}
if (xAirplane != null && yAirplane == null)
{
return -1;
}
if (xAirplane.GetAirplane.GetType().Name != yAirplane.GetAirplane.GetType().Name)
{
if (xAirplane.GetAirplane.GetType().Name == "DrawingAirplane")
{
return -1;
}
return 1;
}
var speedCompare = xAirplane.GetAirplane.Airplane.Speed.CompareTo(yAirplane.GetAirplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirplane.GetAirplane.Airplane.Weight.CompareTo(yAirplane.GetAirplane.Airplane.Weight);
}
}
}

View File

@ -14,6 +14,8 @@ namespace AirplaneWithRadar
_airplane = airplane;
}
public float Step => _airplane?.Airplane?.Step ?? 0;
public DrawingAirplane GetAirplane => _airplane;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _airplane?.GetCurrentPosition() ?? default;
@ -33,5 +35,50 @@ namespace AirplaneWithRadar
public string GetInfo() => _airplane?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectAirplane(data.CreateDrawingAirplane());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherAirplane = other as DrawingObjectAirplane;
if (otherAirplane == null)
{
return false;
}
var airplane = _airplane.Airplane;
var otherAirplaneAirplane = otherAirplane._airplane.Airplane;
if (airplane.Speed != otherAirplaneAirplane.Speed)
{
return false;
}
if (airplane.Weight != otherAirplaneAirplane.Weight)
{
return false;
}
if (airplane.BodyColor != otherAirplaneAirplane.BodyColor)
{
return false;
}
// TODO доделать проверки в случае продвинутого объекта
if (airplane is EntityAirplaneWithRadar airpl && otherAirplaneAirplane is EntityAirplaneWithRadar otherAirpl)
{
if (airpl.DopColor != otherAirpl.DopColor)
{
return false;
}
if (airpl.Radar != otherAirpl.Radar)
{
return false;
}
if (airpl.ExtraFuelTank != otherAirpl.ExtraFuelTank)
{
return false;
}
}
else if (airplane is EntityAirplaneWithRadar || otherAirplaneAirplane is EntityAirplaneWithRadar) return false;
return true;
}
}
}

View File

@ -48,7 +48,11 @@ namespace AirplaneWithRadar
SetData();
Draw();
}
/// <summary>
/// Ïåðåìåùåíèå
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
string name = ((Button)sender)?.Name ?? string.Empty;

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBox = 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.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -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.buttonRight);
this.groupBox.Controls.Add(this.buttonDown);
@ -72,11 +76,31 @@
this.groupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox.Location = new System.Drawing.Point(901, 33);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(257, 750);
this.groupBox.Size = new System.Drawing.Size(257, 859);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(23, 424);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(222, 34);
this.buttonSortByColor.TabIndex = 13;
this.buttonSortByColor.Text = "Сортировка по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(23, 384);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(222, 34);
this.buttonSortByType.TabIndex = 12;
this.buttonSortByType.Text = " Сортировка по типу ";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
@ -144,7 +168,7 @@
//
this.buttonRight.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(153, 701);
this.buttonRight.Location = new System.Drawing.Point(149, 792);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 10;
@ -155,7 +179,7 @@
//
this.buttonDown.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(117, 701);
this.buttonDown.Location = new System.Drawing.Point(113, 792);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 9;
@ -166,7 +190,7 @@
//
this.buttonLeft.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(81, 701);
this.buttonLeft.Location = new System.Drawing.Point(77, 792);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -177,7 +201,7 @@
//
this.buttonUp.BackgroundImage = global::AirplaneWithRadar.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(117, 665);
this.buttonUp.Location = new System.Drawing.Point(113, 756);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -186,7 +210,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(23, 603);
this.buttonShowOnMap.Location = new System.Drawing.Point(23, 690);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(222, 34);
this.buttonShowOnMap.TabIndex = 6;
@ -196,7 +220,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(23, 549);
this.buttonShowStorage.Location = new System.Drawing.Point(23, 638);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(222, 37);
this.buttonShowStorage.TabIndex = 5;
@ -206,7 +230,7 @@
//
// buttonRemoveAirplane
//
this.buttonRemoveAirplane.Location = new System.Drawing.Point(23, 497);
this.buttonRemoveAirplane.Location = new System.Drawing.Point(23, 588);
this.buttonRemoveAirplane.Name = "buttonRemoveAirplane";
this.buttonRemoveAirplane.Size = new System.Drawing.Size(222, 34);
this.buttonRemoveAirplane.TabIndex = 4;
@ -216,7 +240,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(23, 460);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(23, 551);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(222, 31);
@ -224,7 +248,7 @@
//
// buttonAddAirplane
//
this.buttonAddAirplane.Location = new System.Drawing.Point(23, 406);
this.buttonAddAirplane.Location = new System.Drawing.Point(23, 496);
this.buttonAddAirplane.Name = "buttonAddAirplane";
this.buttonAddAirplane.Size = new System.Drawing.Size(222, 34);
this.buttonAddAirplane.TabIndex = 2;
@ -237,7 +261,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 33);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(901, 750);
this.pictureBox.Size = new System.Drawing.Size(901, 859);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -286,7 +310,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1158, 783);
this.ClientSize = new System.Drawing.Size(1158, 892);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.menuStrip);
@ -330,5 +354,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -306,6 +306,35 @@ namespace AirplaneWithRadar
}
}
}
/// <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 AirplaneCompareByType());
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)
{
// TODO прописать логику
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AirplaneCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

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

View File

@ -13,7 +13,7 @@ namespace AirplaneWithRadar
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetAirplanesGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -138,6 +138,15 @@ namespace AirplaneWithRadar
_setAirplanes.Insert(DrawingObjectAirplane.Create(rec) as T);
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setAirplanes.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>

View File

@ -11,7 +11,7 @@ namespace AirplaneWithRadar
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetAirplanesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -59,6 +59,10 @@ namespace AirplaneWithRadar
{
// TODO проверка позиции
// TODO вставка по позиции
if (_places.Contains(airplane))
{
throw new ArgumentException($"Данный объект ({airplane}) уже есть в наборе");
}
if (position < 0 || position >= _maxCount)
{
throw new StorageOverflowException(_maxCount);
@ -137,5 +141,17 @@ namespace AirplaneWithRadar
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}