From ddba502b6014dae4f709100e06a799f09a39a05d Mon Sep 17 00:00:00 2001 From: "[USERNAME]" Date: Thu, 16 Nov 2023 23:54:30 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bulldozer/Bulldozer/AbstractStrategy.cs | 71 ++++++ Bulldozer/Bulldozer/DrawningBulldozer.cs | 207 ++++++------------ Bulldozer/Bulldozer/DrawningFastBulldozer.cs | 55 +++++ .../Bulldozer/DrawningObjectBulldozer.cs | 34 +++ Bulldozer/Bulldozer/EntityBulldozer.cs | 47 +--- Bulldozer/Bulldozer/EntityFastBulldozer.cs | 22 ++ Bulldozer/Bulldozer/FormBulldozer.Designer.cs | 164 ++++++++------ Bulldozer/Bulldozer/FormBulldozer.cs | 87 ++++++-- Bulldozer/Bulldozer/IMoveableObject.cs | 32 +++ Bulldozer/Bulldozer/MoveToBorder.cs | 42 ++++ Bulldozer/Bulldozer/MoveToCenter.cs | 56 +++++ Bulldozer/Bulldozer/ObjectParameters.cs | 29 +++ Bulldozer/Bulldozer/Program.cs | 2 +- Bulldozer/Bulldozer/Status.cs | 15 ++ 14 files changed, 597 insertions(+), 266 deletions(-) create mode 100644 Bulldozer/Bulldozer/AbstractStrategy.cs create mode 100644 Bulldozer/Bulldozer/DrawningFastBulldozer.cs create mode 100644 Bulldozer/Bulldozer/DrawningObjectBulldozer.cs create mode 100644 Bulldozer/Bulldozer/EntityFastBulldozer.cs create mode 100644 Bulldozer/Bulldozer/IMoveableObject.cs create mode 100644 Bulldozer/Bulldozer/MoveToBorder.cs create mode 100644 Bulldozer/Bulldozer/MoveToCenter.cs create mode 100644 Bulldozer/Bulldozer/ObjectParameters.cs create mode 100644 Bulldozer/Bulldozer/Status.cs diff --git a/Bulldozer/Bulldozer/AbstractStrategy.cs b/Bulldozer/Bulldozer/AbstractStrategy.cs new file mode 100644 index 0000000..abab1a7 --- /dev/null +++ b/Bulldozer/Bulldozer/AbstractStrategy.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Bulldozer.DrawningObjects; + +namespace Bulldozer.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(DirectionTypeBulldozer.Left); + protected bool MoveRight() => MoveTo(DirectionTypeBulldozer.Right); + protected bool MoveUp() => MoveTo(DirectionTypeBulldozer.Up); + protected bool MoveDown() => MoveTo(DirectionTypeBulldozer.Down); + protected ObjectParameters? 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(DirectionTypeBulldozer DirectionTypeBulldozer) + { + if (_state != Status.InProgress) + { + return false; + } + if (_movebleObject?.CheckCanMove(DirectionTypeBulldozer) ?? false) + { + _movebleObject.MoveObject(DirectionTypeBulldozer); + return true; + } + return false; + } + } +} diff --git a/Bulldozer/Bulldozer/DrawningBulldozer.cs b/Bulldozer/Bulldozer/DrawningBulldozer.cs index 0badc2a..0dcda1c 100644 --- a/Bulldozer/Bulldozer/DrawningBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawningBulldozer.cs @@ -3,72 +3,45 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - -namespace Bulldozer +using Bulldozer.Entities; +namespace Bulldozer.DrawningObjects { public class DrawningBulldozer { - /// - /// Класс-сущность - /// - public EntityBulldozer? EntityBulldozer { get; private set; } - /// - /// Ширина окна - /// + public EntityBulldozer? EntityBulldozer { get; protected set; } private int _pictureWidth; - /// - /// Высота окна - /// private int _pictureHeight; - /// - /// Левая координата прорисовки бульдозера - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки бульдозера - /// - private int _startPosY; - /// - /// Ширина прорисовки бульдозера - /// - private readonly int bulldozerWidth = 170; - /// - /// Высота прорисовки бульдозера - /// - private readonly int bulldozerHeight = 85; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Цвет для ковша - /// Признак наличия переднего ковша - /// Признак наличия заднего ковша - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, - public bool Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, bool hasMoldboardfront, bool hasRipper, int width, int height) + protected int _startPosX; + protected int _startPosY; + protected readonly int _bulldozerWidth = 160; + protected readonly int _bulldozerHeight = 80; + public DrawningBulldozer(int speed, double weight, Color mainColor, int width, int heigth) { - if (width < _pictureWidth || height < _pictureHeight) + if (width <= _bulldozerWidth || heigth <= _bulldozerHeight) { - return false; + return; } _pictureWidth = width; - _pictureHeight = height; - EntityBulldozer = new EntityBulldozer(); - EntityBulldozer.Init(speed, weight, bulldozerColor, cabinColor, covshColor, hasMoldboardfront, hasRipper); - return true; + _pictureHeight = heigth; + EntityBulldozer = new EntityBulldozer(speed, weight, mainColor); + } + protected DrawningBulldozer(int speed, double weight, + Color mainColor, int width, int heigth, + int bulldozerWidth, int bulldozerHeight) + { + if (width <= _bulldozerWidth || heigth <= _bulldozerHeight) + { + return; + } + _pictureHeight = heigth; + _pictureWidth = width; + _bulldozerHeight = bulldozerHeight; + _bulldozerWidth = bulldozerWidth; + EntityBulldozer = new EntityBulldozer(speed, weight, mainColor); } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y public void SetPosition(int x, int y) { - if (x < 0 || y < 0 || x + bulldozerWidth > _pictureWidth || y + bulldozerHeight > _pictureHeight) + if (x < 0 || y < 0 || x + _bulldozerWidth > _pictureWidth || y + _bulldozerHeight > _pictureHeight) { x = 10; y = 10; @@ -76,84 +49,61 @@ namespace Bulldozer _startPosX = x; _startPosY = y; } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionTypeBulldozer direction) + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _bulldozerWidth; + public int GetHeight => _bulldozerHeight; + public bool CanMove(DirectionTypeBulldozer direction) { if (EntityBulldozer == null) - + { + return false; + } + return direction switch + { + DirectionTypeBulldozer.Left => _startPosX - EntityBulldozer.Step > 0, + DirectionTypeBulldozer.Up => _startPosY - EntityBulldozer.Step > 0, + DirectionTypeBulldozer.Right => _startPosX + EntityBulldozer.Step + _bulldozerWidth <= _pictureWidth, + DirectionTypeBulldozer.Down => _startPosY + EntityBulldozer.Step + _bulldozerHeight <= _pictureHeight, + _ => false, + }; + } + public void MoveTransport(DirectionTypeBulldozer direction) + { + if (!CanMove(direction) || EntityBulldozer == null) { return; } switch (direction) { - //влево case DirectionTypeBulldozer.Left: - if (_startPosX - EntityBulldozer.Step > 0) - { - _startPosX -= (int)EntityBulldozer.Step; - } - if (_startPosX - EntityBulldozer.Step < 0) - { - _startPosX -= _startPosX - (int)EntityBulldozer.Step; - } + _startPosX -= (int)EntityBulldozer.Step; break; - //вверх case DirectionTypeBulldozer.Up: - if (_startPosY - EntityBulldozer.Step > 0) - { - _startPosY -= (int)EntityBulldozer.Step; - } - else if (_startPosY - EntityBulldozer.Step < 0) - { - _startPosY -= _startPosY - (int)EntityBulldozer.Step; - } + _startPosY -= (int)EntityBulldozer.Step; break; - // вправо case DirectionTypeBulldozer.Right: - if (_startPosX + EntityBulldozer.Step + bulldozerWidth < _pictureWidth) - { - _startPosX += (int)EntityBulldozer.Step; - } - else if (_startPosX + EntityBulldozer.Step + bulldozerWidth > _pictureWidth) - { - _startPosX += _pictureWidth - _startPosX - bulldozerWidth; - } + _startPosX += (int)EntityBulldozer.Step; break; - //вниз case DirectionTypeBulldozer.Down: - if (_startPosY + EntityBulldozer.Step + bulldozerHeight < _pictureHeight) - { - _startPosY += (int)EntityBulldozer.Step; - } - else if (_startPosY + EntityBulldozer.Step + bulldozerHeight > _pictureHeight) - { - _startPosY += _pictureHeight - _startPosY - bulldozerHeight; - } + _startPosY += (int)EntityBulldozer.Step; break; } } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) + public virtual void DrawTrasport(Graphics g) { if (EntityBulldozer == null) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityBulldozer.CabinColor); - + Brush mainBrush = new SolidBrush(EntityBulldozer.MainColor); // Тело трактора - Brush bulldozerColor = new SolidBrush(EntityBulldozer.BulldozerColor); - g.FillRectangle(bulldozerColor, _startPosX + 25, _startPosY + 20, 110, 30); - g.FillRectangle(bulldozerColor, _startPosX + 60, _startPosY, 10, 30); - - int x = _startPosX + 30; // начальная позиция X + Brush tractorColor = new SolidBrush(EntityBulldozer.MainColor); + g.FillRectangle(tractorColor, _startPosX + 50, _startPosY + 20, 100, 30); + g.FillRectangle(tractorColor, _startPosX + 80, _startPosY, 10, 30); + //g.DrawEllipse(pen, _startPosX, _startPosY + 60, 90, 40); + int x = _startPosX + 50; // начальная позиция X int y = _startPosY; // начальная позиция Y int width = 110; // ширина прямоугольника int height = 30; // высота прямоугольника @@ -166,44 +116,15 @@ namespace Bulldozer g.DrawArc(pen, x + width - radius * 2 - 5, y + height - radius * 2 + 50, radius * 2, radius * 2, 0, 90); // нижний правый угол g.DrawLine(pen, x + width - radius - 5, y + height + 50, x + radius - 5, y + height + 50); // нижняя горизонталь g.DrawArc(pen, x - 5, y + height - radius * 2 + 50, radius * 2, radius * 2, 90, 90); // нижний левый угол - int wheelRadius = 15; - // Рисуем колеса трактора - g.DrawEllipse(pen, _startPosX + 30, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); - g.FillEllipse(additionalBrush, _startPosX + 30, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); - g.DrawEllipse(pen, _startPosX + 65, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); - g.FillEllipse(additionalBrush, _startPosX + 65, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); - g.DrawEllipse(pen, _startPosX + 100, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); - g.FillEllipse(additionalBrush, _startPosX + 100, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); - + g.DrawEllipse(pen, _startPosX + 50, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); + g.FillEllipse(mainBrush, _startPosX + 50, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); + g.DrawEllipse(pen, _startPosX + 120, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); + g.FillEllipse(mainBrush, _startPosX + 120, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); // Кабина - Brush cabinColor = new SolidBrush(EntityBulldozer.CabinColor); - g.FillRectangle(cabinColor, _startPosX + 105, _startPosY, 30, 20); - - // Рисуем ковш спереди - if (EntityBulldozer.HasMoldboardfront) - { - Point[] trianglePoints = new Point[] - { - new Point(_startPosX + 25, _startPosY + 30), - new Point(_startPosX + 25, _startPosY + 80), - new Point(_startPosX, _startPosY + 80), - }; - g.DrawPolygon(pen, trianglePoints); - } - // Рисуем ковш сзади - if (EntityBulldozer.HasRipper) - { - Point[] trianglePoints2 = new Point[] - { - new Point(_startPosX + 130, _startPosY + 50), - new Point(_startPosX + 160, _startPosY + 50), - new Point(_startPosX + 160, _startPosY + 80) - - }; - g.DrawPolygon(pen, trianglePoints2); - } + Brush cabinColor = new SolidBrush(EntityBulldozer.MainColor); + g.FillRectangle(cabinColor, _startPosX + 120, _startPosY, 30, 20); } } } diff --git a/Bulldozer/Bulldozer/DrawningFastBulldozer.cs b/Bulldozer/Bulldozer/DrawningFastBulldozer.cs new file mode 100644 index 0000000..eb31d2d --- /dev/null +++ b/Bulldozer/Bulldozer/DrawningFastBulldozer.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Bulldozer.Entities; + +namespace Bulldozer.DrawningObjects +{ + public class DrawningFastBulldozer : DrawningBulldozer + { + public DrawningFastBulldozer(int speed, double weight, Color mainColor, Color optionalColor, bool covsh, bool rearbucket, int width, int height) : base(speed, weight, mainColor, width, height, 200, 110) + { + if (EntityBulldozer != null) + { + EntityBulldozer = new EntityFastBulldozer(speed, weight, mainColor, + optionalColor, covsh, rearbucket); + } + } + public override void DrawTrasport(Graphics g) + { + if (EntityBulldozer is not EntityFastBulldozer fastBulldozer) + { + return; + } + Pen pen = new(Color.Black); + Brush optionalBrush = new SolidBrush(fastBulldozer.OptionalColor); + if (fastBulldozer.Covsh) + { + Point[] trianglePoints = new Point[] + { + new Point(_startPosX+50, _startPosY + 60), + new Point(_startPosX+50, _startPosY + 110), + new Point(_startPosX + 10, _startPosY + 110) + }; + // Рисуем треугольник + g.DrawPolygon(pen, trianglePoints); + } + if (fastBulldozer.Rearbucket) + { + Point[] trianglePoints = new Point[] + { + new Point(_startPosX+150, _startPosY + 60), + new Point(_startPosX+200, _startPosY + 60), + new Point(_startPosX + 200, _startPosY + 110) + }; + // Рисуем треугольник + g.DrawPolygon(pen, trianglePoints); + } + _startPosY += 30; + base.DrawTrasport(g); + _startPosY -= 30; + } + } +} diff --git a/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs b/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs new file mode 100644 index 0000000..761eafc --- /dev/null +++ b/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Bulldozer.DrawningObjects; + +namespace Bulldozer.MovementStrategy +{ + public class DrawningObjectBulldozer : IMoveableObject + { + private readonly DrawningBulldozer? _drawningBulldozer = null; + public DrawningObjectBulldozer(DrawningBulldozer drawningBulldozer) + { + _drawningBulldozer = drawningBulldozer; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningBulldozer == null || _drawningBulldozer.EntityBulldozer == null) + { + return null; + } + return new ObjectParameters(_drawningBulldozer.GetPosX, + _drawningBulldozer.GetPosY, _drawningBulldozer.GetWidth, + _drawningBulldozer.GetHeight); + } + } + public int GetStep => (int)(_drawningBulldozer?.EntityBulldozer?.Step ?? 0); + public bool CheckCanMove(DirectionTypeBulldozer direction) => _drawningBulldozer?.CanMove(direction) ?? false; + public void MoveObject(DirectionTypeBulldozer direction) => _drawningBulldozer?.MoveTransport(direction); + } +} diff --git a/Bulldozer/Bulldozer/EntityBulldozer.cs b/Bulldozer/Bulldozer/EntityBulldozer.cs index 75372f8..c205e4f 100644 --- a/Bulldozer/Bulldozer/EntityBulldozer.cs +++ b/Bulldozer/Bulldozer/EntityBulldozer.cs @@ -4,58 +4,19 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Bulldozer +namespace Bulldozer.Entities { public class EntityBulldozer { - /// - /// Скорость - /// public int Speed { get; private set; } - /// - /// Вес - /// public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BulldozerColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color CabinColor { get; private set; } - public Color CovshColor { get; private set; } - /// - /// Признак (опция) наличия переднего ковша - /// - public bool HasMoldboardfront { get; private set; } - /// - /// Признак (опция) наличия заднего ковша - /// - public bool HasRipper { get; private set; } - /// - /// Шаг перемещения автомобиля - /// + public Color MainColor { get; private set; } public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса спортивного автомобиля - /// - /// Скорость - /// Вес автомобиля - /// Основной цвет - /// Дополнительный цвет - /// Цвет для ковша - /// Признак наличия переднего ковша - /// Признак наличия заднего ковша - public void Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, bool hasMoldboardfront, bool hasRipper) + public EntityBulldozer(int speed, double weight, Color mainColor) { Speed = speed; Weight = weight; - BulldozerColor = bulldozerColor; - CabinColor = cabinColor; - CovshColor = covshColor; - HasMoldboardfront = hasMoldboardfront; - HasRipper = hasRipper; + MainColor = mainColor; } } } diff --git a/Bulldozer/Bulldozer/EntityFastBulldozer.cs b/Bulldozer/Bulldozer/EntityFastBulldozer.cs new file mode 100644 index 0000000..ae7499f --- /dev/null +++ b/Bulldozer/Bulldozer/EntityFastBulldozer.cs @@ -0,0 +1,22 @@ +using Bulldozer.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.Entities +{ + public class EntityFastBulldozer : EntityBulldozer + { + public Color OptionalColor { get; private set; } + public bool Covsh { get; private set; } + public bool Rearbucket { get; private set; } + public EntityFastBulldozer(int speed, double weight, Color mainColor, Color optionalColor, bool covsh, bool rearbucket) : base(speed, weight, mainColor) + { + OptionalColor = optionalColor; + Covsh = covsh; + Rearbucket = rearbucket; + } + } +} diff --git a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs index 4a14d64..8a836ec 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs @@ -1,6 +1,6 @@ namespace Bulldozer { - partial class FormBulldozer + partial class FastBulldozer { /// /// Required designer variable. @@ -28,53 +28,55 @@ /// private void InitializeComponent() { - pictureBoxBulldozer = new PictureBox(); - ButtonCreateBulldozer = new Button(); - buttonLeft = new Button(); + pictureBoxFastBulldozer = new PictureBox(); + buttonCreateBulldozer = new Button(); + buttonCreateFastBulldozer = new Button(); buttonRight = new Button(); - buttonUp = new Button(); buttonDown = new Button(); - ((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).BeginInit(); + buttonLeft = new Button(); + buttonUp = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxFastBulldozer).BeginInit(); SuspendLayout(); // - // pictureBoxBulldozer + // pictureBoxFastBulldozer // - pictureBoxBulldozer.Dock = DockStyle.Fill; - pictureBoxBulldozer.Location = new Point(0, 0); - pictureBoxBulldozer.Name = "pictureBoxBulldozer"; - pictureBoxBulldozer.Size = new Size(800, 450); - pictureBoxBulldozer.SizeMode = PictureBoxSizeMode.AutoSize; - pictureBoxBulldozer.TabIndex = 0; - pictureBoxBulldozer.TabStop = false; + pictureBoxFastBulldozer.Dock = DockStyle.Fill; + pictureBoxFastBulldozer.Location = new Point(0, 0); + pictureBoxFastBulldozer.Name = "pictureBoxFastBulldozer"; + pictureBoxFastBulldozer.Size = new Size(884, 461); + pictureBoxFastBulldozer.SizeMode = PictureBoxSizeMode.AutoSize; + pictureBoxFastBulldozer.TabIndex = 0; + pictureBoxFastBulldozer.TabStop = false; // - // ButtonCreateBulldozer + // buttonCreateBulldozer // - ButtonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - ButtonCreateBulldozer.Location = new Point(54, 396); - ButtonCreateBulldozer.Name = "ButtonCreateBulldozer"; - ButtonCreateBulldozer.Size = new Size(75, 23); - ButtonCreateBulldozer.TabIndex = 1; - ButtonCreateBulldozer.Text = "Создать"; - ButtonCreateBulldozer.UseVisualStyleBackColor = true; - ButtonCreateBulldozer.Click += ButtonCreateBulldozer_Click; + buttonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBulldozer.Location = new Point(12, 426); + buttonCreateBulldozer.Name = "buttonCreateBulldozer"; + buttonCreateBulldozer.Size = new Size(119, 23); + buttonCreateBulldozer.TabIndex = 1; + buttonCreateBulldozer.Text = "Создать Трактор"; + buttonCreateBulldozer.UseVisualStyleBackColor = true; + buttonCreateBulldozer.Click += ButtonCreateBulldozer_Click; // - // buttonLeft + // buttonCreateFastBulldozer // - buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(660, 373); - buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); - buttonLeft.TabIndex = 2; - buttonLeft.Text = "<"; - buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += ButtonMove_Click; + buttonCreateFastBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateFastBulldozer.Location = new Point(137, 426); + buttonCreateFastBulldozer.Name = "buttonCreateFastBulldozer"; + buttonCreateFastBulldozer.Size = new Size(162, 23); + buttonCreateFastBulldozer.TabIndex = 2; + buttonCreateFastBulldozer.Text = "Создать быстрый трактор"; + buttonCreateFastBulldozer.UseVisualStyleBackColor = true; + buttonCreateFastBulldozer.Click += ButtonCreateFastBulldozer_Click; // // buttonRight // buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(732, 373); + buttonRight.Location = new Point(842, 419); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 3; @@ -82,55 +84,95 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // - // buttonUp - // - buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(696, 342); - buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); - buttonUp.TabIndex = 4; - buttonUp.Text = "^"; - buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += ButtonMove_Click; - // // buttonDown // buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(696, 403); + buttonDown.Location = new Point(806, 419); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(30, 30); - buttonDown.TabIndex = 5; + buttonDown.TabIndex = 4; buttonDown.Text = "v"; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // - // FormBulldozer + // buttonLeft + // + buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; + buttonLeft.Location = new Point(770, 419); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new Size(30, 30); + buttonLeft.TabIndex = 5; + buttonLeft.Text = "<"; + buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += ButtonMove_Click; + // + // buttonUp + // + buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonUp.BackgroundImageLayout = ImageLayout.Zoom; + buttonUp.Location = new Point(806, 383); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(30, 30); + buttonUp.TabIndex = 6; + buttonUp.Text = "^"; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMove_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "Move to center", "Move to border" }); + comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += Buttonstep_Click; + // + // FastBulldozer // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); - Controls.Add(buttonDown); + ClientSize = new Size(884, 461); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonUp); - Controls.Add(buttonRight); Controls.Add(buttonLeft); - Controls.Add(ButtonCreateBulldozer); - Controls.Add(pictureBoxBulldozer); - Name = "FormBulldozer"; - Text = "Бульдозер"; - ((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).EndInit(); + Controls.Add(buttonDown); + Controls.Add(buttonRight); + Controls.Add(buttonCreateFastBulldozer); + Controls.Add(buttonCreateBulldozer); + Controls.Add(pictureBoxFastBulldozer); + Name = "FastBulldozer"; + StartPosition = FormStartPosition.CenterScreen; + Text = "FastBulldozer"; + ((System.ComponentModel.ISupportInitialize)pictureBoxFastBulldozer).EndInit(); ResumeLayout(false); PerformLayout(); } #endregion - private PictureBox pictureBoxBulldozer; - private Button ButtonCreateBulldozer; - private Button buttonLeft; + private PictureBox pictureBoxFastBulldozer; + private Button buttonCreateBulldozer; + private Button buttonCreateFastBulldozer; private Button buttonRight; - private Button buttonUp; private Button buttonDown; + private Button buttonLeft; + private Button buttonUp; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozer.cs b/Bulldozer/Bulldozer/FormBulldozer.cs index 4e1870e..796d4ad 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.cs @@ -1,11 +1,13 @@ -using System.Windows.Forms; +using Bulldozer.DrawningObjects; +using Bulldozer.MovementStrategy; namespace Bulldozer { - public partial class FormBulldozer : Form + public partial class FastBulldozer : Form { private DrawningBulldozer? _drawningBulldozer; - public FormBulldozer() + private AbstractStrategy? _abstractStrategy; + public FastBulldozer() { InitializeComponent(); } @@ -15,11 +17,37 @@ namespace Bulldozer { return; } - Bitmap bmp = new(pictureBoxBulldozer.Width, - pictureBoxBulldozer.Height); + Bitmap bmp = new(pictureBoxFastBulldozer.Width, + pictureBoxFastBulldozer.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningBulldozer.DrawTransport(gr); - pictureBoxBulldozer.Image = bmp; + _drawningBulldozer.DrawTrasport(gr); + pictureBoxFastBulldozer.Image = bmp; + } + private void ButtonCreateFastBulldozer_Click(object sender, EventArgs e) + { + Random random = new Random(); + _drawningBulldozer = new DrawningFastBulldozer( + 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)), + Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2)), + pictureBoxFastBulldozer.Width, + pictureBoxFastBulldozer.Height); + _drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void ButtonCreateBulldozer_Click(object sender, EventArgs e) + { + Random random = new Random(); + _drawningBulldozer = new DrawningBulldozer( + random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + pictureBoxFastBulldozer.Width, + pictureBoxFastBulldozer.Height); + _drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); } private void ButtonMove_Click(object sender, EventArgs e) { @@ -45,19 +73,42 @@ namespace Bulldozer } Draw(); } - - private void ButtonCreateBulldozer_Click(object sender, EventArgs e) + private void Buttonstep_Click(object sender, EventArgs e) { - Random random = new(); - _drawningBulldozer = new DrawningBulldozer(); - _drawningBulldozer.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)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); - _drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); + if (_drawningBulldozer == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData( + new DrawningObjectBulldozer(_drawningBulldozer), + pictureBoxFastBulldozer.Width, + pictureBoxFastBulldozer.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } } } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/IMoveableObject.cs b/Bulldozer/Bulldozer/IMoveableObject.cs new file mode 100644 index 0000000..a5fbcff --- /dev/null +++ b/Bulldozer/Bulldozer/IMoveableObject.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Bulldozer.DrawningObjects; + +namespace Bulldozer.MovementStrategy +{ + public interface IMoveableObject + { + /// + /// получение координаты + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// шаг + /// + int GetStep { get; } + /// + /// проверка можно ли инди в этом направлении + /// + /// + /// + bool CheckCanMove(DirectionTypeBulldozer direction); + /// + /// изменение напрвления перемещения + /// + /// + void MoveObject(DirectionTypeBulldozer direction); + } +} diff --git a/Bulldozer/Bulldozer/MoveToBorder.cs b/Bulldozer/Bulldozer/MoveToBorder.cs new file mode 100644 index 0000000..7315b6d --- /dev/null +++ b/Bulldozer/Bulldozer/MoveToBorder.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.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(); + } + } + } +} diff --git a/Bulldozer/Bulldozer/MoveToCenter.cs b/Bulldozer/Bulldozer/MoveToCenter.cs new file mode 100644 index 0000000..783c718 --- /dev/null +++ b/Bulldozer/Bulldozer/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParametrs; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + 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(); + } + } + } + } +} diff --git a/Bulldozer/Bulldozer/ObjectParameters.cs b/Bulldozer/Bulldozer/ObjectParameters.cs new file mode 100644 index 0000000..b495ded --- /dev/null +++ b/Bulldozer/Bulldozer/ObjectParameters.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.MovementStrategy +{ + 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 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 ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/Bulldozer/Bulldozer/Program.cs b/Bulldozer/Bulldozer/Program.cs index 74d0a17..b7362fc 100644 --- a/Bulldozer/Bulldozer/Program.cs +++ b/Bulldozer/Bulldozer/Program.cs @@ -11,7 +11,7 @@ namespace Bulldozer // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormBulldozer()); + Application.Run(new FastBulldozer()); } } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/Status.cs b/Bulldozer/Bulldozer/Status.cs new file mode 100644 index 0000000..0d4bde0 --- /dev/null +++ b/Bulldozer/Bulldozer/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Bulldozer.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}