Nugaev D.N. Lab_work08 #8

Closed
Damir_Nugaev_ISEbd-22 wants to merge 4 commits from Lab_work08 into Lab_work07
9 changed files with 240 additions and 105 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -54,7 +54,6 @@ namespace Bus
public Bitmap MoveObject(Direction direction)
{
_drawingObject.MoveObject(direction);
bool collision = CheckCollision();
@ -80,23 +79,6 @@ namespace Bus
return DrawMapWithObject();
}
private Direction MoveObjectBack(Direction direction)
{
switch (direction)
{
case Direction.Up:
return Direction.Down;
case Direction.Down:
return Direction.Up;
case Direction.Left:
return Direction.Right;
case Direction.Right:
return Direction.Left;
}
return Direction.None;
}
private bool SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
@ -135,8 +117,6 @@ namespace Bus
return bmp;
}
private bool CheckCollision()
{
var pos = _drawingObject.GetCurrentPosition();
@ -157,12 +137,38 @@ namespace Bus
}
}
}
return false;
}
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 ||
_map != other._map ||
_width != other._width ||
_size_x != other._size_x ||
_size_y != other._size_y ||
_height != other._height ||
GetType() != other.GetType() ||
_map.GetLength(0) != other._map.GetLength(0) ||
_map.GetLength(1) != other._map.GetLength(1))
{
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

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class BusCompareByColor : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
var xBus = x as DrawingObjectBus;
var yBus = y as DrawingObjectBus;
if (xBus == yBus)
{
return 0;
}
if (xBus == null)
{
return 1;
}
if (yBus == null)
{
return -1;
}
var xEntity = xBus._bus.Bus;
var yEntity = yBus._bus.Bus;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 ||
xEntity is not EntitySportBus xEntitySportBus ||
yEntity is not EntitySportBus yEntitySportBus)
{
return colorWeight;
}
return xEntitySportBus.DopColor.ToArgb().CompareTo(yEntitySportBus.DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class BusCompareByType : IComparer<IDrawingObject>
{
public int Compare(IDrawingObject? x, IDrawingObject? y)
{
var xBus = x as DrawingObjectBus;
var yBus = y as DrawingObjectBus;
if (xBus == yBus)
{
return 0;
}
if (xBus == null)
{
return 1;
}
if (yBus == null)
{
return -1;
}
if (xBus._bus.GetType().Name != yBus._bus.GetType().Name)
{
if (xBus._bus.GetType() == typeof(DrawingBus))
{
return -1;
}
return 1;
}
var speedCompare = xBus._bus.Bus.Speed.CompareTo(yBus._bus.Bus.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xBus._bus.Bus.Weight.CompareTo(yBus._bus.Bus.Weight);
}
}
}

View File

@ -8,8 +8,9 @@ namespace Bus
{
internal class DrawingObjectBus : IDrawingObject
{
private DrawingBus _bus = null;
public DrawingBus _bus = null;
public float Step => _bus?.Bus?.Step ?? 0;
public DrawingBus GetBus => _bus;
public DrawingObjectBus(DrawingBus bus)
{
@ -39,6 +40,34 @@ namespace Bus
public string GetInfo() => _bus?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBus(data.CreateDrawingBus());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherBus = other as DrawingObjectBus;
var entity = _bus.Bus;
var otherEntity = otherBus._bus.Bus;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntitySportBus entitySportBus &&
otherEntity is EntitySportBus otherEntitySportBus && (
entitySportBus.Wing != otherEntitySportBus.Wing ||
entitySportBus.DopColor != otherEntitySportBus.DopColor ||
entitySportBus.Sportline != otherEntitySportBus.Sportline))
{
return false;
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
@ -59,6 +61,8 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonSortByColor);
this.groupBox1.Controls.Add(this.buttonSortByType);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonLeft);
@ -71,17 +75,37 @@
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(598, 28);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 590);
this.groupBox1.Size = new System.Drawing.Size(250, 650);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(17, 299);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(173, 29);
this.buttonSortByColor.TabIndex = 13;
this.buttonSortByColor.Text = "Сохранить по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(16, 260);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(174, 29);
this.buttonSortByType.TabIndex = 12;
this.buttonSortByType.Text = "Сохранить по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Bus.Properties.Resources.Frame_4;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(95, 538);
this.buttonRight.Location = new System.Drawing.Point(95, 598);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(40, 40);
this.buttonRight.TabIndex = 10;
@ -93,7 +117,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Bus.Properties.Resources.Frame_5;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(55, 538);
this.buttonDown.Location = new System.Drawing.Point(55, 598);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(40, 40);
this.buttonDown.TabIndex = 9;
@ -105,7 +129,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Bus.Properties.Resources.Frame_6;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(15, 538);
this.buttonLeft.Location = new System.Drawing.Point(15, 598);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(40, 40);
this.buttonLeft.TabIndex = 8;
@ -117,7 +141,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Bus.Properties.Resources.Frame_3;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(55, 498);
this.buttonUp.Location = new System.Drawing.Point(55, 558);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(40, 40);
this.buttonUp.TabIndex = 7;
@ -126,7 +150,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 426);
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 523);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(193, 29);
this.buttonShowOnMap.TabIndex = 5;
@ -136,7 +160,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(15, 391);
this.buttonShowStorage.Location = new System.Drawing.Point(15, 488);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(193, 29);
this.buttonShowStorage.TabIndex = 4;
@ -146,14 +170,14 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 320);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 417);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(100, 27);
this.maskedTextBoxPosition.TabIndex = 11;
//
// buttonRemoveBus
//
this.buttonRemoveBus.Location = new System.Drawing.Point(15, 353);
this.buttonRemoveBus.Location = new System.Drawing.Point(15, 450);
this.buttonRemoveBus.Name = "buttonRemoveBus";
this.buttonRemoveBus.Size = new System.Drawing.Size(129, 32);
this.buttonRemoveBus.TabIndex = 2;
@ -163,7 +187,7 @@
//
// buttonAddBus
//
this.buttonAddBus.Location = new System.Drawing.Point(15, 281);
this.buttonAddBus.Location = new System.Drawing.Point(15, 378);
this.buttonAddBus.Name = "buttonAddBus";
this.buttonAddBus.Size = new System.Drawing.Size(129, 33);
this.buttonAddBus.TabIndex = 1;
@ -188,7 +212,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(598, 590);
this.pictureBox.Size = new System.Drawing.Size(598, 650);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -265,14 +289,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(177, 26);
this.SaveToolStripMenuItem.Text = "Сохранение";
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(177, 26);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -288,7 +312,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(848, 618);
this.ClientSize = new System.Drawing.Size(848, 678);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
@ -333,5 +357,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -13,8 +13,6 @@ namespace Bus
{
public partial class FormMapWithSetDoubleDeckerBus : Form
{
private MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap> _mapBusCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{"Простая карта", new SimpleMap() },
@ -31,9 +29,8 @@ namespace Bus
comboBoxSelectorMap.Items.Clear();
foreach (var item in _mapsDict)
{
comboBoxSelectorMap.Items.Add(item.Key);//
comboBoxSelectorMap.Items.Add(item.Key);
}
}
private void ReloadMaps()
@ -65,21 +62,13 @@ namespace Bus
case "Водная карта":
map = new MyMap();
break;
}
if(map != null)
{
_mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBusCollectionGeneric = null;
}
}
}
private void ButtonAddBus_Click(object sender, EventArgs e)
{
var formBusConfig = new FormBusConfig();
formBusConfig.AddEvent(AddBus);
formBusConfig.AddEvent(new(AddBus));
formBusConfig.Show();
}
@ -89,15 +78,14 @@ namespace Bus
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
MessageBox.Show("Перед добавлением объекта необходимо создать карту");
}
DrawingObjectBus boat = new(bus);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0)
else if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectBus(bus) != -1)
{
MessageBox.Show("Объект добавлен");
_logger.LogInformation("Добавлен объект {@Airbus}", bus);
_logger.LogInformation("Добавлен объект {@bus}", bus);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
else
{
MessageBox.Show("Не удалось добавить объект");
@ -106,39 +94,31 @@ namespace Bus
}
catch (StorageOverflowException ex)
{
_logger.LogWarning("Ошибка, переполнение хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка, хранилище переполнено: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airbus}", ex.Message, bus);
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@bus}", ex.Message, bus);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonRemoveBus_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
try
{
if (_mapBusCollectionGeneric - pos != null)
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBusCollectionGeneric.ShowSet();
return;
}
else
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
MessageBox.Show("Не удалось удалить объект");
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
}
catch (BusNotFoundException ex)
{
@ -272,5 +252,19 @@ namespace Bus
}
}
}
private void SortBy(IComparer<IDrawingObject> comparer)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(comparer);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByType_Click(object sender, EventArgs e) => SortBy(new BusCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new BusCompareByColor());
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Bus
{
internal class MapWithSetDoubleDeckerBusGeneric<T, U>
where T : class, IDrawingObject
where T : class, IEquatable<T>, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
@ -92,6 +92,11 @@ namespace Bus
}
}
public void Sort(IComparer<T> comparer)
{
_setBus.SortSet(comparer);
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);

View File

@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class SetDoubleDeckerBusGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
private int BusPlaces = 0;
private readonly int _maxCount;
public SetDoubleDeckerBusGeneric(int count)
@ -33,33 +32,25 @@ namespace Bus
public int Insert(T bus, int position)
{
if (position > _maxCount && position < 0)
{
return -1;
}
if (_places.Contains(bus))
{
throw new ArgumentException($"Объект {bus} уже есть в наборе");
}
if (Count == _maxCount)
if (_places.Contains(bus)) return 0;
if (position < 0) return -1;
if (Count >= _maxCount)
{
throw new StorageOverflowException(_maxCount);
}
_places.Insert(position, bus);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= _maxCount)
{
if (!isCorrectPosition(position))
return null;
}
if (position >= Count || position < 0)
{
var result = this[position];
if (result == null)
throw new BusNotFoundException(position);
}
var result = _places[position];
_places.RemoveAt(position);
return result;
}
@ -68,14 +59,7 @@ namespace Bus
{
get
{
if (position >= 0 && position < _maxCount && position < Count)
{
return _places[position];
}
else
{
return null;
}
return isCorrectPosition(position) && position < Count ? _places[position] : null;
}
set
{
@ -97,5 +81,14 @@ namespace Bus
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}