Lab8 ready

This commit is contained in:
Saanechkaa 2023-01-23 01:21:46 +04:00
parent 5f39e71615
commit 7604e76b7c
10 changed files with 293 additions and 34 deletions

View File

@ -1,6 +1,6 @@
namespace RoadTrain
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -110,6 +110,32 @@
_drawningObject.DrawningObject(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

@ -1,9 +1,13 @@
namespace RoadTrain
using System.Net.Sockets;
namespace RoadTrain
{
internal class DrawningObjectRoadTrain : IDrawningObject
{
private DrawningRoadTrain _roadTrain = null;
public DrawningRoadTrain GetRoadTrain => _roadTrain;
public DrawningObjectRoadTrain(DrawningRoadTrain roadTrain)
{
_roadTrain = roadTrain;
@ -34,5 +38,52 @@
public string GetInfo() => _roadTrain?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectRoadTrain(data.CreateDrawningRoadTrain());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherRoadTrain = other as DrawningObjectRoadTrain;
if (otherRoadTrain == null)
{
return false;
}
var roadTrain = _roadTrain.RoadTrain;
var otherRoadTrainRoadTrain = otherRoadTrain._roadTrain.RoadTrain;
if (roadTrain.GetType() != otherRoadTrainRoadTrain.GetType())
{
return false;
}
if (roadTrain.Speed != otherRoadTrainRoadTrain.Speed)
{
return false;
}
if (roadTrain.Weight != otherRoadTrainRoadTrain.Weight)
{
return false;
}
if (roadTrain.BodyColor != otherRoadTrainRoadTrain.BodyColor)
{
return false;
}
if (roadTrain is EntitySweeperRoadTrain srt && otherRoadTrainRoadTrain is EntitySweeperRoadTrain otherSrt)
{
if (srt.DopColor != otherSrt.DopColor)
{
return false;
}
if (srt.WaterTank!= otherSrt.WaterTank)
{
return false;
}
if (srt.SweepingBush != otherSrt.SweepingBush)
{
return false;
}
}
return true;
}
}
}

View File

@ -30,6 +30,8 @@
{
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox = new System.Windows.Forms.GroupBox();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.buttonAddMap = new System.Windows.Forms.Button();
@ -60,12 +62,14 @@
//
this.pictureBox.Location = new System.Drawing.Point(3, 27);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(644, 421);
this.pictureBox.Size = new System.Drawing.Size(644, 480);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// groupBox
//
this.groupBox.Controls.Add(this.ButtonSortByType);
this.groupBox.Controls.Add(this.ButtonSortByColor);
this.groupBox.Controls.Add(this.groupBoxMaps);
this.groupBox.Controls.Add(this.buttonRight);
this.groupBox.Controls.Add(this.buttonShowOnMap);
@ -78,11 +82,31 @@
this.groupBox.Controls.Add(this.buttonAddRoadTrain);
this.groupBox.Location = new System.Drawing.Point(653, 3);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(175, 445);
this.groupBox.Size = new System.Drawing.Size(175, 504);
this.groupBox.TabIndex = 1;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
//
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(6, 230);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(160, 25);
this.ButtonSortByType.TabIndex = 16;
this.ButtonSortByType.Text = "Сортировать по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(6, 258);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(160, 25);
this.ButtonSortByColor.TabIndex = 15;
this.ButtonSortByColor.Text = "Сортировать по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
@ -92,7 +116,7 @@
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(6, 22);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(163, 211);
this.groupBoxMaps.Size = new System.Drawing.Size(163, 200);
this.groupBoxMaps.TabIndex = 14;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
@ -116,7 +140,7 @@
//
// buttonRemoveMap
//
this.buttonRemoveMap.Location = new System.Drawing.Point(6, 180);
this.buttonRemoveMap.Location = new System.Drawing.Point(6, 165);
this.buttonRemoveMap.Name = "buttonRemoveMap";
this.buttonRemoveMap.Size = new System.Drawing.Size(151, 25);
this.buttonRemoveMap.TabIndex = 15;
@ -130,7 +154,7 @@
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(6, 110);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(151, 64);
this.listBoxMaps.Size = new System.Drawing.Size(151, 49);
this.listBoxMaps.TabIndex = 1;
this.listBoxMaps.Click += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
@ -152,7 +176,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::RoadTrain.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(103, 418);
this.buttonRight.Location = new System.Drawing.Point(103, 477);
this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(26, 22);
@ -162,7 +186,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 357);
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 420);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(160, 25);
this.buttonShowOnMap.TabIndex = 4;
@ -175,7 +199,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::RoadTrain.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(74, 390);
this.buttonUp.Location = new System.Drawing.Point(74, 449);
this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(26, 22);
@ -188,7 +212,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::RoadTrain.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(74, 417);
this.buttonDown.Location = new System.Drawing.Point(74, 476);
this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(26, 22);
@ -201,7 +225,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::RoadTrain.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(43, 417);
this.buttonLeft.Location = new System.Drawing.Point(43, 476);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(26, 22);
@ -211,14 +235,14 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 268);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 328);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(160, 23);
this.maskedTextBoxPosition.TabIndex = 5;
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 330);
this.buttonShowStorage.Location = new System.Drawing.Point(6, 393);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(160, 25);
this.buttonShowStorage.TabIndex = 3;
@ -228,7 +252,7 @@
//
// buttonRemoveRoadTrain
//
this.buttonRemoveRoadTrain.Location = new System.Drawing.Point(6, 295);
this.buttonRemoveRoadTrain.Location = new System.Drawing.Point(6, 355);
this.buttonRemoveRoadTrain.Name = "buttonRemoveRoadTrain";
this.buttonRemoveRoadTrain.Size = new System.Drawing.Size(160, 27);
this.buttonRemoveRoadTrain.TabIndex = 2;
@ -238,7 +262,7 @@
//
// buttonAddRoadTrain
//
this.buttonAddRoadTrain.Location = new System.Drawing.Point(6, 239);
this.buttonAddRoadTrain.Location = new System.Drawing.Point(6, 299);
this.buttonAddRoadTrain.Name = "buttonAddRoadTrain";
this.buttonAddRoadTrain.Size = new System.Drawing.Size(160, 25);
this.buttonAddRoadTrain.TabIndex = 1;
@ -284,7 +308,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(831, 450);
this.ClientSize = new System.Drawing.Size(831, 519);
this.Controls.Add(this.groupBox);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.menuStrip1);
@ -327,5 +351,7 @@
private MenuStrip menuStrip1;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -321,5 +321,37 @@ namespace RoadTrain
}
ReloadMaps();
}
/// <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 RoadTrainCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
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 RoadTrainCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

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

View File

@ -6,7 +6,7 @@
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetRoadTrainsGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -146,6 +146,15 @@
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setRoadTrains.SortSet(comparer);
}
/// <summary>
/// Метод отрисовки фона
/// </summary>

View File

@ -91,17 +91,6 @@ namespace RoadTrain
}
}
/// <summary>
/// Метод записи информации в файл
/// </summary>
/// <param name="text">Строка, которую следует записать</param>
/// <param name="stream">Поток для записи</param>
private static void WriteToFile(string text, FileStream stream)
{
byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length);
}
/// <summary>
/// Сохранение информации по грузовикам хранилища в файл
/// </summary>
@ -134,7 +123,6 @@ namespace RoadTrain
{
throw new Exception("Файл не найден");
}
string bufferTextFromFile = "";
using (StreamReader sr = new(filename))
{
string str = "";

View File

@ -0,0 +1,58 @@
namespace RoadTrain
{
internal class RoadTrainCompareByColor : 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 xRoadTrain = x as DrawningObjectRoadTrain;
var yRoadTrain = y as DrawningObjectRoadTrain;
if (xRoadTrain == null && yRoadTrain == null)
{
return 0;
}
if (xRoadTrain == null && yRoadTrain != null)
{
return 1;
}
if (xRoadTrain != null && yRoadTrain == null)
{
return -1;
}
int xColor = xRoadTrain.GetRoadTrain.RoadTrain.BodyColor.ToArgb();
int yColor = yRoadTrain.GetRoadTrain.RoadTrain.BodyColor.ToArgb();
if (xColor != yColor)
return xColor.CompareTo(yColor);
if (xRoadTrain.GetRoadTrain.RoadTrain is EntitySweeperRoadTrain xSweeper && yRoadTrain.GetRoadTrain.RoadTrain is EntitySweeperRoadTrain ySweeper)
{
xColor = xSweeper.DopColor.ToArgb();
yColor = ySweeper.DopColor.ToArgb();
if (xColor != yColor)
return xColor.CompareTo(yColor);
}
var speedCompare = xRoadTrain.GetRoadTrain.RoadTrain.Speed.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xRoadTrain.GetRoadTrain.RoadTrain.Weight.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Weight);
}
}
}

View File

@ -0,0 +1,49 @@
namespace RoadTrain
{
internal class RoadTrainCompareByType : 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 xRoadTrain = x as DrawningObjectRoadTrain;
var yRoadTrain = y as DrawningObjectRoadTrain;
if (xRoadTrain == null && yRoadTrain == null)
{
return 0;
}
if (xRoadTrain == null && yRoadTrain != null)
{
return 1;
}
if (xRoadTrain != null && yRoadTrain == null)
{
return -1;
}
if (xRoadTrain.GetRoadTrain.GetType().Name != yRoadTrain.GetRoadTrain.GetType().Name)
{
if (xRoadTrain.GetRoadTrain.GetType().Name == "DrawningRoadTrain")
{
return -1;
}
return 1;
}
var speedCompare = xRoadTrain.GetRoadTrain.RoadTrain.Speed.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xRoadTrain.GetRoadTrain.RoadTrain.Weight.CompareTo(yRoadTrain.GetRoadTrain.RoadTrain.Weight);
}
}
}

View File

@ -5,7 +5,7 @@
/// </summary>
/// <typeparam name="T"></typeparam>
public class SetRoadTrainsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Массив объектов, которые храним
@ -39,7 +39,7 @@
// проверка на _maxCount
if (_places.Count + 1 >= _maxCount)
throw new StorageOverflowException(_maxCount);
_places.Insert(0, roadTrain);
Insert(roadTrain, 0);
return 0;
}
@ -51,6 +51,10 @@
/// <returns></returns>
public int Insert(T roadTrain, int position)
{
// проверка на уникальность
if (_places.Contains(roadTrain))
return -1;
// проверка позиции
if (position < 0 || position >= _maxCount)
return -1;
@ -60,7 +64,7 @@
throw new StorageOverflowException(_maxCount);
// вставка по позиции
_places[position] = roadTrain;
_places.Insert(position, roadTrain);
return position;
}
@ -133,5 +137,18 @@
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}