From 536b1d422e14a67e691703a30697a58a9dd65ee6 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 20 Dec 2023 00:16:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=208?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/DrawningShipEqutables.cs | 60 +++++++++++++++++++ .../Battleship/FormShipCollection.Designer.cs | 48 +++++++++++---- Battleship/Battleship/FormShipCollection.cs | 28 ++++++++- Battleship/Battleship/SetGeneric.cs | 13 ++-- Battleship/Battleship/ShipCompareByColor.cs | 49 +++++++++++++++ Battleship/Battleship/ShipCompareByType.cs | 36 +++++++++++ .../Battleship/ShipGenericCollection.cs | 8 +-- Battleship/Battleship/ShipsCollectionInfo.cs | 30 ++++++++++ Battleship/Battleship/ShipsGenericStorage.cs | 27 ++++----- 9 files changed, 264 insertions(+), 35 deletions(-) create mode 100644 Battleship/Battleship/DrawningShipEqutables.cs create mode 100644 Battleship/Battleship/ShipCompareByColor.cs create mode 100644 Battleship/Battleship/ShipCompareByType.cs create mode 100644 Battleship/Battleship/ShipsCollectionInfo.cs diff --git a/Battleship/Battleship/DrawningShipEqutables.cs b/Battleship/Battleship/DrawningShipEqutables.cs new file mode 100644 index 0000000..732f51d --- /dev/null +++ b/Battleship/Battleship/DrawningShipEqutables.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Battleship.DrawningObjects; +using Battleship.Entities; +using System.Diagnostics.CodeAnalysis; + +namespace Battleship.Generics +{ + internal class DrawningShipEqutables: IEqualityComparer + { + public bool Equals(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return false; + } + if (x.EntityShip.Speed != y.EntityShip.Speed) + { + return false; + } + if (x.EntityShip.Weight != y.EntityShip.Weight) + { + return false; + } + if (x.EntityShip.BodyColor != y.EntityShip.BodyColor) + { + return false; + } + if (x is DrawningBattleship && y is DrawningBattleship) + { + EntityBattleship EntityX = (EntityBattleship)x.EntityShip; + EntityBattleship EntityY = (EntityBattleship)y.EntityShip; + + if (EntityX.Tower != EntityY.Tower) + return false; + if(EntityX.Section != EntityY.Section) + return false; + if(EntityX.AdditionalColor != EntityY.AdditionalColor) + return false; + } + return true; + } + + public int GetHashCode([DisallowNull] DrawningShip obj) + { + return obj.GetHashCode(); + } + } +} diff --git a/Battleship/Battleship/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs index cae2aff..52069b7 100644 --- a/Battleship/Battleship/FormShipCollection.Designer.cs +++ b/Battleship/Battleship/FormShipCollection.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.groupBoxBattleShip = new System.Windows.Forms.GroupBox(); + this.buttonSortByColor = new System.Windows.Forms.Button(); + this.buttonSortByType = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.textBoxStorageName = new System.Windows.Forms.TextBox(); this.listBoxStorages = new System.Windows.Forms.ListBox(); @@ -55,6 +57,8 @@ // this.groupBoxBattleShip.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.groupBoxBattleShip.Controls.Add(this.buttonSortByColor); + this.groupBoxBattleShip.Controls.Add(this.buttonSortByType); this.groupBoxBattleShip.Controls.Add(this.groupBox1); this.groupBoxBattleShip.Controls.Add(this.maskedTextBoxNumber); this.groupBoxBattleShip.Controls.Add(this.buttonRefreshCollection); @@ -68,15 +72,35 @@ this.groupBoxBattleShip.TabStop = false; this.groupBoxBattleShip.Text = "Инструменты"; // + // buttonSortByColor + // + this.buttonSortByColor.Location = new System.Drawing.Point(24, 267); + this.buttonSortByColor.Name = "buttonSortByColor"; + this.buttonSortByColor.Size = new System.Drawing.Size(146, 23); + this.buttonSortByColor.TabIndex = 6; + this.buttonSortByColor.Text = "Сортировка по цвету"; + this.buttonSortByColor.UseVisualStyleBackColor = true; + this.buttonSortByColor.Click += new System.EventHandler(this.buttonSortByColor_Click); + // + // buttonSortByType + // + this.buttonSortByType.Location = new System.Drawing.Point(24, 238); + this.buttonSortByType.Name = "buttonSortByType"; + this.buttonSortByType.Size = new System.Drawing.Size(146, 23); + this.buttonSortByType.TabIndex = 5; + this.buttonSortByType.Text = "Сортировка по типу"; + this.buttonSortByType.UseVisualStyleBackColor = true; + this.buttonSortByType.Click += new System.EventHandler(this.buttonSortByType_Click); + // // groupBox1 // this.groupBox1.Controls.Add(this.textBoxStorageName); this.groupBox1.Controls.Add(this.listBoxStorages); this.groupBox1.Controls.Add(this.buttonAddObject); this.groupBox1.Controls.Add(this.buttonDelObject); - this.groupBox1.Location = new System.Drawing.Point(6, 61); + this.groupBox1.Location = new System.Drawing.Point(6, 46); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(174, 223); + this.groupBox1.Size = new System.Drawing.Size(174, 186); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Набор"; @@ -92,9 +116,9 @@ // this.listBoxStorages.FormattingEnabled = true; this.listBoxStorages.ItemHeight = 15; - this.listBoxStorages.Location = new System.Drawing.Point(18, 95); + this.listBoxStorages.Location = new System.Drawing.Point(18, 80); this.listBoxStorages.Name = "listBoxStorages"; - this.listBoxStorages.Size = new System.Drawing.Size(147, 79); + this.listBoxStorages.Size = new System.Drawing.Size(147, 64); this.listBoxStorages.TabIndex = 3; this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); // @@ -110,7 +134,7 @@ // // buttonDelObject // - this.buttonDelObject.Location = new System.Drawing.Point(18, 181); + this.buttonDelObject.Location = new System.Drawing.Point(18, 150); this.buttonDelObject.Name = "buttonDelObject"; this.buttonDelObject.Size = new System.Drawing.Size(147, 23); this.buttonDelObject.TabIndex = 1; @@ -120,7 +144,7 @@ // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(24, 335); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(24, 345); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23); this.maskedTextBoxNumber.TabIndex = 3; @@ -137,9 +161,9 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(23, 364); + this.buttonRemoveShip.Location = new System.Drawing.Point(23, 374); this.buttonRemoveShip.Name = "buttonRemoveShip"; - this.buttonRemoveShip.Size = new System.Drawing.Size(147, 36); + this.buttonRemoveShip.Size = new System.Drawing.Size(147, 26); this.buttonRemoveShip.TabIndex = 1; this.buttonRemoveShip.Text = "Удалить корабль"; this.buttonRemoveShip.UseVisualStyleBackColor = true; @@ -147,7 +171,7 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(23, 290); + this.buttonAddShip.Location = new System.Drawing.Point(23, 309); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(147, 30); this.buttonAddShip.TabIndex = 0; @@ -177,14 +201,14 @@ // сохранениеToolStripMenuItem // this.сохранениеToolStripMenuItem.Name = "сохранениеToolStripMenuItem"; - this.сохранениеToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.сохранениеToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.сохранениеToolStripMenuItem.Text = "Сохранение"; this.сохранениеToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // загрузкаToolStripMenuItem // this.загрузкаToolStripMenuItem.Name = "загрузкаToolStripMenuItem"; - this.загрузкаToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.загрузкаToolStripMenuItem.Size = new System.Drawing.Size(141, 22); this.загрузкаToolStripMenuItem.Text = "Загрузка"; this.загрузкаToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // @@ -247,5 +271,7 @@ private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; private ToolStripMenuItem загрузкаToolStripMenuItem; + private Button buttonSortByColor; + private Button buttonSortByType; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs index e9298e0..2775ce1 100644 --- a/Battleship/Battleship/FormShipCollection.cs +++ b/Battleship/Battleship/FormShipCollection.cs @@ -86,7 +86,7 @@ namespace Battleship listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { - listBoxStorages.Items.Add(_storage.Keys[i]); + listBoxStorages.Items.Add(_storage.Keys[i].Name); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) @@ -196,7 +196,12 @@ namespace Battleship MessageBox.Show(ex.Message); MessageBox.Show("Не удалось добавить объект"); _logger.LogWarning($"{ex.Message} в наборе {listBoxStorages.SelectedItem.ToString()}"); - } + } + catch (ArgumentException ex) + { + MessageBox.Show(ex.Message); + _logger.LogWarning($"Не удалось добавить объект: {ex.Message}"); + } } private void buttonRemoveShip_Click(object sender, EventArgs e) @@ -258,6 +263,25 @@ namespace Battleship } pictureBoxCollection.Image = obj.ShowShips(); } + + private void buttonSortByType_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByType()); + private void buttonSortByColor_Click(object sender, EventArgs e) => CompareShips(new ShipCompareByColor()); + private void CompareShips(IComparer comparer) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + obj.Sort(comparer); + pictureBoxCollection.Image = obj.ShowShips(); + } + } } diff --git a/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs index 270d0cc..0523939 100644 --- a/Battleship/Battleship/SetGeneric.cs +++ b/Battleship/Battleship/SetGeneric.cs @@ -14,24 +14,29 @@ namespace Battleship.Generics private readonly List _places; public int Count => _places.Count; private readonly int _maxCount; + + public void SortSet(IComparer comparer) => _places.Sort(comparer); public SetGeneric(int count) { _maxCount = count; _places = new List(_maxCount); } - public bool Insert(T ship) + public bool Insert(T ship, IEqualityComparer? equal = null) { - return Insert(ship, 0); + return Insert(ship, 0, equal); } - public bool Insert(T ship, int position) + public bool Insert(T ship, int position, IEqualityComparer? equal = null) { if (position < 0 || position >= _maxCount) throw new ShipNotFoundException(position); if (Count >= _maxCount) - throw new StorageOverflowException(_maxCount); + throw new StorageOverflowException(_maxCount); + + if (equal != null && _places.Contains(ship, equal)) + throw new ArgumentException("Данный объект уже есть в коллекции"); _places.Insert(0, ship); return true; } diff --git a/Battleship/Battleship/ShipCompareByColor.cs b/Battleship/Battleship/ShipCompareByColor.cs new file mode 100644 index 0000000..2c098d4 --- /dev/null +++ b/Battleship/Battleship/ShipCompareByColor.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Battleship.DrawningObjects; +using Battleship.Entities; + +namespace Battleship.Generics +{ + internal class ShipCompareByColor: IComparer + { + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + var bodyColorCompare = x.EntityShip.BodyColor.Name.CompareTo(y.EntityShip.BodyColor.Name); + if (bodyColorCompare != 0) + { + return bodyColorCompare; + } + if (x.EntityShip is EntityBattleship xEntityBattleship && y.EntityShip is EntityBattleship yEntityBattleship) + { + var BodyColorCompare = xEntityBattleship.BodyColor.Name.CompareTo(yEntityBattleship.BodyColor.Name); + if (BodyColorCompare != 0) + { + return BodyColorCompare; + } + var AdditionalColorCompare = xEntityBattleship.AdditionalColor.Name.CompareTo(yEntityBattleship.AdditionalColor.Name); + if (AdditionalColorCompare != 0) + { + return AdditionalColorCompare; + } + } + var speedCompare = x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } + } +} diff --git a/Battleship/Battleship/ShipCompareByType.cs b/Battleship/Battleship/ShipCompareByType.cs new file mode 100644 index 0000000..8284da2 --- /dev/null +++ b/Battleship/Battleship/ShipCompareByType.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Battleship.DrawningObjects; + + +namespace Battleship.Generics +{ + internal class ShipCompareByType: IComparer + { + public int Compare(DrawningShip? x, DrawningShip? y) + { + if (x == null || x.EntityShip == null) + { + throw new ArgumentNullException(nameof(x)); + } + if (y == null || y.EntityShip == null) + { + throw new ArgumentNullException(nameof(y)); + } + if (x.GetType().Name != y.GetType().Name) + { + return x.GetType().Name.CompareTo(y.GetType().Name); + } + var speedCompare = + x.EntityShip.Speed.CompareTo(y.EntityShip.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return x.EntityShip.Weight.CompareTo(y.EntityShip.Weight); + } + } +} diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs index 2acbe24..7bea83b 100644 --- a/Battleship/Battleship/ShipGenericCollection.cs +++ b/Battleship/Battleship/ShipGenericCollection.cs @@ -21,6 +21,7 @@ namespace Battleship.Generics /// Получение объектов коллекции /// public IEnumerable GetShips => _collection.GetShips(); + public void Sort(IComparer comparer) => _collection.SortSet(comparer); public ShipGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -31,12 +32,11 @@ namespace Battleship.Generics } public static bool operator +(ShipGenericCollection? collect, T? obj) { - if (obj != null && collect != null) + if (obj == null) { - collect._collection.Insert(obj); - return true; + return false; } - return false; + return collect?._collection.Insert(obj, new DrawningShipEqutables()) ?? false; } public static T? operator -(ShipGenericCollection? collect, int pos) diff --git a/Battleship/Battleship/ShipsCollectionInfo.cs b/Battleship/Battleship/ShipsCollectionInfo.cs new file mode 100644 index 0000000..3b53814 --- /dev/null +++ b/Battleship/Battleship/ShipsCollectionInfo.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship.Generics +{ + internal class ShipsCollectionInfo : IEquatable + { + public string Name { get; private set; } + public string Description { get; private set; } + public ShipsCollectionInfo(string name, string description) + { + Name = name; + Description = description; + } + public bool Equals(ShipsCollectionInfo? other) + { + if (Name == other?.Name) + return true; + + return false; + } + public override int GetHashCode() + { + return Name.GetHashCode(); + } + } +} diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs index a948da3..1c7fe92 100644 --- a/Battleship/Battleship/ShipsGenericStorage.cs +++ b/Battleship/Battleship/ShipsGenericStorage.cs @@ -11,10 +11,10 @@ namespace Battleship.Generics { internal class ShipsGenericStorage { - readonly Dictionary> _shipStorages; - public List Keys => _shipStorages.Keys.ToList(); + public List Keys => _shipStorages.Keys.ToList(); private readonly int _pictureWidth; @@ -34,7 +34,7 @@ namespace Battleship.Generics public ShipsGenericStorage(int pictureWidth, int pictureHeight) { - _shipStorages = new Dictionary>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; @@ -42,16 +42,15 @@ namespace Battleship.Generics public void AddSet(string name) { - if (_shipStorages.ContainsKey(name)) - return; - _shipStorages[name] = new ShipGenericCollection(_pictureWidth, _pictureHeight); + if (!_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty))) + _shipStorages.Add(new ShipsCollectionInfo(name, string.Empty), + new ShipGenericCollection(_pictureWidth, _pictureHeight)); } public void DelSet(string name) { - if (!_shipStorages.ContainsKey(name)) - return; - _shipStorages.Remove(name); + if (_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty))) + _shipStorages.Remove(new ShipsCollectionInfo(name, string.Empty)); } public ShipGenericCollection? @@ -59,8 +58,8 @@ namespace Battleship.Generics { get { - if (_shipStorages.ContainsKey(ind)) - return _shipStorages[ind]; + if (_shipStorages.ContainsKey(new ShipsCollectionInfo(ind, string.Empty))) + return _shipStorages[new ShipsCollectionInfo(ind, string.Empty)]; return null; } } @@ -76,14 +75,14 @@ namespace Battleship.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _shipStorages) + foreach (KeyValuePair> record in _shipStorages) { StringBuilder records = new(); foreach (DrawningShip? elem in record.Value.GetShips) { records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); } - data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + data.AppendLine($"{record.Key.Name}{_separatorForKeyValue}{records}"); } if (data.Length == 0) { @@ -155,7 +154,7 @@ namespace Battleship.Generics } } } - _shipStorages.Add(record[0], collection); + _shipStorages.Add(new ShipsCollectionInfo(record[0], string.Empty), collection); } } }