diff --git a/ProjectContainerShip/ProjectContainerShip/Drawings/DirectionType.cs b/ProjectContainerShip/ProjectContainerShip/Drawings/DirectionType.cs
index 735683f..9706f84 100644
--- a/ProjectContainerShip/ProjectContainerShip/Drawings/DirectionType.cs
+++ b/ProjectContainerShip/ProjectContainerShip/Drawings/DirectionType.cs
@@ -2,6 +2,11 @@
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
diff --git a/ProjectContainerShip/ProjectContainerShip/Drawings/DrawningShip.cs b/ProjectContainerShip/ProjectContainerShip/Drawings/DrawningShip.cs
index af340f2..8d15fa6 100644
--- a/ProjectContainerShip/ProjectContainerShip/Drawings/DrawningShip.cs
+++ b/ProjectContainerShip/ProjectContainerShip/Drawings/DrawningShip.cs
@@ -44,6 +44,26 @@ public class DrawningShip
///
private readonly int _drawningContainerShipHeight = 30;
+ ///
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координата Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningContainerShipWidth;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningContainerShipHeight;
+
///
/// Пустой конструктор
///
diff --git a/ProjectContainerShip/ProjectContainerShip/FormContainerShip.Designer.cs b/ProjectContainerShip/ProjectContainerShip/FormContainerShip.Designer.cs
index 80ca97c..c044adc 100644
--- a/ProjectContainerShip/ProjectContainerShip/FormContainerShip.Designer.cs
+++ b/ProjectContainerShip/ProjectContainerShip/FormContainerShip.Designer.cs
@@ -1,4 +1,5 @@
-namespace ProjectContainerShip
+
+namespace ProjectContainerShip
{
partial class FormContainerShip
{
@@ -35,6 +36,8 @@
buttonDown = new Button();
buttonUp = new Button();
buttonCreateShip = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxContainerShip).BeginInit();
SuspendLayout();
//
@@ -46,6 +49,7 @@
pictureBoxContainerShip.Size = new Size(1256, 529);
pictureBoxContainerShip.TabIndex = 0;
pictureBoxContainerShip.TabStop = false;
+ pictureBoxContainerShip.Click += pictureBoxContainerShip_Click;
//
// buttonCreateContainerShip
//
@@ -117,11 +121,33 @@
buttonCreateShip.UseVisualStyleBackColor = true;
buttonCreateShip.Click += ButtonCreateShip_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру ", "К краю" });
+ comboBoxStrategy.Location = new Point(1002, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(242, 40);
+ comboBoxStrategy.TabIndex = 7;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(1094, 58);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(150, 46);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += ButtonStrategyStep_Click;
+ //
// FormContainerShip
//
AutoScaleDimensions = new SizeF(13F, 32F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1256, 529);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateShip);
Controls.Add(buttonUp);
Controls.Add(buttonDown);
@@ -135,6 +161,11 @@
ResumeLayout(false);
}
+ private void pictureBoxContainerShip_Click(object sender, EventArgs e)
+ {
+ throw new NotImplementedException();
+ }
+
#endregion
@@ -146,5 +177,7 @@
private Button buttonDown;
private Button buttonUp;
private Button buttonCreateShip;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectContainerShip/ProjectContainerShip/FormContainerShip.cs b/ProjectContainerShip/ProjectContainerShip/FormContainerShip.cs
index 5992cab..9f02e7f 100644
--- a/ProjectContainerShip/ProjectContainerShip/FormContainerShip.cs
+++ b/ProjectContainerShip/ProjectContainerShip/FormContainerShip.cs
@@ -1,5 +1,6 @@
using System;
using ProjectContainerShip.Drawings;
+using ProjectContainerShip.MovementStrategy;
namespace ProjectContainerShip;
@@ -13,12 +14,18 @@ public partial class FormContainerShip : Form
///
private DrawningShip? _drawningShip;
+ ///
+ /// Стратегия перемещения
+ ///
+ private AbstractStrategy? _strategy;
+
///
/// Конструктор формы
///
public FormContainerShip()
{
InitializeComponent();
+ _strategy = null;
}
///
@@ -58,6 +65,8 @@ public partial class FormContainerShip : Form
_drawningShip.SetPictureSize(pictureBoxContainerShip.Width, pictureBoxContainerShip.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
@@ -110,4 +119,44 @@ public partial class FormContainerShip : Form
Draw();
}
}
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ private void ButtonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningShip == null) return;
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableShip(_drawningShip), pictureBoxContainerShip.Width, pictureBoxContainerShip.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
\ No newline at end of file
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/AbstractStrategy.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..3cae4ba
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,132 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+public abstract class AbstractStrategy
+{
+ ///
+ /// Перемещаемый объект
+ ///
+ private IMoveableObjects? _moveableObject;
+
+ ///
+ /// Статус перемещения
+ ///
+ private StrategyStatus _state = StrategyStatus.NotInit;
+
+ ///
+ /// Ширина поля
+ ///
+ protected int FieldWidth { get; private set; }
+
+ ///
+ /// Высота поля
+ ///
+ protected int FieldHeight { get; private set; }
+
+ ///
+ /// Статус перемещения
+ ///
+ public StrategyStatus GetStatus() { return _state; }
+
+ ///
+ /// Установка данных
+ ///
+ /// Перемещаемый объект
+ /// Ширина поля
+ /// Высота поля
+ public void SetData(IMoveableObjects moveableObject, int width, int height)
+ {
+ if (moveableObject == null)
+ {
+ _state = StrategyStatus.NotInit;
+ return;
+ }
+ _state = StrategyStatus.InProgress;
+ _moveableObject = moveableObject;
+ FieldWidth = width;
+ FieldHeight = height;
+ }
+
+ ///
+ /// Шаг перемещения
+ ///
+ public void MakeStep()
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return;
+ }
+ if (IsTargetDestinaion())
+ {
+ _state = StrategyStatus.Finish;
+ return;
+ }
+ MoveToTarget();
+ }
+
+ ///
+ /// Перемещение влево
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveLeft() => MoveTo(MovementDirection.Left);
+
+ ///
+ /// Перемещение вправо
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveRight() => MoveTo(MovementDirection.Right);
+
+ ///
+ /// Перемещение вверх
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveUp() => MoveTo(MovementDirection.Up);
+
+ ///
+ /// Перемещение вниз
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveDown() => MoveTo(MovementDirection.Down);
+
+ ///
+ /// Параметры объекта
+ ///
+ protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
+
+ ///
+ /// Шаг объекта
+ ///
+ ///
+ protected int? GetStep()
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return null;
+ }
+ return _moveableObject?.GetStep;
+ }
+
+ ///
+ /// Перемещение к цели
+ ///
+ protected abstract void MoveToTarget();
+
+ ///
+ /// Достигнута ли цель
+ ///
+ ///
+ protected abstract bool IsTargetDestinaion();
+
+ ///
+ /// Попытка перемещения в требуемом направлении
+ ///
+ /// Направление
+ /// Результат попытки (true - удалось переместиться, false - неудача)
+ private bool MoveTo(MovementDirection movementDirection)
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return false;
+ }
+ return _moveableObject?.TryMoveObject(movementDirection) ?? false;
+ }
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/IMoveableObjects.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/IMoveableObjects.cs
new file mode 100644
index 0000000..290841d
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/IMoveableObjects.cs
@@ -0,0 +1,21 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+public interface IMoveableObjects
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveToBorder.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..b82a287
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,31 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+public class MoveToBorder : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null) return false;
+
+ return objParams.RightBorder + GetStep() >= FieldWidth
+ && objParams.DownBorder + GetStep() >= FieldHeight;
+ }
+
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null) return;
+
+ int diffX = objParams.LeftBorder - FieldWidth;
+ if (Math.Abs(diffX) > GetStep())
+ {
+ MoveRight();
+ }
+
+ int diffY = objParams.DownBorder - FieldHeight;
+ if (Math.Abs(diffY) > GetStep())
+ {
+ MoveDown();
+ }
+ }
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveToCenter.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..954f4b9
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,54 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+///
+/// Стратегия перемещения объекта в центр экрана
+///
+public class MoveToCenter : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ 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 (diffY > 0)
+ {
+ MoveUp();
+ }
+ else
+ {
+ MoveDown();
+ }
+ }
+ }
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveableShip.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveableShip.cs
new file mode 100644
index 0000000..dc99630
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MoveableShip.cs
@@ -0,0 +1,64 @@
+using ProjectContainerShip.Drawings;
+
+namespace ProjectContainerShip.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObjects с использованием DrawningShip
+///
+public class MoveableShip : IMoveableObjects
+{
+ ///
+ /// Поле-объект класса DrawningCar или его наследника
+ ///
+ private readonly DrawningShip? _ship = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningShip
+ public MoveableShip(DrawningShip ship)
+ {
+ _ship = ship;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_ship == null || _ship.EntityShip == null || !_ship.GetPosX.HasValue || !_ship.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_ship.GetPosX.Value, _ship.GetPosY.Value, _ship.GetWidth, _ship.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_ship?.EntityShip?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_ship == null || _ship.EntityShip == null)
+ {
+ return false;
+ }
+
+ return _ship.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,
+ };
+ }
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MovementDirection.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..9fe86a8
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,27 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+///
+/// Направление перемещения
+///
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+
+ ///
+ /// Влево
+ ///
+ Left = 3,
+
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/ObjectParameters.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..34f5d88
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,69 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+public class ObjectParameters
+{
+ ///
+ /// Координата X
+ ///
+ private readonly int _x;
+
+ ///
+ /// Координата Y
+ ///
+ private readonly int _y;
+
+ ///
+ /// Ширина объекта
+ ///
+ private readonly int _width;
+
+ ///
+ /// Высота объекта
+ ///
+ private readonly int _height;
+
+ ///
+ /// Левая граница
+ ///
+ public int LeftBorder => _x;
+
+ ///
+ /// Верхняя граница
+ ///
+ public int TopBorder => _y;
+
+ ///
+ /// Правая граница
+ ///
+ public int RightBorder => _x + _width;
+
+ ///
+ /// Нижняя граница
+ ///
+ public int DownBorder => _y + _height;
+
+ ///
+ /// Середина объекта
+ ///
+ public int ObjectMiddleHorizontal => _x + _width / 2;
+
+ ///
+ /// Середина объекта
+ ///
+ public int ObjectMiddleVertical => _y + _height / 2;
+
+ ///
+ /// Конструктор
+ ///
+ /// Координата X
+ /// Координата Y
+ /// Ширина объекта
+ /// Высота объекта
+ public ObjectParameters(int x, int y, int width, int height)
+ {
+ _x = x;
+ _y = y;
+ _width = width;
+ _height = height;
+ }
+}
diff --git a/ProjectContainerShip/ProjectContainerShip/MovementStrategy/StrategyStatus.cs b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..0252124
--- /dev/null
+++ b/ProjectContainerShip/ProjectContainerShip/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,22 @@
+namespace ProjectContainerShip.MovementStrategy;
+
+///
+/// Статус выполнения операции перемещения
+///
+public enum StrategyStatus
+{
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+}