From 6066c882560d228d6698cf0a094e11dda3b45623 Mon Sep 17 00:00:00 2001 From: "yu.shlyapin" Date: Mon, 3 Feb 2025 21:56:56 +0400 Subject: [PATCH] second part lab2 --- .../AircraftCarrier/AircraftCarrier.csproj | 4 -- .../FormAircraftCarrier.Designer.cs | 28 +++++++++ .../AircraftCarrier/FormAircraftCarrier.cs | 43 ++++++++++++-- .../MovementStategy/AbstractStrategy.cs | 57 +++++++++++++++++++ .../MovementStategy/IMoveableObject.cs | 8 +++ .../MovementStategy/MoveToCenter.cs | 44 ++++++++++++++ .../MovementStategy/MoveToCorner.cs | 29 ++++++++++ .../MovementStategy/MoveableCarrier.cs | 46 +++++++++++++++ .../MovementStategy/MovementDirection.cs | 9 +++ .../MovementStategy/ObjectParameters.cs | 23 ++++++++ .../MovementStategy/StrategyStatus.cs | 8 +++ 11 files changed, 289 insertions(+), 10 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/AbstractStrategy.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/IMoveableObject.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCenter.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCorner.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/MoveableCarrier.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/MovementDirection.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/ObjectParameters.cs create mode 100644 AircraftCarrier/AircraftCarrier/MovementStategy/StrategyStatus.cs diff --git a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj index e0f9d61..af03d74 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj +++ b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj @@ -23,8 +23,4 @@ - - - - \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs index b0cf125..f426a2b 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs @@ -35,6 +35,8 @@ buttonRight = new Button(); buttonUp = new Button(); buttonCreateSimple = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategy = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit(); SuspendLayout(); // @@ -122,11 +124,35 @@ buttonCreateSimple.UseVisualStyleBackColor = true; buttonCreateSimple.Click += buttonCreateSimple_Click; // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "к центру", "к углу" }); + comboBoxStrategy.Location = new Point(637, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategy + // + buttonStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStrategy.Location = new Point(694, 46); + buttonStrategy.Name = "buttonStrategy"; + buttonStrategy.Size = new Size(94, 29); + buttonStrategy.TabIndex = 8; + buttonStrategy.Text = "Шаг"; + buttonStrategy.UseVisualStyleBackColor = true; + buttonStrategy.Click += ButtonStrategy_Click; + // // FormAircraftCarrier // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 438); + Controls.Add(buttonStrategy); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateSimple); Controls.Add(buttonUp); Controls.Add(buttonRight); @@ -151,5 +177,7 @@ private Button buttonRight; private Button buttonUp; private Button buttonCreateSimple; + private ComboBox comboBoxStrategy; + private Button buttonStrategy; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index d3a8f73..9991b5b 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -1,16 +1,19 @@ using System.Security.Cryptography; using AircraftCarrier.Drawing; using AircraftCarrier.Drawings; +using AircraftCarrier.MovementStategy; namespace AircraftCarrier; public partial class FormAircraftCarrier : Form { private DrawingSimpleAircraftCarrier? _drawingAircraftCarrier; + private AbstractStrategy? _strategy; public FormAircraftCarrier() { InitializeComponent(); + _strategy = null; } private void Draw() @@ -22,7 +25,7 @@ public partial class FormAircraftCarrier : Form pictureBoxAircraftCarrier.Image = bmp; } - private void CreatedrawingAircraftCarrier(string name) + private void CreateDrawingAircraftCarrier(string name) { Random random = new(); switch (name) @@ -37,20 +40,21 @@ public partial class FormAircraftCarrier : Form _drawingAircraftCarrier = new DrawingSimpleAircraftCarrier(random.Next(100, 200), random.Next(1000, 2000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); break; default: - _drawingAircraftCarrier = null; - break; + return; } - if (_drawingAircraftCarrier == null) return; _drawingAircraftCarrier.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); _drawingAircraftCarrier.SetPosition(random.Next(10, 50), random.Next(10, 50)); Draw(); + + _strategy = null; + comboBoxStrategy.Enabled = true; } - private void ButtonCreate_Click(object sender, EventArgs e) => CreatedrawingAircraftCarrier(nameof(DrawingAircraftCarrier)); + private void ButtonCreate_Click(object sender, EventArgs e) => CreateDrawingAircraftCarrier(nameof(DrawingAircraftCarrier)); - private void buttonCreateSimple_Click(object sender, EventArgs e) => CreatedrawingAircraftCarrier(nameof(DrawingSimpleAircraftCarrier)); + private void buttonCreateSimple_Click(object sender, EventArgs e) => CreateDrawingAircraftCarrier(nameof(DrawingSimpleAircraftCarrier)); private void ButtonMove_Click(object sender, EventArgs e) { @@ -106,4 +110,31 @@ public partial class FormAircraftCarrier : Form Draw(); } } + + private void ButtonStrategy_Click(object sender, EventArgs e) + { + if (_drawingAircraftCarrier == null) return; + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToCorner(), + _ => null + }; + if (_strategy == null) return; + _strategy.SetData(new MoveableCarrier(_drawingAircraftCarrier), pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height); + } + if (_strategy == null) return; + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/AbstractStrategy.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/AbstractStrategy.cs new file mode 100644 index 0000000..7def9e1 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/AbstractStrategy.cs @@ -0,0 +1,57 @@ +namespace AircraftCarrier.MovementStategy; + +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 => _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 (IsTargetDestination()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + protected bool MoveRight() => MoveTo(MovementDirection.Right); + protected bool MoveUp() => MoveTo(MovementDirection.Up); + protected bool MoveDown() => MoveTo(MovementDirection.Down); + + protected abstract void MoveToTarget(); + protected abstract bool IsTargetDestination(); + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) return null; + return _moveableObject?.GetStep; + } + + private bool MoveTo(MovementDirection direction) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + return _moveableObject?.TryMoveObject(direction) ?? false; + } +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/IMoveableObject.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/IMoveableObject.cs new file mode 100644 index 0000000..d456ddb --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/IMoveableObject.cs @@ -0,0 +1,8 @@ +namespace AircraftCarrier.MovementStategy; + +public interface IMoveableObject +{ + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool TryMoveObject(MovementDirection direction); +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCenter.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCenter.cs new file mode 100644 index 0000000..9ed28e5 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCenter.cs @@ -0,0 +1,44 @@ +namespace AircraftCarrier.MovementStategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objectParameters = GetObjectParameters; + if (objectParameters == null) return false; + return objectParameters.CenterX - GetStep() <= FieldWidth/2 && objectParameters.CenterX + GetStep() >= FieldWidth/2 && + objectParameters.CenterY - GetStep() <= FieldHeight/2 && objectParameters.CenterY + GetStep() >= FieldHeight/2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objectParameters = GetObjectParameters; + if (objectParameters == null) return; + + int diffX = objectParameters.CenterX - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objectParameters.CenterY - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCorner.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCorner.cs new file mode 100644 index 0000000..f4b50f9 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/MoveToCorner.cs @@ -0,0 +1,29 @@ +namespace AircraftCarrier.MovementStategy; + +public class MoveToCorner : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objectParameters = GetObjectParameters; + if (objectParameters == null) return false; + return objectParameters.RightBorder + GetStep() >= FieldWidth && objectParameters.BottomBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objectParameters = GetObjectParameters; + if (objectParameters == null) return; + + int diffX = objectParameters.CenterX - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = objectParameters.CenterY - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/MoveableCarrier.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/MoveableCarrier.cs new file mode 100644 index 0000000..a48df85 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/MoveableCarrier.cs @@ -0,0 +1,46 @@ +using AircraftCarrier.Drawing; +using AircraftCarrier.Drawings; + +namespace AircraftCarrier.MovementStategy; + +public class MoveableCarrier : IMoveableObject +{ + private readonly DrawingSimpleAircraftCarrier? _aircraftCarrier = null; + + public MoveableCarrier(DrawingSimpleAircraftCarrier aircraftCarrier) + { + _aircraftCarrier = aircraftCarrier; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_aircraftCarrier == null || _aircraftCarrier.entityAircraftCarrier == null || + !_aircraftCarrier.GetPosX.HasValue || !_aircraftCarrier.GetPosY.HasValue) + return null; + return new ObjectParameters(_aircraftCarrier.GetPosX.Value, _aircraftCarrier.GetPosY.Value, _aircraftCarrier.GetWidth, _aircraftCarrier.GetHeight); + } + } + + public int GetStep => (int)(_aircraftCarrier?.entityAircraftCarrier?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_aircraftCarrier == null || _aircraftCarrier.entityAircraftCarrier == null) + return false; + return _aircraftCarrier.MoveAircraftCarrier(GetDirectionType(direction)); + } + + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + _ => DirectionType.Unknown, + }; + } +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/MovementDirection.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/MovementDirection.cs new file mode 100644 index 0000000..a6decd0 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/MovementDirection.cs @@ -0,0 +1,9 @@ +namespace AircraftCarrier.MovementStategy; + +public enum MovementDirection +{ + Up = 1, + Down = 2, + Left = 3, + Right = 4 +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/ObjectParameters.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/ObjectParameters.cs new file mode 100644 index 0000000..c5a1ecc --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/ObjectParameters.cs @@ -0,0 +1,23 @@ +namespace AircraftCarrier.MovementStategy; + +public class ObjectParameters +{ + private readonly int _x; + private readonly int _y; + private readonly int _width; + private readonly int _height; + + public int LeftBorder => _x; + public int RightBorder => _x + _width; + public int TopBorder => _y; + public int BottomBorder => _y + _height; + public int CenterX => _x + _width / 2; + public int CenterY => _y + _height / 2; + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/AircraftCarrier/AircraftCarrier/MovementStategy/StrategyStatus.cs b/AircraftCarrier/AircraftCarrier/MovementStategy/StrategyStatus.cs new file mode 100644 index 0000000..abb0589 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MovementStategy/StrategyStatus.cs @@ -0,0 +1,8 @@ +namespace AircraftCarrier.MovementStategy; + +public enum StrategyStatus +{ + NotInit, + InProgress, + Finish +}