From 20300279e7c967976e9635d57176267eab621d61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sun, 8 Oct 2023 18:48:39 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D1=8B=20=D0=BF=D0=BE=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/AbstractStrategy.cs | 132 +++++++++++++++++++++ Sailboat/Sailboat/DrawingBoat.cs | 124 +++++++++++++++++++ Sailboat/Sailboat/DrawingSailboat.cs | 113 ++++-------------- Sailboat/Sailboat/EntityBoat.cs | 22 ++++ Sailboat/Sailboat/EntitySailboat.cs | 45 +------ Sailboat/Sailboat/FormSailboat.Designer.cs | 1 + Sailboat/Sailboat/FormSailboat.cs | 11 +- Sailboat/Sailboat/IMoveableObject.cs | 33 ++++++ Sailboat/Sailboat/ObjectParameters.cs | 55 +++++++++ Sailboat/Sailboat/Status.cs | 18 +++ 10 files changed, 418 insertions(+), 136 deletions(-) create mode 100644 Sailboat/Sailboat/AbstractStrategy.cs create mode 100644 Sailboat/Sailboat/DrawingBoat.cs create mode 100644 Sailboat/Sailboat/EntityBoat.cs create mode 100644 Sailboat/Sailboat/IMoveableObject.cs create mode 100644 Sailboat/Sailboat/ObjectParameters.cs create mode 100644 Sailboat/Sailboat/Status.cs diff --git a/Sailboat/Sailboat/AbstractStrategy.cs b/Sailboat/Sailboat/AbstractStrategy.cs new file mode 100644 index 0000000..fc58dd3 --- /dev/null +++ b/Sailboat/Sailboat/AbstractStrategy.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +namespace Sailboat.MovementStrategy +{ + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + 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; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs new file mode 100644 index 0000000..d278e68 --- /dev/null +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.Entities; + +namespace Sailboat.DrawingObjects +{ + public class DrawingBoat + { + public EntityBoat? EntityBoat { get; protected set; } + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + private readonly int _boatWidth = 160; + private readonly int _boatHeight = 160; + public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < _boatWidth || height < _boatHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityBoat = new EntityBoat(speed, weight, bodyColor); + } + + protected DrawingBoat(int speed, double weight, Color bodyColor, int width, int height, int boatWidth, int boatHeight) + { + if (width < _boatWidth || height < _boatHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _boatWidth = boatWidth; + _boatHeight = boatHeight; + EntityBoat = new EntityBoat(speed, weight, bodyColor); + } + public void SetPosition(int x, int y) + { + if (x < 0 || x + _boatWidth > _pictureWidth) + { + x = 0; + } + if (y < 0 || y + _boatHeight > _pictureHeight) + { + y = 0; + } + _startPosX = x; + _startPosY = y; + } + public void MoveTransport(DirectionType direction) + { + if (EntityBoat == null) + { + return; + } + switch (direction) + { + case DirectionType.Left: + if (_startPosX - EntityBoat.Step > 0) + { + _startPosX -= (int)EntityBoat.Step; + } + break; + case DirectionType.Up: + if (_startPosY - EntityBoat.Step > 0) + { + _startPosY -= (int)EntityBoat.Step; + } + break; + case DirectionType.Right: + if (_startPosX + EntityBoat.Step + _boatWidth < _pictureWidth) + { + _startPosX += (int)EntityBoat.Step; + } + break; + case DirectionType.Down: + if (_startPosY + EntityBoat.Step + _boatHeight < _pictureHeight) + { + _startPosY += (int)EntityBoat.Step; + } + break; + } + } + public virtual void DrawTransport(Graphics g) + { + if (EntityBoat == null) + { + return; + } + Pen pen = new(Color.Black, 4); + Brush additionalBrush = new + SolidBrush(EntityBoat.BodyColor); + + //основной корпус парусника + Brush Brush = new + SolidBrush(EntityBoat.BodyColor); + + Point[] hull = new Point[] + { + new Point(_startPosX + 10, _startPosY + 90), + new Point(_startPosX + 110, _startPosY + 90), + new Point(_startPosX + 140, _startPosY + 120), + new Point(_startPosX + 110, _startPosY + 150), + new Point(_startPosX + 10, _startPosY + 150) + }; + g.FillPolygon(Brush, hull); + g.DrawPolygon(pen, hull); + + Brush addBrush = new + SolidBrush(Color.Green); + + g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 90, 40); + g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40); + + } + } + } + diff --git a/Sailboat/Sailboat/DrawingSailboat.cs b/Sailboat/Sailboat/DrawingSailboat.cs index 97f82b7..e715b3c 100644 --- a/Sailboat/Sailboat/DrawingSailboat.cs +++ b/Sailboat/Sailboat/DrawingSailboat.cs @@ -1,91 +1,38 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Sailboat +using Sailboat.Entities; + +namespace Sailboat.DrawingObjects { - public class DrawingSailboat + public class DrawingSailboat : DrawingBoat { - public EntitySailboat? EntitySailboat { get; private set; } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private readonly int _boatWidth = 160; - private readonly int _boatHeight = 160; - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height) + public DrawingSailboat(int speed, double weight, Color bodyColor, Color +additionalColor, bool hull, bool sail, int width, int height) : + base(speed, weight, bodyColor, width, height, 160, 160) { - if (width < _boatWidth || height < _boatHeight) + if (EntityBoat != null) { - return false; + EntityBoat = new EntitySailboat(speed, weight, bodyColor, + additionalColor, hull, sail); } - _pictureWidth = width; - _pictureHeight = height; - EntitySailboat = new EntitySailboat(); - EntitySailboat.Init(speed, weight, bodyColor, additionalColor, hull, sail); - return true; } - public void SetPosition(int x, int y) + public override void DrawTransport(Graphics g) { - if (x < 0 || x + _boatWidth > _pictureWidth) - { - x = 0; - } - if (y < 0 || y + _boatHeight > _pictureHeight) - { - y = 0; - } - _startPosX = x; - _startPosY = y; - } - public void MoveTransport(DirectionType direction) - { - if (EntitySailboat == null) + if (EntityBoat is not EntitySailboat sailboat) { return; } - switch (direction) - { - case DirectionType.Left: - if (_startPosX - EntitySailboat.Step > 0) - { - _startPosX -= (int)EntitySailboat.Step; - } - break; - case DirectionType.Up: - if (_startPosY - EntitySailboat.Step > 0) - { - _startPosY -= (int)EntitySailboat.Step; - } - break; - case DirectionType.Right: - if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth) - { - _startPosX += (int)EntitySailboat.Step; - } - break; - case DirectionType.Down: - if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight) - { - _startPosY += (int)EntitySailboat.Step; - } - break; - } - } - public void DrawTransport(Graphics g) - { - if (EntitySailboat == null) - { - return; - } - Pen pen = new(Color.Black, 4); + Pen pen = new(Color.Black); Brush additionalBrush = new - SolidBrush(EntitySailboat.AdditionalColor); - + SolidBrush(sailboat.AdditionalColor); + //усиленный корпус парусника - if (EntitySailboat.Hull) + if (sailboat.Hull) { Point[] hullCooler = new Point[] { @@ -98,34 +45,14 @@ namespace Sailboat g.FillPolygon(additionalBrush, hullCooler); g.DrawPolygon(pen, hullCooler); } - - //основной корпус парусника - Brush Brush = new - SolidBrush(EntitySailboat.BodyColor); - - Point[] hull = new Point[] - { - new Point(_startPosX + 10, _startPosY + 90), - new Point(_startPosX + 110, _startPosY + 90), - new Point(_startPosX + 140, _startPosY + 120), - new Point(_startPosX + 110, _startPosY + 150), - new Point(_startPosX + 10, _startPosY + 150) - }; - g.FillPolygon(Brush, hull); - g.DrawPolygon(pen, hull); - - Brush addBrush = new - SolidBrush(Color.Green); - - g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 90, 40); - g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40); + base.DrawTransport(g); //парус - if (EntitySailboat.Sail) + if (sailboat.Sail) { Brush sailBrush = new - SolidBrush(EntitySailboat.AdditionalColor); + SolidBrush(sailboat.AdditionalColor); Point[] sail = new Point[] { diff --git a/Sailboat/Sailboat/EntityBoat.cs b/Sailboat/Sailboat/EntityBoat.cs new file mode 100644 index 0000000..6f1da8a --- /dev/null +++ b/Sailboat/Sailboat/EntityBoat.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Entities +{ + public class EntityBoat + { + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color BodyColor { get; private set; } + public double Step => (double)Speed * 100 / Weight; + public EntityBoat(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/Sailboat/Sailboat/EntitySailboat.cs b/Sailboat/Sailboat/EntitySailboat.cs index 8aa513f..aff4b68 100644 --- a/Sailboat/Sailboat/EntitySailboat.cs +++ b/Sailboat/Sailboat/EntitySailboat.cs @@ -4,53 +4,16 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Sailboat +namespace Sailboat.Entities { - public class EntitySailboat + public class EntitySailboat : EntityBoat { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// public Color AdditionalColor { get; private set; } - /// - /// Признак (опция) наличия усиленного корпуса - /// public bool Hull { get; private set; } - /// - /// Признак (опция) наличия паруса - /// public bool Sail { get; private set; } - /// - /// Шаг перемещения автомобиля - /// - public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса спортивного автомобиля - /// - /// Скорость - /// Вес автомобиля - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия усиленного корпуса - /// Признак наличия паруса - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool hull, bool sail) + public EntitySailboat(int speed, double weight, Color bodyColor, Color + additionalColor, bool hull, bool sail) : base (speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Hull = hull; Sail = sail; diff --git a/Sailboat/Sailboat/FormSailboat.Designer.cs b/Sailboat/Sailboat/FormSailboat.Designer.cs index 4226e22..84dd3de 100644 --- a/Sailboat/Sailboat/FormSailboat.Designer.cs +++ b/Sailboat/Sailboat/FormSailboat.Designer.cs @@ -47,6 +47,7 @@ this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxSailboat.TabIndex = 0; this.pictureBoxSailboat.TabStop = false; + this.pictureBoxSailboat.Click += new System.EventHandler(this.pictureBoxSailboat_Click); // // buttonCreate // diff --git a/Sailboat/Sailboat/FormSailboat.cs b/Sailboat/Sailboat/FormSailboat.cs index 66139d6..b700c1f 100644 --- a/Sailboat/Sailboat/FormSailboat.cs +++ b/Sailboat/Sailboat/FormSailboat.cs @@ -1,3 +1,6 @@ +using Sailboat.Entities; +using Sailboat.DrawingObjects; + namespace Sailboat { public partial class FormSailboat : Form @@ -22,8 +25,7 @@ namespace Sailboat private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawingSailboat = new DrawingSailboat(); - _drawingSailboat.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)), + _drawingSailboat = new DrawingSailboat(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)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); _drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -54,5 +56,10 @@ namespace Sailboat Draw(); } + + private void pictureBoxSailboat_Click(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/Sailboat/Sailboat/IMoveableObject.cs b/Sailboat/Sailboat/IMoveableObject.cs new file mode 100644 index 0000000..6990729 --- /dev/null +++ b/Sailboat/Sailboat/IMoveableObject.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; + +namespace Sailboat.MovementStrategy +{ + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/Sailboat/Sailboat/ObjectParameters.cs b/Sailboat/Sailboat/ObjectParameters.cs new file mode 100644 index 0000000..7f80284 --- /dev/null +++ b/Sailboat/Sailboat/ObjectParameters.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + + } +} diff --git a/Sailboat/Sailboat/Status.cs b/Sailboat/Sailboat/Status.cs new file mode 100644 index 0000000..3d1bc4d --- /dev/null +++ b/Sailboat/Sailboat/Status.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} -- 2.25.1 From e68ab7b607ff74ce8e215fd4ff0ac2c3501e656a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Tue, 10 Oct 2023 17:27:58 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingBoat.cs | 27 ++++++++- Sailboat/Sailboat/DrawingObjectBoat.cs | 38 ++++++++++++ Sailboat/Sailboat/FormSailboat.Designer.cs | 68 +++++++++++++++++---- Sailboat/Sailboat/FormSailboat.cs | 69 +++++++++++++++++----- Sailboat/Sailboat/MoveToBorder.cs | 57 ++++++++++++++++++ Sailboat/Sailboat/MoveToCenter.cs | 56 ++++++++++++++++++ 6 files changed, 286 insertions(+), 29 deletions(-) create mode 100644 Sailboat/Sailboat/DrawingObjectBoat.cs create mode 100644 Sailboat/Sailboat/MoveToBorder.cs create mode 100644 Sailboat/Sailboat/MoveToCenter.cs diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs index d278e68..882d8f4 100644 --- a/Sailboat/Sailboat/DrawingBoat.cs +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -17,6 +17,10 @@ namespace Sailboat.DrawingObjects protected int _startPosY; private readonly int _boatWidth = 160; private readonly int _boatHeight = 160; + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _boatWidth; + public int GetHeight => _boatHeight; public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height) { if (width < _boatWidth || height < _boatHeight) @@ -53,9 +57,28 @@ namespace Sailboat.DrawingObjects _startPosX = x; _startPosY = y; } - public void MoveTransport(DirectionType direction) + public bool CanMove(DirectionType direction) { if (EntityBoat == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityBoat.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityBoat.Step > 0, + // вправо + DirectionType.Right => _startPosX + EntityBoat.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + EntityBoat.Step < _pictureHeight, + _ => false + }; + } + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityBoat == null) { return; } @@ -93,7 +116,7 @@ namespace Sailboat.DrawingObjects { return; } - Pen pen = new(Color.Black, 4); + Pen pen = new(Color.Black); Brush additionalBrush = new SolidBrush(EntityBoat.BodyColor); diff --git a/Sailboat/Sailboat/DrawingObjectBoat.cs b/Sailboat/Sailboat/DrawingObjectBoat.cs new file mode 100644 index 0000000..21e0011 --- /dev/null +++ b/Sailboat/Sailboat/DrawingObjectBoat.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; + +namespace Sailboat.MovementStrategy +{ + public class DrawingObjectBoat : IMoveableObject + { + private readonly DrawingBoat? _drawingBoat = null; + public DrawingObjectBoat(DrawingBoat drawingBoat) + { + _drawingBoat = drawingBoat; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawingBoat == null || _drawingBoat.EntityBoat == + null) + { + return null; + } + return new ObjectParameters(_drawingBoat.GetPosX, + _drawingBoat.GetPosY, _drawingBoat.GetWidth, _drawingBoat.GetHeight); + } + } + public int GetStep => (int)(_drawingBoat?.EntityBoat?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawingBoat?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawingBoat?.MoveTransport(direction); + + } +} diff --git a/Sailboat/Sailboat/FormSailboat.Designer.cs b/Sailboat/Sailboat/FormSailboat.Designer.cs index 84dd3de..7c24302 100644 --- a/Sailboat/Sailboat/FormSailboat.Designer.cs +++ b/Sailboat/Sailboat/FormSailboat.Designer.cs @@ -30,11 +30,14 @@ { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSailboat)); this.pictureBoxSailboat = new System.Windows.Forms.PictureBox(); - this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonCreateBoat = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); + this.buttonCreateSailboat = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.buttonStep = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxSailboat)).BeginInit(); this.SuspendLayout(); // @@ -47,18 +50,17 @@ this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxSailboat.TabIndex = 0; this.pictureBoxSailboat.TabStop = false; - this.pictureBoxSailboat.Click += new System.EventHandler(this.pictureBoxSailboat_Click); // - // buttonCreate + // buttonCreateBoat // - this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreate.Location = new System.Drawing.Point(12, 395); - this.buttonCreate.Name = "buttonCreate"; - this.buttonCreate.Size = new System.Drawing.Size(127, 46); - this.buttonCreate.TabIndex = 1; - this.buttonCreate.Text = "Создать"; - this.buttonCreate.UseVisualStyleBackColor = true; - this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + this.buttonCreateBoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateBoat.Location = new System.Drawing.Point(12, 385); + this.buttonCreateBoat.Name = "buttonCreateBoat"; + this.buttonCreateBoat.Size = new System.Drawing.Size(127, 56); + this.buttonCreateBoat.TabIndex = 1; + this.buttonCreateBoat.Text = "Создать лодку"; + this.buttonCreateBoat.UseVisualStyleBackColor = true; + this.buttonCreateBoat.Click += new System.EventHandler(this.buttonCreateBoat_Click); // // buttonLeft // @@ -108,16 +110,53 @@ this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); // + // buttonCreateSailboat + // + this.buttonCreateSailboat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateSailboat.Location = new System.Drawing.Point(175, 385); + this.buttonCreateSailboat.Name = "buttonCreateSailboat"; + this.buttonCreateSailboat.Size = new System.Drawing.Size(127, 56); + this.buttonCreateSailboat.TabIndex = 6; + this.buttonCreateSailboat.Text = "Создать парусник"; + this.buttonCreateSailboat.UseVisualStyleBackColor = true; + this.buttonCreateSailboat.Click += new System.EventHandler(this.buttonCreateSailboat_Click); + // + // comboBoxStrategy + // + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "До центра", + "До края"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(688, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28); + this.comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonStep.Location = new System.Drawing.Point(688, 46); + this.buttonStep.Name = "buttonStep"; + this.buttonStep.Size = new System.Drawing.Size(149, 32); + this.buttonStep.TabIndex = 8; + this.buttonStep.Text = "Шаг"; + this.buttonStep.UseVisualStyleBackColor = true; + this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); + // // FormSailboat // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(882, 453); + this.Controls.Add(this.buttonStep); + this.Controls.Add(this.comboBoxStrategy); + this.Controls.Add(this.buttonCreateSailboat); this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.buttonCreateBoat); this.Controls.Add(this.pictureBoxSailboat); this.Name = "FormSailboat"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; @@ -131,10 +170,13 @@ #endregion private PictureBox pictureBoxSailboat; - private Button buttonCreate; + private Button buttonCreateBoat; private Button buttonLeft; private Button buttonUp; private Button buttonRight; private Button buttonDown; + private Button buttonCreateSailboat; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormSailboat.cs b/Sailboat/Sailboat/FormSailboat.cs index b700c1f..d80f60f 100644 --- a/Sailboat/Sailboat/FormSailboat.cs +++ b/Sailboat/Sailboat/FormSailboat.cs @@ -1,39 +1,49 @@ using Sailboat.Entities; using Sailboat.DrawingObjects; +using Sailboat.MovementStrategy; namespace Sailboat { public partial class FormSailboat : Form { - private DrawingSailboat? _drawingSailboat; + private DrawingBoat? _drawingBoat; + private AbstractStrategy? _abstractStrategy; public FormSailboat() { InitializeComponent(); } private void Draw() { - if (_drawingSailboat == null) + if (_drawingBoat == null) { return; } Bitmap bmp = new(pictureBoxSailboat.Width, pictureBoxSailboat.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingSailboat.DrawTransport(gr); + _drawingBoat.DrawTransport(gr); pictureBoxSailboat.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateBoat_Click(object sender, EventArgs e) { Random random = new(); - _drawingSailboat = new DrawingSailboat(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)), + _drawingBoat = new DrawingBoat(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + private void buttonCreateSailboat_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingBoat = new DrawingSailboat(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)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); - _drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) { - if (_drawingSailboat == null) + if (_drawingBoat == null) { return; } @@ -41,25 +51,56 @@ namespace Sailboat switch (name) { case "buttonUp": - _drawingSailboat.MoveTransport(DirectionType.Up); + _drawingBoat.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawingSailboat.MoveTransport(DirectionType.Down); + _drawingBoat.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawingSailboat.MoveTransport(DirectionType.Left); + _drawingBoat.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawingSailboat.MoveTransport(DirectionType.Right); + _drawingBoat.MoveTransport(DirectionType.Right); break; } Draw(); - } - private void pictureBoxSailboat_Click(object sender, EventArgs e) + private void buttonStep_Click(object sender, EventArgs e) { - + if (_drawingBoat == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawingObjectBoat(_drawingBoat), pictureBoxSailboat.Width, + pictureBoxSailboat.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/Sailboat/Sailboat/MoveToBorder.cs b/Sailboat/Sailboat/MoveToBorder.cs new file mode 100644 index 0000000..9e83e4a --- /dev/null +++ b/Sailboat/Sailboat/MoveToBorder.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + 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 = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/Sailboat/Sailboat/MoveToCenter.cs b/Sailboat/Sailboat/MoveToCenter.cs new file mode 100644 index 0000000..a3392ee --- /dev/null +++ b/Sailboat/Sailboat/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + 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 = GetObjectParameters; + 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(); + } + } + } + } +} -- 2.25.1 From 1d208a3dbe9a36ba56dcaa422a1311cd73265430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sat, 21 Oct 2023 22:04:26 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingObjectBoat.cs | 3 +-- Sailboat/Sailboat/DrawingSailboat.cs | 3 +-- Sailboat/Sailboat/FormSailboat.cs | 2 -- Sailboat/Sailboat/ObjectParameters.cs | 1 - 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/Sailboat/Sailboat/DrawingObjectBoat.cs b/Sailboat/Sailboat/DrawingObjectBoat.cs index 21e0011..6c7f211 100644 --- a/Sailboat/Sailboat/DrawingObjectBoat.cs +++ b/Sailboat/Sailboat/DrawingObjectBoat.cs @@ -24,8 +24,7 @@ namespace Sailboat.MovementStrategy { return null; } - return new ObjectParameters(_drawingBoat.GetPosX, - _drawingBoat.GetPosY, _drawingBoat.GetWidth, _drawingBoat.GetHeight); + return new ObjectParameters(_drawingBoat.GetPosX, _drawingBoat.GetPosY, _drawingBoat.GetWidth, _drawingBoat.GetHeight); } } public int GetStep => (int)(_drawingBoat?.EntityBoat?.Step ?? 0); diff --git a/Sailboat/Sailboat/DrawingSailboat.cs b/Sailboat/Sailboat/DrawingSailboat.cs index e715b3c..130b9cc 100644 --- a/Sailboat/Sailboat/DrawingSailboat.cs +++ b/Sailboat/Sailboat/DrawingSailboat.cs @@ -11,8 +11,7 @@ namespace Sailboat.DrawingObjects { public class DrawingSailboat : DrawingBoat { - public DrawingSailboat(int speed, double weight, Color bodyColor, Color -additionalColor, bool hull, bool sail, int width, int height) : + public DrawingSailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height) : base(speed, weight, bodyColor, width, height, 160, 160) { if (EntityBoat != null) diff --git a/Sailboat/Sailboat/FormSailboat.cs b/Sailboat/Sailboat/FormSailboat.cs index d80f60f..e6d4d05 100644 --- a/Sailboat/Sailboat/FormSailboat.cs +++ b/Sailboat/Sailboat/FormSailboat.cs @@ -65,7 +65,6 @@ namespace Sailboat } Draw(); } - private void buttonStep_Click(object sender, EventArgs e) { if (_drawingBoat == null) @@ -101,6 +100,5 @@ namespace Sailboat _abstractStrategy = null; } } - } } \ No newline at end of file diff --git a/Sailboat/Sailboat/ObjectParameters.cs b/Sailboat/Sailboat/ObjectParameters.cs index 7f80284..f3a5888 100644 --- a/Sailboat/Sailboat/ObjectParameters.cs +++ b/Sailboat/Sailboat/ObjectParameters.cs @@ -50,6 +50,5 @@ namespace Sailboat.MovementStrategy _width = width; _height = height; } - } } -- 2.25.1 From 5ed6618142a10e923ee6d122502470d7c8bca653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 25 Oct 2023 09:48:24 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D0=B3=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingBoat.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs index 882d8f4..dc93ed9 100644 --- a/Sailboat/Sailboat/DrawingBoat.cs +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -117,8 +117,6 @@ namespace Sailboat.DrawingObjects return; } Pen pen = new(Color.Black); - Brush additionalBrush = new - SolidBrush(EntityBoat.BodyColor); //основной корпус парусника Brush Brush = new @@ -140,7 +138,6 @@ namespace Sailboat.DrawingObjects g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 90, 40); g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40); - } } } -- 2.25.1