diff --git a/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs b/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs index d6c8da6..f1a237c 100644 --- a/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs +++ b/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs @@ -2,6 +2,11 @@ public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningBoat.cs b/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningBoat.cs index 9ecd91a..c80533c 100644 --- a/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningBoat.cs +++ b/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningBoat.cs @@ -37,12 +37,29 @@ public class DrawningBoat /// /// Ширина прорисовки катамарана /// - private readonly int _drawningCatamaranWidth = 80; + private readonly int _drawningBoatWidth = 80; /// /// Высота прорисовки катамарана /// - private readonly int _drawningCatamaranHeight = 62; + private readonly int _drawningBoatHeight = 80; + + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _drawningBoatWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningBoatHeight; /// /// Пустой конструктор @@ -73,8 +90,8 @@ public class DrawningBoat /// Высота прорисовки катамарана protected DrawningBoat(int drawningCatamaranWidth, int drawningCatamaranHeight) : this() { - _drawningCatamaranWidth = drawningCatamaranWidth; - _drawningCatamaranHeight = drawningCatamaranHeight; + _drawningBoatWidth = drawningCatamaranWidth; + _pictureHeight = drawningCatamaranHeight; } @@ -86,7 +103,7 @@ public class DrawningBoat /// - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { - if (_drawningCatamaranHeight > height || _drawningCatamaranWidth > width) + if (_drawningBoatHeight > height || _drawningBoatWidth > width) { return false; } @@ -115,18 +132,18 @@ public class DrawningBoat return; } - if (x < 0 || x + _drawningCatamaranWidth > _pictureWidth) + if (x < 0 || x + _drawningBoatWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawningCatamaranWidth; + _startPosX = _pictureWidth - _drawningBoatWidth; } else { _startPosX = x; } - if (y < 0 || y + _drawningCatamaranHeight > _pictureHeight) + if (y < 0 || y + _drawningBoatHeight > _pictureHeight) { - _startPosY = _pictureHeight - _drawningCatamaranHeight; + _startPosY = _pictureHeight - _drawningBoatHeight; } else { @@ -163,7 +180,7 @@ public class DrawningBoat return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityBoat.Step + _drawningCatamaranWidth < _pictureWidth) + if (_startPosX.Value + EntityBoat.Step + _drawningBoatWidth < _pictureWidth) { _startPosX += (int)EntityBoat.Step; return true; @@ -171,7 +188,7 @@ public class DrawningBoat return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityBoat.Step + _drawningCatamaranHeight < _pictureHeight) + if (_startPosY.Value + EntityBoat.Step + _drawningBoatHeight < _pictureHeight) { _startPosY += (int)EntityBoat.Step; } diff --git a/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaran.cs b/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaran.cs index 55c2890..40f166f 100644 --- a/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaran.cs +++ b/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaran.cs @@ -8,36 +8,6 @@ namespace ProjectCatamaran.Drawnings; public class DrawningCatamaran : DrawningBoat { - /// - /// Ширина окна - /// - private int? _pictureWidth; - - /// - /// Высота окна - /// - private int? _pictureHeight; - - /// - /// Левая координата прорисовки катамарана - /// - private int? GetPosX => _startPosX; - - /// - /// Верхняя координата прорисовки катамарана - /// - private int? GetPosY => _startPosY; - - /// - /// Ширина прорисовки катамарана - /// - private readonly int _drawningCatamaranWidth = 79; - - /// - /// Высота прорисовки катамарана - /// - private readonly int _drawningCatamaranHeight = 75; - /// /// Конструктор /// @@ -48,7 +18,7 @@ public class DrawningCatamaran : DrawningBoat /// /// /// - public DrawningCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool leftfloater, bool rightfloater) : base(79, 75) + public DrawningCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool leftfloater, bool rightfloater) : base(80, 80) { EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, sail, leftfloater, rightfloater); diff --git a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs index ece7e00..bc91fa0 100644 --- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs +++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonRight = new Button(); buttonCreatBoat = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCatamaran).BeginInit(); SuspendLayout(); // @@ -119,11 +121,33 @@ buttonCreatBoat.UseVisualStyleBackColor = true; buttonCreatBoat.Click += ButtonCreatBoat_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(653, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(135, 28); + comboBoxStrategy.TabIndex = 8; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(694, 46); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(94, 29); + buttonStrategyStep.TabIndex = 9; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormCatamaran // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreatBoat); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -146,5 +170,7 @@ private Button buttonDown; private Button buttonRight; private Button buttonCreatBoat; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs index a57d253..8b7a425 100644 --- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs +++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectCatamaran.Drawnings; +using ProjectCatamaran.MovementStrategy; namespace ProjectCatamaran { @@ -18,12 +19,19 @@ namespace ProjectCatamaran /// private DrawningBoat? _drawningBoat; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + + /// /// Конструктор формы /// public FormCatamaran() { InitializeComponent(); + _strategy = null; } /// @@ -51,6 +59,8 @@ namespace ProjectCatamaran } _drawningBoat.SetPictureSize(pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); _drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -121,6 +131,48 @@ namespace ProjectCatamaran } } + /// + /// обработка нажатия кнопки шаг + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningBoat == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + + if (_strategy == null) + { + return; + } + _strategy.SetData(new MovebleBoat(_drawningBoat), pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/AbstractStrategy.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..394eab7 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.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/ProjectCatamaran/ProjectCatamaran/MovementStrategy/IMoveableObject.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..969fa2e --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToBorder.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..9208068 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder - GetStep() <= FieldWidth + && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight + && objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} + diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToCenter.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..ae5a690 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.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/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovebleBoat.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovebleBoat.cs new file mode 100644 index 0000000..11ba420 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovebleBoat.cs @@ -0,0 +1,62 @@ +using ProjectCatamaran.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.MovementStrategy; + +public class MovebleBoat : IMoveableObject +{ + /// + /// Поле-объект класса DrawningBoat или его наследника + /// + private readonly DrawningBoat? _boat = null; + /// + /// Конструктор + /// + /// Объект класса DrawningBoat + public MovebleBoat(DrawningBoat boat) + { + _boat = boat; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_boat == null || _boat.EntityBoat == null || + !_boat.GetPosX.HasValue || !_boat.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_boat.GetPosX.Value, + _boat.GetPosY.Value, _boat.GetWidth, _boat.GetHeight); + } + } + public int GetStep => (int)(_boat?.EntityBoat?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_boat == null || _boat.EntityBoat == null) + { + return false; + } + return _boat.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/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovementDirection.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..47cf476 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovementDirection.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.MovementStrategy; + +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/ObjectParameters.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..7c50ff0 --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.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/ProjectCatamaran/ProjectCatamaran/MovementStrategy/StrategyStatus.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..cc85dcf --- /dev/null +++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCatamaran.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +}