diff --git a/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.metadata.v5.1 b/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.metadata.v5.1 index 07c9367..629be43 100644 Binary files a/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.metadata.v5.1 and b/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.metadata.v5.1 differ diff --git a/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.projects.v5.1 b/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.projects.v5.1 index 7175ac0..c863212 100644 Binary files a/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.projects.v5.1 and b/ProjectMotorShip/.vs/ProjectEvaluation/projectmotorship.projects.v5.1 differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/DesignTimeBuild/.dtbcache.v2 b/ProjectMotorShip/.vs/ProjectMotorShip/DesignTimeBuild/.dtbcache.v2 index 9b8e41b..441196e 100644 Binary files a/ProjectMotorShip/.vs/ProjectMotorShip/DesignTimeBuild/.dtbcache.v2 and b/ProjectMotorShip/.vs/ProjectMotorShip/DesignTimeBuild/.dtbcache.v2 differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/048680c1-deb0-4289-9d63-994d9cfd4972.vsidx b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/048680c1-deb0-4289-9d63-994d9cfd4972.vsidx deleted file mode 100644 index 49ec159..0000000 Binary files a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/048680c1-deb0-4289-9d63-994d9cfd4972.vsidx and /dev/null differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/1d0a5900-fea2-4656-8583-0cb9de495eab.vsidx b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/1d0a5900-fea2-4656-8583-0cb9de495eab.vsidx deleted file mode 100644 index fc924ee..0000000 Binary files a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/1d0a5900-fea2-4656-8583-0cb9de495eab.vsidx and /dev/null differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/4acb6d64-cc2b-4a61-b073-2b6ea35bb85b.vsidx b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/4acb6d64-cc2b-4a61-b073-2b6ea35bb85b.vsidx deleted file mode 100644 index aa1ec7f..0000000 Binary files a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/4acb6d64-cc2b-4a61-b073-2b6ea35bb85b.vsidx and /dev/null differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/6040c552-c871-4935-ae4b-f49d55440834.vsidx b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/6040c552-c871-4935-ae4b-f49d55440834.vsidx new file mode 100644 index 0000000..ae8af39 Binary files /dev/null and b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/6040c552-c871-4935-ae4b-f49d55440834.vsidx differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/65f0ad14-34a4-42e2-a008-4dbb4b0d0de3.vsidx b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/65f0ad14-34a4-42e2-a008-4dbb4b0d0de3.vsidx new file mode 100644 index 0000000..cf4e24f Binary files /dev/null and b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/65f0ad14-34a4-42e2-a008-4dbb4b0d0de3.vsidx differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/d55440e2-3113-417c-ba0f-2f7e588acc21.vsidx b/ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/bfbbdf96-5175-4149-8ea5-6eac8bc450c2.vsidx similarity index 100% rename from ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/d55440e2-3113-417c-ba0f-2f7e588acc21.vsidx rename to ProjectMotorShip/.vs/ProjectMotorShip/FileContentIndex/bfbbdf96-5175-4149-8ea5-6eac8bc450c2.vsidx diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/v17/.futdcache.v2 b/ProjectMotorShip/.vs/ProjectMotorShip/v17/.futdcache.v2 index 7e24314..fa7746e 100644 Binary files a/ProjectMotorShip/.vs/ProjectMotorShip/v17/.futdcache.v2 and b/ProjectMotorShip/.vs/ProjectMotorShip/v17/.futdcache.v2 differ diff --git a/ProjectMotorShip/.vs/ProjectMotorShip/v17/.suo b/ProjectMotorShip/.vs/ProjectMotorShip/v17/.suo index 92208a4..7582b12 100644 Binary files a/ProjectMotorShip/.vs/ProjectMotorShip/v17/.suo and b/ProjectMotorShip/.vs/ProjectMotorShip/v17/.suo differ diff --git a/ProjectMotorShip/ProjectMotorShip/AbstractStrategy.cs b/ProjectMotorShip/ProjectMotorShip/AbstractStrategy.cs new file mode 100644 index 0000000..f3dac70 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/AbstractStrategy.cs @@ -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; + } + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/DrawningMotorShip.cs b/ProjectMotorShip/ProjectMotorShip/DrawningMotorShip.cs index eb9bc5a..51662d2 100644 --- a/ProjectMotorShip/ProjectMotorShip/DrawningMotorShip.cs +++ b/ProjectMotorShip/ProjectMotorShip/DrawningMotorShip.cs @@ -3,183 +3,50 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectMotorShip.Entities; -namespace ProjectMotorShip +namespace ProjectMotorShip.DrawingObjects { - public class DrawningMotorShip + public class DrawningMotorShip : DrawningShip { - /// - /// Класс-сущность - /// - public EntityMotorShip? EntityMotorShip { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки автомобиля - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки автомобиля - /// - private int _startPosY; - /// - /// Ширина прорисовки автомобиля - /// - private readonly int _MotorShipWidth = 100; - /// - /// Высота прорисовки автомобиля - /// - private readonly int _MotorShipHeight = 70; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Цвет корпуса - /// Дополнительный цвет - /// Признак наличия труб - /// Признак наличия отсека для топлива - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pipes, bool section, - int width, int height) + public DrawningMotorShip(int speed, double weight, + Color mainColor, Color optionalColor, bool pipes, + bool fuelCompartment, int width, int height) : + base(speed, weight, mainColor, width, height, 100, 60) { - 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; + } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) + + public override void DrawTrasport(Graphics g) { - /// - /// Проверка, что x и y не выходят за пределы формы - /// - if (x < 0 || x + _MotorShipWidth > _pictureWidth) - { - x = 20; - } - if (y < 0 || y + _MotorShipHeight > _pictureHeight) - { - y = 20; - } - _startPosX = x; - _startPosY = y; - } - /// - /// Изменение направления перемещения - /// - /// Направление - 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; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityMotorShip == null) + if (EntityShip is not EntityMotorShip motorShip) { + return; + } Pen pen = new(Color.Black); - Brush optionalBrush = new SolidBrush(EntityMotorShip.BodyColor); - if (EntityMotorShip.Pipes) + Brush optionalBrush = new SolidBrush(motorShip.OptionalColor); + if (motorShip.Pipes) { g.FillRectangle(optionalBrush, _startPosX + 70, _startPosY, 10, 30); g.FillRectangle(optionalBrush, _startPosX + 50, _startPosY + 10, 10, 20); g.DrawRectangle(pen, _startPosX + 50, _startPosY + 10, 10, 20); g.DrawRectangle(pen, _startPosX + 70, _startPosY, 10, 30); } - if (EntityMotorShip.Section) + if (motorShip.FuelCompartment) { g.FillRectangle(optionalBrush, _startPosX + 10, _startPosY + 30, 10, 10); g.DrawRectangle(pen, _startPosX + 10, _startPosY + 30, 10, 10); } - Brush mainBrush = new SolidBrush(EntityMotorShip.AdditionalColor); - //палуба - g.FillRectangle(mainBrush, _startPosX + 30, _startPosY + 30, 60, 10); - 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(); + _startPosY += 30; + base.DrawTrasport(g); + _startPosY -= 30; } } } diff --git a/ProjectMotorShip/ProjectMotorShip/DrawningObjectShip.cs b/ProjectMotorShip/ProjectMotorShip/DrawningObjectShip.cs new file mode 100644 index 0000000..7706a95 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/DrawningObjectShip.cs @@ -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); + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/DrawningShip.cs b/ProjectMotorShip/ProjectMotorShip/DrawningShip.cs new file mode 100644 index 0000000..58d2b4c --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/DrawningShip.cs @@ -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); + } + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/EntityMotorShip.cs b/ProjectMotorShip/ProjectMotorShip/EntityMotorShip.cs index 4e52f15..999dda3 100644 --- a/ProjectMotorShip/ProjectMotorShip/EntityMotorShip.cs +++ b/ProjectMotorShip/ProjectMotorShip/EntityMotorShip.cs @@ -5,56 +5,20 @@ using System.Text; using System.Threading.Tasks; using static System.Collections.Specialized.BitVector32; -namespace ProjectMotorShip +namespace ProjectMotorShip.Entities { - public class EntityMotorShip + public class EntityMotorShip : EntityShip { - /// - /// Скорость - /// - 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 Color OptionalColor { get; private set; } public bool Pipes { get; private set; } - /// - /// Признак (опция) наличия отсека для топлива - /// - public bool Section { get; private set; } - /// - /// Шаг перемещения теплохода - /// - public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса спортивного автомобиля - /// - /// Скорость - /// Вес теплохода - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия труб - /// Признак наличия отсека для топлива - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool pipes, bool section) + public bool FuelCompartment { get; private set; } + public EntityMotorShip(int speed, double weight, + Color mainColor, Color optionalColor, + bool pipes, bool fuelCompartment) : base(speed, weight, mainColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; + OptionalColor = optionalColor; Pipes = pipes; - Section = section; + FuelCompartment = fuelCompartment; } } } diff --git a/ProjectMotorShip/ProjectMotorShip/EntityShip.cs b/ProjectMotorShip/ProjectMotorShip/EntityShip.cs new file mode 100644 index 0000000..8b3bc56 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/EntityShip.cs @@ -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; + } + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/IMoveableObject.cs b/ProjectMotorShip/ProjectMotorShip/IMoveableObject.cs new file mode 100644 index 0000000..c7da6d5 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/IMoveableObject.cs @@ -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); + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/MotorShip.Designer.cs b/ProjectMotorShip/ProjectMotorShip/MotorShip.Designer.cs index c1333a5..70d053f 100644 --- a/ProjectMotorShip/ProjectMotorShip/MotorShip.Designer.cs +++ b/ProjectMotorShip/ProjectMotorShip/MotorShip.Designer.cs @@ -34,6 +34,9 @@ this.buttonRight = new System.Windows.Forms.Button(); this.buttonUp = 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(); this.SuspendLayout(); // @@ -50,11 +53,11 @@ // buttonCreate // 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.Size = new System.Drawing.Size(94, 29); + this.buttonCreate.Size = new System.Drawing.Size(144, 29); this.buttonCreate.TabIndex = 1; - this.buttonCreate.Text = "Создать"; + this.buttonCreate.Text = "Создать теплоход"; this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click_1); // @@ -104,10 +107,44 @@ this.buttonDown.UseVisualStyleBackColor = true; 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 // - // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); 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.buttonUp); this.Controls.Add(this.buttonRight); @@ -130,5 +167,8 @@ private Button buttonRight; private Button buttonUp; private Button buttonDown; + private ComboBox comboBoxStrategy; + private Button buttonStep; + private Button buttonCreateShip; } } \ No newline at end of file diff --git a/ProjectMotorShip/ProjectMotorShip/MotorShip.cs b/ProjectMotorShip/ProjectMotorShip/MotorShip.cs index b0e18c6..a3318dc 100644 --- a/ProjectMotorShip/ProjectMotorShip/MotorShip.cs +++ b/ProjectMotorShip/ProjectMotorShip/MotorShip.cs @@ -1,4 +1,9 @@ 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 { @@ -8,7 +13,10 @@ namespace ProjectMotorShip /// /// - /// - private DrawningMotorShip? _drawningMotorShip; + private DrawningShip? _drawningShip; + private AbstractStrategy? _abstractStrategy; + private object _drawingShip; + public MotorShip() { InitializeComponent(); @@ -18,18 +26,18 @@ namespace ProjectMotorShip /// private void Draw() { - if (_drawningMotorShip == null) + if (_drawningShip == null) { return; } Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); 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) { - if (_drawningMotorShip == null) + if (_drawningShip == null) { return; } @@ -37,16 +45,16 @@ namespace ProjectMotorShip switch (name) { case "buttonUp": - _drawningMotorShip.MoveTransport(DirectionType.Up); + _drawningShip.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningMotorShip.MoveTransport(DirectionType.Down); + _drawningShip.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawningMotorShip.MoveTransport(DirectionType.Left); + _drawningShip.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawningMotorShip.MoveTransport(DirectionType.Right); + _drawningShip.MoveTransport(DirectionType.Right); break; } Draw(); @@ -55,14 +63,64 @@ namespace ProjectMotorShip private void buttonCreate_Click_1(object sender, EventArgs e) { Random random = new(); - _drawningMotorShip = new DrawningMotorShip(); - _drawningMotorShip.Init(random.Next(100, 300), random.Next(1000, 3000), + _drawningShip = new DrawningMotorShip(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)), /*Convert.ToBoolean(random.Next(0, 2))*/ 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(); } } diff --git a/ProjectMotorShip/ProjectMotorShip/MoveToBorder.cs b/ProjectMotorShip/ProjectMotorShip/MoveToBorder.cs new file mode 100644 index 0000000..d627873 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/MoveToBorder.cs @@ -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(); + } + } + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/MoveToCenter.cs b/ProjectMotorShip/ProjectMotorShip/MoveToCenter.cs new file mode 100644 index 0000000..1cd0cc9 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/MoveToCenter.cs @@ -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(); + } + } + } + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/ObjectParametrs.cs b/ProjectMotorShip/ProjectMotorShip/ObjectParametrs.cs new file mode 100644 index 0000000..8cd2192 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/ObjectParametrs.cs @@ -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; + } + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/Status.cs b/ProjectMotorShip/ProjectMotorShip/Status.cs new file mode 100644 index 0000000..3f2c1f7 --- /dev/null +++ b/ProjectMotorShip/ProjectMotorShip/Status.cs @@ -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 + } +} diff --git a/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.dll b/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.dll index 7a6c161..77550f5 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.dll and b/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.dll differ diff --git a/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.pdb b/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.pdb index 4b44557..f7b1871 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.pdb and b/ProjectMotorShip/ProjectMotorShip/bin/Debug/net6.0-windows/ProjectMotorShip.pdb differ diff --git a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.CoreCompileInputs.cache b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.CoreCompileInputs.cache index ef7bd10..9e6744e 100644 --- a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.CoreCompileInputs.cache +++ b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -92a56f81e67c3af2f643156bfb15fe83c802e1d6 +556daec231b7e3b877f10be99e1b34a9bd49f3d7 diff --git a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.GenerateResource.cache b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.GenerateResource.cache index fb40c74..6df0329 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.GenerateResource.cache and b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.csproj.GenerateResource.cache differ diff --git a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.dll b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.dll index 7a6c161..77550f5 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.dll and b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.dll differ diff --git a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.pdb b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.pdb index 4b44557..f7b1871 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.pdb and b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ProjectMotorShip.pdb differ diff --git a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ref/ProjectMotorShip.dll b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ref/ProjectMotorShip.dll index d8f8707..c3db125 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ref/ProjectMotorShip.dll and b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/ref/ProjectMotorShip.dll differ diff --git a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/refint/ProjectMotorShip.dll b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/refint/ProjectMotorShip.dll index d8f8707..c3db125 100644 Binary files a/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/refint/ProjectMotorShip.dll and b/ProjectMotorShip/ProjectMotorShip/obj/Debug/net6.0-windows/refint/ProjectMotorShip.dll differ