готовая 8 лаба

This commit is contained in:
Казначеева Елизавета 2023-12-20 00:16:39 +04:00
parent 83035ffc77
commit 536b1d422e
9 changed files with 264 additions and 35 deletions

View File

@ -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<DrawningShip?>
{
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();
}
}
}

View File

@ -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;
}
}

View File

@ -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))
@ -197,6 +197,11 @@ namespace Battleship
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<DrawningShip?> 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();
}
}
}

View File

@ -14,24 +14,29 @@ namespace Battleship.Generics
private readonly List<T?> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public void SortSet(IComparer<T?> comparer) => _places.Sort(comparer);
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(_maxCount);
}
public bool Insert(T ship)
public bool Insert(T ship, IEqualityComparer<T?>? 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<T>? equal = null)
{
if (position < 0 || position >= _maxCount)
throw new ShipNotFoundException(position);
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
if (equal != null && _places.Contains(ship, equal))
throw new ArgumentException("Данный объект уже есть в коллекции");
_places.Insert(0, ship);
return true;
}

View File

@ -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<DrawningShip?>
{
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);
}
}
}

View File

@ -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<DrawningShip?>
{
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);
}
}
}

View File

@ -21,6 +21,7 @@ namespace Battleship.Generics
/// Получение объектов коллекции
/// </summary>
public IEnumerable<T?> GetShips => _collection.GetShips();
public void Sort(IComparer<T?> 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<T, U>? 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<T, U>? collect, int pos)

View File

@ -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<ShipsCollectionInfo>
{
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();
}
}
}

View File

@ -11,10 +11,10 @@ namespace Battleship.Generics
{
internal class ShipsGenericStorage
{
readonly Dictionary<string, ShipGenericCollection<DrawningShip,
readonly Dictionary<ShipsCollectionInfo, ShipGenericCollection<DrawningShip,
DrawningObjectShip>> _shipStorages;
public List<string> Keys => _shipStorages.Keys.ToList();
public List<ShipsCollectionInfo> Keys => _shipStorages.Keys.ToList();
private readonly int _pictureWidth;
@ -34,7 +34,7 @@ namespace Battleship.Generics
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string,
_shipStorages = new Dictionary<ShipsCollectionInfo,
ShipGenericCollection<DrawningShip, DrawningObjectShip>>();
_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<DrawningShip, DrawningObjectShip>(_pictureWidth, _pictureHeight);
if (!_shipStorages.ContainsKey(new ShipsCollectionInfo(name, string.Empty)))
_shipStorages.Add(new ShipsCollectionInfo(name, string.Empty),
new ShipGenericCollection<DrawningShip, DrawningObjectShip>(_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<DrawningShip, DrawningObjectShip>?
@ -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<string, ShipGenericCollection<DrawningShip, DrawningObjectShip>> record in _shipStorages)
foreach (KeyValuePair<ShipsCollectionInfo, ShipGenericCollection<DrawningShip, DrawningObjectShip>> 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);
}
}
}