diff --git a/LabOOP_1/LabOOP_1/Drawnings/DirectionType.cs b/LabOOP_1/LabOOP_1/Drawnings/DirectionType.cs index d332859..e1f8a2d 100644 --- a/LabOOP_1/LabOOP_1/Drawnings/DirectionType.cs +++ b/LabOOP_1/LabOOP_1/Drawnings/DirectionType.cs @@ -8,6 +8,13 @@ namespace Project_Catamaran.Drawnings; public enum DirectionType { + + /// + /// Вверх + /// + Unknow = -1, + + /// /// Вверх /// diff --git a/LabOOP_1/LabOOP_1/Drawnings/DrawningSimpleCatamaran.cs b/LabOOP_1/LabOOP_1/Drawnings/DrawningSimpleCatamaran.cs index ef3dfa6..157da07 100644 --- a/LabOOP_1/LabOOP_1/Drawnings/DrawningSimpleCatamaran.cs +++ b/LabOOP_1/LabOOP_1/Drawnings/DrawningSimpleCatamaran.cs @@ -37,12 +37,38 @@ public class DrawningSimpleCatamaran /// /// Ширина прорисовки катамарана /// - private readonly int _drawningCatamaranWidth = 90; + private readonly int _drawningCatamaranWidth = 100; /// /// Высота прорисовки катамарана /// - private readonly int _drawningCatamaranHeight = 60; + private readonly int _drawningCatamaranHeight = 85; + + + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningCatamaranWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningCatamaranHeight; + + /// + /// Пустой конструктор + /// + /// /// Пустой конструктор @@ -207,6 +233,7 @@ public class DrawningSimpleCatamaran Pen pen = new(Color.Black); pen.Width = 2; + Brush simpleBrush = new SolidBrush(EntitySimpleCatamaran.BodyColor); @@ -219,7 +246,7 @@ public class DrawningSimpleCatamaran g.DrawLine(pen, _startPosX.Value + 70, _startPosY.Value + 30, _startPosX.Value + 90, _startPosY.Value + 40); g.DrawLine(pen, _startPosX.Value + 70, _startPosY.Value + 50, _startPosX.Value + 90, _startPosY.Value + 40); //отрисовка цвета палубы - g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 30, 60, 20); + g.FillEllipse(simpleBrush, _startPosX.Value + 5, _startPosY.Value + 30, 60, 20); // отрисовка поплавков diff --git a/LabOOP_1/LabOOP_1/FormCatamaran.Designer.cs b/LabOOP_1/LabOOP_1/FormCatamaran.Designer.cs index 0488367..7f1ba4e 100644 --- a/LabOOP_1/LabOOP_1/FormCatamaran.Designer.cs +++ b/LabOOP_1/LabOOP_1/FormCatamaran.Designer.cs @@ -35,6 +35,8 @@ partial class FormCatamaran buttonRight = new Button(); pictureBoxCatamaran = new PictureBox(); buttonCreateCatamaran = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCatamaran).BeginInit(); SuspendLayout(); // @@ -117,11 +119,33 @@ partial class FormCatamaran buttonCreateCatamaran.UseVisualStyleBackColor = true; buttonCreateCatamaran.Click += buttonCreateCatamaran_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(606, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(182, 33); + comboBoxStrategy.TabIndex = 8; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(676, 51); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(112, 34); + buttonStrategyStep.TabIndex = 9; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormCatamaran // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateCatamaran); Controls.Add(buttonRight); Controls.Add(buttonUp); @@ -143,4 +167,6 @@ partial class FormCatamaran private Button buttonRight; private PictureBox pictureBoxCatamaran; private Button buttonCreateCatamaran; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } \ No newline at end of file diff --git a/LabOOP_1/LabOOP_1/FormCatamaran.cs b/LabOOP_1/LabOOP_1/FormCatamaran.cs index ce5ce19..b3b8fec 100644 --- a/LabOOP_1/LabOOP_1/FormCatamaran.cs +++ b/LabOOP_1/LabOOP_1/FormCatamaran.cs @@ -1,4 +1,5 @@ using Project_Catamaran.Drawnings; +using Project_Catamaran.MovementStrategy; using System; using System.Collections.Generic; using System.ComponentModel; @@ -13,11 +14,19 @@ namespace Project_Catamaran; public partial class FormCatamaran : Form { + private DrawningSimpleCatamaran? _drawningSimpleCatamaran; + /// + /// Стратегия перемещения + /// + + private AbstractStrategy? _strategy; + public FormCatamaran() { InitializeComponent(); + _strategy = null; } private void Draw() @@ -59,6 +68,8 @@ public partial class FormCatamaran : Form } _drawningSimpleCatamaran.SetPictureSize(pictureBoxCatamaran.Width, pictureBoxCatamaran.Height); _drawningSimpleCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -114,4 +125,40 @@ public partial class FormCatamaran : Form } } -} + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningSimpleCatamaran == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableSimpleCatamaran(_drawningSimpleCatamaran), pictureBoxCatamaran.Width, pictureBoxCatamaran.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/LabOOP_1/LabOOP_1/MovementStrategy/AbstractStrategy.cs b/LabOOP_1/LabOOP_1/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..0f8d6ee --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.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/LabOOP_1/LabOOP_1/MovementStrategy/IMoveableObject.cs b/LabOOP_1/LabOOP_1/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..e79d8a2 --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/LabOOP_1/LabOOP_1/MovementStrategy/MoveToBorder.cs b/LabOOP_1/LabOOP_1/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..898e0a6 --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.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(); + } + + } + } +} \ No newline at end of file diff --git a/LabOOP_1/LabOOP_1/MovementStrategy/MoveToCenter.cs b/LabOOP_1/LabOOP_1/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..43dc551 --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,59 @@ +using Project_Catamaran.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.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/LabOOP_1/LabOOP_1/MovementStrategy/MoveableSimpleCatamaran.cs b/LabOOP_1/LabOOP_1/MovementStrategy/MoveableSimpleCatamaran.cs new file mode 100644 index 0000000..30d8b90 --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/MoveableSimpleCatamaran.cs @@ -0,0 +1,66 @@ +using Project_Catamaran.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.MovementStrategy; + +public class MoveableSimpleCatamaran : IMoveableObject +{ + /// + /// Поле-объект класса DrawningSimpleCatamaran или его наследника + /// + private readonly DrawningSimpleCatamaran? _catamaran = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningSimpleCatamaran + public MoveableSimpleCatamaran(DrawningSimpleCatamaran catamaran) + { + _catamaran = catamaran; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_catamaran == null || _catamaran.EntitySimpleCatamaran == null || !_catamaran.GetPosX.HasValue || !_catamaran.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_catamaran.GetPosX.Value, _catamaran.GetPosY.Value, _catamaran.GetWidth, _catamaran.GetHeight); + } + } + + public int GetStep => (int)(_catamaran?.EntitySimpleCatamaran?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_catamaran == null || _catamaran.EntitySimpleCatamaran == null) + { + return false; + } + + return _catamaran.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/LabOOP_1/LabOOP_1/MovementStrategy/MovementDirection.cs b/LabOOP_1/LabOOP_1/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..ad83e2f --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/MovementDirection.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} + diff --git a/LabOOP_1/LabOOP_1/MovementStrategy/ObjectParameters.cs b/LabOOP_1/LabOOP_1/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..fd1337f --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + namespace Project_Catamaran.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/LabOOP_1/LabOOP_1/MovementStrategy/StrategyStatus.cs b/LabOOP_1/LabOOP_1/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..5374ad0 --- /dev/null +++ b/LabOOP_1/LabOOP_1/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Project_Catamaran.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish + +}