Коммит

This commit is contained in:
Катя Ихонкина 2022-12-16 20:46:30 +04:00
parent 41ccf1460c
commit 99871550b4
9 changed files with 294 additions and 45 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace MotorBoat
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -170,6 +170,47 @@ namespace MotorBoat
return true;
}
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;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MotorBoat
{
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 xShip = x as DrawningObjectBoat;
var yShip = y as DrawningObjectBoat;
if (xShip == null && yShip == null)
{
return 0;
}
if (xShip == null && yShip != null)
{
return 1;
}
if (xShip != null && yShip == null)
{
return -1;
}
var xEntityShip = xShip.GetShip.Boat;
var yEntityShip = yShip.GetShip.Boat;
var baseColorCompare = xEntityShip.BodyColor.ToArgb().CompareTo(yEntityShip.BodyColor.ToArgb());
if (baseColorCompare != 0)
{
return baseColorCompare;
}
if (xEntityShip is EntityMotorBoat xWarmlyShip && yEntityShip is EntityMotorBoat yWarmlyShip)
{
var dopColorCompare = xWarmlyShip.DopColor.ToArgb().CompareTo(yWarmlyShip.DopColor.ToArgb());
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xShip.GetShip.Boat.Speed.CompareTo(yShip.GetShip.Boat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xShip.GetShip.Boat.Weight.CompareTo(yShip.GetShip.Boat.Weight);
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MotorBoat
{
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 xCar = x as DrawningObjectBoat;
var yCar = y as DrawningObjectBoat;
if (xCar == null && yCar == null)
{
return 0;
}
if (xCar == null && yCar != null)
{
return 1;
}
if (xCar != null && yCar == null)
{
return -1;
}
if (xCar.GetShip.GetType().Name != yCar.GetShip.GetType().Name)
{
if (xCar.GetShip.GetType().Name == "DrawningBoat")
{
return -1;
}
return 1;
}
var speedCompare =
xCar.GetShip.Boat.Speed.CompareTo(yCar.GetShip.Boat.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xCar.GetShip.Boat.Weight.CompareTo(yCar.GetShip.Boat.Weight);
}
}
}

View File

@ -19,6 +19,9 @@ namespace MotorBoat
{
return _boat?.GetCurrentPosition() ?? default;
}
public DrawningBoat GetShip => _boat;
public void MoveObject(Direction direction)
{
_boat?.MoveTransport(direction);
@ -34,5 +37,52 @@ namespace MotorBoat
}
public string GetInfo() => _boat?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectBoat(data.CreateDrawningBoat());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherShip = other as DrawningObjectBoat;
if (otherShip == null)
{
return false;
}
var ship = _boat.Boat;
var otherShipShip = otherShip._boat.Boat;
if (ship.GetType().Name != otherShipShip.GetType().Name)
{
return false;
}
if (ship.Speed != otherShipShip.Speed)
{
return false;
}
if (ship.Weight != otherShipShip.Weight)
{
return false;
}
if (ship.BodyColor != otherShipShip.BodyColor)
{
return false;
}
if (ship is EntityMotorBoat liner && otherShipShip is EntityMotorBoat otherLiner)
{
if (liner.DopColor != otherLiner.DopColor)
{
return false;
}
if (liner.SportLine != otherLiner.SportLine)
{
return false;
}
if (liner.Wing != otherLiner.Wing)
{
return false;
}
}
return true;
}
}
}

View File

@ -29,6 +29,8 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.ButtonSortColor = new System.Windows.Forms.Button();
this.ButtonSortType = new System.Windows.Forms.Button();
this.ButtonDeleteMap = new System.Windows.Forms.Button();
this.ListBoxMaps = new System.Windows.Forms.ListBox();
this.ButtonAddMap = new System.Windows.Forms.Button();
@ -57,6 +59,8 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.ButtonSortColor);
this.groupBoxTools.Controls.Add(this.ButtonSortType);
this.groupBoxTools.Controls.Add(this.ButtonDeleteMap);
this.groupBoxTools.Controls.Add(this.ListBoxMaps);
this.groupBoxTools.Controls.Add(this.ButtonAddMap);
@ -79,6 +83,26 @@
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "v";
//
// ButtonSortColor
//
this.ButtonSortColor.Location = new System.Drawing.Point(130, 268);
this.ButtonSortColor.Name = "ButtonSortColor";
this.ButtonSortColor.Size = new System.Drawing.Size(83, 23);
this.ButtonSortColor.TabIndex = 36;
this.ButtonSortColor.Text = "По цвету";
this.ButtonSortColor.UseVisualStyleBackColor = true;
this.ButtonSortColor.Click += new System.EventHandler(this.ButtonSortColor_Click);
//
// ButtonSortType
//
this.ButtonSortType.Location = new System.Drawing.Point(38, 268);
this.ButtonSortType.Name = "ButtonSortType";
this.ButtonSortType.Size = new System.Drawing.Size(86, 23);
this.ButtonSortType.TabIndex = 35;
this.ButtonSortType.Text = "По типу";
this.ButtonSortType.UseVisualStyleBackColor = true;
this.ButtonSortType.Click += new System.EventHandler(this.ButtonSortType_Click);
//
// ButtonDeleteMap
//
this.ButtonDeleteMap.Location = new System.Drawing.Point(38, 209);
@ -118,7 +142,7 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 328);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 359);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -127,7 +151,7 @@
//
// buttonRemoveBoat
//
this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 357);
this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 388);
this.buttonRemoveBoat.Name = "buttonRemoveBoat";
this.buttonRemoveBoat.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveBoat.TabIndex = 14;
@ -137,7 +161,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(38, 412);
this.buttonShowStorage.Location = new System.Drawing.Point(38, 443);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 15;
@ -150,7 +174,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::MotorBoat.Properties.Resources.d;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(112, 529);
this.buttonDown.Location = new System.Drawing.Point(112, 560);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 20;
@ -162,7 +186,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::MotorBoat.Properties.Resources.up;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(148, 529);
this.buttonRight.Location = new System.Drawing.Point(148, 560);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 19;
@ -174,7 +198,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::MotorBoat.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(76, 529);
this.buttonLeft.Location = new System.Drawing.Point(76, 560);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 18;
@ -186,7 +210,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::MotorBoat.Properties.Resources.r;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(112, 493);
this.buttonUp.Location = new System.Drawing.Point(112, 524);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 17;
@ -195,7 +219,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(38, 453);
this.buttonShowOnMap.Location = new System.Drawing.Point(38, 484);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 16;
@ -205,7 +229,7 @@
//
// buttonAddBoat
//
this.buttonAddBoat.Location = new System.Drawing.Point(38, 287);
this.buttonAddBoat.Location = new System.Drawing.Point(38, 318);
this.buttonAddBoat.Name = "buttonAddBoat";
this.buttonAddBoat.Size = new System.Drawing.Size(175, 35);
this.buttonAddBoat.TabIndex = 12;
@ -225,7 +249,6 @@
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23);
this.comboBoxSelectorMap.TabIndex = 11;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox
//
@ -258,14 +281,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);
//
@ -322,5 +345,7 @@
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
private Button ButtonSortColor;
private Button ButtonSortType;
}
}

View File

@ -16,7 +16,6 @@ namespace MotorBoat
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapShipCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapDict = new()
{
{"Простая карта", new SimpleMap() },
@ -60,32 +59,6 @@ namespace MotorBoat
}
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender,
EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Розовая карта":
map = new PinkMap();
break;
case "Морская карта":
map = new SeaMap();
break;
}
if (map != null)
{
_mapShipCollectionGeneric = new MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapShipCollectionGeneric = null;
}
}
/// <summary>
/// Выбор карты
/// </summary>
@ -314,6 +287,27 @@ namespace MotorBoat
}
}
}
private void ButtonSortType_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();
}
private void ButtonSortColor_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 MotorBoat
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// <summary>
/// Шаг перемещения объекта

View File

@ -2,7 +2,7 @@
{
/// Карта с набром объектов под нее
internal class MapWithSetBoatsGeneric<T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// Ширина окна отрисовки
@ -67,6 +67,13 @@
return new(_pictureWidth, _pictureHeight);
}
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
///
public void Sort(IComparer<T> comparer)
{
_setBoats.SortSet(comparer);
}
private void Shaking()
{
int j = _setBoats.Count - 1;

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace MotorBoat
{
internal class SetBoatsGeneric<T>
where T : class
where T : class, IEquatable<T>
{
private readonly List<T> _places;
public int Count => _places.Count;
@ -19,7 +19,12 @@ namespace MotorBoat
}
public int Insert(T boat)
{
if (_places.Count > _maxCount)
if (_places.Contains(boat))
{
return -1;
}
if (_places.Count > _maxCount)
{
return -1;
}
@ -75,5 +80,14 @@ namespace MotorBoat
}
}
}
}
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
}
}