From f3f138ff6235464b2e18fbad6b058d3e01f24fa9 Mon Sep 17 00:00:00 2001 From: Shtyrkin_Egor Date: Mon, 15 Apr 2024 10:32:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81?= =?UTF-8?q?=D1=8C=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 5 ++ .../Drawnings/DrawningWarship.cs | 23 ++++++ .../Entities/EntityAircraftCarrier.cs | 7 +- .../FormAircraftCarrier.Designer.cs | 66 ++++++++++------- .../FormAircraftCarrier.cs | 54 +++++++++++++- .../MovementStrategy/AbstractStrategy.cs | 37 ++++++++++ .../MovementStrategy/MoveToBorder.cs | 61 ++++++++++++++++ .../MovementStrategy/MoveToCenter.cs | 57 +++++++++++++++ .../MovementStrategy/MoveableWarship.cs | 71 +++++++++++++++++++ 9 files changed, 350 insertions(+), 31 deletions(-) create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveableWarship.cs diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DirectionType.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DirectionType.cs index 535eb13..d95fc19 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DirectionType.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DirectionType.cs @@ -5,6 +5,11 @@ /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarship.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarship.cs index 888d41a..91f319a 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarship.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Drawnings/DrawningWarship.cs @@ -7,6 +7,9 @@ using System.Threading.Tasks; namespace ProjectAircraftCarrier.Drawnings; +/// +/// Класс отвечающий за прорисовку и перемещение базового объекта-сущности +/// public class DrawningWarship { /// @@ -44,6 +47,26 @@ public class DrawningWarship /// private readonly int _drawningWarshipHeight = 50; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningWarshipWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningWarshipHeight; + /// /// Пустой конструктор /// diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/Entities/EntityAircraftCarrier.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/Entities/EntityAircraftCarrier.cs index 5253039..5df894c 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/Entities/EntityAircraftCarrier.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/Entities/EntityAircraftCarrier.cs @@ -20,11 +20,6 @@ public class EntityAircraftCarrier : EntityWarship /// public bool ControlRoom { get; private set; } - /// - /// Шаг перемещения авианосца - /// - public double Step => Speed * 100 / Weight; - /// /// Инициализация полей объекта-класса авианосца /// @@ -34,7 +29,7 @@ public class EntityAircraftCarrier : EntityWarship /// Дополнительный цвет /// Признак наличия взлётно-посадочной полосы /// Признак наличия рубки управления - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom) + public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; AircraftDeck = aircraftDeck; diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs index 1308807..b430dfd 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonLeft = new Button(); buttonCreateWarship = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit(); SuspendLayout(); // @@ -42,9 +44,8 @@ // pictureBoxAircraftCarrier.Dock = DockStyle.Fill; pictureBoxAircraftCarrier.Location = new Point(0, 0); - pictureBoxAircraftCarrier.Margin = new Padding(3, 4, 3, 4); pictureBoxAircraftCarrier.Name = "pictureBoxAircraftCarrier"; - pictureBoxAircraftCarrier.Size = new Size(918, 628); + pictureBoxAircraftCarrier.Size = new Size(803, 471); pictureBoxAircraftCarrier.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxAircraftCarrier.TabIndex = 0; pictureBoxAircraftCarrier.TabStop = false; @@ -52,24 +53,22 @@ // buttonCreateAircraftCarrier // buttonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateAircraftCarrier.Location = new Point(14, 581); - buttonCreateAircraftCarrier.Margin = new Padding(3, 4, 3, 4); + buttonCreateAircraftCarrier.Location = new Point(12, 436); buttonCreateAircraftCarrier.Name = "buttonCreateAircraftCarrier"; - buttonCreateAircraftCarrier.Size = new Size(188, 31); + buttonCreateAircraftCarrier.Size = new Size(164, 23); buttonCreateAircraftCarrier.TabIndex = 1; buttonCreateAircraftCarrier.Text = "Создать авианосец"; buttonCreateAircraftCarrier.UseVisualStyleBackColor = true; - buttonCreateAircraftCarrier.Click += buttonCreate_Click; + buttonCreateAircraftCarrier.Click += ButtonCreateAircraftCarrier_Click; // // buttonRight // buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(876, 585); - buttonRight.Margin = new Padding(3, 4, 3, 4); + buttonRight.Location = new Point(766, 439); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); + buttonRight.Size = new Size(26, 22); buttonRight.TabIndex = 2; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; @@ -79,10 +78,9 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.arrowUp; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(840, 547); - buttonUp.Margin = new Padding(3, 4, 3, 4); + buttonUp.Location = new Point(735, 410); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); + buttonUp.Size = new Size(26, 22); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; @@ -92,10 +90,9 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(840, 585); - buttonDown.Margin = new Padding(3, 4, 3, 4); + buttonDown.Location = new Point(735, 439); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); + buttonDown.Size = new Size(26, 22); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; @@ -105,10 +102,9 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(804, 585); - buttonLeft.Margin = new Padding(3, 4, 3, 4); + buttonLeft.Location = new Point(704, 439); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); + buttonLeft.Size = new Size(26, 22); buttonLeft.TabIndex = 5; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; @@ -116,20 +112,41 @@ // buttonCreateWarship // buttonCreateWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateWarship.Location = new Point(208, 581); - buttonCreateWarship.Margin = new Padding(3, 4, 3, 4); + buttonCreateWarship.Location = new Point(182, 436); buttonCreateWarship.Name = "buttonCreateWarship"; - buttonCreateWarship.Size = new Size(208, 31); + buttonCreateWarship.Size = new Size(182, 23); buttonCreateWarship.TabIndex = 6; buttonCreateWarship.Text = "Создать военный корабль"; buttonCreateWarship.UseVisualStyleBackColor = true; buttonCreateWarship.Click += ButtonCreateWarship_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(670, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(716, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormAircraftCarrier // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(918, 628); + ClientSize = new Size(803, 471); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateWarship); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -137,7 +154,6 @@ Controls.Add(buttonRight); Controls.Add(buttonCreateAircraftCarrier); Controls.Add(pictureBoxAircraftCarrier); - Margin = new Padding(3, 4, 3, 4); Name = "FormAircraftCarrier"; StartPosition = FormStartPosition.CenterScreen; Text = "Авианосец"; @@ -155,5 +171,7 @@ private Button buttonDown; private Button buttonLeft; private Button buttonCreateWarship; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs index 56376d7..0f52da8 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/FormAircraftCarrier.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectAircraftCarrier.Drawnings; +using ProjectAircraftCarrier.MovementStrategy; namespace ProjectAircraftCarrier; @@ -21,12 +22,18 @@ public partial class FormAircraftCarrier : Form /// private DrawningWarship? _drawningWarship; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + /// /// Конструктор формы /// public FormAircraftCarrier() { InitializeComponent(); + _strategy = null; } /// @@ -66,6 +73,8 @@ public partial class FormAircraftCarrier : Form _drawningWarship.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); _drawningWarship.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -82,7 +91,7 @@ public partial class FormAircraftCarrier : Form /// /// private void ButtonCreateWarship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarship)); - + /// /// Перемещение объекта по форме (нажатие кнопок навигации) /// @@ -118,4 +127,47 @@ public partial class FormAircraftCarrier : Form Draw(); } } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningWarship == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToCenter(), + _ => null, + }; + if(_strategy == null) + { + return; + } + _strategy.SetData(new MoveableWarship(_drawningWarship), pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); + } + + if(_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if(_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/AbstractStrategy.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/AbstractStrategy.cs index 643303f..bf258b1 100644 --- a/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/AbstractStrategy.cs +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/AbstractStrategy.cs @@ -104,5 +104,42 @@ public abstract class AbstractStrategy /// protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if(_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение цели + /// + protected abstract void MoveToTarget(); + + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestination(); + + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if(_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } } diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToBorder.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..0f103d1 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToCenter.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..adb68d2 --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objParams = GetObjectParameters; + if(objParams == null) + { + return false; + } + + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if(objParams == null) + { + return; + } + + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if(diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffX > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveableWarship.cs b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveableWarship.cs new file mode 100644 index 0000000..8acc61d --- /dev/null +++ b/ProjectAircraftCarrier/ProjectAircraftCarrier/MovementStrategy/MoveableWarship.cs @@ -0,0 +1,71 @@ +using ProjectAircraftCarrier.Drawnings; +using ProjectAircraftCarrier.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAircraftCarrier.MovementStrategy; + +/// +/// Класс реализация IMoveableObject с использованием DrawningWarship +/// +public class MoveableWarship : IMoveableObject +{ + /// + /// Поле-объект класса DrawningWarship или его наследника + /// + private readonly DrawningWarship? _warship = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningWarship + public MoveableWarship(DrawningWarship warship) + { + _warship = warship; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_warship == null || _warship.EntityWarship == null || !_warship.GetPosX.HasValue || !_warship.GetPosY.HasValue) + { + return null; + } + + return new ObjectParameters(_warship.GetPosX.Value, _warship.GetPosY.Value, _warship.GetWidth, _warship.GetHeight); + } + } + + public int GetStep => (int)(_warship?.EntityWarship?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if(_warship == null || _warship.EntityWarship == null) + { + return false; + } + + return _warship.MoveTransport(GetDirectionType(direction)); + } + + /// + /// Конвертация из MovementDirection в DirectionType + /// + /// MovementDirection + /// DirectionType + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } +}