diff --git a/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs b/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs index 4a5e951..b2c2bcf 100644 --- a/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs +++ b/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace ProjectExcavator.MovementStrategy { diff --git a/ProjectExcavator/ProjectExcavator/DrawingKatkiCircle.cs b/ProjectExcavator/ProjectExcavator/DrawKatkiCircle.cs similarity index 91% rename from ProjectExcavator/ProjectExcavator/DrawingKatkiCircle.cs rename to ProjectExcavator/ProjectExcavator/DrawKatkiCircle.cs index 271e7fa..645e5de 100644 --- a/ProjectExcavator/ProjectExcavator/DrawingKatkiCircle.cs +++ b/ProjectExcavator/ProjectExcavator/DrawKatkiCircle.cs @@ -36,6 +36,21 @@ namespace ProjectExcavator } } } + public int GetShape() + { + return 0; + } + public int GetAmount() + { + int x = 0; + if (KatNum == KatkiNumber.Four) + x = 1; + if (KatNum == KatkiNumber.Five) + x = 2; + if (KatNum == KatkiNumber.Six) + x = 3; + return x; + } public void Draw(int _startPosX, int _startPosY, Color katkiColor, Graphics g) { Pen pen = new Pen(Color.Black); diff --git a/ProjectExcavator/ProjectExcavator/DrawKatkiSquare.cs b/ProjectExcavator/ProjectExcavator/DrawKatkiSquare.cs index b797370..63f592a 100644 --- a/ProjectExcavator/ProjectExcavator/DrawKatkiSquare.cs +++ b/ProjectExcavator/ProjectExcavator/DrawKatkiSquare.cs @@ -36,6 +36,21 @@ namespace ProjectExcavator } } } + public int GetShape() + { + return 1; + } + public int GetAmount() + { + int x = 0; + if (KatNum == KatkiNumber.Four) + x = 1; + if (KatNum == KatkiNumber.Five) + x = 2; + if (KatNum == KatkiNumber.Six) + x = 3; + return x; + } public void Draw(int _startPosX, int _startPosY, Color katkiColor, Graphics g) { Pen pen = new Pen(Color.Black); diff --git a/ProjectExcavator/ProjectExcavator/DrawKatkiTriangle.cs b/ProjectExcavator/ProjectExcavator/DrawKatkiTriangle.cs index d6c9fc9..d5d6ef7 100644 --- a/ProjectExcavator/ProjectExcavator/DrawKatkiTriangle.cs +++ b/ProjectExcavator/ProjectExcavator/DrawKatkiTriangle.cs @@ -36,6 +36,21 @@ namespace ProjectExcavator } } } + public int GetShape() + { + return 2; + } + public int GetAmount() + { + int x = 0; + if (KatNum == KatkiNumber.Four) + x = 1; + if (KatNum == KatkiNumber.Five) + x = 2; + if (KatNum == KatkiNumber.Six) + x = 3; + return x; + } public void Draw(int _startPosX, int _startPosY, Color katkiColor, Graphics g) { Pen pen = new Pen(Color.Black); diff --git a/ProjectExcavator/ProjectExcavator/DrawingExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawingExcavator.cs index ceadf79..0394cd3 100644 --- a/ProjectExcavator/ProjectExcavator/DrawingExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawingExcavator.cs @@ -4,12 +4,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectExcavator.Entities; +using ProjectExcavator.MovementStrategy; using ProjectExcavator; namespace ProjectExcavator.DrawingObjects { public class DrawingExcavator { + public IMoveableObject GetMoveableObject => new DrawingObjectExcavator(this); private IDrawingKatki? DrawingKatki; /// /// Класс-сущность @@ -212,33 +214,32 @@ namespace ProjectExcavator.DrawingObjects Brush brYellow = new SolidBrush(Color.Yellow); Brush brGray = new SolidBrush(Color.Gray); - - //отрисовка экскаватора без ковша - g.DrawRectangle(pen, _startPosX + 15, _startPosY + 25, 75, 25); - g.DrawRectangle(pen, _startPosX + 60, _startPosY, 30, 25); - g.DrawRectangle(pen, _startPosX + 30, _startPosY + 5, 10, 20); - g.DrawRectangle(pen, _startPosX + 10, _startPosY + 55, 86, 20); - g.DrawPie(pen, _startPosX, _startPosY + 55, 20, 20, 90, 180); - g.DrawPie(pen, _startPosX + 85, _startPosY + 55, 20, 20, 270, 180); - g.DrawEllipse(pen, _startPosX + 5, _startPosY + 58, 15, 15); - g.DrawEllipse(pen, _startPosX + 85, _startPosY + 58, 15, 15); - g.DrawEllipse(pen, _startPosX + 25, _startPosY + 65, 8, 8); - g.DrawEllipse(pen, _startPosX + 45, _startPosY + 65, 8, 8); - g.DrawEllipse(pen, _startPosX + 65, _startPosY + 65, 8, 8); - g.DrawEllipse(pen, _startPosX + 37, _startPosY + 58, 6, 6); - g.DrawEllipse(pen, _startPosX + 57, _startPosY + 58, 6, 6); + //экскаватор + g.DrawRectangle(pen, _startPosX + 50, _startPosY + 35, 75, 25); + g.DrawRectangle(pen, _startPosX + 95, _startPosY + 10, 30, 25); + g.DrawRectangle(pen, _startPosX + 60, _startPosY + 15, 10, 20); + g.DrawRectangle(pen, _startPosX + 44, _startPosY + 65, 86, 20); + g.DrawPie(pen, _startPosX + 34, _startPosY + 65, 20, 20, 90, 180); + g.DrawPie(pen, _startPosX + 120, _startPosY + 65, 20, 20, 270, 180); + g.DrawEllipse(pen, _startPosX + 40, _startPosY + 68, 15, 15); + g.DrawEllipse(pen, _startPosX + 120, _startPosY + 68, 15, 15); + g.DrawEllipse(pen, _startPosX + 60, _startPosY + 76, 8, 8); + g.DrawEllipse(pen, _startPosX + 80, _startPosY + 76, 8, 8); + g.DrawEllipse(pen, _startPosX + 100, _startPosY + 76, 8, 8); + g.DrawEllipse(pen, _startPosX + 72, _startPosY + 68, 6, 6); + g.DrawEllipse(pen, _startPosX + 92, _startPosY + 68, 6, 6); //кабина водителя - g.FillRectangle(brBlue, _startPosX + 61, _startPosY + 1, 29, 24); + g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 11, 29, 24); // кузов - g.FillRectangle(brYellow, _startPosX + 16, _startPosY + 26, 74, 24); + g.FillRectangle(brYellow, _startPosX + 51, _startPosY + 36, 74, 24); // труба - g.FillRectangle(brYellow, _startPosX + 31, _startPosY + 6, 9, 19); + g.FillRectangle(brYellow, _startPosX + 61, _startPosY + 16, 9, 19); //гусеница - g.FillPie(brGray, _startPosX, _startPosY + 55, 20, 20, 90, 180); - g.FillPie(brGray, _startPosX + 85, _startPosY + 55, 20, 20, 270, 180); - g.FillRectangle(brGray, _startPosX + 10, _startPosY + 55, 86, 20); + g.FillPie(brGray, _startPosX + 34, _startPosY + 65, 20, 20, 90, 180); + g.FillPie(brGray, _startPosX + 120, _startPosY + 65, 20, 20, 270, 180); + g.FillRectangle(brGray, _startPosX + 44, _startPosY + 65, 86, 20); //катки орнамент(4,5,6) - DrawingKatki.Draw(_startPosX, _startPosY, EntityExcavator.BodyColor, g); + DrawingKatki.Draw(_startPosX + 35, _startPosY + 10, EntityExcavator.BodyColor, g); } } } diff --git a/ProjectExcavator/ProjectExcavator/DrawingExcavatorKovsh.cs b/ProjectExcavator/ProjectExcavator/DrawingExcavatorKovsh.cs index 0e1d8bf..9615a20 100644 --- a/ProjectExcavator/ProjectExcavator/DrawingExcavatorKovsh.cs +++ b/ProjectExcavator/ProjectExcavator/DrawingExcavatorKovsh.cs @@ -13,7 +13,6 @@ namespace ProjectExcavator.DrawingObjects /// public class DrawingExcavatorKovsh : DrawingExcavator { - private IDrawingKatki? DrawingKatki; /// /// Конструктор /// @@ -33,20 +32,6 @@ namespace ProjectExcavator.DrawingObjects EntityExcavator = new EntityExcavatorKovsh(speed, weight, bodyColor, additionalColor, kovsh, katki); } - int choose = numchoose % 3; - switch (choose) - { - case 0: - DrawingKatki = new DrawKatkiSquare(); - break; - case 1: - DrawingKatki = new DrawKatkiTriangle(); - break; - case 2: - DrawingKatki = new DrawKatkiCircle(); - break; - } - DrawingKatki.Properties = numKatki; } public override void DrawTransport(Graphics g) { @@ -59,35 +44,8 @@ namespace ProjectExcavator.DrawingObjects Brush additionalBrush = new SolidBrush(excavatorKovsh.AdditionalColor); Brush brBlack = new SolidBrush(Color.Black); - Brush brBlue = new SolidBrush(Color.LightBlue); - Brush brYellow = new SolidBrush(Color.Yellow); - Brush brGray = new SolidBrush(Color.Gray); - - //экскаватор - g.DrawRectangle(pen, _startPosX + 50, _startPosY + 35, 75, 25); - g.DrawRectangle(pen, _startPosX + 95, _startPosY + 10, 30, 25); - g.DrawRectangle(pen, _startPosX + 60, _startPosY + 15, 10, 20); - g.DrawRectangle(pen, _startPosX + 44, _startPosY + 65, 86, 20); - g.DrawPie(pen, _startPosX + 34, _startPosY + 65, 20, 20, 90, 180); - g.DrawPie(pen, _startPosX + 120, _startPosY + 65, 20, 20, 270, 180); - g.DrawEllipse(pen, _startPosX + 40, _startPosY + 68, 15, 15); - g.DrawEllipse(pen, _startPosX + 120, _startPosY + 68, 15, 15); - g.DrawEllipse(pen, _startPosX + 60, _startPosY + 76, 8, 8); - g.DrawEllipse(pen, _startPosX + 80, _startPosY + 76, 8, 8); - g.DrawEllipse(pen, _startPosX + 100, _startPosY + 76, 8, 8); - g.DrawEllipse(pen, _startPosX + 72, _startPosY + 68, 6, 6); - g.DrawEllipse(pen, _startPosX + 92, _startPosY + 68, 6, 6); - //кабина водителя - g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 11, 29, 24); - // кузов - g.FillRectangle(brYellow, _startPosX + 51, _startPosY + 36, 74, 24); - // труба - g.FillRectangle(brYellow, _startPosX + 61, _startPosY + 16, 9, 19); - //гусеница - g.FillPie(brGray, _startPosX + 34, _startPosY + 65, 20, 20, 90, 180); - g.FillPie(brGray, _startPosX + 120, _startPosY + 65, 20, 20, 270, 180); - g.FillRectangle(brGray, _startPosX + 44, _startPosY + 65, 86, 20); + base.DrawTransport(g); //ковш g.DrawLine(pen, _startPosX + 50, _startPosY + 35, _startPosX + 10, _startPosY + 10); g.DrawLine(pen, _startPosX + 58, _startPosY + 35, _startPosX + 12, _startPosY + 5); @@ -130,7 +88,6 @@ namespace ProjectExcavator.DrawingObjects Point point15 = new Point(_startPosX + 14, _startPosY); Point[] krepl = { point12, point13, point14, point15, point12 }; g.FillPolygon(additionalBrush, krepl); - DrawingKatki.Draw(_startPosX + 35, _startPosY + 10, EntityExcavator.BodyColor, g); } } } diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs new file mode 100644 index 0000000..e19b378 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ExcavatorGenericCollection.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectExcavator.DrawingObjects; +using ProjectExcavator.MovementStrategy; + +namespace ProjectExcavator.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawingExcavator + /// + /// + /// + internal class ExcavatorGenericCollection + where T : DrawingExcavator + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 200; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public ExcavatorGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(ExcavatorGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj) ?? -1; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(ExcavatorGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + return true; + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowExcavator() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + {//линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + DrawingExcavator? excavator = _collection.Get(i); + if (excavator == null) + continue; + int r = i / width; + int s = width - 1 - (i % width); + excavator.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight); + excavator.DrawTransport(g); + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs index 61b36ca..21f916d 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavator.Designer.cs @@ -28,134 +28,146 @@ /// private void InitializeComponent() { - this.pictureBoxExcavator = new System.Windows.Forms.PictureBox(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonCreateExKovsh = new System.Windows.Forms.Button(); - this.buttonCreateEx = new System.Windows.Forms.Button(); - this.buttonStep = new System.Windows.Forms.Button(); - this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxExcavator)).BeginInit(); - this.SuspendLayout(); + pictureBoxExcavator = new PictureBox(); + buttonLeft = new Button(); + buttonRight = new Button(); + buttonUp = new Button(); + buttonDown = new Button(); + buttonCreateExKovsh = new Button(); + buttonCreateEx = new Button(); + buttonStep = new Button(); + comboBoxStrategy = new ComboBox(); + buttonSelectExcavator = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit(); + SuspendLayout(); // // pictureBoxExcavator // - this.pictureBoxExcavator.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxExcavator.Location = new System.Drawing.Point(0, 0); - this.pictureBoxExcavator.Name = "pictureBoxExcavator"; - this.pictureBoxExcavator.Size = new System.Drawing.Size(884, 461); - this.pictureBoxExcavator.TabIndex = 0; - this.pictureBoxExcavator.TabStop = false; + pictureBoxExcavator.Dock = DockStyle.Fill; + pictureBoxExcavator.Location = new Point(0, 0); + pictureBoxExcavator.Name = "pictureBoxExcavator"; + pictureBoxExcavator.Size = new Size(884, 461); + pictureBoxExcavator.TabIndex = 0; + pictureBoxExcavator.TabStop = false; // // buttonLeft // - this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.BackgroundImage = global::ProjectExcavator.Properties.Resources.влево; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(762, 404); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 2; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click); + buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonLeft.BackgroundImage = Properties.Resources.влево; + buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; + buttonLeft.Location = new Point(762, 404); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new Size(30, 30); + buttonLeft.TabIndex = 2; + buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += buttonMove_Click; // // buttonRight // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::ProjectExcavator.Properties.Resources.право; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(842, 404); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 3; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); + buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonRight.BackgroundImage = Properties.Resources.право; + buttonRight.BackgroundImageLayout = ImageLayout.Zoom; + buttonRight.Location = new Point(842, 404); + buttonRight.Name = "buttonRight"; + buttonRight.Size = new Size(30, 30); + buttonRight.TabIndex = 3; + buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += buttonMove_Click; // // buttonUp // - this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.BackgroundImage = global::ProjectExcavator.Properties.Resources.up; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(803, 368); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 4; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click); + buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonUp.BackgroundImage = Properties.Resources.up; + buttonUp.BackgroundImageLayout = ImageLayout.Zoom; + buttonUp.Location = new Point(803, 368); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(30, 30); + buttonUp.TabIndex = 4; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += buttonMove_Click; // // buttonDown // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::ProjectExcavator.Properties.Resources.down; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(803, 404); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 5; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); + buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonDown.BackgroundImage = Properties.Resources.down; + buttonDown.BackgroundImageLayout = ImageLayout.Zoom; + buttonDown.Location = new Point(803, 404); + buttonDown.Name = "buttonDown"; + buttonDown.Size = new Size(30, 30); + buttonDown.TabIndex = 5; + buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += buttonMove_Click; // // buttonCreateExKovsh // - this.buttonCreateExKovsh.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreateExKovsh.Location = new System.Drawing.Point(12, 426); - this.buttonCreateExKovsh.Name = "buttonCreateExKovsh"; - this.buttonCreateExKovsh.Size = new System.Drawing.Size(180, 23); - this.buttonCreateExKovsh.TabIndex = 6; - this.buttonCreateExKovsh.Text = "Создать экскаватор с ковшом"; - this.buttonCreateExKovsh.UseVisualStyleBackColor = true; - this.buttonCreateExKovsh.Click += new System.EventHandler(this.buttonCreateExKovsh_Click); + buttonCreateExKovsh.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateExKovsh.Location = new Point(12, 426); + buttonCreateExKovsh.Name = "buttonCreateExKovsh"; + buttonCreateExKovsh.Size = new Size(180, 23); + buttonCreateExKovsh.TabIndex = 6; + buttonCreateExKovsh.Text = "Создать экскаватор с ковшом"; + buttonCreateExKovsh.UseVisualStyleBackColor = true; + buttonCreateExKovsh.Click += buttonCreateExKovsh_Click; // // buttonCreateEx // - this.buttonCreateEx.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreateEx.Location = new System.Drawing.Point(198, 426); - this.buttonCreateEx.Name = "buttonCreateEx"; - this.buttonCreateEx.Size = new System.Drawing.Size(133, 23); - this.buttonCreateEx.TabIndex = 7; - this.buttonCreateEx.Text = "Создать"; - this.buttonCreateEx.UseVisualStyleBackColor = true; - this.buttonCreateEx.Click += new System.EventHandler(this.buttonCreateEx_Click); + buttonCreateEx.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateEx.Location = new Point(198, 426); + buttonCreateEx.Name = "buttonCreateEx"; + buttonCreateEx.Size = new Size(133, 23); + buttonCreateEx.TabIndex = 7; + buttonCreateEx.Text = "Создать"; + buttonCreateEx.UseVisualStyleBackColor = true; + buttonCreateEx.Click += buttonCreateEx_Click; // // buttonStep // - this.buttonStep.Location = new System.Drawing.Point(797, 41); - this.buttonStep.Name = "buttonStep"; - this.buttonStep.Size = new System.Drawing.Size(75, 23); - this.buttonStep.TabIndex = 8; - this.buttonStep.Text = "Шаг"; - this.buttonStep.UseVisualStyleBackColor = true; - this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; // // comboBoxStrategy // - this.comboBoxStrategy.FormattingEnabled = true; - this.comboBoxStrategy.Location = new System.Drawing.Point(751, 12); - this.comboBoxStrategy.Name = "comboBoxStrategy"; - this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23); - this.comboBoxStrategy.TabIndex = 9; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); + comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 9; + // + // buttonSelectExcavator + // + buttonSelectExcavator.Location = new Point(370, 426); + buttonSelectExcavator.Name = "buttonSelectExcavator"; + buttonSelectExcavator.Size = new Size(105, 23); + buttonSelectExcavator.TabIndex = 10; + buttonSelectExcavator.Text = "Выбрать обьект"; + buttonSelectExcavator.UseVisualStyleBackColor = true; + buttonSelectExcavator.Click += ButtonSelectExcavator_Click; // // FormExcavator // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(884, 461); - this.Controls.Add(this.comboBoxStrategy); - this.Controls.Add(this.buttonStep); - this.Controls.Add(this.buttonCreateEx); - this.Controls.Add(this.buttonCreateExKovsh); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.pictureBoxExcavator); - this.Name = "FormExcavator"; - this.Text = "FormExcavator"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxExcavator)).EndInit(); - this.ResumeLayout(false); - + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(884, 461); + Controls.Add(buttonSelectExcavator); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonStep); + Controls.Add(buttonCreateEx); + Controls.Add(buttonCreateExKovsh); + Controls.Add(buttonDown); + Controls.Add(buttonUp); + Controls.Add(buttonRight); + Controls.Add(buttonLeft); + Controls.Add(pictureBoxExcavator); + Name = "FormExcavator"; + Text = "FormExcavator"; + ((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).EndInit(); + ResumeLayout(false); } #endregion @@ -169,5 +181,6 @@ private Button buttonCreateEx; private Button buttonStep; private ComboBox comboBoxStrategy; + private Button buttonSelectExcavator; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavator.cs b/ProjectExcavator/ProjectExcavator/FormExcavator.cs index e1a68aa..e7f0867 100644 --- a/ProjectExcavator/ProjectExcavator/FormExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/FormExcavator.cs @@ -11,6 +11,7 @@ namespace ProjectExcavator /// /// private AbstractStrategy? _abstractStrategy; + public DrawingExcavator? SelectedExcavator { get; private set; } public FormExcavator() { InitializeComponent(); @@ -116,5 +117,10 @@ namespace ProjectExcavator _abstractStrategy = null; } } + private void ButtonSelectExcavator_Click(object sender, EventArgs e) + { + SelectedExcavator = _drawingExcavator; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs new file mode 100644 index 0000000..50d106a --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.Designer.cs @@ -0,0 +1,133 @@ +namespace ProjectExcavator +{ + partial class FormExcavatorCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddEx = new System.Windows.Forms.Button(); + this.buttonRemoveEx = new System.Windows.Forms.Button(); + this.buttonRefreshCollection = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.buttonGeneration = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.groupBox1.SuspendLayout(); + this.SuspendLayout(); + // + // pictureBoxCollection + // + this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(909, 461); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(26, 125); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(100, 23); + this.maskedTextBoxNumber.TabIndex = 1; + // + // buttonAddEx + // + this.buttonAddEx.Location = new System.Drawing.Point(6, 57); + this.buttonAddEx.Name = "buttonAddEx"; + this.buttonAddEx.Size = new System.Drawing.Size(150, 23); + this.buttonAddEx.TabIndex = 2; + this.buttonAddEx.Text = "Добавить экскаватор"; + this.buttonAddEx.UseVisualStyleBackColor = true; + this.buttonAddEx.Click += new System.EventHandler(this.ButtonAddEx_Click); + // + // buttonRemoveEx + // + this.buttonRemoveEx.Location = new System.Drawing.Point(6, 166); + this.buttonRemoveEx.Name = "buttonRemoveEx"; + this.buttonRemoveEx.Size = new System.Drawing.Size(150, 23); + this.buttonRemoveEx.TabIndex = 3; + this.buttonRemoveEx.Text = "Удалить экскаватор"; + this.buttonRemoveEx.UseVisualStyleBackColor = true; + // + // buttonRefreshCollection + // + this.buttonRefreshCollection.Location = new System.Drawing.Point(6, 230); + this.buttonRefreshCollection.Name = "buttonRefreshCollection"; + this.buttonRefreshCollection.Size = new System.Drawing.Size(148, 23); + this.buttonRefreshCollection.TabIndex = 4; + this.buttonRefreshCollection.Text = "Обновить коллекцию"; + this.buttonRefreshCollection.UseVisualStyleBackColor = true; + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.buttonGeneration); + this.groupBox1.Controls.Add(this.buttonAddEx); + this.groupBox1.Controls.Add(this.buttonRefreshCollection); + this.groupBox1.Controls.Add(this.maskedTextBoxNumber); + this.groupBox1.Controls.Add(this.buttonRemoveEx); + this.groupBox1.Location = new System.Drawing.Point(749, 0); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(160, 461); + this.groupBox1.TabIndex = 5; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; + // + // buttonGeneration + // + this.buttonGeneration.Location = new System.Drawing.Point(6, 332); + this.buttonGeneration.Name = "buttonGeneration"; + this.buttonGeneration.Size = new System.Drawing.Size(150, 23); + this.buttonGeneration.TabIndex = 5; + this.buttonGeneration.Text = "Форма генерации"; + this.buttonGeneration.UseVisualStyleBackColor = true; + this.buttonGeneration.Click += new System.EventHandler(this.buttonGeneration_Click); + // + // FormExcavatorCollection + // + this.ClientSize = new System.Drawing.Size(909, 461); + this.Controls.Add(this.groupBox1); + this.Controls.Add(this.pictureBoxCollection); + this.Name = "FormExcavatorCollection"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonAddEx; + private Button buttonRemoveEx; + private Button buttonRefreshCollection; + private GroupBox groupBox1; + private Button buttonGeneration; + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs new file mode 100644 index 0000000..f2aec94 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.cs @@ -0,0 +1,87 @@ +using ProjectExcavator.DrawingObjects; +using ProjectExcavator.Generics; +using ProjectExcavator.MovementStrategy; + +namespace ProjectExcavator +{ + /// + /// Форма для работы с набором объектов класса DrawningExcavator + /// + public partial class FormExcavatorCollection : Form + { + /// + /// Набор объектов + /// + private readonly ExcavatorGenericCollection _excavator; + /// + /// Конструктор + /// + public FormExcavatorCollection() + { + InitializeComponent(); + _excavator = new ExcavatorGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddEx_Click(object sender, EventArgs e) + { + FormExcavator form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + int result = _excavator + form.SelectedExcavator; + if (result != -2) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _excavator.ShowExcavator(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveEx_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_excavator - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _excavator.ShowExcavator(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _excavator.ShowExcavator(); + } + + private void buttonGeneration_Click(object sender, EventArgs e) + { + RandGeneration form = new(); + form.ShowDialog(); + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.resx b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/FormExcavatorCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/IDrawingKatki.cs b/ProjectExcavator/ProjectExcavator/IDrawingKatki.cs index 8fcfafb..1d7fb45 100644 --- a/ProjectExcavator/ProjectExcavator/IDrawingKatki.cs +++ b/ProjectExcavator/ProjectExcavator/IDrawingKatki.cs @@ -10,5 +10,7 @@ namespace ProjectExcavator { int Properties { get; set; } void Draw(int _startPosX, int _startPosY, Color katkiColor, Graphics g); + public int GetAmount(); + public int GetShape(); } } diff --git a/ProjectExcavator/ProjectExcavator/ParamGenericObject.cs b/ProjectExcavator/ProjectExcavator/ParamGenericObject.cs new file mode 100644 index 0000000..cb44169 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ParamGenericObject.cs @@ -0,0 +1,79 @@ +using ProjectExcavator; +using ProjectExcavator.DrawingObjects; +using ProjectExcavator.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator +{ + public class ParamGenericObject + where T : EntityExcavator + where U : IDrawingKatki + { + private T[] _excavators; + private U[] _katki; + + private int CountExcavators = 0; + private int CountKatki = 0; + private readonly Random random; + private readonly int Width; + private readonly int Height; + + public ParamGenericObject(int count, int width, int height) + { + _excavators = new T[count]; + _katki = new U[count]; + random = new Random(); + Width = width; + Height = height; + } + + + + public bool Add(T excavator) + { + for (int i = 0; i < _excavators.Length; i++) + { + if (_excavators[i] == null) + { + _excavators[i] = excavator; + CountExcavators++; + return true; + } + } + return false; + } + + public bool Add(U katki) + { + for (int i = 0; i < _excavators.Length; i++) + { + if (_katki[i] == null) + { + _katki[i] = katki; + CountKatki++; + return true; + } + } + return false; + } + + public DrawingExcavator CreateDrawObject() + { + Random rand = new Random(); + EntityExcavator excavator = _excavators[rand.Next(0, CountExcavators)]; + IDrawingKatki katki = _katki[rand.Next(0, CountKatki)]; + if (excavator is EntityExcavatorKovsh kovshExcavator) + { + return new DrawingExcavatorKovsh(excavator.Speed, excavator.Weight, excavator.BodyColor, kovshExcavator.AdditionalColor, kovshExcavator.Kovsh, kovshExcavator.Katki, + Width, Height, katki.GetAmount(), katki.GetShape()); + } + + return new DrawingExcavator(excavator.Speed, excavator.Weight, excavator.BodyColor, Width, Height, katki.GetAmount(), katki.GetShape()); + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/Program.cs b/ProjectExcavator/ProjectExcavator/Program.cs index 59b8c54..8be0f08 100644 --- a/ProjectExcavator/ProjectExcavator/Program.cs +++ b/ProjectExcavator/ProjectExcavator/Program.cs @@ -11,7 +11,7 @@ namespace ProjectExcavator // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormExcavator()); + Application.Run(new FormExcavatorCollection()); } } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/RandGeneration.Designer.cs b/ProjectExcavator/ProjectExcavator/RandGeneration.Designer.cs new file mode 100644 index 0000000..dc587d7 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/RandGeneration.Designer.cs @@ -0,0 +1,74 @@ +namespace ProjectExcavator +{ + partial class RandGeneration + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonGen = new System.Windows.Forms.Button(); + this.pictureBoxGen = new System.Windows.Forms.PictureBox(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGen)).BeginInit(); + this.SuspendLayout(); + // + // buttonGen + // + this.buttonGen.Location = new System.Drawing.Point(12, 415); + this.buttonGen.Name = "buttonGen"; + this.buttonGen.Size = new System.Drawing.Size(166, 23); + this.buttonGen.TabIndex = 0; + this.buttonGen.Text = "Сгенерировать"; + this.buttonGen.UseVisualStyleBackColor = true; + this.buttonGen.Click += new System.EventHandler(this.buttonGen_Click); + // + // pictureBoxGen + // + this.pictureBoxGen.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxGen.Location = new System.Drawing.Point(0, 0); + this.pictureBoxGen.Name = "pictureBoxGen"; + this.pictureBoxGen.Size = new System.Drawing.Size(800, 450); + this.pictureBoxGen.TabIndex = 1; + this.pictureBoxGen.TabStop = false; + // + // RandGeneration + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonGen); + this.Controls.Add(this.pictureBoxGen); + this.Name = "RandGeneration"; + this.Text = "RandGeneration"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxGen)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private Button buttonGen; + private PictureBox pictureBoxGen; + } +} \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/RandGeneration.cs b/ProjectExcavator/ProjectExcavator/RandGeneration.cs new file mode 100644 index 0000000..26fb9cb --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/RandGeneration.cs @@ -0,0 +1,88 @@ +using ProjectExcavator.DrawingObjects; +using ProjectExcavator.Entities; +using ProjectExcavator; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ProjectExcavator +{ + public partial class RandGeneration : Form + { + private DrawingExcavator _drawingExcavator; + private ParamGenericObject objGeneric; + private readonly int _pictureWidth = 250; + private readonly int _pictureHeight = 185; + Random random = new Random(); + public RandGeneration() + { + InitializeComponent(); + } + private void Draw() + { + if (_drawingExcavator == null) + { + return; + } + Bitmap bmp = new(pictureBoxGen.Width, pictureBoxGen.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawingExcavator.DrawTransport(gr); + pictureBoxGen.Image = bmp; + } + private void buttonGen_Click(object sender, EventArgs e) + { + int size = random.Next(1, 10); + objGeneric = new ParamGenericObject(size, _pictureWidth, _pictureHeight); + for (int i = 0; i < size; i++) + { + EntityExcavator excavator = CreateRandomExcavator(); + IDrawingKatki katki = CreateRandomKatki(); + objGeneric.Add(excavator); + objGeneric.Add(katki); + _drawingExcavator = objGeneric.CreateDrawObject(); + _drawingExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + } + public EntityExcavator CreateRandomExcavator() + { + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + EntityExcavator excavator; + switch (random.Next(0, 2)) + { + case 1: + excavator = new EntityExcavatorKovsh(random.Next(100, 300), random.Next(1000, 3000), color, dopColor, Convert.ToBoolean(random.Next(2)), Convert.ToBoolean(random.Next(2))); + break; + default: + excavator = new EntityExcavator(random.Next(100, 300), random.Next(1000, 3000), color); + break; + } + return excavator; + } + public IDrawingKatki CreateRandomKatki() + { + IDrawingKatki _katki; + switch (random.Next(3)) + { + case 1: + _katki = new DrawKatkiTriangle(); + break; + case 2: + _katki = new DrawKatkiSquare(); + break; + default: + _katki = new DrawKatkiCircle(); + break; + } + _katki.Properties = (random.Next(1, 4)); + return _katki; + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/RandGeneration.resx b/ProjectExcavator/ProjectExcavator/RandGeneration.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/RandGeneration.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/SetGeneric.cs b/ProjectExcavator/ProjectExcavator/SetGeneric.cs new file mode 100644 index 0000000..883a270 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/SetGeneric.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый экскаватор + /// + public int Insert(T excavator) + { + for (int i = _places.Length - 1; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = excavator; + return Insert(excavator, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый экскаватор + /// Позиция + /// + public int Insert(T excavator, int position) + { + if (position < 0 || position >= _places.Length) + { + return -1; + } + + if (_places[position] != null) + { + return -1; + } + + int NullIndex = 0; + for (int i = position; i < _places.Length; i++) + { + if (_places[i] == null) + { + NullIndex = i; + break; + } + } + + if (NullIndex == 0) + { + return -1; + } + + for (int i = NullIndex; i >= position; i--) + { + _places[i + 1] = _places[i]; + } + _places[position] = excavator; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= _places.Length) + return null; + + return _places[position]; + } + } +}