Лабораторная работа 8

This commit is contained in:
Sem730 2022-12-06 09:48:20 +03:00
parent c424860090
commit f510c85dc4
10 changed files with 277 additions and 22 deletions

View File

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

View File

@ -14,6 +14,8 @@ namespace ProjectLocomotive
_loc = loc;
}
public float Step => _loc?.Locomotivе?.Step ?? 0;
public DrawningLocomotive GetLocomotive => _loc;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _loc?.GetCurrentPosition() ?? default;
@ -26,12 +28,46 @@ namespace ProjectLocomotive
{
_loc.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
// TODO
_loc.DrawTransport(g);
}
public string getInfo() => _loc?.getDataForSave();
public static IDrawningObject Create(string data) => new DrawningObject(data.createDrawningLocomotive());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherLocomotive = other as DrawningObject;
if (otherLocomotive == null)
{
return false;
}
var locomotive = _loc.Locomotivе;
var otherLocomotiveLocomotive = otherLocomotive._loc.Locomotivе;
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 EntityElectricLocomotive entityWarmlyLocomotive && otherLocomotiveLocomotive is EntityElectricLocomotive otherEntityWarmlyLocomotive)
{
if (entityWarmlyLocomotive.DopColor != otherEntityWarmlyLocomotive.DopColor) return false;
if (entityWarmlyLocomotive.ElectroLines != otherEntityWarmlyLocomotive.ElectroLines) return false;
if (entityWarmlyLocomotive.ElectroBattery != otherEntityWarmlyLocomotive.ElectroBattery) return false;
}
return true;
}
void IDrawningObject.DrawningObject(Graphics g)
{
_loc.DrawTransport(g);
}
}
}

View File

@ -1,4 +1,5 @@
namespace ProjectLocomotive

namespace ProjectLocomotive
{
partial class FormLocomotiveConfig
{

View File

@ -28,6 +28,8 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.ButtonSortByColor = new System.Windows.Forms.Button();
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();
@ -58,6 +60,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);
@ -69,15 +73,35 @@
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonAddLocomotive);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(577, 33);
this.groupBoxTools.Location = new System.Drawing.Point(585, 33);
this.groupBoxTools.Margin = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Padding = new System.Windows.Forms.Padding(4);
this.groupBoxTools.Size = new System.Drawing.Size(220, 510);
this.groupBoxTools.Size = new System.Drawing.Size(220, 481);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(7, 524);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(207, 37);
this.ButtonSortByColor.TabIndex = 10;
this.ButtonSortByColor.Text = "Сортировка по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(6, 567);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(207, 39);
this.ButtonSortByType.TabIndex = 9;
this.ButtonSortByType.Text = "Сортировка по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonDeleteMap);
@ -145,7 +169,7 @@
//
this.buttonLeft.BackgroundImage = global::ProjectLocomotive.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(22, 591);
this.buttonLeft.Location = new System.Drawing.Point(42, 661);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
@ -157,7 +181,7 @@
//
this.buttonRight.BackgroundImage = global::ProjectLocomotive.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(138, 591);
this.buttonRight.Location = new System.Drawing.Point(157, 661);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
@ -169,7 +193,7 @@
//
this.buttonDown.BackgroundImage = global::ProjectLocomotive.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(80, 591);
this.buttonDown.Location = new System.Drawing.Point(100, 661);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50);
@ -181,7 +205,7 @@
//
this.buttonUp.BackgroundImage = global::ProjectLocomotive.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(80, 533);
this.buttonUp.Location = new System.Drawing.Point(100, 613);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50);
@ -191,7 +215,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(0, 489);
this.buttonShowOnMap.Location = new System.Drawing.Point(0, 481);
this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(4);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(220, 36);
@ -202,7 +226,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(7, 418);
this.buttonShowStorage.Location = new System.Drawing.Point(7, 410);
this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(4);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(208, 63);
@ -213,7 +237,7 @@
//
// buttonRemoveLocomotive
//
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(7, 364);
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(7, 356);
this.buttonRemoveLocomotive.Margin = new System.Windows.Forms.Padding(4);
this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
this.buttonRemoveLocomotive.Size = new System.Drawing.Size(207, 46);
@ -248,7 +272,7 @@
this.pictureBox.Location = new System.Drawing.Point(0, 33);
this.pictureBox.Margin = new System.Windows.Forms.Padding(4);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(577, 510);
this.pictureBox.Size = new System.Drawing.Size(585, 481);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -259,7 +283,7 @@
this.fileToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(797, 33);
this.menuStrip.Size = new System.Drawing.Size(805, 33);
this.menuStrip.TabIndex = 2;
//
// fileToolStripMenuItem
@ -297,7 +321,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(797, 543);
this.ClientSize = new System.Drawing.Size(805, 514);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -340,5 +364,7 @@
private ToolStripMenuItem loadToolStripMenuItem;
private OpenFileDialog loadFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -90,6 +90,11 @@ namespace ProjectLocomotive
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Ship}", ex.Message, locomotive);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// Удаление объекта
private void buttonRemoveLocomotive_Click(object sender, EventArgs e)
@ -249,5 +254,23 @@ namespace ProjectLocomotive
}
}
}
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

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

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
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 DrawningObject;
var yLocomotive = y as DrawningObject;
if (xLocomotive == null && yLocomotive == null)
{
return 0;
}
if (xLocomotive == null && yLocomotive != null)
{
return 1;
}
if (xLocomotive != null && yLocomotive == null)
{
return -1;
}
if (xLocomotive.GetLocomotive.Locomotivе.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.R) != 0)
{
return xLocomotive.GetLocomotive.Locomotivе.BodyColor.R.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.R);
}
if (xLocomotive.GetLocomotive.Locomotivе.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.G) != 0)
{
return xLocomotive.GetLocomotive.Locomotivе.BodyColor.G.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.G);
}
if (xLocomotive.GetLocomotive.Locomotivе.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.B) != 0)
{
return xLocomotive.GetLocomotive.Locomotivе.BodyColor.B.CompareTo(yLocomotive.GetLocomotive.Locomotivе.BodyColor.B);
}
if (xLocomotive.GetLocomotive.Locomotivе is EntityElectricLocomotive xWarmlyEntity && yLocomotive.GetLocomotive.Locomotivе is EntityElectricLocomotive 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 = xLocomotive.GetLocomotive.Locomotivе.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xLocomotive.GetLocomotive.Locomotivе.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLocomotive
{
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 DrawningObject;
var yLocomotive = y as DrawningObject;
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.Locomotivе.Speed.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xLocomotive.GetLocomotive.Locomotivе.Weight.CompareTo(yLocomotive.GetLocomotive.Locomotivе.Weight);
}
}
}

View File

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

View File

@ -11,7 +11,7 @@ namespace ProjectLocomotive
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetLocomotivesGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// Список хранимых объектов
private readonly List<T> _places;
@ -32,6 +32,7 @@ namespace ProjectLocomotive
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (_places.Contains(locomotive)) return -1; // Проверка на уникальность
if (position < 0) return -1;
if (Count >= _maxCount) throw new StorageOverflowException(_maxCount);
_places.Insert(position, locomotive);
@ -70,5 +71,13 @@ namespace ProjectLocomotive
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}