Zhimolostnova A.V. Lab work 8 #8

Closed
AnnZhimol wants to merge 4 commits from LabRab_8 into LabRab_7
10 changed files with 274 additions and 25 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Warship
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
@ -148,5 +148,30 @@ namespace Warship
protected abstract void GenerateMap();
protected abstract void DrawWaterPart(Graphics gr, int i, int j);
protected abstract void DrawLandPart(Graphics gr, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null)
return false;
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 (_map[i, j] != other._map[i, j])
return false;
return true;
}
}
}

View File

@ -16,6 +16,8 @@ namespace Warship
}
public float Step => _warship?.Warship?.Step ?? 0;
public DrawingWarship GetWarship => _warship;
void IDrawingObject.DrawingObject(Graphics g)
{
_warship.DrawTransport(g);
@ -39,5 +41,48 @@ namespace Warship
public string GetInfo() => _warship?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectWarship(data.CreateDrawingWarship());
public bool Equals(IDrawingObject? other)
{
if (other == null)
return false;
var otherWarship = other as DrawingObjectWarship;
if (otherWarship == null)
return false;
var warship = _warship.Warship;
var otherWarshipWarship = otherWarship._warship.Warship;
if (warship.GetType() != otherWarshipWarship.GetType())
return false;
if (warship.Speed != otherWarshipWarship.Speed)
return false;
if (warship.Weight != otherWarshipWarship.Weight)
return false;
if (warship.BodyColor != otherWarshipWarship.BodyColor)
return false;
if(warship is EntityAdvancedWarship adv && otherWarshipWarship is EntityAdvancedWarship otherAdv)
{
if (adv.DopColor != otherAdv.DopColor)
return false;
if (adv.Antenna != otherAdv.Antenna)
return false;
if (adv.Helipad != otherAdv.Helipad)
return false;
if (adv.Missile != otherAdv.Missile)
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.ButtonSortType = new System.Windows.Forms.Button();
this.ButtonSortColor = 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.ButtonSortColor);
this.groupBoxTools.Controls.Add(this.ButtonSortType);
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRemoveWarship);
@ -72,7 +76,7 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(859, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(204, 628);
this.groupBoxTools.Size = new System.Drawing.Size(204, 677);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
@ -142,7 +146,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 358);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 344);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -151,7 +155,7 @@
//
// buttonRemoveWarship
//
this.buttonRemoveWarship.Location = new System.Drawing.Point(15, 387);
this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 373);
this.buttonRemoveWarship.Name = "buttonRemoveWarship";
this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveWarship.TabIndex = 3;
@ -161,7 +165,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(15, 452);
this.buttonShowStorage.Location = new System.Drawing.Point(20, 427);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 4;
@ -174,7 +178,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Warship.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(91, 578);
this.buttonDown.Location = new System.Drawing.Point(92, 641);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -186,7 +190,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Warship.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(127, 578);
this.buttonRight.Location = new System.Drawing.Point(128, 641);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -198,7 +202,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Warship.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(55, 578);
this.buttonLeft.Location = new System.Drawing.Point(56, 641);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -210,7 +214,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Warship.Properties.Resources.arrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(91, 542);
this.buttonUp.Location = new System.Drawing.Point(92, 605);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -219,7 +223,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 493);
this.buttonShowOnMap.Location = new System.Drawing.Point(20, 468);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 5;
@ -229,7 +233,7 @@
//
// buttonAddWarship
//
this.buttonAddWarship.Location = new System.Drawing.Point(15, 317);
this.buttonAddWarship.Location = new System.Drawing.Point(20, 303);
this.buttonAddWarship.Name = "buttonAddWarship";
this.buttonAddWarship.Size = new System.Drawing.Size(175, 35);
this.buttonAddWarship.TabIndex = 1;
@ -242,7 +246,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 24);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(859, 628);
this.pictureBox.Size = new System.Drawing.Size(859, 677);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -267,14 +271,14 @@
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(141, 22);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
@ -286,11 +290,31 @@
//
this.SaveFileDialog.Filter = "txt file | *.txt";
//
// ButtonSortType
//
this.ButtonSortType.Location = new System.Drawing.Point(22, 523);
this.ButtonSortType.Name = "ButtonSortType";
this.ButtonSortType.Size = new System.Drawing.Size(173, 35);
this.ButtonSortType.TabIndex = 12;
this.ButtonSortType.Text = "Сортировать по типу";
this.ButtonSortType.UseVisualStyleBackColor = true;
this.ButtonSortType.Click += new System.EventHandler(this.ButtonSortType_Click);
//
// ButtonSortColor
//
this.ButtonSortColor.Location = new System.Drawing.Point(22, 564);
this.ButtonSortColor.Name = "ButtonSortColor";
this.ButtonSortColor.Size = new System.Drawing.Size(173, 35);
this.ButtonSortColor.TabIndex = 13;
this.ButtonSortColor.Text = "Сортировать по цвету";
this.ButtonSortColor.UseVisualStyleBackColor = true;
this.ButtonSortColor.Click += new System.EventHandler(this.ButtonSortColor_Click);
//
// FormMapWithSetWarships
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1063, 652);
this.ClientSize = new System.Drawing.Size(1063, 701);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Controls.Add(this.MenuStrip);
@ -334,5 +358,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog OpenFileDialog;
private SaveFileDialog SaveFileDialog;
private Button ButtonSortColor;
private Button ButtonSortType;
}
}

View File

@ -64,10 +64,9 @@ namespace Warship
{
return;
}
DrawingObjectWarship warship = new(drawingWarship);
try
{
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship != -1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawingObjectWarship(drawingWarship) != -1)
{
_logger.LogInformation("Новый объект добавлен");
MessageBox.Show("Объект добавлен");
@ -248,5 +247,23 @@ namespace Warship
}
}
}
private void ButtonSortType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
return;
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new WarshipCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
return;
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new WarshipCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

@ -66,4 +66,7 @@
<metadata name="SaveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>265, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Warship
{
internal interface IDrawingObject
internal interface IDrawingObject : IEquatable<IDrawingObject>
{
public float Step { get; }
void SetObject(int x, int y, int width, int height);

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Warship
{
internal class MapWithSetWarshipsGeneric<T,U>
where T : class, IDrawingObject
where T : class, IDrawingObject, IEquatable<T>
where U : AbstractMap
{
private readonly int _pictureWidth;
@ -83,6 +83,11 @@ namespace Warship
}
}
public void Sort(IComparer<T> comparer)
{
_setWarship.SortSet(comparer);
}
public void Shaking()
{
int j = _setWarship.Count - 1;

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Warship
{
internal class SetWarshipsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
@ -23,17 +23,22 @@ namespace Warship
public int Insert(T warship)
{
if (Count >= _maxCount)
throw new StorageOverflowException(_maxCount);
_places.Insert(0, warship);
return 0;
return Insert(warship, 0);
}
public int Insert(T warship, int position)
{
if (position >= _maxCount || position < 0)
if (Count == _maxCount)
throw new StorageOverflowException(_maxCount);
if (position < 0 || position > Count)
return -1;
if (_places.Contains(warship))
return -1;
_places.Insert(position, warship);
return position;
}
@ -80,5 +85,13 @@ namespace Warship
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
return;
_places.Sort(comparer);
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal class WarshipCompareByColor : 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 xWarship = x as DrawingObjectWarship;
var yWarship = y as DrawingObjectWarship;
if (xWarship == null && yWarship == null)
return 0;
if (xWarship == null && yWarship != null)
return 1;
if (xWarship != null && yWarship == null)
return -1;
int xColor = xWarship.GetWarship.Warship.BodyColor.ToArgb();
int yColor = yWarship.GetWarship.Warship.BodyColor.ToArgb();
if (xColor != yColor)
return xColor.CompareTo(yColor);
if (xWarship.GetWarship.Warship is EntityAdvancedWarship xAdvanced && yWarship.GetWarship.Warship is EntityAdvancedWarship yAdvanced)
{
xColor = xAdvanced.DopColor.ToArgb();
yColor = yAdvanced.DopColor.ToArgb();
if (xColor != yColor)
return xColor.CompareTo(yColor);
}
var speedCompare = xWarship.GetWarship.Warship.Speed.CompareTo(yWarship.GetWarship.Warship.Speed);
if (speedCompare != 0)
return speedCompare;
return xWarship.GetWarship.Warship.Weight.CompareTo(yWarship.GetWarship.Warship.Weight);
}
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal class WarshipCompareByType : 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 xWarship = x as DrawingObjectWarship;
var yWarship = y as DrawingObjectWarship;
if (xWarship == null && yWarship == null)
return 0;
if (xWarship == null && yWarship != null)
return 1;
if (xWarship != null && yWarship == null)
return -1;
if (xWarship.GetWarship.GetType().Name != yWarship.GetWarship.GetType().Name)
{
if (xWarship.GetWarship.GetType().Name == "DrawingWarship")
return -1;
return 1;
}
var speedCompare = xWarship.GetWarship.Warship.Speed.CompareTo(yWarship.GetWarship.Warship.Speed);
if (speedCompare != 0)
return speedCompare;
return xWarship.GetWarship.Warship.Weight.CompareTo(yWarship.GetWarship.Warship.Weight);
}
}
}