second part lab2

This commit is contained in:
yu.shlyapin 2025-02-03 21:56:56 +04:00
parent aa2c0de477
commit 6066c88256
11 changed files with 289 additions and 10 deletions

View File

@ -23,8 +23,4 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="MovementStategy\" />
</ItemGroup>
</Project>

View File

@ -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;
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
namespace AircraftCarrier.MovementStategy;
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool TryMoveObject(MovementDirection direction);
}

View File

@ -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();
}
}
}
}

View File

@ -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();
}
}
}

View File

@ -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,
};
}
}

View File

@ -0,0 +1,9 @@
namespace AircraftCarrier.MovementStategy;
public enum MovementDirection
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
namespace AircraftCarrier.MovementStategy;
public enum StrategyStatus
{
NotInit,
InProgress,
Finish
}