Compare commits

...

4 Commits

Author SHA1 Message Date
yu.shlyapin
6066c88256 second part lab2 2025-02-03 21:56:56 +04:00
yu.shlyapin
aa2c0de477 first part lab2 2025-02-03 17:02:01 +04:00
yu.shlyapin
d35129e2c1 lab1 release 2025-02-03 12:00:21 +04:00
yu.shlyapin
635b9a84bd lab1 prerelease 2025-02-03 00:34:02 +04:00
26 changed files with 1020 additions and 77 deletions

View File

@ -8,4 +8,19 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,10 @@
namespace AircraftCarrier.Drawings;
public enum DirectionType
{
Unknown = -1,
Up = 1,
Down = 2,
Left = 3,
Right = 4,
}

View File

@ -0,0 +1,37 @@
using AircraftCarrier.Entities;
namespace AircraftCarrier.Drawing;
public class DrawingAircraftCarrier : DrawingSimpleAircraftCarrier
{
public DrawingAircraftCarrier(int speed, double weight, Color primaryColor, Color secondaryColor,
bool hasDeck, bool hasСabin) : base()
{
entityAircraftCarrier = new EntityAircraftCarrier(speed, weight, primaryColor, secondaryColor, hasDeck, hasСabin);
}
public override void DrawAircraftCarrier(Graphics g)
{
if (entityAircraftCarrier == null ||
!(entityAircraftCarrier is EntityAircraftCarrier entitySimpleAircraftCarrier) ||
!_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
base.DrawAircraftCarrier(g);
Brush brushPC = new SolidBrush(entitySimpleAircraftCarrier.PrimaryColor);
Brush brushSC = new SolidBrush(entitySimpleAircraftCarrier.SecondaryColor);
if (entitySimpleAircraftCarrier.HasDeck)
{
g.FillRectangle(brushPC, _startPosX.Value + 50, _startPosY.Value + 40, 55, 20);
}
if (entitySimpleAircraftCarrier.HasСabin)
{
g.FillRectangle(brushSC, _startPosX.Value + 105, _startPosY.Value + 25, 30, 50);
}
}
}

View File

@ -0,0 +1,114 @@
using AircraftCarrier.Drawings;
namespace AircraftCarrier.Drawing;
public class DrawingSimpleAircraftCarrier
{
public EntitySimpleAircraftCarrier? entityAircraftCarrier { get; protected set; }
protected int? _startPosX;
protected int? _startPosY;
private int? _pictureWidth;
private int? _pictureHeight;
private readonly int _drawingCarWidth = 250;
private readonly int _drawingCarHeight = 100;
public int? GetPosX => _startPosX;
public int? GetPosY => _startPosY;
public int GetWidth => _drawingCarWidth;
public int GetHeight => _drawingCarHeight;
protected DrawingSimpleAircraftCarrier()
{
_startPosX = null;
_startPosY = null;
_pictureWidth = null;
_pictureHeight = null;
}
public DrawingSimpleAircraftCarrier(int speed, double weight, Color primaryColor) : this()
{
entityAircraftCarrier = new EntitySimpleAircraftCarrier(speed, weight, primaryColor);
}
public void SetPictureSize(int width, int height)
{
if (_drawingCarHeight > height || _drawingCarWidth > width) return;
_pictureWidth = width;
_pictureHeight = height;
}
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) return;
if (x < 0 || y < 0 ||
x + _drawingCarWidth > _pictureWidth ||
y + _drawingCarHeight > _pictureHeight) return;
_startPosX = x;
_startPosY = y;
}
public bool MoveAircraftCarrier(DirectionType direction)
{
if (entityAircraftCarrier == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
case DirectionType.Up:
if (_startPosY - entityAircraftCarrier.Step > 0)
{
_startPosY -= (int)entityAircraftCarrier.Step;
}
return true;
case DirectionType.Down:
if (_startPosY + entityAircraftCarrier.Step + _drawingCarHeight < _pictureHeight)
{
_startPosY += (int)entityAircraftCarrier.Step;
}
return true;
case DirectionType.Left:
if (_startPosX - entityAircraftCarrier.Step > 0)
{
_startPosX -= (int)entityAircraftCarrier.Step;
}
return true;
case DirectionType.Right:
if (_startPosX + entityAircraftCarrier.Step + _drawingCarWidth < _pictureWidth)
{
_startPosX += (int)entityAircraftCarrier.Step;
}
return true;
default:
return false;
}
}
public virtual void DrawAircraftCarrier(Graphics g)
{
if (entityAircraftCarrier == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Point[] point = {
new Point(_startPosX.Value, _startPosY.Value),
new Point(_startPosX.Value + 200, _startPosY.Value),
new Point(_startPosX.Value + 250, _startPosY.Value + 50),
new Point(_startPosX.Value + 200, _startPosY.Value + 100),
new Point(_startPosX.Value, _startPosY.Value + 100),
};
g.DrawPolygon(pen, point);
g.DrawEllipse(pen, _startPosX.Value + 175, _startPosY.Value + 35, 30, 30);
}
}

View File

@ -0,0 +1,17 @@
namespace AircraftCarrier.Entities;
public class EntityAircraftCarrier : EntitySimpleAircraftCarrier
{
public Color SecondaryColor { get; private set; }
public bool HasDeck { get; private set; }
public bool HasСabin { get; private set; }
public EntityAircraftCarrier(int speed, double weight, Color primaryColor, Color secondaryColor,
bool hasDeck, bool hasСabin)
:base(speed, weight, primaryColor)
{
SecondaryColor = secondaryColor;
HasDeck = hasDeck;
HasСabin = hasСabin;
}
}

View File

@ -0,0 +1,16 @@
namespace AircraftCarrier;
public class EntitySimpleAircraftCarrier
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public double Step => Speed * 100 / Weight;
public Color PrimaryColor { get; private set; }
public EntitySimpleAircraftCarrier(int speed, double weight, Color primaryColor)
{
Speed = speed;
Weight = weight;
PrimaryColor = primaryColor;
}
}

View File

@ -1,39 +0,0 @@
namespace AircraftCarrier
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
}

View File

@ -1,10 +0,0 @@
namespace AircraftCarrier
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,183 @@
namespace AircraftCarrier
{
partial class FormAircraftCarrier
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
pictureBoxAircraftCarrier = new PictureBox();
buttonCreate = new Button();
buttonLeft = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonUp = new Button();
buttonCreateSimple = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategy = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit();
SuspendLayout();
//
// pictureBoxAircraftCarrier
//
pictureBoxAircraftCarrier.BorderStyle = BorderStyle.FixedSingle;
pictureBoxAircraftCarrier.Dock = DockStyle.Fill;
pictureBoxAircraftCarrier.Location = new Point(0, 0);
pictureBoxAircraftCarrier.Name = "pictureBoxAircraftCarrier";
pictureBoxAircraftCarrier.Size = new Size(800, 438);
pictureBoxAircraftCarrier.TabIndex = 0;
pictureBoxAircraftCarrier.TabStop = false;
//
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(20, 389);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.left_arrow;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(677, 386);
buttonLeft.Margin = new Padding(0);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.down_arrow;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(710, 386);
buttonDown.Margin = new Padding(0);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 3;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.right_arrow;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(743, 386);
buttonRight.Margin = new Padding(0);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.up_arrow;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(710, 353);
buttonUp.Margin = new Padding(0);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 5;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonCreateSimple
//
buttonCreateSimple.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateSimple.Location = new Point(120, 389);
buttonCreateSimple.Name = "buttonCreateSimple";
buttonCreateSimple.Size = new Size(128, 29);
buttonCreateSimple.TabIndex = 6;
buttonCreateSimple.Text = "Создать контур";
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);
Controls.Add(buttonDown);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxAircraftCarrier);
KeyPreview = true;
Name = "FormAircraftCarrier";
Text = "FormAircraftCarrier";
KeyDown += FormAircraftCarrier_KeyDown;
((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).EndInit();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxAircraftCarrier;
private Button buttonCreate;
private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private Button buttonUp;
private Button buttonCreateSimple;
private ComboBox comboBoxStrategy;
private Button buttonStrategy;
}
}

View File

@ -0,0 +1,140 @@
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()
{
if (_drawingAircraftCarrier == null) return;
Bitmap bmp = new Bitmap(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
Graphics g = Graphics.FromImage(bmp);
_drawingAircraftCarrier.DrawAircraftCarrier(g);
pictureBoxAircraftCarrier.Image = bmp;
}
private void CreateDrawingAircraftCarrier(string name)
{
Random random = new();
switch (name)
{
case nameof(DrawingAircraftCarrier):
_drawingAircraftCarrier = new DrawingAircraftCarrier(random.Next(100, 200), random.Next(1000, 2000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
break;
case nameof(DrawingSimpleAircraftCarrier):
_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:
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 buttonCreateSimple_Click(object sender, EventArgs e) => CreateDrawingAircraftCarrier(nameof(DrawingSimpleAircraftCarrier));
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingAircraftCarrier == null) return;
string nameButton = ((Button)sender)?.Name ?? string.Empty;
DirectionType? directionType = null;
switch (nameButton)
{
case "buttonUp":
directionType = DirectionType.Up;
break;
case "buttonDown":
directionType = DirectionType.Down;
break;
case "buttonLeft":
directionType = DirectionType.Left;
break;
case "buttonRight":
directionType = DirectionType.Right;
break;
}
if (directionType.HasValue)
{
_drawingAircraftCarrier.MoveAircraftCarrier(directionType.Value);
Draw();
}
}
private void FormAircraftCarrier_KeyDown(object sender, KeyEventArgs e)
{
if (_drawingAircraftCarrier == null) return;
DirectionType? directionType = null;
switch (e.KeyCode)
{
case Keys.W:
directionType = DirectionType.Up;
break;
case Keys.S:
directionType = DirectionType.Down;
break;
case Keys.A:
directionType = DirectionType.Left;
break;
case Keys.D:
directionType = DirectionType.Right;
break;
}
if (directionType.HasValue)
{
_drawingAircraftCarrier.MoveAircraftCarrier(directionType.Value);
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

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<root> <root>
<!-- <!--
Microsoft ResX Schema Microsoft ResX Schema
Version 2.0 Version 2.0
The primary goals of this format is to allow a simple XML format The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes various data types are done through the TypeConverter classes
associated with the data types. associated with the data types.
Example: Example:
... ado.net/XML headers & schema ... ... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader> <resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader> <resheader name="version">2.0</resheader>
@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment> <comment>This is a comment</comment>
</data> </data>
There are any number of "resheader" rows that contain simple There are any number of "resheader" rows that contain simple
name/value pairs. name/value pairs.
Each data row contains a name, and value. The row also contains a Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture. text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the Classes that don't support this are serialized and stored with the
mimetype set. mimetype set.
The mimetype is used for serialized objects, and tells the The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly: extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below. read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64 mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64 mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64 mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter : using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
--> -->

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
}

View File

@ -11,7 +11,7 @@ namespace AircraftCarrier
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new Form1()); Application.Run(new FormAircraftCarrier());
} }
} }
} }

View File

@ -0,0 +1,103 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Этот код создан программой.
// Исполняемая версия:4.0.30319.42000
//
// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае
// повторной генерации кода.
// </auto-generated>
//------------------------------------------------------------------------------
namespace AircraftCarrier.Properties {
using System;
/// <summary>
/// Класс ресурса со строгой типизацией для поиска локализованных строк и т.д.
/// </summary>
// Этот класс создан автоматически классом StronglyTypedResourceBuilder
// с помощью такого средства, как ResGen или Visual Studio.
// Чтобы добавить или удалить член, измените файл .ResX и снова запустите ResGen
// с параметром /str или перестройте свой проект VS.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Возвращает кэшированный экземпляр ResourceManager, использованный этим классом.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AircraftCarrier.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Перезаписывает свойство CurrentUICulture текущего потока для всех
/// обращений к ресурсу с помощью этого класса ресурса со строгой типизацией.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap down_arrow {
get {
object obj = ResourceManager.GetObject("down-arrow", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap left_arrow {
get {
object obj = ResourceManager.GetObject("left-arrow", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap right_arrow {
get {
object obj = ResourceManager.GetObject("right-arrow", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Поиск локализованного ресурса типа System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap up_arrow {
get {
object obj = ResourceManager.GetObject("up-arrow", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}

View File

@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="down-arrow" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>down-arrow.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="left-arrow" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>left-arrow.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="right-arrow" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>right-arrow.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="up-arrow" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>up-arrow.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB