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

This commit is contained in:
Efi 2023-04-21 01:24:52 +04:00
parent d7880d438d
commit a0f252b9e9
8 changed files with 249 additions and 59 deletions

View File

@ -10,6 +10,8 @@ namespace Monorail
{
private DrawingLocomotive _locomotive = null;
public DrawingLocomotive GetLocomotive => _locomotive;
public DrawingObjectLocomotive(DrawingLocomotive locomotive)
{
_locomotive = locomotive;
@ -36,6 +38,34 @@ namespace Monorail
{
_locomotive.DrawTransport(g);
}
public bool Equals(IDrawingObject? other)
{
if (other is not DrawingObjectLocomotive otherLocomotive)
{
return false;
}
var entity = _locomotive.Locomotive;
var otherEntity = otherLocomotive._locomotive.Locomotive;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityMonorailLocomotive entityMonorailLocomotive &&
otherEntity is EntityMonorailLocomotive otherEntityMonorailLocomotive && (
entityMonorailLocomotive.Monorail != otherEntityMonorailLocomotive.Monorail ||
entityMonorailLocomotive.DopColor != otherEntityMonorailLocomotive.DopColor ||
entityMonorailLocomotive.DopCabin != otherEntityMonorailLocomotive.DopCabin))
{
return false;
}
return true;
}
public string GetInfo() => _locomotive?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectLocomotive(data.CreateDrawingLocomotive());

View File

@ -40,6 +40,8 @@
buttonDown = new Button();
buttonRight = new Button();
groupBoxTools = new GroupBox();
buttonSortByColor = new Button();
buttonSortByType = new Button();
groupBoxMaps = new GroupBox();
textBoxNewMapName = new TextBox();
buttonDeleteMap = new Button();
@ -62,7 +64,7 @@
pictureBoxLocomotive.Dock = DockStyle.Left;
pictureBoxLocomotive.Location = new Point(0, 24);
pictureBoxLocomotive.Name = "pictureBoxLocomotive";
pictureBoxLocomotive.Size = new Size(579, 537);
pictureBoxLocomotive.Size = new Size(579, 623);
pictureBoxLocomotive.TabIndex = 0;
pictureBoxLocomotive.TabStop = false;
//
@ -78,7 +80,7 @@
//
// buttonAddCar
//
buttonAddCar.Location = new Point(23, 288);
buttonAddCar.Location = new Point(23, 358);
buttonAddCar.Name = "buttonAddCar";
buttonAddCar.Size = new Size(169, 30);
buttonAddCar.TabIndex = 3;
@ -88,7 +90,7 @@
//
// buttonRemoveCar
//
buttonRemoveCar.Location = new Point(23, 353);
buttonRemoveCar.Location = new Point(23, 421);
buttonRemoveCar.Name = "buttonRemoveCar";
buttonRemoveCar.Size = new Size(169, 30);
buttonRemoveCar.TabIndex = 4;
@ -98,7 +100,7 @@
//
// buttonShowStorage
//
buttonShowStorage.Location = new Point(25, 406);
buttonShowStorage.Location = new Point(25, 470);
buttonShowStorage.Name = "buttonShowStorage";
buttonShowStorage.Size = new Size(169, 30);
buttonShowStorage.TabIndex = 5;
@ -108,7 +110,7 @@
//
// buttonShowOnMap
//
buttonShowOnMap.Location = new Point(25, 443);
buttonShowOnMap.Location = new Point(25, 505);
buttonShowOnMap.Name = "buttonShowOnMap";
buttonShowOnMap.Size = new Size(169, 30);
buttonShowOnMap.TabIndex = 6;
@ -118,7 +120,7 @@
//
// maskedTextBoxPosition
//
maskedTextBoxPosition.Location = new Point(24, 324);
maskedTextBoxPosition.Location = new Point(24, 393);
maskedTextBoxPosition.Name = "maskedTextBoxPosition";
maskedTextBoxPosition.Size = new Size(169, 23);
maskedTextBoxPosition.TabIndex = 7;
@ -127,7 +129,7 @@
//
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(96, 483);
buttonUp.Location = new Point(96, 547);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 8;
@ -138,7 +140,7 @@
//
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(60, 519);
buttonLeft.Location = new Point(60, 583);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 9;
@ -149,7 +151,7 @@
//
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(96, 519);
buttonDown.Location = new Point(96, 583);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 10;
@ -160,7 +162,7 @@
//
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(132, 519);
buttonRight.Location = new Point(132, 583);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 11;
@ -170,6 +172,8 @@
// groupBoxTools
//
groupBoxTools.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
groupBoxTools.Controls.Add(buttonSortByColor);
groupBoxTools.Controls.Add(buttonSortByType);
groupBoxTools.Controls.Add(groupBoxMaps);
groupBoxTools.Controls.Add(buttonRight);
groupBoxTools.Controls.Add(buttonDown);
@ -180,13 +184,33 @@
groupBoxTools.Controls.Add(buttonShowStorage);
groupBoxTools.Controls.Add(buttonRemoveCar);
groupBoxTools.Controls.Add(buttonAddCar);
groupBoxTools.Location = new Point(585, 0);
groupBoxTools.Location = new Point(585, 24);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(212, 560);
groupBoxTools.Size = new Size(212, 623);
groupBoxTools.TabIndex = 12;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// buttonSortByColor
//
buttonSortByColor.Location = new Point(25, 311);
buttonSortByColor.Name = "buttonSortByColor";
buttonSortByColor.Size = new Size(169, 30);
buttonSortByColor.TabIndex = 18;
buttonSortByColor.Text = "Сортировать по цвету";
buttonSortByColor.UseVisualStyleBackColor = true;
buttonSortByColor.Click += ButtonSortByColor_Click;
//
// buttonSortByType
//
buttonSortByType.Location = new Point(24, 277);
buttonSortByType.Name = "buttonSortByType";
buttonSortByType.Size = new Size(169, 30);
buttonSortByType.TabIndex = 17;
buttonSortByType.Text = "Сортировать по типу";
buttonSortByType.UseVisualStyleBackColor = true;
buttonSortByType.Click += ButtonSortByType_Click;
//
// groupBoxMaps
//
groupBoxMaps.Controls.Add(textBoxNewMapName);
@ -194,7 +218,7 @@
groupBoxMaps.Controls.Add(buttonAddMap);
groupBoxMaps.Controls.Add(listBoxMaps);
groupBoxMaps.Controls.Add(comboBoxSelectorMap);
groupBoxMaps.Location = new Point(8, 21);
groupBoxMaps.Location = new Point(9, 22);
groupBoxMaps.Name = "groupBoxMaps";
groupBoxMaps.Size = new Size(197, 249);
groupBoxMaps.TabIndex = 16;
@ -257,14 +281,14 @@
// SaveToolStripMenuItem
//
SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
SaveToolStripMenuItem.Size = new Size(180, 22);
SaveToolStripMenuItem.Size = new Size(141, 22);
SaveToolStripMenuItem.Text = "Сохранение";
SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click;
//
// LoadToolStripMenuItem
//
LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
LoadToolStripMenuItem.Size = new Size(180, 22);
LoadToolStripMenuItem.Size = new Size(141, 22);
LoadToolStripMenuItem.Text = "Загрузка";
LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click;
//
@ -280,7 +304,7 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 561);
ClientSize = new Size(800, 647);
Controls.Add(groupBoxTools);
Controls.Add(pictureBoxLocomotive);
Controls.Add(menuStrip);
@ -323,5 +347,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -14,7 +14,7 @@ namespace Monorail
{
public partial class FormMapWithSetLocomotive : Form
{
private MapWithSetLocomotiveGeneric<DrawingObjectLocomotive, AbstractMap> _mapLocomotiveCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapDict = new()
{
{"Простая карта", new SimpleMap() },
@ -53,32 +53,6 @@ namespace Monorail
listBoxMaps.SelectedIndex = index;
}
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender,
EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Карта с грязью":
map = new FieldMap();
break;
case "Карта с кустами":
map = new BushesMap();
break;
}
if (map != null)
{
_mapLocomotiveCollectionGeneric = new MapWithSetLocomotiveGeneric<DrawingObjectLocomotive, AbstractMap>(
pictureBoxLocomotive.Width, pictureBoxLocomotive.Height, map);
}
else
{
_mapLocomotiveCollectionGeneric = null;
}
}
private void ButtonAddLocomotive_Click(object sender, EventArgs e)
{
var FormLocmotiveConfig = new FormLocomotiveConfig();
@ -272,5 +246,28 @@ namespace Monorail
}
}
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ??
string.Empty].Sort(new LocomotiveCompareByType());
pictureBoxLocomotive.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());
pictureBoxLocomotive.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

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

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal class LocomotiveCompareByColor : 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 xLocomotive = x as DrawingObjectLocomotive;
var yLocomotive = y as DrawingObjectLocomotive;
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 EntityMonorailLocomotive xMonorailLocomotive && yEntityLocomotive is EntityMonorailLocomotive yMonorailLocomotive)
{
var dopColorCompare = xMonorailLocomotive.DopColor.ToArgb().CompareTo(yMonorailLocomotive.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,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monorail
{
internal class LocomotiveCompareByType : 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 xLocomotive = x as DrawingObjectLocomotive;
var yLocomotive = y as DrawingObjectLocomotive;
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 == "DrawingLocomotive")
{
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 Monorail
{
internal class MapWithSetLocomotiveGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
// Ширина окна отрисовки
@ -55,7 +55,7 @@ namespace Monorail
public Bitmap ShowOnMap()
{
Shaking();
foreach (var locomotive in _setLocomotive.GetLocomotives())
foreach (var locomotive in _setLocomotive.GetLocomotive())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
}
@ -118,7 +118,7 @@ namespace Monorail
int xPosition = _pictureWidth - _placeSizeWidth;
int yPosition = 12;
foreach (var locomotive in _setLocomotive.GetLocomotives())
foreach (var locomotive in _setLocomotive.GetLocomotive())
{
locomotive.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
locomotive.DrawingObject(g);
@ -135,7 +135,7 @@ namespace Monorail
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var boat in _setLocomotive.GetLocomotives())
foreach (var boat in _setLocomotive.GetLocomotive())
{
data += $"{boat.GetInfo()}{separatorData}";
}
@ -148,5 +148,10 @@ namespace Monorail
_setLocomotive.Insert(DrawingObjectLocomotive.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setLocomotive.SortSet(comparer);
}
}
}

View File

@ -7,29 +7,30 @@ using System.Threading.Tasks;
namespace Monorail
{
internal class SetLocomotiveGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetLocomotiveGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T locomotive)
{
return Insert(locomotive, 0);
}
public int Insert(T locomotive, int position)
{
if (Count == _maxCount)
if (_places.Contains(locomotive))
{
throw new ArgumentException($"Объект {locomotive} уже присутствует в наборе");
}
if (position < 0 || position > Count || _maxCount == Count)
{
throw new StorageOverflowException(_maxCount);
}
if (position < 0 || position > _maxCount) return -1;
_places.Insert(position, locomotive);
return position;
}
@ -43,23 +44,27 @@ namespace Monorail
_places.RemoveAt(position);
return ship;
}
public T this[int position]
{
get
{
if (position < _places.Count && position >= 0)
return _places[position];
else
if (position >= Count || position < 0)
{
return null;
}
return _places[position];
}
set
{
if (position >= Count || position < 0)
{
return;
}
Insert(value, position);
}
}
public IEnumerable<T> GetLocomotives()
public IEnumerable<T> GetLocomotive()
{
foreach (var locomotive in _places)
{
@ -73,5 +78,14 @@ namespace Monorail
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}