From 4335e642a90646dafaa95d88239e8a7fbe8d30ce Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:14:41 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=92=D0=BE=D1=81=D1=8C=D0=BC=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=20=D0=A1?= =?UTF-8?q?=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ContainerShip/ContainerShip/AbstractMap.cs | 30 ++++++++- ContainerShip/ContainerShip/CompareByColor.cs | 65 +++++++++++++++++++ ContainerShip/ContainerShip/CompareByType.cs | 55 ++++++++++++++++ .../ContainerShip/DrawingObjectShip.cs | 47 +++++++++++++- .../FormMapWithSetShips.Designer.cs | 44 ++++++++++--- .../ContainerShip/FormMapWithSetShips.cs | 26 +++++++- ContainerShip/ContainerShip/IDrawingObject.cs | 2 +- .../ContainerShip/MapWithSetShipsGeneric.cs | 6 +- ContainerShip/ContainerShip/MapsCollection.cs | 2 +- .../ContainerShip/SetShipsGeneric.cs | 23 ++++++- 10 files changed, 280 insertions(+), 20 deletions(-) create mode 100644 ContainerShip/ContainerShip/CompareByColor.cs create mode 100644 ContainerShip/ContainerShip/CompareByType.cs diff --git a/ContainerShip/ContainerShip/AbstractMap.cs b/ContainerShip/ContainerShip/AbstractMap.cs index 22310a9..7269414 100644 --- a/ContainerShip/ContainerShip/AbstractMap.cs +++ b/ContainerShip/ContainerShip/AbstractMap.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { 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); diff --git a/ContainerShip/ContainerShip/CompareByColor.cs b/ContainerShip/ContainerShip/CompareByColor.cs new file mode 100644 index 0000000..2d3b04f --- /dev/null +++ b/ContainerShip/ContainerShip/CompareByColor.cs @@ -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 + { + 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; + } + } +} + diff --git a/ContainerShip/ContainerShip/CompareByType.cs b/ContainerShip/ContainerShip/CompareByType.cs new file mode 100644 index 0000000..7c40885 --- /dev/null +++ b/ContainerShip/ContainerShip/CompareByType.cs @@ -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 + { + 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); + } + } +} diff --git a/ContainerShip/ContainerShip/DrawingObjectShip.cs b/ContainerShip/ContainerShip/DrawingObjectShip.cs index f322be6..41d7cc3 100644 --- a/ContainerShip/ContainerShip/DrawingObjectShip.cs +++ b/ContainerShip/ContainerShip/DrawingObjectShip.cs @@ -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; + } } } diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs index 926fae2..8749ef7 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs index 19aeb7f..e541213 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -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(); + } } } diff --git a/ContainerShip/ContainerShip/IDrawingObject.cs b/ContainerShip/ContainerShip/IDrawingObject.cs index 7a4f72c..0f2b95b 100644 --- a/ContainerShip/ContainerShip/IDrawingObject.cs +++ b/ContainerShip/ContainerShip/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal interface IDrawingObject + internal interface IDrawingObject: IEquatable { /// /// Шаг перемещения объекта diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs index 4afb290..3004dfe 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ContainerShip { internal class MapWithSetShipsGeneric - where T : class, IDrawingObject + where T : class, IDrawingObject, IEquatable where U : AbstractMap { /// @@ -167,6 +167,10 @@ namespace ContainerShip i++; } } + public void Sort(IComparer comparer) + { + _setShips.SortSet(comparer); + } /// /// Перегрузка операторов /// diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs index 094674c..4358174 100644 --- a/ContainerShip/ContainerShip/MapsCollection.cs +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -16,7 +16,7 @@ namespace ContainerShip /// /// Словарь (хранилище) с картами /// - readonly Dictionary> _mapStorages; + readonly Dictionary> _mapStorages; /// /// Возвращение списка названий карт /// diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index d42a56b..92ee32c 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ContainerShip { internal class SetShipsGeneric - where T: class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -44,7 +44,16 @@ namespace ContainerShip /// public int Insert(T ship, int position) { - if(position < 0 || position >= _maxCount) + foreach (var checkShip in _places) + { + if (checkShip.Equals(ship)) { + throw new ArgumentException(); + return -1; + + } + + } + if (position < 0 || position >= _maxCount) { throw new ShipNotFoundException(position); } @@ -111,5 +120,13 @@ namespace ContainerShip } } } - } + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } +} } -- 2.25.1 From 28981e02a6248bd13320e7c191ea02a2812cbe7c Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:19:34 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=B2=D0=BE=D0=B4=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= =?UTF-8?q?=20ArgumentException.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ContainerShip/ContainerShip/SetShipsGeneric.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index 92ee32c..8f8ed68 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -48,8 +48,6 @@ namespace ContainerShip { if (checkShip.Equals(ship)) { throw new ArgumentException(); - return -1; - } } -- 2.25.1