diff --git a/ArmoredCar/ArmoredCar/AbstractMap.cs b/ArmoredCar/ArmoredCar/AbstractMap.cs index 8b2f5c0..a8bab00 100644 --- a/ArmoredCar/ArmoredCar/AbstractMap.cs +++ b/ArmoredCar/ArmoredCar/AbstractMap.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ArmoredCar { - internal abstract class AbstractMap + internal abstract class AbstractMap : IEquatable { private IDrawningObject _drawningObject = null; protected int[,] _map = null; @@ -31,7 +31,7 @@ namespace ArmoredCar return DrawMapWithObject(); } public Bitmap MoveObject(Direction direction) - { + { bool flag = true; float step = _drawningObject.Step; (float left, float right, float top, float bottom) = _drawningObject.GetCurrentPosition(); @@ -40,7 +40,7 @@ namespace ArmoredCar int y1_obj_next = (int)((top - step) / _size_y); int x2_obj_next = (int)((right + step) / _size_x); int y2_obj_next = (int)((bottom + step) / _size_y); - + int x1_obj_current = (int)(left / _size_x); int y1_obj_current = (int)(top / _size_y); int x2_obj_current = (int)(right / _size_x); @@ -53,7 +53,7 @@ namespace ArmoredCar { if (x1_obj_next < 0) { - flag = false; + flag = false; } else { @@ -132,9 +132,9 @@ namespace ArmoredCar } } break; - } - } - + } + } + if (flag) { _drawningObject.MoveObject(direction); @@ -151,9 +151,9 @@ namespace ArmoredCar int y = _random.Next(0, 10); _drawningObject.SetObject(x, y, _width, _height); (float left, float right, float top, float bottom) = _drawningObject.GetCurrentPosition(); - for (int i = (int)(x / _size_x); i <= (int) (right / _size_x); i++) + for (int i = (int)(x / _size_x); i <= (int)(right / _size_x); i++) { - for (int j = (int)(y / _size_y); j <= (int) (bottom / _size_y); j++) + for (int j = (int)(y / _size_y); j <= (int)(bottom / _size_y); j++) { if (_map[i, j] == _barrier) { @@ -171,7 +171,7 @@ namespace ArmoredCar return bmp; } Graphics gr = Graphics.FromImage(bmp); - + for (int i = 0; i < _map.GetLength(0); ++i) { for (int j = 0; j < _map.GetLength(1); ++j) @@ -193,5 +193,43 @@ namespace ArmoredCar 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; + } } } diff --git a/ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs b/ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs new file mode 100644 index 0000000..ce3292b --- /dev/null +++ b/ArmoredCar/ArmoredCar/ArmoredCarCompareByColor.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class ArmoredCarCompareByColor : IComparer + { + 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 DrawningObjectArmCar; + var yCar = y as DrawningObjectArmCar; + if (xCar == null && yCar == null) + { + return 0; + } + if (xCar == null && yCar != null) + { + return 1; + } + if (xCar != null && yCar == null) + { + return -1; + } + string xCarColor = xCar.GetArmoredCar.ArmoredCar.BodyColor.Name; + string yCarColor = yCar.GetArmoredCar.ArmoredCar.BodyColor.Name; + if (xCarColor != yCarColor) + { + return xCarColor.CompareTo(yCarColor); + } + + if (xCar.GetArmoredCar.ArmoredCar is EntityTank xTank && yCar.GetArmoredCar.ArmoredCar is EntityTank yTank) + { + string xCarDopColor = xTank.DopColor.Name; + string yCarDopColor = yTank.DopColor.Name; + if (xCarDopColor != yCarDopColor) + { + return xCarDopColor.CompareTo(yCarDopColor); + } + } + + var speedCompare = xCar.GetArmoredCar.ArmoredCar.Speed.CompareTo(yCar.GetArmoredCar.ArmoredCar.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xCar.GetArmoredCar.ArmoredCar.Weight.CompareTo(yCar.GetArmoredCar.ArmoredCar.Weight); + } + } +} diff --git a/ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs b/ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs new file mode 100644 index 0000000..a02d251 --- /dev/null +++ b/ArmoredCar/ArmoredCar/ArmoredCarCompareByType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ArmoredCar +{ + internal class ArmoredCarCompareByType : IComparer + { + 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 DrawningObjectArmCar; + var yCar = y as DrawningObjectArmCar; + if (xCar == null && yCar == null) + { + return 0; + } + if (xCar == null && yCar != null) + { + return 1; + } + if (xCar != null && yCar == null) + { + return -1; + } + if (xCar.GetArmoredCar.GetType().Name != yCar.GetArmoredCar.GetType().Name) + { + if (xCar.GetArmoredCar.GetType().Name == "DrawningArmoredCar") + { + return -1; + } + return 1; + } + var speedCompare = xCar.GetArmoredCar.ArmoredCar.Speed.CompareTo(yCar.GetArmoredCar.ArmoredCar.Speed); + if (speedCompare != 0) + { + return speedCompare; + } + return xCar.GetArmoredCar.ArmoredCar.Weight.CompareTo(yCar.GetArmoredCar.ArmoredCar.Weight); + } + } +} diff --git a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs index 22effca..8b14e12 100644 --- a/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs +++ b/ArmoredCar/ArmoredCar/DrawningObjectArmCar.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.DirectoryServices.ActiveDirectory; using System.Drawing; using System.Linq; using System.Text; @@ -10,6 +11,8 @@ namespace ArmoredCar internal class DrawningObjectArmCar : IDrawningObject { private DrawningArmoredCar _armCar = null; + + public DrawningArmoredCar GetArmoredCar => _armCar; public DrawningObjectArmCar(DrawningArmoredCar car) { _armCar = car; @@ -34,7 +37,54 @@ namespace ArmoredCar _armCar.DrawTransport(g); } public string GetInfo() => _armCar?.GetDataForSave(); - public static IDrawningObject Create(string data) => new - DrawningObjectArmCar(data.CreateDrawningArmoredCar()); + public static IDrawningObject Create(string data) => new DrawningObjectArmCar(data.CreateDrawningArmoredCar()); + + public bool Equals(IDrawningObject? other) + { + if (other == null) + { + return false; + } + var otherCar = other as DrawningObjectArmCar; + if (otherCar == null) + { + return false; + } + var car = _armCar.ArmoredCar; + var otherCarCar = otherCar._armCar.ArmoredCar; + + if (car.GetType() != otherCarCar.GetType()) + return false; + + if (car.Speed != otherCarCar.Speed) + { + return false; + } + if (car.Weight != otherCarCar.Weight) + { + return false; + } + if (car.BodyColor != otherCarCar.BodyColor) + { + return false; + } + if (car is EntityTank tank && otherCarCar is EntityTank otherTank) + { + if (tank.DopColor != otherTank.DopColor) + { + return false; + } + if (tank.TowerWeapon != otherTank.TowerWeapon) + { + return false; + } + if (tank.AMachineGun != otherTank.AMachineGun) + { + return false; + } + } + return true; + } + } } diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs index 0cd41b5..8752fdf 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.Designer.cs @@ -31,6 +31,8 @@ namespace ArmoredCar private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.ButtonSortByType = new System.Windows.Forms.Button(); + this.ButtonSortByColor = new System.Windows.Forms.Button(); this.groupBoxMaps = new System.Windows.Forms.GroupBox(); this.buttonAddMap = new System.Windows.Forms.Button(); this.buttonDeleteMap = new System.Windows.Forms.Button(); @@ -61,6 +63,8 @@ namespace ArmoredCar // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.ButtonSortByType); + this.groupBoxTools.Controls.Add(this.ButtonSortByColor); this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveCar); @@ -72,13 +76,33 @@ namespace ArmoredCar this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonAddCar); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(802, 24); + this.groupBoxTools.Location = new System.Drawing.Point(798, 24); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(204, 637); + this.groupBoxTools.Size = new System.Drawing.Size(204, 639); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // ButtonSortByType + // + this.ButtonSortByType.Location = new System.Drawing.Point(17, 269); + this.ButtonSortByType.Name = "ButtonSortByType"; + this.ButtonSortByType.Size = new System.Drawing.Size(175, 35); + this.ButtonSortByType.TabIndex = 13; + this.ButtonSortByType.Text = "Сортировать по типу"; + this.ButtonSortByType.UseVisualStyleBackColor = true; + this.ButtonSortByType.Click += new System.EventHandler(this.ButtonSortByType_Click); + // + // ButtonSortByColor + // + this.ButtonSortByColor.Location = new System.Drawing.Point(17, 311); + this.ButtonSortByColor.Name = "ButtonSortByColor"; + this.ButtonSortByColor.Size = new System.Drawing.Size(175, 35); + this.ButtonSortByColor.TabIndex = 12; + this.ButtonSortByColor.Text = "Сортировать по цвету"; + this.ButtonSortByColor.UseVisualStyleBackColor = true; + this.ButtonSortByColor.Click += new System.EventHandler(this.ButtonSortByColor_Click); + // // groupBoxMaps // this.groupBoxMaps.Controls.Add(this.buttonAddMap); @@ -143,7 +167,7 @@ namespace ArmoredCar // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 355); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 393); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -152,7 +176,7 @@ namespace ArmoredCar // // buttonRemoveCar // - this.buttonRemoveCar.Location = new System.Drawing.Point(17, 384); + this.buttonRemoveCar.Location = new System.Drawing.Point(17, 422); this.buttonRemoveCar.Name = "buttonRemoveCar"; this.buttonRemoveCar.Size = new System.Drawing.Size(175, 35); this.buttonRemoveCar.TabIndex = 3; @@ -162,7 +186,7 @@ namespace ArmoredCar // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(17, 437); + this.buttonShowStorage.Location = new System.Drawing.Point(17, 475); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 4; @@ -175,7 +199,7 @@ namespace ArmoredCar this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ArmoredCar.Properties.Resources.Down; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(91, 587); + this.buttonDown.Location = new System.Drawing.Point(91, 603); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 10; @@ -187,7 +211,7 @@ namespace ArmoredCar this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ArmoredCar.Properties.Resources.Right; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(127, 587); + this.buttonRight.Location = new System.Drawing.Point(127, 603); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -199,7 +223,7 @@ namespace ArmoredCar this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ArmoredCar.Properties.Resources.Left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(55, 587); + this.buttonLeft.Location = new System.Drawing.Point(55, 603); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -211,7 +235,7 @@ namespace ArmoredCar this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ArmoredCar.Properties.Resources.Up; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(91, 551); + this.buttonUp.Location = new System.Drawing.Point(91, 567); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -220,7 +244,7 @@ namespace ArmoredCar // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(17, 487); + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 525); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); this.buttonShowOnMap.TabIndex = 5; @@ -230,7 +254,7 @@ namespace ArmoredCar // // buttonAddCar // - this.buttonAddCar.Location = new System.Drawing.Point(17, 314); + this.buttonAddCar.Location = new System.Drawing.Point(17, 352); this.buttonAddCar.Name = "buttonAddCar"; this.buttonAddCar.Size = new System.Drawing.Size(175, 35); this.buttonAddCar.TabIndex = 1; @@ -243,7 +267,7 @@ namespace ArmoredCar 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(802, 637); + this.pictureBox.Size = new System.Drawing.Size(798, 639); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -253,7 +277,7 @@ namespace ArmoredCar this.FileToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(1006, 24); + this.menuStrip1.Size = new System.Drawing.Size(1002, 24); this.menuStrip1.TabIndex = 2; this.menuStrip1.Text = "menuStrip1"; // @@ -292,7 +316,7 @@ namespace ArmoredCar // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1006, 661); + this.ClientSize = new System.Drawing.Size(1002, 663); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.menuStrip1); @@ -336,5 +360,7 @@ namespace ArmoredCar private ToolStripMenuItem LoadToolStripMenuItem; private OpenFileDialog openFileDialog; private SaveFileDialog saveFileDialog; + private Button ButtonSortByType; + private Button ButtonSortByColor; } } \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs index 60af64b..9af7a3c 100644 --- a/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs +++ b/ArmoredCar/ArmoredCar/FormMapWithSetArmoredCars.cs @@ -313,5 +313,24 @@ namespace ArmoredCar } } + private void ButtonSortByType_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].Sort(new ArmoredCarCompareByType()); + 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 ArmoredCarCompareByColor()); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } } } \ No newline at end of file diff --git a/ArmoredCar/ArmoredCar/IDrawningObject.cs b/ArmoredCar/ArmoredCar/IDrawningObject.cs index 4e97bfa..d7ae519 100644 --- a/ArmoredCar/ArmoredCar/IDrawningObject.cs +++ b/ArmoredCar/ArmoredCar/IDrawningObject.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ArmoredCar { - internal interface IDrawningObject + internal interface IDrawningObject : IEquatable { /// /// Шаг перемещения объекта diff --git a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs index e4a7e64..184d333 100644 --- a/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/MapWithSetArmoredCarsGeneric.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace ArmoredCar { internal class MapWithSetArmoredCarsGeneric - where T : class, IDrawningObject + where T : class, IDrawningObject, IEquatable where U : AbstractMap { /// @@ -206,5 +206,14 @@ namespace ArmoredCar _setCars.Insert(DrawningObjectArmCar.Create(rec) as T); } } + + /// + /// Сортировка + /// + /// + public void Sort(IComparer comparer) + { + _setCars.SortSet(comparer); + } } } diff --git a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs index 6d63325..3fa6226 100644 --- a/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs +++ b/ArmoredCar/ArmoredCar/SetArmoredCarsGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ArmoredCar { internal class SetArmoredCarsGeneric - where T : class + where T : class, IEquatable { /// /// Список объектов, которые храним @@ -43,13 +43,19 @@ namespace ArmoredCar /// Позиция /// public int Insert(T armoredCar, int position) - { + { if (Count >= _maxCount) throw new StorageOverflowException(Count); if (position < 0 || position >= _maxCount) return -1; + // проверка на уникальность + if (_places.Contains(armoredCar)) + { + return -1; + } + _places.Insert(position, armoredCar); return position; } @@ -108,6 +114,18 @@ namespace ArmoredCar } } } + /// + /// Сортировка набора объектов + /// + /// + public void SortSet(IComparer comparer) + { + if (comparer == null) + { + return; + } + _places.Sort(comparer); + } } }