From 7603d99eb97732026e3e8196480d38b8faec5fd3 Mon Sep 17 00:00:00 2001 From: Extrimal Date: Wed, 18 Oct 2023 02:22:03 +0300 Subject: [PATCH] laba2 --- .../DrawningAircraftCarrier.cs | 1 + .../FormAircraftCarrier.Designer.cs | 70 +++++++++--- .../AircraftCarrier/FormAircraftCarrier.cs | 100 +++++++++++++----- .../AircraftCarrier/MoveToBorder.cs | 29 +++++ 4 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/MoveToBorder.cs diff --git a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs index 7c5d711..a50cd9a 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs @@ -37,6 +37,7 @@ namespace AircraftCarrier.DrawningObjects { g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9); } + base.DrawTransport(g); } } } diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs index bd09e0b..aa67b69 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBox = new PictureBox(); - buttonCreate = new Button(); buttonUp = new Button(); buttonLeft = new Button(); buttonDown = new Button(); buttonRight = new Button(); + ButtonCreateAircraftCarrier = new Button(); + ButtonCreateAircraft = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); SuspendLayout(); // @@ -48,17 +51,6 @@ pictureBox.TabStop = false; pictureBox.Click += buttonMove_Click; // - // buttonCreate - // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(23, 403); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; - // // buttonUp // buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -107,16 +99,63 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // ButtonCreateAircraftCarrier + // + ButtonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreateAircraftCarrier.Location = new Point(26, 381); + ButtonCreateAircraftCarrier.Name = "ButtonCreateAircraftCarrier"; + ButtonCreateAircraftCarrier.Size = new Size(204, 39); + ButtonCreateAircraftCarrier.TabIndex = 6; + ButtonCreateAircraftCarrier.Text = "Создать военный корабль"; + ButtonCreateAircraftCarrier.UseVisualStyleBackColor = true; + ButtonCreateAircraftCarrier.Click += ButtonCreateAircraftCarrier_Click; + // + // ButtonCreateAircraft + // + ButtonCreateAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreateAircraft.Location = new Point(246, 381); + ButtonCreateAircraft.Name = "ButtonCreateAircraft"; + ButtonCreateAircraft.Size = new Size(204, 39); + ButtonCreateAircraft.TabIndex = 7; + ButtonCreateAircraft.Text = "Создать корабль"; + ButtonCreateAircraft.UseVisualStyleBackColor = true; + ButtonCreateAircraft.Click += ButtonCreateAircraft_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" }); + comboBoxStrategy.Location = new Point(706, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 8; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(763, 46); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(94, 29); + buttonStep.TabIndex = 9; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // // FormAircraftCarrier // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 453); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(ButtonCreateAircraft); + Controls.Add(ButtonCreateAircraftCarrier); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonUp); - Controls.Add(buttonCreate); Controls.Add(pictureBox); Name = "FormAircraftCarrier"; StartPosition = FormStartPosition.CenterScreen; @@ -129,10 +168,13 @@ #endregion private PictureBox pictureBox; - private Button buttonCreate; private Button buttonUp; private Button buttonLeft; private Button buttonDown; private Button buttonRight; + private Button ButtonCreateAircraft; + private Button ButtonCreateAircraftCarrier; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index f9cf77b..425164a 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -1,43 +1,31 @@ +using AircraftCarrier.DrawningObjects; +using AircraftCarrier.MovementStrategy; + namespace AircraftCarrier { - public partial class FormAircraftCarrier : System.Windows.Forms.Form + public partial class FormAircraftCarrier : Form { - private DrawningAircraftCarrier? _drawningAircraftCarrier; + private DrawningAircraft? _drawingAircraft; + + private AbstractStrategy? _abstractStrategy; public FormAircraftCarrier() { InitializeComponent(); } private void Draw() { - if (_drawningAircraftCarrier == null) + if (_drawingAircraft == null) { return; } Bitmap bmp = new(pictureBox.Width, pictureBox.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningAircraftCarrier.DrawTransport(gr); + _drawingAircraft.DrawTransport(gr); pictureBox.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningAircraftCarrier = new DrawningAircraftCarrier(); - _drawningAircraftCarrier.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)), Convert.ToBoolean(random.Next(0, 2)), - pictureBox.Width, pictureBox.Height); - _drawningAircraftCarrier.SetPosition(random.Next(10, 100), - random.Next(10, 100)); - Draw(); - } private void buttonMove_Click(object sender, EventArgs e) { - if (_drawningAircraftCarrier == null) + if (_drawingAircraft == null) { return; } @@ -45,19 +33,79 @@ namespace AircraftCarrier switch (name) { case "buttonUp": - _drawningAircraftCarrier.MoveTransport(Direction.Up); + _drawingAircraft.MoveTransport(Direction.Up); break; case "buttonDown": - _drawningAircraftCarrier.MoveTransport(Direction.Down); + _drawingAircraft.MoveTransport(Direction.Down); break; case "buttonLeft": - _drawningAircraftCarrier.MoveTransport(Direction.Left); + _drawingAircraft.MoveTransport(Direction.Left); break; case "buttonRight": - _drawningAircraftCarrier.MoveTransport(Direction.Right); + _drawingAircraft.MoveTransport(Direction.Right); break; } Draw(); } + + private void ButtonCreateAircraft_Click(object sender, EventArgs e) + { + Random random = new Random(); + _drawingAircraft = new DrawningAircraftCarrier(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)), + pictureBox.Width, pictureBox.Height); + _drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e) + { + Random rnd = new Random(); + _drawingAircraft = new DrawningAircraft(rnd.Next(100, 300), rnd.Next(1000, 3000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + pictureBox.Width, pictureBox.Height); + _drawingAircraft.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); + Draw(); + } + + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawingAircraft == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectAircraft(_drawingAircraft), pictureBox.Width, + pictureBox.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/AircraftCarrier/AircraftCarrier/MoveToBorder.cs b/AircraftCarrier/AircraftCarrier/MoveToBorder.cs new file mode 100644 index 0000000..7a11a6f --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MoveToBorder.cs @@ -0,0 +1,29 @@ +using AircraftCarrier.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier.MovementStrategy +{ + internal 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(); + } + } +}