Compare commits

...

3 Commits

Author SHA1 Message Date
a7027333d8 IEquatable AbstractMap 2022-11-21 19:43:07 +04:00
5a1cdcd4b2 Сортировка 2022-11-21 19:27:25 +04:00
38def0fbc5 Сравнение объектов 2022-11-21 17:38:47 +04:00
11 changed files with 291 additions and 15 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -164,5 +164,30 @@ namespace Locomotive
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

@ -17,6 +17,8 @@ namespace Locomotive
public float Step => _locomotive?.Locomotive?.Step ?? 0;
public DrawningLocomotive GetLocomotive => _locomotive;
public void DrawningObject(Graphics g)
{
_locomotive?.DrawTransport(g);
@ -39,5 +41,44 @@ namespace Locomotive
public string getInfo() => _locomotive?.getDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.createDrawningLocomotive());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherLocomotive = other as DrawningObjectLocomotive;
if (otherLocomotive == null)
{
return false;
}
var locomotive = _locomotive.Locomotive;
var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive;
if (locomotive.GetType().Name != otherLocomotiveLocomotive.GetType().Name) return false;
if (locomotive.Speed != otherLocomotiveLocomotive.Speed)
{
return false;
}
if (locomotive.Weight != otherLocomotiveLocomotive.Weight)
{
return false;
}
if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor)
{
return false;
}
// проверка в случае продвинутого объекта
if (locomotive is EntityWarmlyLocomotive entityWarmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherEntityWarmlyLocomotive)
{
if (entityWarmlyLocomotive.ExtraColor != otherEntityWarmlyLocomotive.ExtraColor) return false;
if (entityWarmlyLocomotive.Pipe != otherEntityWarmlyLocomotive.Pipe) return false;
if (entityWarmlyLocomotive.FuelStorage != otherEntityWarmlyLocomotive.FuelStorage) return false;
}
return true;
}
}
}

View File

@ -29,6 +29,7 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -51,6 +52,7 @@
this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.loadFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.ButtonSortByColor);
this.groupBoxTools.Controls.Add(this.ButtonSortByType);
this.groupBoxTools.Controls.Add(this.groupBox1);
this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonRight);
@ -72,11 +76,21 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(580, 28);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(220, 540);
this.groupBoxTools.Size = new System.Drawing.Size(220, 637);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools";
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(7, 456);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(207, 29);
this.ButtonSortByType.TabIndex = 9;
this.ButtonSortByType.Text = "Sort By Type";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonDeleteMap);
@ -144,7 +158,7 @@
//
this.buttonLeft.BackgroundImage = global::Locomotive.Properties.Resources.left_arrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(42, 502);
this.buttonLeft.Location = new System.Drawing.Point(48, 591);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(40, 40);
this.buttonLeft.TabIndex = 7;
@ -155,7 +169,7 @@
//
this.buttonRight.BackgroundImage = global::Locomotive.Properties.Resources.right_arrow;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(134, 502);
this.buttonRight.Location = new System.Drawing.Point(140, 591);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(40, 40);
this.buttonRight.TabIndex = 7;
@ -166,7 +180,7 @@
//
this.buttonDown.BackgroundImage = global::Locomotive.Properties.Resources.down_arrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(88, 502);
this.buttonDown.Location = new System.Drawing.Point(94, 591);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(40, 40);
this.buttonDown.TabIndex = 7;
@ -177,7 +191,7 @@
//
this.buttonUp.BackgroundImage = global::Locomotive.Properties.Resources.up_arrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(88, 456);
this.buttonUp.Location = new System.Drawing.Point(94, 545);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(40, 40);
this.buttonUp.TabIndex = 6;
@ -237,7 +251,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 28);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(580, 540);
this.pictureBox.Size = new System.Drawing.Size(580, 637);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -263,14 +277,14 @@
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.saveToolStripMenuItem.Size = new System.Drawing.Size(125, 26);
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(224, 26);
this.loadToolStripMenuItem.Size = new System.Drawing.Size(125, 26);
this.loadToolStripMenuItem.Text = "Load";
this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click);
//
@ -282,11 +296,21 @@
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(7, 491);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(207, 29);
this.ButtonSortByColor.TabIndex = 10;
this.ButtonSortByColor.Text = "Sort By Color";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetLocomotives
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 568);
this.ClientSize = new System.Drawing.Size(800, 665);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -330,5 +354,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -254,5 +254,25 @@ namespace Locomotive
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new LocomotiveCompareByType());
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 LocomotiveCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -67,6 +67,6 @@
<value>311, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>33</value>
<value>25</value>
</metadata>
</root>

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// Шаг перемещения объекта
public float Step { get; }

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class LocomotiveCompareByColor : 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 xLocomotive = x as DrawningObjectLocomotive;
var yLocomotive = y as DrawningObjectLocomotive;
if (xLocomotive == null && yLocomotive == null)
{
return 0;
}
if (xLocomotive == null && yLocomotive != null)
{
return 1;
}
if (xLocomotive != null && yLocomotive == null)
{
return -1;
}
if (xLocomotive.GetLocomotive.Locomotive.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.R) != 0)
{
return xLocomotive.GetLocomotive.Locomotive.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.R);
}
if (xLocomotive.GetLocomotive.Locomotive.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.G) != 0)
{
return xLocomotive.GetLocomotive.Locomotive.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.G);
}
if (xLocomotive.GetLocomotive.Locomotive.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.B) != 0)
{
return xLocomotive.GetLocomotive.Locomotive.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotive.BodyColor.B);
}
if (xLocomotive.GetLocomotive.Locomotive is EntityWarmlyLocomotive xWarmlyEntity && yLocomotive.GetLocomotive.Locomotive is EntityWarmlyLocomotive yWarmlyEntity)
{
if (xWarmlyEntity.ExtraColor.R.CompareTo(yWarmlyEntity.ExtraColor.R) != 0)
{
return xWarmlyEntity.ExtraColor.R.CompareTo(yWarmlyEntity.ExtraColor.R);
}
if (xWarmlyEntity.ExtraColor.G.CompareTo(yWarmlyEntity.ExtraColor.G) != 0)
{
return xWarmlyEntity.ExtraColor.G.CompareTo(yWarmlyEntity.ExtraColor.G);
}
if (xWarmlyEntity.ExtraColor.B.CompareTo(yWarmlyEntity.ExtraColor.B) != 0)
{
return xWarmlyEntity.ExtraColor.B.CompareTo(yWarmlyEntity.ExtraColor.B);
}
}
var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class LocomotiveCompareByType : 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 xLocomotive = x as DrawningObjectLocomotive;
var yLocomotive = y as DrawningObjectLocomotive;
if (xLocomotive == null && yLocomotive == null)
{
return 0;
}
if (xLocomotive == null && yLocomotive != null)
{
return 1;
}
if (xLocomotive != null && yLocomotive == null)
{
return -1;
}
if (xLocomotive.GetLocomotive.GetType().Name != yLocomotive.GetLocomotive.GetType().Name)
{
if (xLocomotive.GetLocomotive.GetType().Name == "DrawningLocomotive")
{
return -1;
}
return 1;
}
var speedCompare = xLocomotive.GetLocomotive.Locomotive.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotive.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xLocomotive.GetLocomotive.Locomotive.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotive.Weight);
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal class MapWithSetLocomotivesGeneric <T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// Ширина окна отрисовки
@ -178,6 +178,9 @@ namespace Locomotive
}
}
public void Sort(IComparer<T> comparer)
{
_setLocomotives.SortSet(comparer);
}
}
}

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal class SetLocomotivesGeneric <T>
where T : class
where T : class, IEquatable<T>
{
/// Список хранимых объектов
private readonly List<T> _places;
@ -31,6 +31,7 @@ namespace Locomotive
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (_places.Contains(locomotive)) return -1; // Проверка на уникальность
if (position < 0) return -1;
if (Count >= _maxCount) {
Log.Warning("StorageOverflowException");
@ -76,5 +77,15 @@ namespace Locomotive
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

17
Locomotive/log.clef Normal file
View File

@ -0,0 +1,17 @@
{"@t":"2022-11-21T15:23:13.2775821Z","@mt":"Map lalaland added"}
{"@t":"2022-11-21T15:23:13.3466012Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:23:31.6565447Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:23:57.7770111Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:01.0533266Z","@mt":"Map lalaland added"}
{"@t":"2022-11-21T15:25:01.0794891Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:25:01.4998647Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:25:06.1595618Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:18.9994738Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:31.8129464Z","@mt":"Map lalaland added"}
{"@t":"2022-11-21T15:25:31.8344280Z","@mt":"Map switched to lalaland"}
{"@t":"2022-11-21T15:25:37.1362070Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:25:46.1930394Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:26:02.0790751Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:26:24.6631252Z","@mt":"Object Locomotive.DrawningWarmlyLocomotive added"}
{"@t":"2022-11-21T15:26:31.0236570Z","@mt":"Object Locomotive.DrawningLocomotive added"}
{"@t":"2022-11-21T15:26:36.0331011Z","@mt":"Object Locomotive.DrawningWarmlyLocomotive added"}