From 84be2f6a1cf26ba704000513d9077182aed48492 Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 21:44:09 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ElectricLocomotive/Direction.cs | 2 +- .../DrawningElectricLocomotive.cs | 8 +- .../ElectricLocomotive/DrawningLocomotive.cs | 41 ++-- .../DrawningObjectLocomotive.cs | 6 +- .../ElectricLocomotive/EntityLocomotive.cs | 2 +- .../FormLocomotive.Designer.cs | 14 ++ .../ElectricLocomotive/FormLocomotive.cs | 91 +++++---- .../ElectricLocomotive/FormMap.cs | 105 ----------- ...s => FormMapWithSetLocomotive.Designer.cs} | 175 +++++++++--------- .../FormMapWithSetLocomotive.cs | 168 +++++++++++++++++ ...Map.resx => FormMapWithSetLocomotive.resx} | 3 - .../MapWithSetLocomotivGeneric.cs | 162 ++++++++++++++++ .../ElectricLocomotive/Program.cs | 2 +- .../ElectricLocomotive/SetLocomotivGeneric.cs | 80 ++++++++ 14 files changed, 603 insertions(+), 256 deletions(-) delete mode 100644 ElectricLocomotive/ElectricLocomotive/FormMap.cs rename ElectricLocomotive/ElectricLocomotive/{FormMap.Designer.cs => FormMapWithSetLocomotive.Designer.cs} (61%) create mode 100644 ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs rename ElectricLocomotive/ElectricLocomotive/{FormMap.resx => FormMapWithSetLocomotive.resx} (93%) create mode 100644 ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs create mode 100644 ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs diff --git a/ElectricLocomotive/ElectricLocomotive/Direction.cs b/ElectricLocomotive/ElectricLocomotive/Direction.cs index a1ba87f..50cc2b1 100644 --- a/ElectricLocomotive/ElectricLocomotive/Direction.cs +++ b/ElectricLocomotive/ElectricLocomotive/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ElectricLocomotive { - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs index f43a454..c087fe4 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs @@ -27,14 +27,14 @@ namespace ElectricLocomotive if (sportCar.BodyKit) { - Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 15, Convert.ToInt32(_startPosY) - 20), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) - 30), new Point(Convert.ToInt32(_startPosX) + 75, Convert.ToInt32(_startPosY) - 20), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) + 10) }; - g.DrawPolygon(window, pts); + Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 15, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY)), new Point(Convert.ToInt32(_startPosX) + 75, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 45, Convert.ToInt32(_startPosY) + 40) }; + g.DrawPolygon(Pens.Black, pts); } if (sportCar.SportLine) { - Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 150, Convert.ToInt32(_startPosY) + 60)}; - g.FillPolygon(Brushes.Black, pts); + Point[] pts = { new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 90), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 150, Convert.ToInt32(_startPosY) + 90) }; + g.FillPolygon(dopBrush, pts); } base.DrawTransport(g); diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs index 48f0f2c..8c9b34d 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ElectricLocomotive { - internal class DrawningLocomotive + public class DrawningLocomotive { public EntityLocomotive Locomotive { get; protected set; } protected float _startPosX; @@ -16,7 +16,7 @@ namespace ElectricLocomotive protected int? _pictureWidth = null; protected int? _pictureHeight = null; - protected readonly int _locomotiveWidth = 160; + protected readonly int _locomotiveWidth = 200; protected readonly int _locomotiveHeight = 90; public DrawningLocomotive(int speed, float weight, Color bodyColor) { @@ -31,8 +31,8 @@ namespace ElectricLocomotive return; } - _startPosX = x + 20; - _startPosY = y + 30; + _startPosX = x + 10; + _startPosY = y; _pictureWidth = width; _pictureHeight = height; } @@ -53,14 +53,14 @@ namespace ElectricLocomotive break; //влево case Direction.Left: - if(_startPosX - Locomotive.Step > 0) + if (_startPosX - Locomotive.Step > 0) { _startPosX -= Locomotive.Step; } break; //вверх case Direction.Up: - if(_startPosY - Locomotive.Step - 30 > 0) + if (_startPosY - Locomotive.Step - 30 > 0) { _startPosY -= Locomotive.Step; } @@ -82,6 +82,7 @@ namespace ElectricLocomotive _locomotiveWidth = locomotiveWidth; _locomotiveHeight = locomotiveHeight; } + public virtual void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 @@ -94,37 +95,37 @@ namespace ElectricLocomotive Pen pen = new Pen(Color.Black); Pen window = new Pen(Color.Blue); - g.FillPolygon(brBody, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60) }); - g.FillRectangle(blackturbo, _startPosX + 5, _startPosY + 15, 5, 40); + g.FillPolygon(brBody, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 90), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 40), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 40), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 90) }); + g.FillRectangle(blackturbo, _startPosX + 5, _startPosY + 45, 5, 40); - g.FillEllipse(brBody, _startPosX + 15, _startPosY + 60, 20, 20); - g.FillEllipse(brBody, _startPosX + 45, _startPosY + 60, 20, 20); + g.FillEllipse(brBody, _startPosX + 15, _startPosY + 90, 20, 20); + g.FillEllipse(brBody, _startPosX + 45, _startPosY + 90, 20, 20); - g.FillEllipse(brBody, _startPosX + 85, _startPosY + 60, 20, 20); - g.FillEllipse(brBody, _startPosX + 115, _startPosY + 60, 20, 20); + g.FillEllipse(brBody, _startPosX + 85, _startPosY + 90, 20, 20); + g.FillEllipse(brBody, _startPosX + 115, _startPosY + 90, 20, 20); // Окна - g.DrawRectangle(window, _startPosX + 20, _startPosY + 20, 20, 25); - g.DrawRectangle(window, _startPosX + 50, _startPosY + 20, 20, 25); + g.DrawRectangle(window, _startPosX + 20, _startPosY + 50, 20, 25); + g.DrawRectangle(window, _startPosX + 50, _startPosY + 50, 20, 25); // Колёса - g.DrawEllipse(pen, _startPosX + 15, _startPosY + 60, 20, 20); - g.DrawEllipse(pen, _startPosX + 45, _startPosY + 60, 20, 20); + g.DrawEllipse(pen, _startPosX + 15, _startPosY + 90, 20, 20); + g.DrawEllipse(pen, _startPosX + 45, _startPosY + 90, 20, 20); - g.DrawEllipse(pen, _startPosX + 85, _startPosY + 60, 20, 20); - g.DrawEllipse(pen, _startPosX + 115, _startPosY + 60, 20, 20); + g.DrawEllipse(pen, _startPosX + 85, _startPosY + 90, 20, 20); + g.DrawEllipse(pen, _startPosX + 115, _startPosY + 90, 20, 20); // Дверь - g.DrawRectangle(pen, _startPosX + 85, _startPosY + 15, 20, 40); + g.DrawRectangle(pen, _startPosX + 85, _startPosY + 45, 20, 40); // Локомотив - g.DrawPolygon(pen, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 10), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 30), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60) }); + g.DrawPolygon(pen, new Point[] { new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 90), new Point(Convert.ToInt32(_startPosX) + 10, Convert.ToInt32(_startPosY) + 40), new Point(Convert.ToInt32(_startPosX) + 110, Convert.ToInt32(_startPosY) + 40), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 60), new Point(Convert.ToInt32(_startPosX) + 140, Convert.ToInt32(_startPosY) + 90) }); } public void ChangeBorders(int width, int height) diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs index 5bf23d0..6e45855 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs @@ -9,12 +9,16 @@ namespace ElectricLocomotive internal class DrawningObjectLocomotive : IDrawningObject { private DrawningLocomotive _locomotive = null; - public DrawningObjectLocomotive(DrawningLocomotive locomotive) { _locomotive = locomotive; } + public void Metod(int x) + { + x = 150; + Console.WriteLine(x); + } public float Step => _locomotive?.Locomotive?.Step ?? 0; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() diff --git a/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs index dca2328..6452613 100644 --- a/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace ElectricLocomotive { - internal class EntityLocomotive + public class EntityLocomotive { public int Speed { get; private set; } public float Weight { get; private set; } diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs index d50fedc..0ee7bcd 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs @@ -39,6 +39,7 @@ this.buttonRight = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonselectlocomotive = new System.Windows.Forms.Button(); this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); @@ -95,6 +96,7 @@ this.pictureBox1.Size = new System.Drawing.Size(800, 428); this.pictureBox1.TabIndex = 2; this.pictureBox1.TabStop = false; + this.pictureBox1.Click += new System.EventHandler(this.ButtonCreateModif_Click); // // buttonLeft // @@ -158,11 +160,22 @@ this.buttonCreateModif.UseVisualStyleBackColor = true; this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); // + // buttonselectlocomotive + // + this.buttonselectlocomotive.Location = new System.Drawing.Point(202, 396); + this.buttonselectlocomotive.Name = "buttonselectlocomotive"; + this.buttonselectlocomotive.Size = new System.Drawing.Size(82, 23); + this.buttonselectlocomotive.TabIndex = 8; + this.buttonselectlocomotive.Text = "Выбрать"; + this.buttonselectlocomotive.UseVisualStyleBackColor = true; + this.buttonselectlocomotive.Click += new System.EventHandler(this.ButtonSelectCar_Click); + // // FormLocomotive // 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.buttonselectlocomotive); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonRight); @@ -195,5 +208,6 @@ private Button buttonRight; private Button buttonUp; private Button buttonCreateModif; + private Button buttonselectlocomotive; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs index f6c1309..712c4b4 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs @@ -1,4 +1,4 @@ -using System; + using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -13,19 +13,14 @@ namespace ElectricLocomotive public partial class FormLocomotive : Form { private DrawningLocomotive _locomotive; + public DrawningLocomotive SelectedLocomotive { get; private set; } public FormLocomotive() { InitializeComponent(); } - - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - _locomotive = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(); - Draw(); - } - + /// + /// Метод прорисовки машины + /// private void Draw() { Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); @@ -34,15 +29,34 @@ namespace ElectricLocomotive pictureBox1.Image = bmp; } /// - /// Обработка нажатия кнопки "Создать" + /// Метод установки данных /// - /// - /// - /// - /// Изменение размеров формы - /// - /// - /// + private void SetData() + { + Random rnd = new(); + _locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), + pictureBox1.Width, pictureBox1.Height); + toolStripStatusLabelSpeed.Text = $"Скорость: {_locomotive.Locomotive.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {_locomotive.Locomotive.Weight}"; + toolStripStatusLabelColor.Text = $"Цвет: { _locomotive.Locomotive.BodyColor.Name} "; + } + + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random rnd = new(); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), + rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _locomotive = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), + color); + SetData(); + Draw(); + } + private void ButtonMove_Click(object sender, EventArgs e) { //получаем имя кнопки @@ -64,35 +78,42 @@ namespace ElectricLocomotive } Draw(); } - /// - /// Изменение размеров формы - /// - /// - /// + private void PictureBoxCar_Resize(object sender, EventArgs e) { _locomotive?.ChangeBorders(pictureBox1.Width, pictureBox1.Height); Draw(); } - private void SetData() - { - Random rnd = new(); - _locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBox1.Width, pictureBox1.Height); - toolStripStatusLabelSpeed.Text = $"Скорость: {_locomotive.Locomotive.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {_locomotive.Locomotive.Weight}"; - toolStripStatusLabelColor.Text = $"Цвет: {_locomotive.Locomotive.BodyColor.Name}"; - } - private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), + rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), + rnd.Next(0, 256)); + ColorDialog dialogDop = new(); + if (dialogDop.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDop.Color; + } _locomotive = new DrawningElectricLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + color, dopColor, + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, + 2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(); Draw(); } + + private void ButtonSelectCar_Click(object sender, EventArgs e) + { + SelectedLocomotive = _locomotive; + DialogResult = DialogResult.OK; + } } } diff --git a/ElectricLocomotive/ElectricLocomotive/FormMap.cs b/ElectricLocomotive/ElectricLocomotive/FormMap.cs deleted file mode 100644 index 546a435..0000000 --- a/ElectricLocomotive/ElectricLocomotive/FormMap.cs +++ /dev/null @@ -1,105 +0,0 @@ -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 ElectricLocomotive -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - - private void SetData(DrawningLocomotive locomotive) - { - toolStripStatusLabelSpeed.Text = $"Скорость: {locomotive.Locomotive.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {locomotive.Locomotive.Weight}"; - toolStripStatusLabelColor.Text = $"Цвет: {locomotive.Locomotive.BodyColor.Name}"; - pictureBox1.Image = _abstractMap.CreateMap(pictureBox1.Width, pictureBox1.Height, - new DrawningObjectLocomotive(locomotive)); - } - /// - /// Обработка нажатия кнопки "Создать" - /// - /// - /// - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var car = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(car); - } - /// - /// Изменение размеров формы - /// - /// - /// - private void ButtonMove_Click(object sender, EventArgs e) - { - //получаем имя кнопки - string name = ((Button)sender)?.Name ?? string.Empty; - Direction dir = Direction.None; - switch (name) - { - case "buttonUp": - dir = Direction.Up; - break; - case "buttonDown": - dir = Direction.Down; - break; - case "buttonLeft": - dir = Direction.Left; - break; - case "buttonRight": - dir = Direction.Right; - break; - } - pictureBox1.Image = _abstractMap?.MoveObject(dir); - } - /// - /// Обработка нажатия кнопки "Модификация" - /// - /// - /// - private void ButtonCreateModif_Click(object sender, EventArgs e) - { - Random rnd = new(); - var car = new DrawningElectricLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); - SetData(car); - } - /// - /// Смена карты - /// - /// - /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - - case "Поле с грязью": - _abstractMap = new FieldMap(); - break; - case "Кусты на карте": - _abstractMap = new BushesMap(); - break; - } - } - - } -} diff --git a/ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs similarity index 61% rename from ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs rename to ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs index 67b4f68..994c835 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMap.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs @@ -1,6 +1,6 @@ namespace ElectricLocomotive { - partial class FormMap + partial class FormMapWithSetLocomotive { /// /// Required designer variable. @@ -28,34 +28,21 @@ /// private void InitializeComponent() { - this.ButtonCreate = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); - this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel(); - this.buttonCreateModif = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.buttonAddLocomotive = new System.Windows.Forms.Button(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonRemoveCar = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); - this.statusStrip1.SuspendLayout(); this.SuspendLayout(); // - // ButtonCreate - // - this.ButtonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.ButtonCreate.AutoSize = true; - this.ButtonCreate.Location = new System.Drawing.Point(12, 396); - this.ButtonCreate.Name = "ButtonCreate"; - this.ButtonCreate.Size = new System.Drawing.Size(75, 25); - this.ButtonCreate.TabIndex = 8; - this.ButtonCreate.Text = "Создать"; - this.ButtonCreate.UseVisualStyleBackColor = true; - this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); - // // pictureBox1 // this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -63,57 +50,17 @@ | System.Windows.Forms.AnchorStyles.Right))); this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(800, 428); + this.pictureBox1.Size = new System.Drawing.Size(631, 449); this.pictureBox1.TabIndex = 10; this.pictureBox1.TabStop = false; // - // statusStrip1 - // - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelColor}); - this.statusStrip1.Location = new System.Drawing.Point(0, 428); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 22); - this.statusStrip1.TabIndex = 9; - this.statusStrip1.Text = "statusStrip1"; - // - // toolStripStatusLabelSpeed - // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17); - this.toolStripStatusLabelSpeed.Text = "Скорость"; - // - // toolStripStatusLabelWeight - // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17); - this.toolStripStatusLabelWeight.Text = "Вес"; - // - // toolStripStatusLabelColor - // - this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor"; - this.toolStripStatusLabelColor.Size = new System.Drawing.Size(33, 17); - this.toolStripStatusLabelColor.Text = "Цвет"; - // - // buttonCreateModif - // - this.buttonCreateModif.Location = new System.Drawing.Point(93, 396); - this.buttonCreateModif.Name = "buttonCreateModif"; - this.buttonCreateModif.Size = new System.Drawing.Size(103, 23); - this.buttonCreateModif.TabIndex = 15; - this.buttonCreateModif.Text = "Модификация"; - this.buttonCreateModif.UseVisualStyleBackColor = true; - this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); - // // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.AutoSize = true; this.buttonUp.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowup; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(712, 353); + this.buttonUp.Location = new System.Drawing.Point(699, 372); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 14; @@ -126,7 +73,7 @@ this.buttonRight.AutoSize = true; this.buttonRight.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowright; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(748, 389); + this.buttonRight.Location = new System.Drawing.Point(735, 408); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 13; @@ -139,7 +86,7 @@ this.buttonDown.AutoSize = true; this.buttonDown.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowdown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(712, 389); + this.buttonDown.Location = new System.Drawing.Point(699, 408); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 12; @@ -152,64 +99,122 @@ this.buttonLeft.AutoSize = true; this.buttonLeft.BackgroundImage = global::ElectricLocomotive.Properties.Resources.arrowleft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(676, 389); + this.buttonLeft.Location = new System.Drawing.Point(663, 408); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 11; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(637, 9); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(83, 15); + this.label1.TabIndex = 15; + this.label1.Text = "Инструменты"; + // + // buttonAddLocomotive + // + this.buttonAddLocomotive.Location = new System.Drawing.Point(637, 98); + this.buttonAddLocomotive.Name = "buttonAddLocomotive"; + this.buttonAddLocomotive.Size = new System.Drawing.Size(151, 28); + this.buttonAddLocomotive.TabIndex = 17; + this.buttonAddLocomotive.Text = "Добавить локомотив"; + this.buttonAddLocomotive.UseVisualStyleBackColor = true; + this.buttonAddLocomotive.Click += new System.EventHandler(this.ButtonAddCar_Click); + // // comboBoxSelectorMap // this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", - "Поле с грязью", - "Кусты на карте"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); + "Карта с грязью", + "Карта с кустами"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(637, 43); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(104, 23); - this.comboBoxSelectorMap.TabIndex = 16; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 23); + this.comboBoxSelectorMap.TabIndex = 22; this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // - // FormMap + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(637, 151); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(151, 23); + this.maskedTextBoxPosition.TabIndex = 23; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRemoveCar + // + this.buttonRemoveCar.Location = new System.Drawing.Point(637, 178); + this.buttonRemoveCar.Name = "buttonRemoveCar"; + this.buttonRemoveCar.Size = new System.Drawing.Size(151, 28); + this.buttonRemoveCar.TabIndex = 24; + this.buttonRemoveCar.Text = "Удалить локомотив"; + this.buttonRemoveCar.UseVisualStyleBackColor = true; + this.buttonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(637, 239); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(151, 28); + this.buttonShowStorage.TabIndex = 25; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(637, 328); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(151, 28); + this.buttonShowOnMap.TabIndex = 26; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // FormMapWithSetLocomotive // 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.buttonRemoveCar); + this.Controls.Add(this.buttonShowStorage); + this.Controls.Add(this.buttonShowOnMap); + this.Controls.Add(this.maskedTextBoxPosition); this.Controls.Add(this.comboBoxSelectorMap); - this.Controls.Add(this.ButtonCreate); - this.Controls.Add(this.statusStrip1); - this.Controls.Add(this.buttonCreateModif); + this.Controls.Add(this.buttonAddLocomotive); + this.Controls.Add(this.label1); this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.pictureBox1); - this.Name = "FormMap"; - this.Text = "Карта"; + this.Name = "FormMapWithSetLocomotive"; + this.Text = "Карта с набором объектов"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); - this.statusStrip1.ResumeLayout(false); - this.statusStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion - - private Button ButtonCreate; private PictureBox pictureBox1; - private StatusStrip statusStrip1; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabelColor; - private Button buttonCreateModif; private Button buttonUp; private Button buttonRight; private Button buttonDown; private Button buttonLeft; + private Label label1; + private Button buttonAddLocomotive; private ComboBox comboBoxSelectorMap; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonRemoveCar; + private Button buttonShowStorage; + private Button buttonShowOnMap; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs new file mode 100644 index 0000000..fc656b3 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs @@ -0,0 +1,168 @@ +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 ElectricLocomotive +{ + public partial class FormMapWithSetLocomotive : Form + { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetLocomotivGeneric _mapCarsCollectionGeneric; + /// + /// Конструктор + /// + public FormMapWithSetLocomotive() + { + InitializeComponent(); + } + /// + /// Выбор карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, + EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Карта с грязью": + map = new FieldMap(); + break; + case "Карта с кустами": + map = new BushesMap(); + break; + } + if (map != null) + { + _mapCarsCollectionGeneric = new MapWithSetLocomotivGeneric(pictureBox1.Width, pictureBox1.Height, map); + } + else + { + _mapCarsCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddCar_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + FormLocomotive form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawningObjectLocomotive ship = new(form.SelectedLocomotive); + if (_mapCarsCollectionGeneric + ship != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox1.Image = _mapCarsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapCarsCollectionGeneric - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox1.Image = _mapCarsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + pictureBox1.Image = _mapCarsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + pictureBox1.Image = _mapCarsCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapCarsCollectionGeneric == null) + { + return; + } + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBox1.Image = _mapCarsCollectionGeneric.MoveObject(dir); + } + } +} + diff --git a/ElectricLocomotive/ElectricLocomotive/FormMap.resx b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx similarity index 93% rename from ElectricLocomotive/ElectricLocomotive/FormMap.resx rename to ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx index 5cb320f..f298a7b 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMap.resx +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.resx @@ -57,7 +57,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs new file mode 100644 index 0000000..6377c9c --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class MapWithSetLocomotivGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + // Ширина окна отрисовки + private readonly int _pictureWidth; + // Высота окна отрисовки + private readonly int _pictureHeight; + // Размер занимаемого объектом места (ширина) + private readonly int _placeSizeWidth = 180; + // Размер занимаемого объектом места (высота) + private readonly int _placeSizeHeight = 150; + // Набор объектов + private readonly SetLocomotivGeneric _setLocomotive; + // Карта + private readonly U _map; + + private readonly T[] _places; + // Конструктор + public MapWithSetLocomotivGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setLocomotive = new SetLocomotivGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + // Перегрузка оператора сложения + public static int operator +(MapWithSetLocomotivGeneric map, T Ship) + { + return map._setLocomotive.Insert(Ship); + } + // Перегрузка оператора вычитания + public static T operator -(MapWithSetLocomotivGeneric map, int position) + { + + return map._setLocomotive.Remove(position); + } + + public static bool operator >(MapWithSetLocomotivGeneric map, int Peremen) + { + return map._places.Length > Peremen; + } + // Перегрузка оператора вычитания + public static bool operator <(MapWithSetLocomotivGeneric map, int Peremen) + { + return map._places.Length < Peremen; + } + + // Вывод всего набора объектов + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawShip(gr); + return bmp; + } + // Просмотр объекта на карте + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setLocomotive.Count; i++) + { + var Ship = _setLocomotive.Get(i); + if (Ship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, Ship); + } + } + return new(_pictureWidth, _pictureHeight); + } + // Перемещение объекта по карте + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + // "Взбалтываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setLocomotive.Count - 1; + for (int i = 0; i < _setLocomotive.Count; i++) + { + if (_setLocomotive.Get(i) == null) + { + for (; j > i; j--) + { + var Ship = _setLocomotive.Get(j); + if (Ship != null) + { + _setLocomotive.Insert(Ship, i); + _setLocomotive.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + // Метод отрисовки фона + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Sienna, 3); + Brush brush = new SolidBrush(Color.DodgerBlue); + g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth / 2 + 40, j * _placeSizeHeight + 2); + } + g.DrawLine(pen, i * _placeSizeWidth + 7, 2, i * _placeSizeWidth + 7, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth + 3, 2, i * _placeSizeWidth + 3, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + // Метод прорисовки объектов + private void DrawShip(Graphics g) + { + int widthEl = _pictureWidth / _placeSizeWidth; + int heightEl = _pictureHeight / _placeSizeHeight; + + int curWidth = 0; + int curHeight = 0; + + for (int i = _setLocomotive.Count; i >= 0; i--) + { + _setLocomotive.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 85, + curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + _setLocomotive.Get(i)?.DrawningObject(g); + + if (curWidth < widthEl) + curWidth++; + else + { + curWidth = 1; + curHeight++; + } + if (curHeight > heightEl) + { + return; + } + + } + } + } +} \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/Program.cs b/ElectricLocomotive/ElectricLocomotive/Program.cs index 2cc6ef2..c8d658b 100644 --- a/ElectricLocomotive/ElectricLocomotive/Program.cs +++ b/ElectricLocomotive/ElectricLocomotive/Program.cs @@ -11,7 +11,7 @@ namespace ElectricLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetLocomotive()); } } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs new file mode 100644 index 0000000..c4077a9 --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/SetLocomotivGeneric.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectricLocomotive +{ + internal class SetLocomotivGeneric +where T : class + { + // Массив объектов, которые храним + private readonly T[] _places; + // Количество объектов в массиве + public int Count => _places.Length; + // Конструктор + public SetLocomotivGeneric(int count) + { + _places = new T[count]; + } + // Добавление объекта в набор + public int Insert(T Locomotive) + { + // вставка в начало набора + return Insert(Locomotive, 0); + } + // Добавление объекта в набор на конкретную позицию + public int Insert(T Locomotive, int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) + return -1; + //проверка, что элемент массива по этой позиции пустой, если нет, то + if (_places[position] == null) + { + _places[position] = Locomotive; + return position; + } + //проверка, что после вставляемого элемента в массиве есть пустой элемент + int findEmptyPos = -1; + + for (int i = position + 1; i < Count; i++) + { + if (_places[i] == null) + { + findEmptyPos = i; + break; + } + } + if (findEmptyPos < 0) return -1; + + //сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + for (int i = findEmptyPos; i > position; i--) + { + _places[i] = _places[i - 1]; + } + // вставка по позиции + _places[position] = Locomotive; + return position; + } + // Удаление объекта из набора с конкретной позиции + public T Remove(int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) return null; + // удаление объекта из массива, присовив элементу массива значение null + T temp = _places[position]; + _places[position] = null; + return temp; + } + // Получение объекта из набора по позиции + public T Get(int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) + return null; + return _places[position]; + } + } +} -- 2.25.1 From 5879f21e46d5ff6540e85cb6a691f7e512c21277 Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 21:59:35 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningObjectLocomotive.cs | 6 -- .../FormLocomotive.Designer.cs | 2 +- .../ElectricLocomotive/FormLocomotive.cs | 11 +-- .../FormMapWithSetLocomotive.Designer.cs | 4 +- .../FormMapWithSetLocomotive.cs | 70 +++++-------------- .../MapWithSetLocomotivGeneric.cs | 20 +++--- 6 files changed, 32 insertions(+), 81 deletions(-) diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs index 6e45855..53378eb 100644 --- a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs @@ -13,12 +13,6 @@ namespace ElectricLocomotive { _locomotive = locomotive; } - - public void Metod(int x) - { - x = 150; - Console.WriteLine(x); - } public float Step => _locomotive?.Locomotive?.Step ?? 0; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs index 0ee7bcd..4ac4aa2 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.Designer.cs @@ -168,7 +168,7 @@ this.buttonselectlocomotive.TabIndex = 8; this.buttonselectlocomotive.Text = "Выбрать"; this.buttonselectlocomotive.UseVisualStyleBackColor = true; - this.buttonselectlocomotive.Click += new System.EventHandler(this.ButtonSelectCar_Click); + this.buttonselectlocomotive.Click += new System.EventHandler(this.ButtonSelectLocomotive_Click); // // FormLocomotive // diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs index 712c4b4..f09b942 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotive.cs @@ -18,9 +18,6 @@ namespace ElectricLocomotive { InitializeComponent(); } - /// - /// Метод прорисовки машины - /// private void Draw() { Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); @@ -28,9 +25,6 @@ namespace ElectricLocomotive _locomotive?.DrawTransport(gr); pictureBox1.Image = bmp; } - /// - /// Метод установки данных - /// private void SetData() { Random rnd = new(); @@ -40,7 +34,6 @@ namespace ElectricLocomotive toolStripStatusLabelWeight.Text = $"Вес: {_locomotive.Locomotive.Weight}"; toolStripStatusLabelColor.Text = $"Цвет: { _locomotive.Locomotive.BodyColor.Name} "; } - private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); @@ -79,7 +72,7 @@ namespace ElectricLocomotive Draw(); } - private void PictureBoxCar_Resize(object sender, EventArgs e) + private void PictureBoxLocomotive_Resize(object sender, EventArgs e) { _locomotive?.ChangeBorders(pictureBox1.Width, pictureBox1.Height); Draw(); @@ -110,7 +103,7 @@ namespace ElectricLocomotive Draw(); } - private void ButtonSelectCar_Click(object sender, EventArgs e) + private void ButtonSelectLocomotive_Click(object sender, EventArgs e) { SelectedLocomotive = _locomotive; DialogResult = DialogResult.OK; diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs index 994c835..9f7b19b 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.Designer.cs @@ -123,7 +123,7 @@ this.buttonAddLocomotive.TabIndex = 17; this.buttonAddLocomotive.Text = "Добавить локомотив"; this.buttonAddLocomotive.UseVisualStyleBackColor = true; - this.buttonAddLocomotive.Click += new System.EventHandler(this.ButtonAddCar_Click); + this.buttonAddLocomotive.Click += new System.EventHandler(this.ButtonAddLocomotive_Click); // // comboBoxSelectorMap // @@ -156,7 +156,7 @@ this.buttonRemoveCar.TabIndex = 24; this.buttonRemoveCar.Text = "Удалить локомотив"; this.buttonRemoveCar.UseVisualStyleBackColor = true; - this.buttonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click); + this.buttonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveLocomotive_Click); // // buttonShowStorage // diff --git a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs index fc656b3..1bddbe1 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormMapWithSetLocomotive.cs @@ -12,22 +12,11 @@ namespace ElectricLocomotive { public partial class FormMapWithSetLocomotive : Form { - /// - /// Объект от класса карты с набором объектов - /// - private MapWithSetLocomotivGeneric _mapCarsCollectionGeneric; - /// - /// Конструктор - /// + private MapWithSetLocomotivGeneric _mapLocomotivesCollectionGeneric; public FormMapWithSetLocomotive() { InitializeComponent(); } - /// - /// Выбор карты - /// - /// - /// private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { @@ -46,32 +35,27 @@ namespace ElectricLocomotive } if (map != null) { - _mapCarsCollectionGeneric = new MapWithSetLocomotivGeneric(pictureBox1.Width, pictureBox1.Height, map); + _mapLocomotivesCollectionGeneric = new MapWithSetLocomotivGeneric(pictureBox1.Width, pictureBox1.Height, map); } else { - _mapCarsCollectionGeneric = null; + _mapLocomotivesCollectionGeneric = null; } } - /// - /// Добавление объекта - /// - /// - /// - private void ButtonAddCar_Click(object sender, EventArgs e) + private void ButtonAddLocomotive_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapLocomotivesCollectionGeneric == null) { return; } FormLocomotive form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawningObjectLocomotive ship = new(form.SelectedLocomotive); - if (_mapCarsCollectionGeneric + ship != -1) + DrawningObjectLocomotive Locomotive = new(form.SelectedLocomotive); + if (_mapLocomotivesCollectionGeneric + Locomotive != -1) { MessageBox.Show("Объект добавлен"); - pictureBox1.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); } else { @@ -79,12 +63,7 @@ namespace ElectricLocomotive } } } - /// - /// Удаление объекта - /// - /// - /// - private void ButtonRemoveCar_Click(object sender, EventArgs e) + private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { @@ -96,50 +75,35 @@ namespace ElectricLocomotive return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapCarsCollectionGeneric - pos != null) + if (_mapLocomotivesCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); - pictureBox1.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); } } - /// - /// Вывод набора - /// - /// - /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapLocomotivesCollectionGeneric == null) { return; } - pictureBox1.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowSet(); } - /// - /// Вывод карты - /// - /// - /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapLocomotivesCollectionGeneric == null) { return; } - pictureBox1.Image = _mapCarsCollectionGeneric.ShowOnMap(); + pictureBox1.Image = _mapLocomotivesCollectionGeneric.ShowOnMap(); } - /// - /// Перемещение - /// - /// - /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapLocomotivesCollectionGeneric == null) { return; } @@ -161,7 +125,7 @@ namespace ElectricLocomotive dir = Direction.Right; break; } - pictureBox1.Image = _mapCarsCollectionGeneric.MoveObject(dir); + pictureBox1.Image = _mapLocomotivesCollectionGeneric.MoveObject(dir); } } } diff --git a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs index 6377c9c..9a1bdfe 100644 --- a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs @@ -35,9 +35,9 @@ namespace ElectricLocomotive _map = map; } // Перегрузка оператора сложения - public static int operator +(MapWithSetLocomotivGeneric map, T Ship) + public static int operator +(MapWithSetLocomotivGeneric map, T Locomotive) { - return map._setLocomotive.Insert(Ship); + return map._setLocomotive.Insert(Locomotive); } // Перегрузка оператора вычитания public static T operator -(MapWithSetLocomotivGeneric map, int position) @@ -62,7 +62,7 @@ namespace ElectricLocomotive Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); - DrawShip(gr); + DrawLocomotive(gr); return bmp; } // Просмотр объекта на карте @@ -71,10 +71,10 @@ namespace ElectricLocomotive Shaking(); for (int i = 0; i < _setLocomotive.Count; i++) { - var Ship = _setLocomotive.Get(i); - if (Ship != null) + var Locomotive = _setLocomotive.Get(i); + if (Locomotive != null) { - return _map.CreateMap(_pictureWidth, _pictureHeight, Ship); + return _map.CreateMap(_pictureWidth, _pictureHeight, Locomotive); } } return new(_pictureWidth, _pictureHeight); @@ -98,10 +98,10 @@ namespace ElectricLocomotive { for (; j > i; j--) { - var Ship = _setLocomotive.Get(j); - if (Ship != null) + var Locomotive = _setLocomotive.Get(j); + if (Locomotive != null) { - _setLocomotive.Insert(Ship, i); + _setLocomotive.Insert(Locomotive, i); _setLocomotive.Remove(j); break; } @@ -130,7 +130,7 @@ namespace ElectricLocomotive } } // Метод прорисовки объектов - private void DrawShip(Graphics g) + private void DrawLocomotive(Graphics g) { int widthEl = _pictureWidth / _placeSizeWidth; int heightEl = _pictureHeight / _placeSizeHeight; -- 2.25.1 From 8e5e30238f92e981569f50f9fc4aaf850cb86bfc Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Sat, 19 Nov 2022 22:07:10 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ElectricLocomotive/MapWithSetLocomotivGeneric.cs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs index 9a1bdfe..1647836 100644 --- a/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/MapWithSetLocomotivGeneric.cs @@ -45,17 +45,6 @@ namespace ElectricLocomotive return map._setLocomotive.Remove(position); } - - public static bool operator >(MapWithSetLocomotivGeneric map, int Peremen) - { - return map._places.Length > Peremen; - } - // Перегрузка оператора вычитания - public static bool operator <(MapWithSetLocomotivGeneric map, int Peremen) - { - return map._places.Length < Peremen; - } - // Вывод всего набора объектов public Bitmap ShowSet() { -- 2.25.1