Dolgov D.A. Lab Work 8 #8

Closed
devil_1nc wants to merge 4 commits from LabWork08 into LabWork07
9 changed files with 267 additions and 5 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ProjectPlane
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -156,7 +156,27 @@ namespace ProjectPlane
_drawingObject.DrawingObject(gr);
return bmp;
}
public bool Equals(AbstractMap? other)
{
if (other == null)
{
return false;
}
if (_width != other._width) return false;
if (_height != other._height) return false;
if (_size_x != other._size_x) return false;
if (_size_y != other._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] != other._map[i, j]) return false;
}
}
return true;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);

View File

@ -17,6 +17,7 @@ namespace ProjectPlane
public float Step => _plane?.Plane?.Step ?? 0;
public DrawingPlane GetPlane => _plane;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _plane?.GetCurrentPosition() ?? default;
@ -40,6 +41,52 @@ namespace ProjectPlane
public string GetInfo() => _plane?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObject(data.CreateDrawingPlane());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherPlane = other as DrawingObject;
if (otherPlane == null)
{
return false;
}
var plane = _plane.Plane;
var otherPlanePlane = otherPlane._plane.Plane;
if (plane.Speed != otherPlanePlane.Speed)
{
return false;
}
if (plane.Weight != otherPlanePlane.Weight)
{
return false;
}
if (plane.BodyColor != otherPlanePlane.BodyColor)
{
return false;
}
if (plane.GetType() != otherPlanePlane.GetType())
{
return false;
}
if (plane is EntityWarPlane advanced && otherPlanePlane is EntityWarPlane otheradvanced)
{
if (advanced.DopColor != otheradvanced.DopColor)
{
return false;
}
if (advanced.extraCell != otheradvanced.extraCell)
{
return false;
}
if (advanced.SuperTurbine != otheradvanced.SuperTurbine)
{
return false;
}
}
return true;
}
}
}

View File

@ -52,6 +52,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();
@ -60,6 +62,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.ButtonSortByColor);
this.groupBoxTools.Controls.Add(this.ButtonSortByType);
this.groupBoxTools.Controls.Add(this.buttonRight);
this.groupBoxTools.Controls.Add(this.buttonDown);
this.groupBoxTools.Controls.Add(this.buttonLeft);
@ -78,6 +82,26 @@
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools";
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(17, 506);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(176, 30);
this.ButtonSortByType.TabIndex = 9;
this.ButtonSortByType.Text = "Sort By Type";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(17, 541);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(176, 30);
this.ButtonSortByColor.TabIndex = 10;
this.ButtonSortByColor.Text = "Sort By Color";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -289,7 +313,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(884, 590);
this.ClientSize = new System.Drawing.Size(884, 740);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -333,5 +357,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -306,5 +306,24 @@ namespace ProjectPlane
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new PlaneCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new PlaneCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ProjectPlane
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace ProjectPlane
{
internal class MapWithSetPlanesGeneric <T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -132,6 +132,11 @@ namespace ProjectPlane
_setPlanes.Insert(DrawingObject.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setPlanes.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectPlane
{
internal class PlaneCompareByColor : 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 xPlane = x as DrawingObject;
var yPlane = y as DrawingObject;
if (xPlane == null && yPlane == null)
{
return 0;
}
if (xPlane == null && yPlane != null)
{
return 1;
}
if (xPlane != null && yPlane == null)
{
return -1;
}
if (xPlane.GetPlane.Plane.BodyColor.R.CompareTo(yPlane.GetPlane.Plane.BodyColor.R) != 0)
{
return xPlane.GetPlane.Plane.BodyColor.R.CompareTo(yPlane.GetPlane.Plane.BodyColor.R);
}
if (xPlane.GetPlane.Plane.BodyColor.G.CompareTo(yPlane.GetPlane.Plane.BodyColor.G) != 0)
{
return xPlane.GetPlane.Plane.BodyColor.G.CompareTo(yPlane.GetPlane.Plane.BodyColor.G);
}
if (xPlane.GetPlane.Plane.BodyColor.B.CompareTo(yPlane.GetPlane.Plane.BodyColor.B) != 0)
{
return xPlane.GetPlane.Plane.BodyColor.B.CompareTo(yPlane.GetPlane.Plane.BodyColor.B);
}
if (xPlane.GetPlane.Plane is EntityWarPlane xWarmlyEntity && yPlane.GetPlane.Plane is EntityWarPlane yWarmlyEntity)
{
if (xWarmlyEntity.DopColor.R.CompareTo(yWarmlyEntity.DopColor.R) != 0)
{
return xWarmlyEntity.DopColor.R.CompareTo(yWarmlyEntity.DopColor.R);
}
if (xWarmlyEntity.DopColor.G.CompareTo(yWarmlyEntity.DopColor.G) != 0)
{
return xWarmlyEntity.DopColor.G.CompareTo(yWarmlyEntity.DopColor.G);
}
if (xWarmlyEntity.DopColor.B.CompareTo(yWarmlyEntity.DopColor.B) != 0)
{
return xWarmlyEntity.DopColor.B.CompareTo(yWarmlyEntity.DopColor.B);
}
}
var speedCompare = xPlane.GetPlane.Plane.Speed.CompareTo(yPlane.GetPlane.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xPlane.GetPlane.Plane.Weight.CompareTo(yPlane.GetPlane.Plane.Weight);
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectPlane
{
internal class PlaneCompareByType : 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 xPlane = x as DrawingObject;
var yPlane = y as DrawingObject;
if (xPlane == null && yPlane == null)
{
return 0;
}
if (xPlane == null && yPlane != null)
{
return 1;
}
if (xPlane != null && yPlane == null)
{
return -1;
}
if (xPlane.GetPlane.GetType().Name != yPlane.GetPlane.GetType().Name)
{
if (xPlane.GetPlane.GetType().Name == "DrawingPlane")
{
return -1;
}
return 1;
}
var speedCompare =
xPlane.GetPlane.Plane.Speed.CompareTo(yPlane.GetPlane.Plane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xPlane.GetPlane.Plane.Weight.CompareTo(yPlane.GetPlane.Plane.Weight);
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ProjectPlane
{
internal class SetPlanesGeneric<T> where T : class
internal class SetPlanesGeneric<T> where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -43,6 +43,8 @@ namespace ProjectPlane
/// <returns></returns>
public int Insert(T plane, int position)
{
if (_places.Contains(plane)) return -1;
if (position < 0 || position >= _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, plane);
return position;
@ -99,5 +101,14 @@ namespace ProjectPlane
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}