diff --git a/GasolineTanker/GasolineTanker/Drawnings/DirectionType.cs b/GasolineTanker/GasolineTanker/Drawnings/DirectionType.cs
index 37cbd3d..d516519 100644
--- a/GasolineTanker/GasolineTanker/Drawnings/DirectionType.cs
+++ b/GasolineTanker/GasolineTanker/Drawnings/DirectionType.cs
@@ -5,6 +5,11 @@
///
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
@@ -24,4 +29,4 @@ public enum DirectionType
/// Вправо
///
Right = 4
-}
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/Drawnings/DrawningTanker.cs b/GasolineTanker/GasolineTanker/Drawnings/DrawningTanker.cs
index 6862a76..88f0078 100644
--- a/GasolineTanker/GasolineTanker/Drawnings/DrawningTanker.cs
+++ b/GasolineTanker/GasolineTanker/Drawnings/DrawningTanker.cs
@@ -46,6 +46,26 @@ public class DrawningTanker
///
private readonly int _drawningTankerHeight = 95;
+ ///
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координата Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningTankerWidth;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningTankerHeight;
+
///
/// Пустой конструктор
///
diff --git a/GasolineTanker/GasolineTanker/FormGasolineTanker.Designer.cs b/GasolineTanker/GasolineTanker/FormGasolineTanker.Designer.cs
index a38919e..fef4f6d 100644
--- a/GasolineTanker/GasolineTanker/FormGasolineTanker.Designer.cs
+++ b/GasolineTanker/GasolineTanker/FormGasolineTanker.Designer.cs
@@ -35,6 +35,8 @@
buttonUp = new Button();
buttonLeft = new Button();
buttonCreateTanker = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxGasolineTanker).BeginInit();
SuspendLayout();
//
@@ -43,14 +45,14 @@
pictureBoxGasolineTanker.Dock = DockStyle.Fill;
pictureBoxGasolineTanker.Location = new Point(0, 0);
pictureBoxGasolineTanker.Name = "pictureBoxGasolineTanker";
- pictureBoxGasolineTanker.Size = new Size(945, 474);
+ pictureBoxGasolineTanker.Size = new Size(972, 537);
pictureBoxGasolineTanker.TabIndex = 0;
pictureBoxGasolineTanker.TabStop = false;
//
// ButtonCreateGasolineTanker
//
ButtonCreateGasolineTanker.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
- ButtonCreateGasolineTanker.Location = new Point(12, 421);
+ ButtonCreateGasolineTanker.Location = new Point(12, 484);
ButtonCreateGasolineTanker.Name = "ButtonCreateGasolineTanker";
ButtonCreateGasolineTanker.Size = new Size(153, 35);
ButtonCreateGasolineTanker.TabIndex = 1;
@@ -63,7 +65,7 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
- buttonRight.Location = new Point(884, 421);
+ buttonRight.Location = new Point(911, 484);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 2;
@@ -75,7 +77,7 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
- buttonDown.Location = new Point(843, 421);
+ buttonDown.Location = new Point(870, 484);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 3;
@@ -87,7 +89,7 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
- buttonUp.Location = new Point(843, 380);
+ buttonUp.Location = new Point(870, 443);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 4;
@@ -99,7 +101,7 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
- buttonLeft.Location = new Point(802, 421);
+ buttonLeft.Location = new Point(829, 484);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 5;
@@ -109,7 +111,7 @@
// buttonCreateTanker
//
buttonCreateTanker.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
- buttonCreateTanker.Location = new Point(171, 421);
+ buttonCreateTanker.Location = new Point(171, 484);
buttonCreateTanker.Name = "buttonCreateTanker";
buttonCreateTanker.Size = new Size(153, 35);
buttonCreateTanker.TabIndex = 6;
@@ -117,11 +119,34 @@
buttonCreateTanker.UseVisualStyleBackColor = true;
buttonCreateTanker.Click += buttonCreateTanker_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.AccessibleRole = AccessibleRole.Sound;
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(839, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(121, 23);
+ comboBoxStrategy.TabIndex = 7;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(885, 41);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(75, 23);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += ButtonStrategyStep_Click;
+ //
// FormGasolineTanker
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(945, 474);
+ ClientSize = new Size(972, 537);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateTanker);
Controls.Add(buttonLeft);
Controls.Add(buttonUp);
@@ -144,5 +169,7 @@
private Button buttonUp;
private Button buttonLeft;
private Button buttonCreateTanker;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/FormGasolineTanker.cs b/GasolineTanker/GasolineTanker/FormGasolineTanker.cs
index bab1d2e..8652d79 100644
--- a/GasolineTanker/GasolineTanker/FormGasolineTanker.cs
+++ b/GasolineTanker/GasolineTanker/FormGasolineTanker.cs
@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using GasolineTanker.Drawnings;
+using GasolineTanker.MovementStrategy;
namespace GasolineTanker
{
@@ -16,9 +17,15 @@ namespace GasolineTanker
{
private DrawningTanker? _drawningTanker;
+ ///
+ /// Стратегия перемещения
+ ///
+ private AbstractStrategy? _strategy;
+
public FormGasolineTanker()
{
InitializeComponent();
+ _strategy = null;
}
///
@@ -68,6 +75,8 @@ namespace GasolineTanker
else
{
_drawningTanker.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
}
@@ -78,7 +87,7 @@ namespace GasolineTanker
///
///
private void buttonCreateGasolineTanker_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningGasolineTanker));
-
+
///
/// Обработка нажатия кнопки "Создать грузовик"
///
@@ -122,5 +131,48 @@ namespace GasolineTanker
Draw();
}
}
+
+ ///
+ /// Обработка нажатия кнопки "Шаг"
+ ///
+ ///
+ ///
+ private void ButtonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningTanker == null)
+ {
+ return;
+ }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableTanker(_drawningTanker), pictureBoxGasolineTanker.Width, pictureBoxGasolineTanker.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
}
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/AbstractStrategy.cs b/GasolineTanker/GasolineTanker/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..70891d1
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,139 @@
+namespace GasolineTanker.MovementStrategy;
+
+///
+/// Класс-стратегия перемещения объекта
+///
+public abstract class AbstractStrategy
+{
+ ///
+ /// Перемещаемый объект
+ ///
+ private IMoveableObject? _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(IMoveableObject 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;
+ }
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/IMoveableObject.cs b/GasolineTanker/GasolineTanker/MovementStrategy/IMoveableObject.cs
new file mode 100644
index 0000000..887171b
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/IMoveableObject.cs
@@ -0,0 +1,24 @@
+namespace GasolineTanker.MovementStrategy;
+
+///
+/// Интерфейс для работы с перемещаемым объектом
+///
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/MoveToBorder.cs b/GasolineTanker/GasolineTanker/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..cddf055
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,42 @@
+namespace GasolineTanker.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 = FieldWidth;
+ if (Math.Abs(diffX) > GetStep())
+ {
+ if (diffX > 0)
+ {
+ MoveRight();
+ }
+ }
+
+ int diffY = FieldHeight;
+ if (Math.Abs(diffY) > GetStep())
+ {
+ if (diffY > 0)
+ {
+ MoveDown();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/MoveToCenter.cs b/GasolineTanker/GasolineTanker/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..1cd3ffa
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,51 @@
+namespace GasolineTanker.MovementStrategy;
+
+internal 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();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/MoveableTanker.cs b/GasolineTanker/GasolineTanker/MovementStrategy/MoveableTanker.cs
new file mode 100644
index 0000000..3e175cf
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/MoveableTanker.cs
@@ -0,0 +1,64 @@
+using GasolineTanker.Drawnings;
+
+namespace GasolineTanker.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObject с использованием DrawningTanker
+///
+public class MoveableTanker : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningTanker или его наследника
+ ///
+ private readonly DrawningTanker? _tanker = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningTanker
+ public MoveableTanker(DrawningTanker tanker)
+ {
+ _tanker = tanker;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_tanker == null || _tanker.EntityTanker == null || !_tanker.GetPosX.HasValue || !_tanker.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_tanker.GetPosX.Value, _tanker.GetPosY.Value, _tanker.GetWidth, _tanker.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_tanker?.EntityTanker?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_tanker == null || _tanker.EntityTanker == null)
+ {
+ return false;
+ }
+
+ return _tanker.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,
+ };
+ }
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/MovementDirection.cs b/GasolineTanker/GasolineTanker/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..5affae0
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,27 @@
+namespace GasolineTanker.MovementStrategy;
+
+///
+/// Направление перемещения
+///
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+
+ ///
+ /// Влево
+ ///
+ Left = 3,
+
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/ObjectParameters.cs b/GasolineTanker/GasolineTanker/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..ffc3d2b
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,72 @@
+namespace GasolineTanker.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;
+ }
+}
\ No newline at end of file
diff --git a/GasolineTanker/GasolineTanker/MovementStrategy/StrategyStatus.cs b/GasolineTanker/GasolineTanker/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..1e96c39
--- /dev/null
+++ b/GasolineTanker/GasolineTanker/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,22 @@
+namespace GasolineTanker.MovementStrategy;
+
+///
+/// Статус выполнения операции перемещения
+///
+public enum StrategyStatus
+{
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+}