Vaksman V.D. Lab work 8 #11

Closed
just_valery__ wants to merge 1 commits from Lab8 into Lab7
9 changed files with 230 additions and 29 deletions

View File

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

@ -8,36 +8,62 @@ namespace WarmlyShip
{
internal class DrawningObjectShip : IDrawningObject
{
private DrawningShip _ship = null;
public DrawningObjectShip(DrawningShip ship)
{
_ship = ship;
Ship = ship;
}
public float Step => _ship?.Ship?.Step ?? 0;
public float Step => Ship?.Ship?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _ship?.GetCurrentPosition() ?? default;
return Ship?.GetCurrentPosition() ?? default;
}
public DrawningShip Ship { get; private set; }
public void MoveObject(Direction direction)
{
_ship?.MoveTransport(direction);
Ship?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_ship.SetPosition(x, y, width, height);
Ship.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
_ship.DrawTransport(g);
Ship.DrawTransport(g);
}
public string GetInfo() => _ship?.GetDataForSave();
public string GetInfo() => Ship?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectShip(data.CreateDrawningShip());
public bool Equals(IDrawningObject? other)
{
if (other is not DrawningObjectShip otherShip)
{
return false;
}
var entity = Ship.Ship;
var otherEntity = otherShip.Ship.Ship;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityWarmlyShip entityWarmlyShip &&
otherEntity is EntityWarmlyShip otherEntityWarmlyShip && (
entityWarmlyShip.Pipes != otherEntityWarmlyShip.Pipes ||
entityWarmlyShip.DopColor != otherEntityWarmlyShip.DopColor ||
entityWarmlyShip.FuelCompartment != otherEntityWarmlyShip.FuelCompartment))
{
return false;
}
return true;
}
}
}

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.buttonSortByType = new System.Windows.Forms.Button();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonRight);
@ -72,7 +76,7 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(616, 28);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(250, 566);
this.groupBoxTools.Size = new System.Drawing.Size(250, 666);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -144,7 +148,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(73, 516);
this.buttonLeft.Location = new System.Drawing.Point(73, 616);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 10;
@ -157,7 +161,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(145, 515);
this.buttonRight.Location = new System.Drawing.Point(145, 615);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -170,7 +174,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(109, 479);
this.buttonUp.Location = new System.Drawing.Point(109, 579);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 8;
@ -183,7 +187,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(109, 515);
this.buttonDown.Location = new System.Drawing.Point(109, 615);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 7;
@ -193,7 +197,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 478);
this.buttonShowOnMap.Location = new System.Drawing.Point(9, 532);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(238, 29);
this.buttonShowOnMap.TabIndex = 5;
@ -203,7 +207,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 443);
this.buttonShowStorage.Location = new System.Drawing.Point(9, 497);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(238, 29);
this.buttonShowStorage.TabIndex = 4;
@ -213,7 +217,7 @@
//
// buttonRemoveShip
//
this.buttonRemoveShip.Location = new System.Drawing.Point(6, 399);
this.buttonRemoveShip.Location = new System.Drawing.Point(9, 453);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(238, 29);
this.buttonRemoveShip.TabIndex = 3;
@ -223,7 +227,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 366);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(9, 420);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(238, 27);
@ -231,7 +235,7 @@
//
// buttonAddShip
//
this.buttonAddShip.Location = new System.Drawing.Point(6, 331);
this.buttonAddShip.Location = new System.Drawing.Point(9, 385);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(238, 29);
this.buttonAddShip.TabIndex = 1;
@ -244,7 +248,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(616, 566);
this.pictureBox.Size = new System.Drawing.Size(616, 666);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -270,14 +274,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);
//
@ -289,11 +293,31 @@
//
this.openFileDialog.Filter = "txt file | *.txt";
//
// buttonSortByType
//
this.buttonSortByType.Location = new System.Drawing.Point(9, 315);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(238, 29);
this.buttonSortByType.TabIndex = 13;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(9, 350);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(238, 29);
this.buttonSortByColor.TabIndex = 14;
this.buttonSortByColor.Text = "Сортировать по цвету";
this.buttonSortByColor.UseVisualStyleBackColor = true;
this.buttonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetShips
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(866, 594);
this.ClientSize = new System.Drawing.Size(866, 694);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.menuStrip);
@ -337,5 +361,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private SaveFileDialog saveFileDialog;
private OpenFileDialog openFileDialog;
private Button buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -151,6 +151,11 @@ namespace WarmlyShip
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Ship}", ex.Message, ship);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Удаление объекта
@ -288,5 +293,18 @@ namespace WarmlyShip
}
}
}
private void SortBy(IComparer<IDrawningObject> 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 ShipCompareByType());
private void ButtonSortByColor_Click(object sender, EventArgs e) => SortBy(new ShipCompareByColor());
}
}

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal class MapWithSetShipsGeneric<T, U>
where T : class, IDrawningObject
where T : class, IEquatable<T>, IDrawningObject
where U : AbstractMap
{
/// <summary>
@ -198,5 +198,13 @@ namespace WarmlyShip
_setShips.Insert(DrawningObjectShip.Create(rec) as T);
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setShips.SortSet(comparer);
}
}
}

View File

@ -7,8 +7,8 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal class SetShipsGeneric<T>
where T : class
{
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
/// </summary>
@ -48,15 +48,17 @@ namespace WarmlyShip
/// <param name="ship">Добавляемый корабль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T airplane, int position)
public int Insert(T ship, int position)
{
if (_places.Contains(ship))
throw new ArgumentException($"Объект {ship} уже есть в наборе");
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (!isCorrectPosition(position))
{
return -1;
}
_places.Insert(position, airplane);
_places.Insert(position, ship);
return position;
}
/// <summary>
@ -108,5 +110,17 @@ namespace WarmlyShip
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class ShipCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xShip = x as DrawningObjectShip;
var yShip = y as DrawningObjectShip;
if (xShip == yShip)
{
return 0;
}
if (xShip == null)
{
return 1;
}
if (yShip == null)
{
return -1;
}
var xEntity = xShip.Ship.Ship;
var yEntity = yShip.Ship.Ship;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 ||
xEntity is not EntityWarmlyShip xEntityWarmlyShip ||
yEntity is not EntityWarmlyShip yEntityWarmlyShip)
{
return colorWeight;
}
return xEntityWarmlyShip.DopColor.ToArgb().CompareTo(yEntityWarmlyShip.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 WarmlyShip
{
internal class ShipCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xShip = x as DrawningObjectShip;
var yShip = y as DrawningObjectShip;
if (xShip == yShip)
{
return 0;
}
if (xShip == null)
{
return 1;
}
if (yShip == null)
{
return -1;
}
if (xShip.Ship.GetType().Name != yShip.Ship.GetType().Name)
{
if (xShip.Ship.GetType() == typeof(DrawningShip))
{
return -1;
}
return 1;
}
var speedCompare = xShip.Ship.Ship.Speed.CompareTo(yShip.Ship.Ship.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.Ship.Ship.Weight.CompareTo(yShip.Ship.Ship.Weight);
}
}
}