Malin D.V. Lab work 8 #8

Closed
Danil_Malin wants to merge 2 commits from LabWork_08 into LabWork_07
9 changed files with 307 additions and 16 deletions

View File

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

@ -10,6 +10,7 @@ namespace WarmlyLocomotive
{
private DrawningLocomotive _locomotive = null;
public float Step => _locomotive?.Locomotive?.Step ?? 0;
public DrawningLocomotive GetLocomotive => _locomotive;
public DrawningObjectLocomotive(DrawningLocomotive locomotive)
{
_locomotive = locomotive;
@ -32,5 +33,55 @@ namespace WarmlyLocomotive
}
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.Speed != otherLocomotiveLocomotive.Speed)
{
return false;
}
if (locomotive.Weight != otherLocomotiveLocomotive.Weight)
{
return false;
}
if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor)
{
return false;
}
if(locomotive is EntityWarmlyLocomotive warmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherWarmlyLocomotive)
{
if(warmlyLocomotive.Pipe != otherWarmlyLocomotive.Pipe)
{
return false;
}
if (warmlyLocomotive.FuelCompartment != otherWarmlyLocomotive.FuelCompartment)
{
return false;
}
if (warmlyLocomotive.WarmlyLines != otherWarmlyLocomotive.WarmlyLines)
{
return false;
}
if (warmlyLocomotive.DopColor != otherWarmlyLocomotive.DopColor)
{
return false;
}
}
else if(locomotive is EntityWarmlyLocomotive || otherLocomotiveLocomotive is EntityWarmlyLocomotive)
{
return false;
}
return true;
}
}
}

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.buttonDown);
this.groupBox.Controls.Add(this.buttonRight);
@ -72,11 +76,31 @@
this.groupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox.Location = new System.Drawing.Point(668, 24);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(200, 638);
this.groupBox.Size = new System.Drawing.Size(200, 693);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(19, 340);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(169, 30);
this.buttonSortByColor.TabIndex = 12;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(19, 304);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(169, 30);
this.buttonSortByType.TabIndex = 11;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
@ -147,7 +171,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(87, 591);
this.buttonDown.Location = new System.Drawing.Point(87, 646);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 9;
@ -159,7 +183,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(123, 591);
this.buttonRight.Location = new System.Drawing.Point(123, 646);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 8;
@ -171,7 +195,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(51, 591);
this.buttonLeft.Location = new System.Drawing.Point(51, 646);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 7;
@ -183,7 +207,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::WarmlyLocomotive.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(87, 555);
this.buttonUp.Location = new System.Drawing.Point(87, 610);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 6;
@ -193,7 +217,7 @@
// buttonShowOnMap
//
this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonShowOnMap.Location = new System.Drawing.Point(19, 500);
this.buttonShowOnMap.Location = new System.Drawing.Point(19, 554);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(169, 30);
this.buttonShowOnMap.TabIndex = 5;
@ -204,7 +228,7 @@
// buttonShowStorage
//
this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonShowStorage.Location = new System.Drawing.Point(19, 452);
this.buttonShowStorage.Location = new System.Drawing.Point(19, 506);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(169, 30);
this.buttonShowStorage.TabIndex = 4;
@ -215,7 +239,7 @@
// buttonRemoveLocomotive
//
this.buttonRemoveLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(19, 406);
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(19, 460);
this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
this.buttonRemoveLocomotive.Size = new System.Drawing.Size(169, 30);
this.buttonRemoveLocomotive.TabIndex = 3;
@ -226,7 +250,7 @@
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 377);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 431);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(169, 23);
@ -235,7 +259,7 @@
// buttonAddLocomotive
//
this.buttonAddLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddLocomotive.Location = new System.Drawing.Point(19, 341);
this.buttonAddLocomotive.Location = new System.Drawing.Point(19, 395);
this.buttonAddLocomotive.Name = "buttonAddLocomotive";
this.buttonAddLocomotive.Size = new System.Drawing.Size(169, 30);
this.buttonAddLocomotive.TabIndex = 1;
@ -248,7 +272,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(668, 638);
this.pictureBox.Size = new System.Drawing.Size(668, 693);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
@ -297,7 +321,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(868, 662);
this.ClientSize = new System.Drawing.Size(868, 717);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.menuStrip);
@ -341,5 +365,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -102,6 +102,11 @@ namespace WarmlyLocomotive
_logger.LogWarning($"Ошибка переполнения хранилища: {ex.Message}");
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch(ArgumentException ex)
{
_logger.LogWarning($"Ошибка при добавлении: {ex.Message}");
MessageBox.Show($"Ошибка при добавлении: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
_logger.LogWarning($"Неизвестная ошибка: {ex.Message}");
@ -314,5 +319,33 @@ namespace WarmlyLocomotive
}
}
}
/// <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 LocomotiveCompareByType());
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)
{
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

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

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WarmlyLocomotive
{
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;
}
var xEntityLocomotive = xLocomotive.GetLocomotive.Locomotive;
var yEntityLocomotive = yLocomotive.GetLocomotive.Locomotive;
var baseColorCompare = xEntityLocomotive.BodyColor.ToArgb().CompareTo(yEntityLocomotive.BodyColor.ToArgb());
if(baseColorCompare != 0)
{
return baseColorCompare;
}
if(xEntityLocomotive is EntityWarmlyLocomotive xWarmlyLocomotive && yEntityLocomotive is EntityWarmlyLocomotive yWarmlyLocomotive)
{
var dopColorCompare = xWarmlyLocomotive.DopColor.ToArgb().CompareTo(yWarmlyLocomotive.DopColor.ToArgb());
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
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 WarmlyLocomotive
{
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 WarmlyLocomotive
{
internal class MapWithSetLocomotivesGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -133,6 +133,14 @@ namespace WarmlyLocomotive
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setLocomotives.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace WarmlyLocomotive
{
internal class SetLocomotivesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -44,6 +44,10 @@ namespace WarmlyLocomotive
/// <returns></returns>
public int Insert(T locomotive, int position)
{
if (_places.Contains(locomotive))
{
throw new ArgumentException($"Объект {locomotive} уже присутствует в наборе");
}
if (position < 0 || position > Count || _maxCount == Count)
{
throw new StorageOverflowException(_maxCount);
@ -107,5 +111,17 @@ namespace WarmlyLocomotive
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}