From d9e4d18651d8541a8993a1f5605286974bc3afdf Mon Sep 17 00:00:00 2001
From: Petek1234 <149153720+Petek1234@users.noreply.github.com>
Date: Tue, 19 Mar 2024 23:30:47 +0400
Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB=20?=
=?UTF-8?q?=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80?=
=?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
laba 0/laba 0.sln | 2 +-
laba 0/laba 0/Drawnings/DirectionType.cs | 7 +-
laba 0/laba 0/Drawnings/DrawningB.cs | 61 +++++---
laba 0/laba 0/Drawnings/DrawningBoat.cs | 9 +-
laba 0/laba 0/FormBoat.Designer.cs | 26 ++++
laba 0/laba 0/FormBoat.cs | 63 ++++++++
.../{laba 0.csproj => MotorBoat.csproj} | 0
.../MovementStrategy/AbstractStrategy.cs | 142 ++++++++++++++++++
.../MovementStrategy/IMoveableObject.cs | 24 +++
.../laba 0/MovementStrategy/MoveToBorder.cs | 53 +++++++
.../laba 0/MovementStrategy/MoveToCenter.cs | 56 +++++++
laba 0/laba 0/MovementStrategy/MoveableB.cs | 71 +++++++++
.../MovementStrategy/MovementDirection.cs | 24 +++
.../MovementStrategy/ObjectParameters.cs | 75 +++++++++
.../laba 0/MovementStrategy/StrategyStatus.cs | 26 ++++
15 files changed, 612 insertions(+), 27 deletions(-)
rename laba 0/laba 0/{laba 0.csproj => MotorBoat.csproj} (100%)
create mode 100644 laba 0/laba 0/MovementStrategy/AbstractStrategy.cs
create mode 100644 laba 0/laba 0/MovementStrategy/IMoveableObject.cs
create mode 100644 laba 0/laba 0/MovementStrategy/MoveToBorder.cs
create mode 100644 laba 0/laba 0/MovementStrategy/MoveToCenter.cs
create mode 100644 laba 0/laba 0/MovementStrategy/MoveableB.cs
create mode 100644 laba 0/laba 0/MovementStrategy/MovementDirection.cs
create mode 100644 laba 0/laba 0/MovementStrategy/ObjectParameters.cs
create mode 100644 laba 0/laba 0/MovementStrategy/StrategyStatus.cs
diff --git a/laba 0/laba 0.sln b/laba 0/laba 0.sln
index ce7cdea..9d2f0ea 100644
--- a/laba 0/laba 0.sln
+++ b/laba 0/laba 0.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "laba 0", "laba 0\laba 0.csproj", "{20221664-1E5B-4E5F-BBF7-F0E378CB7A24}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MotorBoat", "laba 0\MotorBoat.csproj", "{20221664-1E5B-4E5F-BBF7-F0E378CB7A24}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/laba 0/laba 0/Drawnings/DirectionType.cs b/laba 0/laba 0/Drawnings/DirectionType.cs
index f2ff634..7191c03 100644
--- a/laba 0/laba 0/Drawnings/DirectionType.cs
+++ b/laba 0/laba 0/Drawnings/DirectionType.cs
@@ -5,10 +5,15 @@
///
public enum DirectionType
{
+ ///
+ /// неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// вверх
///
- Up=1,
+ Up = 1,
///
/// вниз
diff --git a/laba 0/laba 0/Drawnings/DrawningB.cs b/laba 0/laba 0/Drawnings/DrawningB.cs
index 568863b..91dd165 100644
--- a/laba 0/laba 0/Drawnings/DrawningB.cs
+++ b/laba 0/laba 0/Drawnings/DrawningB.cs
@@ -37,12 +37,29 @@ public class DrawningB
///
/// ширина прорисовки катера
///
- private readonly int _drawningBoatWidth = 90;
+ private readonly int _drawningBWidth = 120;
///
/// высота прорисовки катера
///
- private readonly int _drawningBoatHeight = 50;
+ private readonly int _drawningBHeight = 50;
+
+ ///
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+ ///
+ /// Координата Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningBWidth;
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningBHeight;
///
/// Пустой конструктор
@@ -69,12 +86,12 @@ public class DrawningB
///
/// Конструктор для наследников
///
- /// ширина прорисовки катера
- /// высота прорисовки катера
- protected DrawningB(int drawningBoatWidth, int drawningBoatHeight) : this()
+ /// ширина прорисовки катера
+ /// высота прорисовки катера
+ protected DrawningB(int drawningBWidth, int drawningBHeight) : this()
{
- _drawningBoatWidth = drawningBoatWidth;
- _drawningBoatHeight = drawningBoatHeight;
+ _drawningBWidth = drawningBWidth;
+ _drawningBHeight = drawningBHeight;
}
///
@@ -85,20 +102,20 @@ public class DrawningB
/// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах
public bool SetPictureSize(int width, int height)
{
- if (_drawningBoatWidth <= width && _drawningBoatHeight <= height)
+ if (_drawningBWidth <= width && _drawningBHeight <= height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosY.HasValue)
{
- if (_startPosX + _drawningBoatWidth > _pictureWidth)
+ if (_startPosX + _drawningBWidth > _pictureWidth)
{
- _startPosX = _pictureWidth - _drawningBoatWidth;
+ _startPosX = _pictureWidth - _drawningBWidth;
}
- if (_startPosY + _drawningBoatHeight > _pictureHeight)
+ if (_startPosY + _drawningBHeight > _pictureHeight)
{
- _startPosY = _drawningBoatHeight - _drawningBoatHeight;
+ _startPosY = _drawningBHeight - _drawningBHeight;
}
}
return true;
@@ -122,18 +139,18 @@ public class DrawningB
{
x = 0; //устанавливаем позицию по оси х равную 0
}
- else if (x + _drawningBoatWidth > _pictureWidth) //если новая позиция выходит за правую границу формы
+ else if (x + _drawningBWidth > _pictureWidth) //если новая позиция выходит за правую границу формы
{
- x = _pictureWidth.Value - _drawningBoatWidth;//корректируем по оси х
+ x = _pictureWidth.Value - _drawningBWidth;//корректируем по оси х
}
if (y < 0)//если новая позиция выше верхней границы формы
{
y = 0;
}
- else if (y + _drawningBoatHeight > _pictureHeight) //если новая позиция выходит за границу формы
+ else if (y + _drawningBHeight > _pictureHeight) //если новая позиция выходит за границу формы
{
- y = _pictureHeight.Value - _drawningBoatHeight;//корректируем по оси у
+ y = _pictureHeight.Value - _drawningBHeight;//корректируем по оси у
}
_startPosX = x;
@@ -161,7 +178,6 @@ public class DrawningB
_startPosX -= (int)EntityB.Step;
}
return true;
-
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityB.Step > 0)
@@ -169,18 +185,16 @@ public class DrawningB
_startPosY -= (int)EntityB.Step;
}
return true;
-
// вправо
case DirectionType.Right:
- if (_startPosX.Value + EntityB.Step + _drawningBoatWidth < _pictureWidth)
+ if (_startPosX.Value + EntityB.Step + _drawningBWidth < _pictureWidth)
{
_startPosX += (int)EntityB.Step;
}
return true;
-
//вниз
case DirectionType.Down:
- if (_startPosY.Value + EntityB.Step + _drawningBoatHeight < _pictureHeight)
+ if (_startPosY.Value + EntityB.Step + _drawningBHeight < _pictureHeight)
{
_startPosY += (int)EntityB.Step;
}
@@ -203,16 +217,21 @@ public class DrawningB
}
Pen pen = new(Color.Black);
+ Brush BodyBrush = new SolidBrush(EntityB.BodyColor);
//корпус
+ g.FillRectangle(BodyBrush, _startPosX.Value + 5, _startPosY.Value, 75, 40);
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value, 75, 40);
+
//лестница
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 10, 20, 20);
+
//корма
Point point1 = new Point(_startPosX.Value + 80, _startPosY.Value);
Point point2 = new Point(_startPosX.Value + 80, _startPosY.Value + 40);
Point point3 = new Point(_startPosX.Value + 120, _startPosY.Value + 20);
Point[] curvePointsKorma = { point1, point2, point3 };
+ g.FillPolygon(BodyBrush, curvePointsKorma);
g.DrawPolygon(pen, curvePointsKorma);
}
}
diff --git a/laba 0/laba 0/Drawnings/DrawningBoat.cs b/laba 0/laba 0/Drawnings/DrawningBoat.cs
index ee69378..2d6346b 100644
--- a/laba 0/laba 0/Drawnings/DrawningBoat.cs
+++ b/laba 0/laba 0/Drawnings/DrawningBoat.cs
@@ -17,7 +17,7 @@ public class DrawningBoat : DrawningB
/// Признак наличия кабины
/// Признак наличия двигателя
- public DrawningBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) : base(110, 60)
+ public DrawningBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool kabina, bool dvigatel) : base(120, 60)
{
EntityB = new EntityBoat(speed, weight, bodyColor, additionalColor, kabina, dvigatel);
}
@@ -30,12 +30,14 @@ public class DrawningBoat : DrawningB
{
Pen pen = new(Color.Black);
- if (EntityB == null || EntityB is not EntityBoat Boat || !_startPosX.HasValue || !_startPosY.HasValue)
+ if (EntityB == null || EntityB is not EntityBoat Boat || !_startPosX.HasValue || !_startPosY.HasValue)
return;
base.DrawTransport(g);
Brush additionalBrush = new SolidBrush(Boat.AdditionalColor);
+ base.DrawTransport(g);
+
//кабина
if (Boat.kabinaBody)
{
@@ -48,13 +50,12 @@ public class DrawningBoat : DrawningB
g.FillPolygon(additionalBrush, curvePointsKabina);
g.DrawPolygon(pen, curvePointsKabina);
}
+
//двигатель
if (Boat.dvigatelBody)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 15, 5, 10);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 15, 5, 10);
}
-
- base.DrawTransport(g);
}
}
diff --git a/laba 0/laba 0/FormBoat.Designer.cs b/laba 0/laba 0/FormBoat.Designer.cs
index 5554186..8ca6ab1 100644
--- a/laba 0/laba 0/FormBoat.Designer.cs
+++ b/laba 0/laba 0/FormBoat.Designer.cs
@@ -36,6 +36,8 @@ partial class FormBoat
buttonUp = new Button();
buttonLeft = new Button();
ButtonCreateB = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBoat).BeginInit();
SuspendLayout();
//
@@ -121,11 +123,33 @@ partial class FormBoat
ButtonCreateB.UseVisualStyleBackColor = true;
ButtonCreateB.Click += ButtonCreateB_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру ", "К краю" });
+ comboBoxStrategy.Location = new Point(507, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(121, 23);
+ comboBoxStrategy.TabIndex = 7;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(553, 41);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(75, 23);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += buttonStrategyStep_Click;
+ //
// FormBoat
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(640, 359);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(ButtonCreateB);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
@@ -149,4 +173,6 @@ partial class FormBoat
private Button buttonUp;
private Button buttonLeft;
private Button ButtonCreateB;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
\ No newline at end of file
diff --git a/laba 0/laba 0/FormBoat.cs b/laba 0/laba 0/FormBoat.cs
index a224d88..c3e1ed6 100644
--- a/laba 0/laba 0/FormBoat.cs
+++ b/laba 0/laba 0/FormBoat.cs
@@ -8,18 +8,34 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using laba_0.Drawning;
+using laba_0.MovementStrategy;
namespace laba_0;
public partial class FormBoat : Form
{
+ ///
+ /// поле-объект для прорисовки объекта
+ ///
private DrawningB? _drawningB;
+ ///
+ /// стратегия перемещения
+ ///
+ private AbstractStrategy? _strategy;
+
+ ///
+ /// конструктор формы
+ ///
public FormBoat()
{
InitializeComponent();
+ _strategy = null;
}
+ ///
+ /// Метод прорисовки катера
+ ///
private void Draw()
{
if (_drawningB == null)
@@ -57,6 +73,8 @@ public partial class FormBoat : Form
}
_drawningB.SetPictureSize(pictureBoxBoat.Width, pictureBoxBoat.Height);
_drawningB.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
@@ -114,4 +132,49 @@ public partial class FormBoat : Form
Draw();
}
}
+
+ ///
+ ///
+ ///
+ ///
+ ///
+ private void buttonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningB == null)
+ {
+ return;
+ }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableB(_drawningB), pictureBoxBoat.Width, pictureBoxBoat.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
+
diff --git a/laba 0/laba 0/laba 0.csproj b/laba 0/laba 0/MotorBoat.csproj
similarity index 100%
rename from laba 0/laba 0/laba 0.csproj
rename to laba 0/laba 0/MotorBoat.csproj
diff --git a/laba 0/laba 0/MovementStrategy/AbstractStrategy.cs b/laba 0/laba 0/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..c506cd5
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,142 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace laba_0.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;
+ }
+}
diff --git a/laba 0/laba 0/MovementStrategy/IMoveableObject.cs b/laba 0/laba 0/MovementStrategy/IMoveableObject.cs
new file mode 100644
index 0000000..1b7da3e
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/IMoveableObject.cs
@@ -0,0 +1,24 @@
+namespace laba_0.MovementStrategy;
+
+///
+/// Интерфейс для работы с перемещаемым объектом
+///
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/laba 0/laba 0/MovementStrategy/MoveToBorder.cs b/laba 0/laba 0/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..9a23dab
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace laba_0.MovementStrategy;
+
+public class MoveToBorder : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ 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/laba 0/laba 0/MovementStrategy/MoveToCenter.cs b/laba 0/laba 0/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..c7ce55a
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace laba_0.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/laba 0/laba 0/MovementStrategy/MoveableB.cs b/laba 0/laba 0/MovementStrategy/MoveableB.cs
new file mode 100644
index 0000000..09b2a59
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/MoveableB.cs
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using laba_0.Drawning;
+
+namespace laba_0.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObject с использованием DrawningB
+///
+public class MoveableB : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningCar или его наследника
+ ///
+ private readonly DrawningB? _B = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningCar
+ public MoveableB(DrawningB B)
+ {
+ _B = B;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ //определение методов интерфейса
+ get
+ {
+ if (_B == null || _B.EntityB == null ||
+ !_B.GetPosX.HasValue || !_B.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_B.GetPosX.Value,
+ _B.GetPosY.Value, _B.GetWidth, _B.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_B?.EntityB?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_B == null || _B.EntityB == null)
+ {
+ return false;
+ }
+ return _B.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/laba 0/laba 0/MovementStrategy/MovementDirection.cs b/laba 0/laba 0/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..ab71386
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,24 @@
+namespace laba_0.MovementStrategy;
+
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+
+ ///
+ /// Влево
+ ///
+ Left = 3,
+
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/laba 0/laba 0/MovementStrategy/ObjectParameters.cs b/laba 0/laba 0/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..ac4790a
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,75 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace laba_0.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/laba 0/laba 0/MovementStrategy/StrategyStatus.cs b/laba 0/laba 0/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..9275cc7
--- /dev/null
+++ b/laba 0/laba 0/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace laba_0.MovementStrategy;
+
+public enum StrategyStatus
+{
+
+ ///
+ /// все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+}