Nevaeva K. A. LabWork08 #8

Closed
ksenia_nevaeva wants to merge 2 commits from Lab_8 into Lab_7
10 changed files with 278 additions and 20 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ContainerShip
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -113,7 +113,33 @@ namespace ContainerShip
_drawingObject.DrawingObject(gr);
return bmp;
}
public bool Equals(AbstractMap? other)
{
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 (other._map[i, j] != _map[j, i]) 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

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class CompareByColor : 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 xShip = x as DrawingObjectShip;
var yShip = y as DrawingObjectShip;
var rgbCompareR = xShip.GetShip.Ship.BodyColor.R.CompareTo(yShip.GetShip.Ship.BodyColor.R);
if (rgbCompareR != 0)
{
return rgbCompareR;
}
var rgbCompareG = xShip.GetShip.Ship.BodyColor.G.CompareTo(yShip.GetShip.Ship.BodyColor.G);
if (rgbCompareG != 0)
{
return rgbCompareG;
}
var rgbCompareB = xShip.GetShip.Ship.BodyColor.B.CompareTo(yShip.GetShip.Ship.BodyColor.B);
if (rgbCompareB != 0)
{
return rgbCompareB;
}
if (xShip.GetShip.Ship is EntityContainerShip xContainerShip && yShip.GetShip.Ship is EntityContainerShip yContainerShip)
{
var rgbDopCompareR = xContainerShip.DopColor.R.CompareTo(yContainerShip.DopColor.R);
if (rgbDopCompareR != 0)
{
return rgbDopCompareR;
}
var rgbDopCompareB = xContainerShip.DopColor.G.CompareTo(yContainerShip.DopColor.G);
if (rgbDopCompareB != 0)
{
return rgbDopCompareB;
}
var rgbDopCompareG = xContainerShip.DopColor.B.CompareTo(yContainerShip.DopColor.B);
if (rgbDopCompareG != 0)
{
return rgbDopCompareG;
}
return 0;
}
return 0;
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal class CompareByType: 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 xShip = x as DrawingObjectShip;
var yShip = y as DrawingObjectShip;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
if (xShip.GetShip.GetType().Name != yShip.GetShip.GetType().Name)
{
if (xShip.GetShip.GetType().Name == "DrawingShip")
{
return -1;
}
return 1;
}
var speedCompare = xShip.GetShip.Ship.Speed.CompareTo(yShip.GetShip.Ship.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetShip.Ship.Weight.CompareTo(yShip.GetShip.Ship.Weight);
}
}
}

View File

@ -9,12 +9,11 @@ namespace ContainerShip
internal class DrawingObjectShip : IDrawingObject
{
private DrawingShip _ship = null;
public DrawingObjectShip(DrawingShip ship)
{
_ship = ship;
}
public DrawingShip GetShip => _ship;
public float Step => _ship?.Ship?.Step ?? 0;
public (float Left, float Top, float Right, float Bottom) GetCurrentPosition()
@ -45,5 +44,49 @@ namespace ContainerShip
}
public string GetInfo() => _ship?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectShip(data.CreateDrawingShip());
public bool Equals(IDrawingObject? other)
{
if (other == null)
{
return false;
}
var otherShip = other as DrawingObjectShip;
if (otherShip == null)
{
return false;
}
var ship = _ship.Ship;
var otherShipShip = otherShip._ship.Ship;
if (ship.Speed != otherShipShip.Speed)
{
return false;
}
if (ship.Weight != otherShipShip.Weight)
{
return false;
}
if (ship.BodyColor != otherShipShip.BodyColor)
{
return false;
}
if (ship is EntityContainerShip containerShip && otherShipShip is EntityContainerShip otherContainerShip)
{
if (containerShip.DopColor != otherContainerShip.DopColor)
{
return false;
}
if (containerShip.Containers != otherContainerShip.Containers)
{
return false;
}
if (containerShip.Crane != otherContainerShip.Crane)
{
return false;
}
}
else if (ship is EntityContainerShip || otherShipShip is EntityContainerShip) return false;
return true;
}
}
}

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
//
// groupBox1
//
this.groupBox1.Controls.Add(this.ButtonSortByType);
this.groupBox1.Controls.Add(this.ButtonSortByColor);
this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.ButtonAddShip);
this.groupBox1.Controls.Add(this.buttonUp);
@ -70,9 +74,9 @@
this.groupBox1.Controls.Add(this.ButtonRemoveShip);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(553, 33);
this.groupBox1.Location = new System.Drawing.Point(559, 33);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(300, 709);
this.groupBox1.Size = new System.Drawing.Size(300, 799);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -154,7 +158,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(131, 591);
this.buttonUp.Location = new System.Drawing.Point(131, 673);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(50, 50);
this.buttonUp.TabIndex = 9;
@ -166,7 +170,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(187, 647);
this.buttonRight.Location = new System.Drawing.Point(187, 729);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(50, 50);
this.buttonRight.TabIndex = 8;
@ -178,7 +182,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(131, 647);
this.buttonDown.Location = new System.Drawing.Point(131, 729);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(50, 50);
this.buttonDown.TabIndex = 7;
@ -190,7 +194,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(75, 647);
this.buttonLeft.Location = new System.Drawing.Point(75, 729);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(50, 50);
this.buttonLeft.TabIndex = 6;
@ -240,7 +244,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 33);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(553, 709);
this.pictureBox.Size = new System.Drawing.Size(559, 799);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -251,7 +255,7 @@
this.файлToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(853, 33);
this.menuStrip.Size = new System.Drawing.Size(859, 33);
this.menuStrip.TabIndex = 2;
this.menuStrip.Text = "menuStrip1";
//
@ -287,11 +291,31 @@
//
this.saveFileDialog.Filter = "txt file|*.txt";
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(6, 594);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(288, 34);
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(6, 634);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(288, 34);
this.ButtonSortByType.TabIndex = 14;
this.ButtonSortByType.Text = "Сорт. по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// FormMapWithSetShips
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(853, 742);
this.ClientSize = new System.Drawing.Size(859, 832);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip);
@ -335,5 +359,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortByType;
private Button ButtonSortByColor;
}
}

View File

@ -81,7 +81,11 @@ namespace ContainerShip
MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
}
catch(StorageOverflowException ex)
catch (ArgumentException ex)
{
MessageBox.Show($"Идентичный корабль уже есть на форме: {ex.Message}");
}
catch (StorageOverflowException ex)
{
MessageBox.Show(ex.Message);
_logger.LogWarning($"Ошибка: {ex.Message}");
@ -294,5 +298,25 @@ namespace ContainerShip
}
}
}
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new CompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new CompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace ContainerShip
{
internal class MapWithSetShipsGeneric<T, U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -167,6 +167,10 @@ namespace ContainerShip
i++;
}
}
public void Sort(IComparer<T> comparer)
{
_setShips.SortSet(comparer);
}
/// <summary>
/// Перегрузка операторов
/// </summary>

View File

@ -16,7 +16,7 @@ namespace ContainerShip
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetShipsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
readonly Dictionary<string, MapWithSetShipsGeneric <IDrawingObject, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace ContainerShip
{
internal class SetShipsGeneric<T>
where T: class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -44,7 +44,14 @@ namespace ContainerShip
/// <returns></returns>
public int Insert(T ship, int position)
{
if(position < 0 || position >= _maxCount)
foreach (var checkShip in _places)
{
if (checkShip.Equals(ship)) {
throw new ArgumentException();
}
}
if (position < 0 || position >= _maxCount)
{
throw new ShipNotFoundException(position);
}
@ -111,5 +118,13 @@ namespace ContainerShip
}
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}