From 00d9cfc7d1d78357508ecebf72823e560a17bff8 Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 26 Nov 2023 17:03:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=D1=8E=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/FormAirBomber.Designer.cs | 65 +++++++++++++--- AirBomber/AirBomber/FormAirBomber.cs | 77 +++++++++++++++---- AirBomber/AirBomber/MoveToBorder.cs | 26 +++++++ AirBomber/AirBomber/MoveToCenter.cs | 56 ++++++++++++++ 4 files changed, 198 insertions(+), 26 deletions(-) create mode 100644 AirBomber/AirBomber/MoveToBorder.cs create mode 100644 AirBomber/AirBomber/MoveToCenter.cs diff --git a/AirBomber/AirBomber/FormAirBomber.Designer.cs b/AirBomber/AirBomber/FormAirBomber.Designer.cs index c0d96ee..a8ba700 100644 --- a/AirBomber/AirBomber/FormAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormAirBomber.Designer.cs @@ -28,25 +28,28 @@ /// private void InitializeComponent() { - buttonCreate = new Button(); + buttonCreateAirBomber = new Button(); buttonLeft = new Button(); buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); pictureBoxAirBomber = new PictureBox(); + buttonCreateAirPlane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); SuspendLayout(); // - // buttonCreate + // buttonCreateAirBomber // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(47, 364); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(186, 45); - buttonCreate.TabIndex = 0; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateAirBomber.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAirBomber.Location = new Point(40, 343); + buttonCreateAirBomber.Name = "buttonCreateAirBomber"; + buttonCreateAirBomber.Size = new Size(186, 66); + buttonCreateAirBomber.TabIndex = 0; + buttonCreateAirBomber.Text = "Создать бомбардировщик"; + buttonCreateAirBomber.UseVisualStyleBackColor = true; + buttonCreateAirBomber.Click += buttonCreateAirBomber_Click; // // buttonLeft // @@ -105,16 +108,51 @@ pictureBoxAirBomber.TabIndex = 5; pictureBoxAirBomber.TabStop = false; // + // buttonCreateAirPlane + // + buttonCreateAirPlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAirPlane.Location = new Point(248, 343); + buttonCreateAirPlane.Name = "buttonCreateAirPlane"; + buttonCreateAirPlane.Size = new Size(186, 66); + buttonCreateAirPlane.TabIndex = 6; + buttonCreateAirPlane.Text = "Создать самолёт"; + buttonCreateAirPlane.UseVisualStyleBackColor = true; + buttonCreateAirPlane.Click += buttonCreateAirPlane_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" }); + comboBoxStrategy.Location = new Point(590, 21); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(182, 33); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(660, 70); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(112, 34); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // // FormAirBomber // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateAirPlane); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateAirBomber); Controls.Add(pictureBoxAirBomber); Name = "FormAirBomber"; Text = "Бомбардировщик"; @@ -124,11 +162,14 @@ #endregion - private Button buttonCreate; + private Button buttonCreateAirBomber; private Button buttonLeft; private Button buttonRight; private Button buttonUp; private Button buttonDown; private PictureBox pictureBoxAirBomber; + private Button buttonCreateAirPlane; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormAirBomber.cs b/AirBomber/AirBomber/FormAirBomber.cs index 2612e1c..1d3a1c3 100644 --- a/AirBomber/AirBomber/FormAirBomber.cs +++ b/AirBomber/AirBomber/FormAirBomber.cs @@ -2,36 +2,48 @@ namespace AirBomber { public partial class FormAirBomber : Form { - private DrawningAirBomber? _drawningAirBomber; + private DrawningAirPlane? _drawningAirPlane; + private AbstractStrategy? _abstractStrategy; public FormAirBomber() { InitializeComponent(); } private void Draw() { - if (_drawningAirBomber == null) + if (_drawningAirPlane == null) { return; } Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAirBomber.DrawBomber(gr); + _drawningAirPlane.DrawPlane(gr); pictureBoxAirBomber.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateAirPlane_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirBomber = new DrawningAirBomber(); - _drawningAirBomber.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), - random.Next(1, 4) * 2, pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + _drawningAirPlane = new DrawningAirPlane(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); - _drawningAirBomber.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + private void buttonCreateAirBomber_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningAirPlane = new DrawningAirBomber(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), + pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + + _drawningAirPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + + } private void buttonMove_Click(object sender, EventArgs e) { - if (_drawningAirBomber == null) + if (_drawningAirPlane == null) { return; } @@ -39,19 +51,56 @@ namespace AirBomber switch (name) { case "buttonUp": - _drawningAirBomber.MoveTransport(DirectionType.Up); + _drawningAirPlane.MovePlane(DirectionType.Up); break; case "buttonDown": - _drawningAirBomber.MoveTransport(DirectionType.Down); + _drawningAirPlane.MovePlane(DirectionType.Down); break; case "buttonLeft": - _drawningAirBomber.MoveTransport(DirectionType.Left); + _drawningAirPlane.MovePlane(DirectionType.Left); break; case "buttonRight": - _drawningAirBomber.MoveTransport(DirectionType.Right); + _drawningAirPlane.MovePlane(DirectionType.Right); break; } Draw(); } + + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawningAirPlane == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectAirPlane(_drawningAirPlane), pictureBoxAirBomber.Width, + pictureBoxAirBomber.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/MoveToBorder.cs b/AirBomber/AirBomber/MoveToBorder.cs new file mode 100644 index 0000000..b853ffd --- /dev/null +++ b/AirBomber/AirBomber/MoveToBorder.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) return false; + + return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep(); + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) return; + if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight(); + if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown(); + } + } +} diff --git a/AirBomber/AirBomber/MoveToCenter.cs b/AirBomber/AirBomber/MoveToCenter.cs new file mode 100644 index 0000000..99711ff --- /dev/null +++ b/AirBomber/AirBomber/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +}