6 Commits

Author SHA1 Message Date
5b23314bce Call of sortions implementation. 2022-12-06 16:31:24 +04:00
e3831212b6 Some fixes. 2022-12-06 16:30:05 +04:00
57fd0ad585 Compare by color logic. 2022-12-06 16:28:58 +04:00
a913fca4df Equals method in AbstractMap. 2022-12-06 16:28:04 +04:00
1dbafea6ea Comparer classes were implemented. 2022-12-06 13:46:45 +04:00
879c1e15ec Method to check equality of objects was implemented. 2022-12-06 13:28:41 +04:00
10 changed files with 316 additions and 25 deletions

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@@ -202,5 +202,45 @@ namespace AirFighter
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null)
{
return false;
}
var diffMap = other;
if (diffMap == null)
{
return false;
}
if (diffMap._height != _height)
{
return false;
}
if (diffMap._width != _width)
{
return false;
}
if (diffMap._size_x != _size_x)
{
return false;
}
if (diffMap._size_y != _size_y )
{
return false;
}
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (diffMap._map[i, j] != _map[j, i])
{
return false;
}
}
}
return true;
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AircraftCompareByColor : 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 xAircraft = x as DrawingObjectAircraft;
var yAircraft = y as DrawingObjectAircraft;
if (xAircraft == null && yAircraft == null)
{
return 0;
}
if (xAircraft == null && yAircraft != null)
{
return 1;
}
if (xAircraft != null && yAircraft == null)
{
return -1;
}
if (xAircraft.GetAircraft.Plane.BodyColor.R.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.R) != 0)
{
return xAircraft.GetAircraft.Plane.BodyColor.R.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.R);
}
if (xAircraft.GetAircraft.Plane.BodyColor.G.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.G) != 0)
{
return xAircraft.GetAircraft.Plane.BodyColor.G.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.G);
}
if (xAircraft.GetAircraft.Plane.BodyColor.B.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.B) != 0)
{
return xAircraft.GetAircraft.Plane.BodyColor.B.CompareTo(yAircraft.GetAircraft.Plane.BodyColor.B);
}
if (xAircraft.GetAircraft.Plane is EntityMilitaryAircraft xMilitaryAircraft && yAircraft.GetAircraft.Plane is EntityMilitaryAircraft yMilitaryAircraft)
{
if (xMilitaryAircraft.ExtraColor.R.CompareTo(yMilitaryAircraft.ExtraColor.R) != 0)
{
return xMilitaryAircraft.ExtraColor.R.CompareTo(yMilitaryAircraft.ExtraColor.R);
}
if (xMilitaryAircraft.ExtraColor.G.CompareTo(yMilitaryAircraft.ExtraColor.G) != 0)
{
return xMilitaryAircraft.ExtraColor.G.CompareTo(yMilitaryAircraft.ExtraColor.G);
}
if (xMilitaryAircraft.ExtraColor.B.CompareTo(yMilitaryAircraft.ExtraColor.B) != 0)
{
return xMilitaryAircraft.ExtraColor.B.CompareTo(yMilitaryAircraft.ExtraColor.B);
}
}
var speedCompare = xAircraft.GetAircraft.Plane.Speed.CompareTo(yAircraft.GetAircraft.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAircraft.GetAircraft.Plane.Weight.CompareTo(yAircraft.GetAircraft.Plane.Weight);
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class AircraftCompareByType : 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 xAircraft = x as DrawingObjectAircraft;
var yAircraft = y as DrawingObjectAircraft;
if (xAircraft == null && yAircraft == null)
{
return 0;
}
if (xAircraft == null && yAircraft != null)
{
return 1;
}
if (xAircraft != null && yAircraft == null)
{
return -1;
}
if (xAircraft.GetAircraft.GetType().Name != yAircraft.GetAircraft.GetType().Name)
{
if (xAircraft.GetAircraft.GetType().Name == "DrawingAircraft")
{
return -1;
}
return 1;
}
var speedCompare = xAircraft.GetAircraft.Plane.Speed.CompareTo(yAircraft.GetAircraft.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAircraft.GetAircraft.Plane.Weight.CompareTo(yAircraft.GetAircraft.Plane.Weight);
}
}
}

View File

@@ -12,6 +12,8 @@ namespace AirFighter
public AircraftNotFoundException() : base() { }
public AircraftNotFoundException(string message) : base(message) { }
public AircraftNotFoundException(string message, Exception exception) : base(message, exception) { }
public AircraftNotFoundException(SerializationInfo info, StreamingContext contex) : base(info,contex) {
public AircraftNotFoundException(SerializationInfo info, StreamingContext contex) : base(info,contex) { }
}
}

View File

@@ -17,6 +17,8 @@ namespace AirFighter
public float Step => _aircraft.Plane?.Step ?? 0;
public DrawingAircraft GetAircraft => _aircraft;
public (float Left, float Top, float Right, float Bottom) GetCurrentPosition()
{
return _aircraft?.GetCurrentPosition() ?? default;
@@ -47,7 +49,55 @@ namespace AirFighter
public string ReceiveInfo() => _aircraft?.ReceiveDataForSaving();
public static IDrawingObject Create(string data) => new DrawingObjectAircraft(data.CreateDrawingAircraft());
}
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var diffAircraft = other as DrawingObjectAircraft;
if (diffAircraft == null)
{
return false;
}
var aircraft = _aircraft.Plane;
var diffAircraftCast = diffAircraft._aircraft.Plane;
if (aircraft.Speed != diffAircraftCast.Speed)
{
return false;
}
if (aircraft.Weight != diffAircraftCast.Weight)
{
return false;
}
if (aircraft.BodyColor != diffAircraftCast.BodyColor)
{
return false;
}
if (aircraft is EntityMilitaryAircraft || diffAircraftCast is EntityMilitaryAircraft)
{
if (aircraft is EntityMilitaryAircraft && diffAircraftCast is EntityMilitaryAircraft)
{
if ((aircraft as EntityMilitaryAircraft).ExtraColor != (diffAircraftCast as EntityMilitaryAircraft).ExtraColor)
{
return false;
}
if ((aircraft as EntityMilitaryAircraft).Rockets != (diffAircraftCast as EntityMilitaryAircraft).Rockets)
{
return false;
}
if ((aircraft as EntityMilitaryAircraft).ExtraWings != (diffAircraftCast as EntityMilitaryAircraft).ExtraWings)
{
return false;
}
return true;
}
return false;
}
return true;
}
}
}

View File

@@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.PanelGroupBox = new System.Windows.Forms.GroupBox();
this.SortByColorButton = new System.Windows.Forms.Button();
this.SortByTypeButton = new System.Windows.Forms.Button();
this.MapsGroupBox = new System.Windows.Forms.GroupBox();
this.DeleteMapButton = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@@ -59,6 +61,8 @@
//
// PanelGroupBox
//
this.PanelGroupBox.Controls.Add(this.SortByColorButton);
this.PanelGroupBox.Controls.Add(this.SortByTypeButton);
this.PanelGroupBox.Controls.Add(this.MapsGroupBox);
this.PanelGroupBox.Controls.Add(this.buttonDown);
this.PanelGroupBox.Controls.Add(this.buttonRight);
@@ -72,11 +76,31 @@
this.PanelGroupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.PanelGroupBox.Location = new System.Drawing.Point(672, 24);
this.PanelGroupBox.Name = "PanelGroupBox";
this.PanelGroupBox.Size = new System.Drawing.Size(218, 599);
this.PanelGroupBox.Size = new System.Drawing.Size(218, 702);
this.PanelGroupBox.TabIndex = 0;
this.PanelGroupBox.TabStop = false;
this.PanelGroupBox.Text = "Function panel";
//
// SortByColorButton
//
this.SortByColorButton.Location = new System.Drawing.Point(12, 339);
this.SortByColorButton.Name = "SortByColorButton";
this.SortByColorButton.Size = new System.Drawing.Size(176, 36);
this.SortByColorButton.TabIndex = 17;
this.SortByColorButton.Text = "Sort by color";
this.SortByColorButton.UseVisualStyleBackColor = true;
this.SortByColorButton.Click += new System.EventHandler(this.SortByColorButton_Click);
//
// SortByTypeButton
//
this.SortByTypeButton.Location = new System.Drawing.Point(12, 297);
this.SortByTypeButton.Name = "SortByTypeButton";
this.SortByTypeButton.Size = new System.Drawing.Size(176, 36);
this.SortByTypeButton.TabIndex = 16;
this.SortByTypeButton.Text = "Sort by type";
this.SortByTypeButton.UseVisualStyleBackColor = true;
this.SortByTypeButton.Click += new System.EventHandler(this.SortByTypeButton_Click);
//
// MapsGroupBox
//
this.MapsGroupBox.Controls.Add(this.DeleteMapButton);
@@ -145,7 +169,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(99, 557);
this.buttonDown.Location = new System.Drawing.Point(99, 660);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 11;
@@ -157,7 +181,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(135, 557);
this.buttonRight.Location = new System.Drawing.Point(135, 660);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 10;
@@ -166,7 +190,7 @@
//
// ShowMapButton
//
this.ShowMapButton.Location = new System.Drawing.Point(6, 483);
this.ShowMapButton.Location = new System.Drawing.Point(12, 578);
this.ShowMapButton.Name = "ShowMapButton";
this.ShowMapButton.Size = new System.Drawing.Size(182, 36);
this.ShowMapButton.TabIndex = 14;
@@ -179,7 +203,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(63, 557);
this.buttonLeft.Location = new System.Drawing.Point(63, 660);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 9;
@@ -188,7 +212,7 @@
//
// ShowHangarButton
//
this.ShowHangarButton.Location = new System.Drawing.Point(6, 447);
this.ShowHangarButton.Location = new System.Drawing.Point(12, 542);
this.ShowHangarButton.Name = "ShowHangarButton";
this.ShowHangarButton.Size = new System.Drawing.Size(182, 30);
this.ShowHangarButton.TabIndex = 13;
@@ -201,7 +225,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(99, 521);
this.buttonUp.Location = new System.Drawing.Point(99, 624);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 8;
@@ -210,7 +234,7 @@
//
// DeleteAircraftButton
//
this.DeleteAircraftButton.Location = new System.Drawing.Point(6, 405);
this.DeleteAircraftButton.Location = new System.Drawing.Point(12, 500);
this.DeleteAircraftButton.Name = "DeleteAircraftButton";
this.DeleteAircraftButton.Size = new System.Drawing.Size(182, 36);
this.DeleteAircraftButton.TabIndex = 12;
@@ -220,7 +244,7 @@
//
// maskedTextBoxPostion
//
this.maskedTextBoxPostion.Location = new System.Drawing.Point(6, 358);
this.maskedTextBoxPostion.Location = new System.Drawing.Point(12, 453);
this.maskedTextBoxPostion.Mask = "00";
this.maskedTextBoxPostion.Name = "maskedTextBoxPostion";
this.maskedTextBoxPostion.Size = new System.Drawing.Size(182, 23);
@@ -228,7 +252,7 @@
//
// AddAircraftButton
//
this.AddAircraftButton.Location = new System.Drawing.Point(6, 317);
this.AddAircraftButton.Location = new System.Drawing.Point(12, 412);
this.AddAircraftButton.Name = "AddAircraftButton";
this.AddAircraftButton.Size = new System.Drawing.Size(182, 35);
this.AddAircraftButton.TabIndex = 10;
@@ -241,7 +265,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(672, 599);
this.pictureBox.Size = new System.Drawing.Size(672, 702);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@@ -266,14 +290,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
this.SaveToolStripMenuItem.Text = "Save";
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(100, 22);
this.LoadToolStripMenuItem.Text = "Load";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@@ -290,7 +314,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(890, 623);
this.ClientSize = new System.Drawing.Size(890, 726);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.PanelGroupBox);
this.Controls.Add(this.menuStrip);
@@ -334,5 +358,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button SortByColorButton;
private Button SortByTypeButton;
}
}

View File

@@ -80,8 +80,6 @@ namespace AirFighter
return;
}
try
{
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectAircraft(aircraft) != -1)
@@ -271,9 +269,31 @@ namespace AirFighter
MessageBox.Show($"Something went wrong: {ex.Message}", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogWarning($"Unable to load: {openFileDialog.FileName} | {ex.Message}.");
}
}
}
private void SortByTypeButton_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new AircraftCompareByType());
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void SortByColorButton_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new AircraftCompareByColor());
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal class MapWithSetAircraftsGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
private readonly int _pictureWidth;
@@ -91,6 +91,11 @@ namespace AirFighter
_setAircrafts.Insert(DrawingObjectAircraft.Create(record) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setAircrafts.SortSet(comparer);
}
private void Shaking()
{
int j = _setAircrafts.Count - 1;

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal class SetAircraftsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
@@ -29,6 +29,13 @@ namespace AirFighter
public int Insert(T aircraft, int position)
{
foreach (var instAircraft in _places)
{
if (aircraft.Equals(instAircraft))
{
return -1;
}
}
if (position > Count || position < 0 || Count == _maxCount)
{
@@ -98,5 +105,14 @@ namespace AirFighter
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}