Compare commits

...

3 Commits

9 changed files with 264 additions and 11 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal abstract class AbstractMap internal abstract class AbstractMap : IEquatable<AbstractMap>
{ {
private IDrawningObject _drawningObject = null; private IDrawningObject _drawningObject = null;
@ -122,5 +122,27 @@ namespace AirBomber
protected abstract void GenerateMap(); protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j); protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j); protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null || _map != other._map || _width != other._width
|| _size_x != other._size_x || _size_y != other._size_y || _height != other._height
|| GetType() != other.GetType() || _map.GetLength(0) != other._map.GetLength(0)
|| _map.GetLength(1) != other._map.GetLength(1))
{
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

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class AirBomberCompareByColor : 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 xAirBomber = x as DrawningObjectBomber;
var yAirBomber = y as DrawningObjectBomber;
if (xAirBomber == null && yAirBomber == null)
{
return 0;
}
if (xAirBomber == null && yAirBomber != null)
{
return 1;
}
if (xAirBomber != null && yAirBomber == null)
{
return -1;
}
string xAirBomberColor = xAirBomber.GetBomber.AirBomber.BodyColor.Name;
string yAirBomberColor = yAirBomber.GetBomber.AirBomber.BodyColor.Name;
if (xAirBomberColor != yAirBomberColor)
{
return xAirBomberColor.CompareTo(yAirBomberColor);
}
if (xAirBomber.GetBomber.AirBomber is EntityWarplane xWarplane && yAirBomber.GetBomber.AirBomber is EntityWarplane yWarplane)
{
string xWarplaneDopColor = xWarplane.DopColor.Name;
string yWarplaneDopColor = yWarplane.DopColor.Name;
var dopColorCompare = xWarplaneDopColor.CompareTo(yWarplaneDopColor);
if (dopColorCompare != 0)
{
return dopColorCompare;
}
}
var speedCompare = xAirBomber.GetBomber.AirBomber.Speed.CompareTo(yAirBomber.GetBomber.AirBomber.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirBomber.GetBomber.AirBomber.Weight.CompareTo(yAirBomber.GetBomber.AirBomber.Weight);
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class AirBomberCompareByType : 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 xAirBomber = x as DrawningObjectBomber;
var yAirBomber = y as DrawningObjectBomber;
if (xAirBomber == null && yAirBomber == null)
{
return 0;
}
if (xAirBomber == null && yAirBomber != null)
{
return 1;
}
if (xAirBomber != null && yAirBomber == null)
{
return -1;
}
if (xAirBomber.GetBomber.GetType().Name != yAirBomber.GetBomber.GetType().Name)
{
if (xAirBomber.GetBomber.GetType().Name == "DrawningBomber")
{
return -1;
}
}
var speedCompare = xAirBomber.GetBomber.AirBomber.Speed.CompareTo(yAirBomber.GetBomber.AirBomber.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirBomber.GetBomber.AirBomber.Weight.CompareTo(yAirBomber.GetBomber.AirBomber.Weight);
}
}
}

View File

@ -17,6 +17,8 @@ namespace AirBomber
public float Step => _airBomber?.AirBomber?.Step ?? 0; public float Step => _airBomber?.AirBomber?.Step ?? 0;
public DrawningBomber GetBomber => _airBomber;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{ {
return _airBomber?.GetCurrentPosition() ?? default; return _airBomber?.GetCurrentPosition() ?? default;
@ -40,5 +42,55 @@ namespace AirBomber
public string GetInfo() => _airBomber?.GetDataForSave(); public string GetInfo() => _airBomber?.GetDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectBomber(data.CreateDrawningAirBomber()); public static IDrawningObject Create(string data) => new DrawningObjectBomber(data.CreateDrawningAirBomber());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherBomber = other as DrawningObjectBomber;
if (otherBomber == null)
{
return false;
}
var airBomber = _airBomber.AirBomber;
var otherBomberBomber = otherBomber._airBomber.AirBomber;
if (airBomber.Speed != otherBomberBomber.Speed)
{
return false;
}
if (airBomber.Weight != otherBomberBomber.Weight)
{
return false;
}
if (airBomber.BodyColor != otherBomberBomber.BodyColor)
{
return false;
}
if ((airBomber is EntityWarplane) && !(otherBomberBomber is EntityWarplane)
|| !(airBomber is EntityWarplane) && (otherBomberBomber is EntityWarplane))
{
return false;
}
if (airBomber is EntityWarplane warplane && otherBomberBomber is EntityWarplane otherWarplane)
{
if (warplane.DopColor != otherWarplane.DopColor)
{
return false;
}
if (warplane.Weapons != otherWarplane.Weapons)
{
return false;
}
if (warplane.Engines != otherWarplane.Engines)
{
return false;
}
}
return true;
}
} }
} }

View File

@ -51,6 +51,8 @@
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.ButtonSortByType = new System.Windows.Forms.Button();
this.ButtonSortByColor = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout(); this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
@ -59,6 +61,8 @@
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.ButtonSortByColor);
this.groupBox1.Controls.Add(this.ButtonSortByType);
this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown); this.groupBox1.Controls.Add(this.buttonDown);
@ -86,7 +90,7 @@
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(22, 32); this.groupBoxMaps.Location = new System.Drawing.Point(22, 32);
this.groupBoxMaps.Name = "groupBoxMaps"; this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(150, 326); this.groupBoxMaps.Size = new System.Drawing.Size(150, 297);
this.groupBoxMaps.TabIndex = 11; this.groupBoxMaps.TabIndex = 11;
this.groupBoxMaps.TabStop = false; this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты"; this.groupBoxMaps.Text = "Карты";
@ -103,7 +107,7 @@
// //
// buttonDeleteMap // buttonDeleteMap
// //
this.buttonDeleteMap.Location = new System.Drawing.Point(0, 273); this.buttonDeleteMap.Location = new System.Drawing.Point(0, 255);
this.buttonDeleteMap.Name = "buttonDeleteMap"; this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(150, 34); this.buttonDeleteMap.Size = new System.Drawing.Size(150, 34);
this.buttonDeleteMap.TabIndex = 4; this.buttonDeleteMap.TabIndex = 4;
@ -267,14 +271,14 @@
// SaveToolStripMenuItem // SaveToolStripMenuItem
// //
this.SaveToolStripMenuItem.Name = "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.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
// //
// LoadToolStripMenuItem // LoadToolStripMenuItem
// //
this.LoadToolStripMenuItem.Name = "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.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
// //
@ -287,6 +291,26 @@
// //
this.saveFileDialog.Filter = "txt file | *.txt"; this.saveFileDialog.Filter = "txt file | *.txt";
// //
// ButtonSortByType
//
this.ButtonSortByType.Location = new System.Drawing.Point(22, 344);
this.ButtonSortByType.Name = "ButtonSortByType";
this.ButtonSortByType.Size = new System.Drawing.Size(150, 29);
this.ButtonSortByType.TabIndex = 12;
this.ButtonSortByType.Text = "Сортировать по типу";
this.ButtonSortByType.UseVisualStyleBackColor = true;
this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click);
//
// ButtonSortByColor
//
this.ButtonSortByColor.Location = new System.Drawing.Point(22, 379);
this.ButtonSortByColor.Name = "ButtonSortByColor";
this.ButtonSortByColor.Size = new System.Drawing.Size(150, 28);
this.ButtonSortByColor.TabIndex = 13;
this.ButtonSortByColor.Text = "Сортировать по цвету";
this.ButtonSortByColor.UseVisualStyleBackColor = true;
this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click);
//
// FormMapWithSetAirBomber // FormMapWithSetAirBomber
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -335,5 +359,7 @@
private ToolStripMenuItem LoadToolStripMenuItem; private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog; private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog; private SaveFileDialog saveFileDialog;
private Button ButtonSortByColor;
private Button ButtonSortByType;
} }
} }

View File

@ -13,8 +13,6 @@ namespace AirBomber
{ {
public partial class FormMapWithSetAirBomber : Form public partial class FormMapWithSetAirBomber : Form
{ {
private MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap> _mapAirBomberCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new() private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{ {
{ "Простая карта", new SimpleMap() }, { "Простая карта", new SimpleMap() },
@ -261,5 +259,25 @@ namespace AirBomber
} }
} }
} }
private void ButtonSortByType_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AirBomberCompareByType());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonSortByColor_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new AirBomberCompareByColor());
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
} }
} }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal interface IDrawningObject internal interface IDrawningObject : IEquatable<IDrawningObject>
{ {
public float Step { get; } public float Step { get; }

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace AirBomber namespace AirBomber
{ {
internal class SetAirBomberGeneric<T> internal class SetAirBomberGeneric<T>
where T : class where T : class, IEquatable<T>
{ {
private readonly List<T> _places; private readonly List<T> _places;
@ -28,6 +28,10 @@ namespace AirBomber
public int Insert(T airBomber, int position) public int Insert(T airBomber, int position)
{ {
if (_places.Contains(airBomber))
{
return -1;
}
if (_places.Count >= _maxCount) if (_places.Count >= _maxCount)
{ {
throw new StorageOverflowException(_maxCount); throw new StorageOverflowException(_maxCount);
@ -87,5 +91,14 @@ namespace AirBomber
} }
} }
} }
public void SortSet(IComparer<T> comparer)
{
if (comparer == null)
{
return;
}
_places.Sort(comparer);
}
} }
} }