Laba8 PIbd-22 Kalyshev Y V #11

Closed
Zyzf wants to merge 4 commits from Laba8 into Laba7
9 changed files with 294 additions and 18 deletions

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -131,5 +131,45 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null)
{
return false;
}
var otherMap = other as AbstractMap;
if (otherMap == null)
{
return false;
}
if (_width != otherMap._width)
{
return false;
}
if (_height != otherMap._height)
{
return false;
}
if (_size_x != otherMap._size_x)
{
return false;
}
if (_size_y != otherMap._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] != otherMap._map[i, j])
{
return false;
}
}
}
return true;
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
internal class BoatCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xBoat = x as DrawningObjectBoat;
var yBoat = y as DrawningObjectBoat;
if (xBoat == null && yBoat == null)
{
return 0;
}
if (xBoat == null && yBoat != null)
{
return 1;
}
if (xBoat != null && yBoat == null)
{
return -1;
}
var colorCompare = xBoat.GetBoat.Boat.BodyColor.Name.CompareTo(yBoat.GetBoat.Boat.BodyColor.Name);
if (colorCompare != 0)
{
return colorCompare;
}
var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
internal class BoatCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y == null)
{
return -1;
}
var xBoat = x as DrawningObjectBoat;
var yBoat = y as DrawningObjectBoat;
if (xBoat == null && yBoat == null)
{
return 0;
}
if (xBoat == null && yBoat != null)
{
return 1;
}
if (xBoat != null && yBoat == null)
{
return -1;
}
if (xBoat.GetBoat.GetType().Name != yBoat.GetBoat.GetType().Name)
{
if (xBoat.GetBoat.GetType().Name == "DrawningBoat")
{
return -1;
}
return 1;
}
var speedCompare = xBoat.GetBoat.Boat.Speed.CompareTo(yBoat.GetBoat.Boat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xBoat.GetBoat.Boat.Weight.CompareTo(yBoat.GetBoat.Boat.Weight);
}
}
}

View File

@ -14,6 +14,7 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
_boat = boat;
}
public float Step => _boat?.Boat?.Step ?? 0;
public DrawningBoat GetBoat => _boat;
public void DrawningObject(Graphics g)
{
_boat?.DrawTransport(g);
@ -32,5 +33,55 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
_boat.SetPosition(x, y, width, height);
}
public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherBoat = other as DrawningObjectBoat;
if (otherBoat == null)
{
return false;
}
var boat = _boat.Boat;
var otherBoatBoat = otherBoat._boat.Boat;
if (boat.GetType().Name != otherBoatBoat.GetType().Name)
{
return false;
}
if (boat.Speed != otherBoatBoat.Speed)
{
return false;
}
if (boat.Weight != otherBoatBoat.Weight)
{
return false;
}
if (boat.BodyColor != otherBoatBoat.BodyColor)
{
return false;
}
if (boat is EntitySpeedboat speedboat && otherBoatBoat is EntitySpeedboat otherSpeedboatSpeedboat)
{
if (speedboat.DopColor != otherSpeedboatSpeedboat.DopColor)
{
return false;
}
if (speedboat.BodyKit != otherSpeedboatSpeedboat.BodyKit)
{
return false;
}
if (speedboat.Wing != otherSpeedboatSpeedboat.Wing)
{
return false;
}
if (speedboat.SportLine != otherSpeedboatSpeedboat.SportLine)
{
return false;
}
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonSortByColor = new System.Windows.Forms.Button();
this.buttonSortByType = new System.Windows.Forms.Button();
this.groupBoxMap = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
@ -59,6 +61,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonSortByColor);
this.groupBoxTools.Controls.Add(this.buttonSortByType);
this.groupBoxTools.Controls.Add(this.groupBoxMap);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRemoveBoat);
@ -72,11 +76,31 @@
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(690, 24);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(204, 563);
this.groupBoxTools.Size = new System.Drawing.Size(204, 626);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// buttonSortByColor
//
this.buttonSortByColor.Location = new System.Drawing.Point(17, 308);
this.buttonSortByColor.Name = "buttonSortByColor";
this.buttonSortByColor.Size = new System.Drawing.Size(175, 35);
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(17, 267);
this.buttonSortByType.Name = "buttonSortByType";
this.buttonSortByType.Size = new System.Drawing.Size(175, 35);
this.buttonSortByType.TabIndex = 12;
this.buttonSortByType.Text = "Сортировать по типу";
this.buttonSortByType.UseVisualStyleBackColor = true;
this.buttonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// groupBoxMap
//
this.groupBoxMap.Controls.Add(this.buttonDeleteMap);
@ -142,7 +166,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 335);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 390);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -151,7 +175,7 @@
//
// buttonRemoveBoat
//
this.buttonRemoveBoat.Location = new System.Drawing.Point(17, 364);
this.buttonRemoveBoat.Location = new System.Drawing.Point(17, 419);
this.buttonRemoveBoat.Name = "buttonRemoveBoat";
this.buttonRemoveBoat.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveBoat.TabIndex = 3;
@ -161,7 +185,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(17, 405);
this.buttonShowStorage.Location = new System.Drawing.Point(17, 460);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 4;
@ -174,7 +198,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(91, 513);
this.buttonDown.Location = new System.Drawing.Point(91, 576);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -186,7 +210,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(127, 513);
this.buttonRight.Location = new System.Drawing.Point(127, 576);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -198,7 +222,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(55, 513);
this.buttonLeft.Location = new System.Drawing.Point(55, 576);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -210,7 +234,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::PIbd_22_Kalyshev_Y_V_MotorBoat_Base.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(91, 477);
this.buttonUp.Location = new System.Drawing.Point(91, 540);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -219,7 +243,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 446);
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 501);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 5;
@ -229,7 +253,7 @@
//
// buttonAddBoat
//
this.buttonAddBoat.Location = new System.Drawing.Point(17, 294);
this.buttonAddBoat.Location = new System.Drawing.Point(17, 349);
this.buttonAddBoat.Name = "buttonAddBoat";
this.buttonAddBoat.Size = new System.Drawing.Size(175, 35);
this.buttonAddBoat.TabIndex = 1;
@ -242,7 +266,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(690, 563);
this.pictureBox.Size = new System.Drawing.Size(690, 626);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -267,14 +291,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);
//
@ -290,7 +314,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(894, 587);
this.ClientSize = new System.Drawing.Size(894, 650);
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 buttonSortByColor;
private Button buttonSortByType;
}
}

View File

@ -294,5 +294,33 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
}
}
}
/// <summary>
/// Сортировка по типу
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Сортировка по цвету
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new BoatCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
}
}

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
internal class MapWithSetBoatsGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
@ -134,6 +134,14 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
}
}
/// <summary>
/// Сортировка
/// </summary>
/// <param name="comparer"></param>
public void Sort(IComparer<T> comparer)
{
_setBoats.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
{
internal class SetBoatsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
/// <summary>
/// Список объектов, которые храним
@ -36,6 +36,10 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
/// <returns></returns>
public int Insert(T boat)
{
if (_places.Contains(boat))
{
return -1;
}
if (_places.Count < _maxCount)
{
_places.Add(boat);
@ -128,5 +132,17 @@ namespace PIbd_22_Kalyshev_Y_V_MotorBoat_Base
}
}
}
/// <summary>
/// Сортировка набора объектов
/// </summary>
/// <param name="comparer"></param>
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}