Compare commits

..

2 Commits

Author SHA1 Message Date
2b478161f0 л2 2023-11-13 14:05:09 +04:00
ce9b9e2d60 лабораторная работа 2 2023-11-13 14:04:53 +04:00
32 changed files with 569 additions and 217 deletions

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using ProjectMotorShip.DrawingObjects;
namespace ProjectMotorShip.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _movebleObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_movebleObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(DirectionType.Left);
protected bool MoveRight() => MoveTo(DirectionType.Right);
protected bool MoveUp() => MoveTo(DirectionType.Up);
protected bool MoveDown() => MoveTo(DirectionType.Down);
protected ObjectParametrs? GetObjectParametrs => _movebleObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _movebleObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestination();
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_movebleObject?.CheckCanMove(directionType) ?? false)
{
_movebleObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -3,183 +3,50 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ProjectMotorShip.Entities;
namespace ProjectMotorShip namespace ProjectMotorShip.DrawingObjects
{ {
public class DrawningMotorShip public class DrawningMotorShip : DrawningShip
{ {
/// <summary> public DrawningMotorShip(int speed, double weight,
/// Класс-сущность Color mainColor, Color optionalColor, bool pipes,
/// </summary> bool fuelCompartment, int width, int height) :
public EntityMotorShip? EntityMotorShip { get; private set; } base(speed, weight, mainColor, width, height, 100, 60)
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки автомобиля
/// </summary>
private int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки автомобиля
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки автомобиля
/// </summary>
private readonly int _MotorShipWidth = 100;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _MotorShipHeight = 70;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет корпуса</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия труб</param>
/// <param name="section">Признак наличия отсека для топлива</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pipes, bool section,
int width, int height)
{ {
if (width < _MotorShipWidth || height < _MotorShipHeight) if (EntityShip != null)
{ {
return false; EntityShip = new EntityMotorShip(speed, weight, mainColor,
optionalColor, pipes, fuelCompartment);
} }
_pictureWidth = width;
_pictureHeight = height;
EntityMotorShip = new EntityMotorShip();
EntityMotorShip.Init(speed, weight, bodyColor, additionalColor, pipes, section);
return true;
} }
/// <summary>
/// Установка позиции public override void DrawTrasport(Graphics g)
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{ {
/// <summary> if (EntityShip is not EntityMotorShip motorShip)
/// Проверка, что x и y не выходят за пределы формы
/// </summary>
if (x < 0 || x + _MotorShipWidth > _pictureWidth)
{
x = 20;
}
if (y < 0 || y + _MotorShipHeight > _pictureHeight)
{
y = 20;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (EntityMotorShip == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityMotorShip.Step > 0)
{
_startPosX -= (int)EntityMotorShip.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityMotorShip.Step > 0)
{
_startPosY -= (int)EntityMotorShip.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + _MotorShipWidth + EntityMotorShip.Step < _pictureWidth)
{
_startPosX += (int)EntityMotorShip.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + _MotorShipHeight + EntityMotorShip.Step < _pictureHeight)
{
_startPosY += (int)EntityMotorShip.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityMotorShip == null)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush optionalBrush = new SolidBrush(EntityMotorShip.BodyColor); Brush optionalBrush = new SolidBrush(motorShip.OptionalColor);
if (EntityMotorShip.Pipes) if (motorShip.Pipes)
{ {
g.FillRectangle(optionalBrush, _startPosX + 70, _startPosY, 10, 30); g.FillRectangle(optionalBrush, _startPosX + 70, _startPosY, 10, 30);
g.FillRectangle(optionalBrush, _startPosX + 50, _startPosY + 10, 10, 20); g.FillRectangle(optionalBrush, _startPosX + 50, _startPosY + 10, 10, 20);
g.DrawRectangle(pen, _startPosX + 50, _startPosY + 10, 10, 20); g.DrawRectangle(pen, _startPosX + 50, _startPosY + 10, 10, 20);
g.DrawRectangle(pen, _startPosX + 70, _startPosY, 10, 30); g.DrawRectangle(pen, _startPosX + 70, _startPosY, 10, 30);
} }
if (EntityMotorShip.Section) if (motorShip.FuelCompartment)
{ {
g.FillRectangle(optionalBrush, _startPosX + 10, _startPosY + 30, 10, 10); g.FillRectangle(optionalBrush, _startPosX + 10, _startPosY + 30, 10, 10);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 30, 10, 10); g.DrawRectangle(pen, _startPosX + 10, _startPosY + 30, 10, 10);
} }
Brush mainBrush = new SolidBrush(EntityMotorShip.AdditionalColor); _startPosY += 30;
//палуба base.DrawTrasport(g);
g.FillRectangle(mainBrush, _startPosX + 30, _startPosY + 30, 60, 10); _startPosY -= 30;
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 30, 60, 10);
//корпус
g.FillPolygon(mainBrush, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX + 100, _startPosY + 40),
new Point(_startPosX + 90, _startPosY + 60),
new Point(_startPosX + 20, _startPosY + 60),
new Point(_startPosX, _startPosY + 40),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX + 100, _startPosY + 40),
new Point(_startPosX + 90, _startPosY + 60),
new Point(_startPosX + 20, _startPosY + 60),
new Point(_startPosX, _startPosY + 40),
}
);
//якорь
g.DrawLine(pen, _startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55);
g.DrawLine(pen, _startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50);
g.DrawLine(pen, _startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55);
}
internal void Init(int v1, int v2, Color color1, Color color2, bool v3, bool v4, bool v5, int width, int height)
{
throw new NotImplementedException();
} }
} }
} }

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectMotorShip.DrawingObjects;
namespace ProjectMotorShip.MovementStrategy
{
public class DrawningObjectShip : IMoveableObject
{
private readonly DrawningShip? _drawningShip = null;
public DrawningObjectShip(DrawningShip drawningShip)
{
_drawningShip = drawningShip;
}
public ObjectParametrs? GetObjectPosition
{
get
{
if (_drawningShip == null || _drawningShip.EntityShip == null)
{
return null;
}
return new ObjectParametrs(_drawningShip.GetPosX,
_drawningShip.GetPosY, _drawningShip.GetWidth,
_drawningShip.GetHeight);
}
}
public int GetStep => (int)(_drawningShip?.EntityShip?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningShip?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningShip?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectMotorShip.Entities;
namespace ProjectMotorShip.DrawingObjects
{
public class DrawningShip
{
public EntityShip? EntityShip { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _shipWidth = 100;
protected readonly int _shipHeight = 30;
public DrawningShip(int speed, double weight, Color mainColor, int width, int heigth)
{
if (width <= _shipWidth || heigth <= _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = heigth;
EntityShip = new EntityShip(speed, weight, mainColor);
}
protected DrawningShip(int speed, double weight,
Color mainColor, int width, int heigth,
int shipWidth, int shipHeight)
{
if (width <= shipWidth || heigth <= shipHeight)
{
return;
}
_pictureHeight = heigth;
_pictureWidth = width;
_shipHeight = shipHeight;
_shipWidth = shipWidth;
EntityShip = new EntityShip(speed, weight, mainColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || y < 0 || x + _shipWidth > _pictureWidth || y + _shipHeight > _pictureHeight)
{
x = 10;
y = 10;
}
_startPosX = x;
_startPosY = y;
}
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _shipWidth;
public int GetHeight => _shipHeight;
public bool CanMove(DirectionType direction)
{
if (EntityShip == null)
{
return false;
}
return direction switch
{
DirectionType.Left => _startPosX - EntityShip.Step > 0,
DirectionType.Up => _startPosY - EntityShip.Step > 0,
DirectionType.Right => _startPosX + EntityShip.Step + _shipWidth <= _pictureWidth,
DirectionType.Down => _startPosY + EntityShip.Step + _shipHeight <= _pictureHeight,
_ => false,
};
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityShip == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
_startPosX -= (int)EntityShip.Step;
break;
case DirectionType.Up:
_startPosY -= (int)EntityShip.Step;
break;
case DirectionType.Right:
_startPosX += (int)EntityShip.Step;
break;
case DirectionType.Down:
_startPosY += (int)EntityShip.Step;
break;
}
}
public virtual void DrawTrasport(Graphics g)
{
if (EntityShip == null)
{
return;
}
Pen pen = new(Color.Black);
Brush mainBrush = new SolidBrush(EntityShip.MainColor);
//палуба
g.FillRectangle(mainBrush, _startPosX + 30, _startPosY, 60, 10);
g.DrawRectangle(pen, _startPosX + 30, _startPosY, 60, 10);
//корпус
g.FillPolygon(mainBrush, new Point[]
{
new Point(_startPosX, _startPosY + 10),
new Point(_startPosX + 100, _startPosY + 10),
new Point(_startPosX + 90, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX, _startPosY + 10),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 10),
new Point(_startPosX + 100, _startPosY + 10),
new Point(_startPosX + 90, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX, _startPosY + 10),
}
);
//якорь
g.DrawLine(pen, _startPosX + 25, _startPosY + 15, _startPosX + 25, _startPosY + 25);
g.DrawLine(pen, _startPosX + 20, _startPosY + 20, _startPosX + 30, _startPosY + 20);
g.DrawLine(pen, _startPosX + 23, _startPosY + 25, _startPosX + 27, _startPosY + 25);
}
}
}

View File

@ -5,56 +5,20 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32; using static System.Collections.Specialized.BitVector32;
namespace ProjectMotorShip namespace ProjectMotorShip.Entities
{ {
public class EntityMotorShip public class EntityMotorShip : EntityShip
{ {
/// <summary> public Color OptionalColor { get; private set; }
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия труб
/// </summary>
public bool Pipes { get; private set; } public bool Pipes { get; private set; }
/// <summary> public bool FuelCompartment { get; private set; }
/// Признак (опция) наличия отсека для топлива public EntityMotorShip(int speed, double weight,
/// </summary> Color mainColor, Color optionalColor,
public bool Section { get; private set; } bool pipes, bool fuelCompartment) : base(speed, weight, mainColor)
/// <summary>
/// Шаг перемещения теплохода
/// </summary>
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес теплохода</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия труб</param>
/// <param name="section">Признак наличия отсека для топлива</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool pipes, bool section)
{ {
Speed = speed; OptionalColor = optionalColor;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Pipes = pipes; Pipes = pipes;
Section = section; FuelCompartment = fuelCompartment;
} }
} }
} }

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMotorShip.Entities
{
public class EntityShip
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color MainColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public EntityShip(int speed, double weight, Color mainColor)
{
Speed = speed;
Weight = weight;
MainColor = mainColor;
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMotorShip.MovementStrategy
{
public interface IMoveableObject
{
ObjectParametrs? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -34,6 +34,9 @@
this.buttonRight = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.buttonStep = new System.Windows.Forms.Button();
this.buttonCreateShip = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -50,11 +53,11 @@
// buttonCreate // buttonCreate
// //
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(62, 402); this.buttonCreate.Location = new System.Drawing.Point(39, 402);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(94, 29); this.buttonCreate.Size = new System.Drawing.Size(144, 29);
this.buttonCreate.TabIndex = 1; this.buttonCreate.TabIndex = 1;
this.buttonCreate.Text = "Создать"; this.buttonCreate.Text = "Создать теплоход";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click_1); this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click_1);
// //
@ -104,11 +107,44 @@
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
// //
// comboBoxStrategy
//
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"1",
"2"});
this.comboBoxStrategy.Location = new System.Drawing.Point(719, 23);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28);
this.comboBoxStrategy.TabIndex = 6;
//
// buttonStep
//
this.buttonStep.Location = new System.Drawing.Point(782, 57);
this.buttonStep.Name = "buttonStep";
this.buttonStep.Size = new System.Drawing.Size(88, 29);
this.buttonStep.TabIndex = 7;
this.buttonStep.Text = "Шаг";
this.buttonStep.UseVisualStyleBackColor = true;
this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click);
//
// buttonCreateShip
//
this.buttonCreateShip.Location = new System.Drawing.Point(201, 402);
this.buttonCreateShip.Name = "buttonCreateShip";
this.buttonCreateShip.Size = new System.Drawing.Size(153, 29);
this.buttonCreateShip.TabIndex = 8;
this.buttonCreateShip.Text = "Создать корабль";
this.buttonCreateShip.UseVisualStyleBackColor = true;
this.buttonCreateShip.Click += new System.EventHandler(this.buttonCreateShip_Click);
//
// MotorShip // MotorShip
// //
// this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
// this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(882, 453); this.ClientSize = new System.Drawing.Size(882, 453);
this.Controls.Add(this.buttonCreateShip);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonRight);
@ -131,5 +167,8 @@
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private Button buttonDown; private Button buttonDown;
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonCreateShip;
} }
} }

View File

@ -1,4 +1,9 @@
using System.Windows.Forms; using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using ProjectMotorShip.DrawingObjects;
using ProjectMotorShip.MovementStrategy;
using Status = ProjectMotorShip.MovementStrategy.Status;
using Button = System.Windows.Forms.Button;
namespace ProjectMotorShip namespace ProjectMotorShip
{ {
@ -8,7 +13,10 @@ namespace ProjectMotorShip
/// <summary> /// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà /// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary> /// </summary>
private DrawningMotorShip? _drawningMotorShip; private DrawningShip? _drawningShip;
private AbstractStrategy? _abstractStrategy;
private object _drawingShip;
public MotorShip() public MotorShip()
{ {
InitializeComponent(); InitializeComponent();
@ -18,18 +26,18 @@ namespace ProjectMotorShip
/// </summary> /// </summary>
private void Draw() private void Draw()
{ {
if (_drawningMotorShip == null) if (_drawningShip == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningMotorShip.DrawTransport(gr); pictureBox1.Image = bmp; _drawningShip.DrawTrasport(gr); pictureBox1.Image = bmp;
} }
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawningMotorShip == null) if (_drawningShip == null)
{ {
return; return;
} }
@ -37,16 +45,16 @@ namespace ProjectMotorShip
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawningMotorShip.MoveTransport(DirectionType.Up); _drawningShip.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawningMotorShip.MoveTransport(DirectionType.Down); _drawningShip.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawningMotorShip.MoveTransport(DirectionType.Left); _drawningShip.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawningMotorShip.MoveTransport(DirectionType.Right); _drawningShip.MoveTransport(DirectionType.Right);
break; break;
} }
Draw(); Draw();
@ -55,14 +63,64 @@ namespace ProjectMotorShip
private void buttonCreate_Click_1(object sender, EventArgs e) private void buttonCreate_Click_1(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningMotorShip = new DrawningMotorShip(); _drawningShip = new DrawningMotorShip(random.Next(100, 300), random.Next(1000, 3000),
_drawningMotorShip.Init(random.Next(100, 300), random.Next(1000, 3000),
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)),
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)),
Convert.ToBoolean(random.Next(0, 2)), /*Convert.ToBoolean(random.Next(0, 2))*/ Convert.ToBoolean(random.Next(0, 2)), /*Convert.ToBoolean(random.Next(0, 2))*/
pictureBox1.Width, pictureBox1.Height); pictureBox1.Width, pictureBox1.Height);
_drawningMotorShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); _drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawningShip == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(
new DrawningObjectShip(_drawningShip),
pictureBox1.Width,
pictureBox1.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void buttonCreateShip_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningShip = new DrawningShip(
random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBox1.Width,
pictureBox1.Height);
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
} }
} }

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMotorShip.MovementStrategy
{
internal class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestination()
{
var objParams = GetObjectParametrs;
if (objParams == null)
{
return false;
}
return objParams.RightBorder <= FieldWidth &&
objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder <= FieldHeight &&
objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParametrs;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
MoveRight();
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMotorShip.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
var objParams = GetObjectParametrs;
if (objParams == null)
{
return false;
}
return
Math.Abs(objParams.ObjectMiddleHorizontal - FieldWidth / 2) <= GetStep()
&&
Math.Abs(objParams.ObjectMiddleVertical - FieldHeight / 2) <= GetStep();
}
protected override void MoveToTarget()
{
var objParams = GetObjectParametrs;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMotorShip.MovementStrategy
{
public class ObjectParametrs
{
private readonly int _x;
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;
public ObjectParametrs(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectMotorShip.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}

View File

@ -1 +1 @@
92a56f81e67c3af2f643156bfb15fe83c802e1d6 556daec231b7e3b877f10be99e1b34a9bd49f3d7