From 81268266beb62b32e28e80dfd66e70aed709522a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sun, 8 Oct 2023 12:28:52 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=BE=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SailBoat/SailBoat/DrawningSailBoat.cs | 113 --------------------- SailBoat/SailBoat/EntitySailBoat.cs | 35 ------- SailBoat/SailBoat/FormSailBoat.Designer.cs | 45 +++++++- SailBoat/SailBoat/FormSailBoat.cs | 52 ++++++++-- 4 files changed, 87 insertions(+), 158 deletions(-) delete mode 100644 SailBoat/SailBoat/DrawningSailBoat.cs delete mode 100644 SailBoat/SailBoat/EntitySailBoat.cs diff --git a/SailBoat/SailBoat/DrawningSailBoat.cs b/SailBoat/SailBoat/DrawningSailBoat.cs deleted file mode 100644 index 8b54845..0000000 --- a/SailBoat/SailBoat/DrawningSailBoat.cs +++ /dev/null @@ -1,113 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SailBoat -{ - internal class DrawningSailBoat - { - public EntitySailBoat? EntitySailBoat { get; private set; } - - private int _pictureWidth; - - private int _pictureHeight; - - private int _startPosX; - - private int _startPosY; - - private readonly int _boatWidth = 180; - - private readonly int _boatHeight = 125; - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody, int width, int height) - { - if (width < _boatWidth) { return false; } - if (height < _boatHeight) { return false; } - - _pictureWidth = width; - _pictureHeight = height; - EntitySailBoat = new EntitySailBoat(); - EntitySailBoat.Init(speed, weight, bodyColor, additionalColor, sail, rainforcedBody); - return true; - } - public void SetPosition(int x, int y) - { - if (x < 0) { x = 0; } - else if (x > _pictureWidth) { x = _pictureWidth; } - if (y < 0) { y = 0; } - else if (y > _pictureHeight) { y = _pictureHeight; } - _startPosX = x; - _startPosY = y; - } - public void MoveTransport(DirectionType direction) - { - if (EntitySailBoat == null) return; - switch (direction) - { - case DirectionType.Left: - if (_startPosX - EntitySailBoat.Step > 0) - { - _startPosX -= (int)EntitySailBoat.Step; - } - break; - case DirectionType.Right: - if (_startPosX + _boatWidth + EntitySailBoat.Step < _pictureWidth) - { - _startPosX += (int)EntitySailBoat.Step; - } - break; - case DirectionType.Up: - if (_startPosY - EntitySailBoat.Step > 0) - { - _startPosY -= (int)EntitySailBoat.Step; - } - break; - case DirectionType.Down: - if (_startPosY + _boatHeight + EntitySailBoat.Step < _pictureHeight) - { - _startPosY += (int)EntitySailBoat.Step; - } - break; - } - } - public void DrawTransport(Graphics g) - { - if (EntitySailBoat == null) return; - Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntitySailBoat.AdditionalColor); - - //границы корабля - g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60); - g.DrawLine(pen, _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90); - g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120); - g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120); - g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60); - g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 65, 95, 50); - g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50); - - if (EntitySailBoat.RainforcedBody) - { - Brush rainforcedBody = new SolidBrush(Color.DarkGray); - g.FillRectangle(rainforcedBody, _startPosX + 2, _startPosY + 65, 10, 50); - g.DrawRectangle(pen, _startPosX + 2, _startPosY + 65, 10, 50); - g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 53, 90, 10); - g.DrawRectangle(pen, _startPosX + 15, _startPosY + 53, 90, 10); - g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 118, 90, 10); - g.DrawRectangle(pen, _startPosX + 15, _startPosY + 118, 90, 10); - } - - if (EntitySailBoat.Sail) - { - Brush mast = new SolidBrush(Color.Brown); - g.FillRectangle(mast, _startPosX + 60, _startPosY , 4, 90); - g.DrawRectangle(pen, _startPosX + 60, _startPosY , 4, 90); - g.DrawLine(pen, _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY); - g.DrawLine(pen, _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50); - g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50); - g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20); - } - } - } -} diff --git a/SailBoat/SailBoat/EntitySailBoat.cs b/SailBoat/SailBoat/EntitySailBoat.cs deleted file mode 100644 index 3c4f407..0000000 --- a/SailBoat/SailBoat/EntitySailBoat.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SailBoat -{ - internal class EntitySailBoat - { - 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 Sail { get; private set; } - - public bool RainforcedBody { get; private set; } - - public double Step => (double)Speed * 100 / Weight; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Sail = sail; - RainforcedBody = rainforcedBody; - } - } -} diff --git a/SailBoat/SailBoat/FormSailBoat.Designer.cs b/SailBoat/SailBoat/FormSailBoat.Designer.cs index 5bac5aa..a701cba 100644 --- a/SailBoat/SailBoat/FormSailBoat.Designer.cs +++ b/SailBoat/SailBoat/FormSailBoat.Designer.cs @@ -34,6 +34,9 @@ buttonLeft = new Button(); buttonUp = new Button(); buttonRight = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); + buttonCreateBoat = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSailBoat).BeginInit(); SuspendLayout(); // @@ -50,11 +53,11 @@ // buttonCreateSailBoat // buttonCreateSailBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateSailBoat.Location = new Point(12, 419); + buttonCreateSailBoat.Location = new Point(12, 407); buttonCreateSailBoat.Name = "buttonCreateSailBoat"; - buttonCreateSailBoat.Size = new Size(96, 30); + buttonCreateSailBoat.Size = new Size(96, 42); buttonCreateSailBoat.TabIndex = 3; - buttonCreateSailBoat.Text = "Создать"; + buttonCreateSailBoat.Text = "Создать Парусник"; buttonCreateSailBoat.UseVisualStyleBackColor = true; buttonCreateSailBoat.Click += buttonCreateSailBoat_Click; // @@ -106,11 +109,44 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "Движение в центр", "Движение в правый нижний угол" }); + comboBoxStrategy.Location = new Point(644, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(228, 23); + comboBoxStrategy.TabIndex = 11; + // + // buttonStep + // + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 12; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // + // buttonCreateBoat + // + buttonCreateBoat.Location = new Point(114, 407); + buttonCreateBoat.Name = "buttonCreateBoat"; + buttonCreateBoat.Size = new Size(75, 42); + buttonCreateBoat.TabIndex = 13; + buttonCreateBoat.Text = "Создать лодку"; + buttonCreateBoat.UseVisualStyleBackColor = true; + buttonCreateBoat.Click += buttonCreateBoat_Click; + // // FormSailBoat // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonCreateBoat); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonUp); @@ -133,5 +169,8 @@ private Button buttonLeft; private Button buttonUp; private Button buttonRight; + private ComboBox comboBoxStrategy; + private Button buttonStep; + private Button buttonCreateBoat; } } \ No newline at end of file diff --git a/SailBoat/SailBoat/FormSailBoat.cs b/SailBoat/SailBoat/FormSailBoat.cs index 0beb2ce..3ef2e56 100644 --- a/SailBoat/SailBoat/FormSailBoat.cs +++ b/SailBoat/SailBoat/FormSailBoat.cs @@ -1,3 +1,6 @@ +using SailBoat.DrawningObjects; +using SailBoat.MovementStrategy; + namespace SailBoat { public partial class FormSailBoat : Form @@ -9,10 +12,10 @@ namespace SailBoat } private void Draw() { - if (_drawningSailBoat == null) { return; } + if (_drawningBoat == null) { return; } Bitmap bmp = new(pictureBoxSailBoat.Width, pictureBoxSailBoat.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningSailBoat.DrawTransport(gr); + _drawningBoat.DrawTransport(gr); pictureBoxSailBoat.Image = bmp; } @@ -24,26 +27,61 @@ namespace SailBoat _drawningSailBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + + private void buttonCreateBoat_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningBoat = new DrawningBoat(random.Next(300, 700), random.Next(2000, 4000), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height); + _drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + private void buttonMove_Click(object sender, EventArgs e) { - if (_drawningSailBoat == null) { return; } + if (_drawningBoat == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; switch (name) { case "buttonUp": - _drawningSailBoat.MoveTransport(DirectionType.Up); + _drawningBoat.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawningSailBoat.MoveTransport(DirectionType.Down); + _drawningBoat.MoveTransport(DirectionType.Down); break; case "buttonRight": - _drawningSailBoat.MoveTransport(DirectionType.Right); + _drawningBoat.MoveTransport(DirectionType.Right); break; case "buttonLeft": - _drawningSailBoat.MoveTransport(DirectionType.Left); + _drawningBoat.MoveTransport(DirectionType.Left); break; } Draw(); } + + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawningBoat == null) return; + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + + }; + if (_abstractStrategy == null) return; + _abstractStrategy.SetData(new DrawningObjectBoat(_drawningBoat), 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 -- 2.25.1 From ae022205ebb20844865a351bb523a296deb801ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sun, 8 Oct 2023 12:43:05 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SailBoat/DrawningObjects/DrawningBoat.cs | 110 ++++++++++++++++++ .../DrawningObjects/DrawningSailBoat.cs | 59 ++++++++++ SailBoat/SailBoat/Entities/EntityBoat.cs | 27 +++++ SailBoat/SailBoat/Entities/EntitySailBoat.cs | 24 ++++ .../MovementStrategy/AbstractStrategy.cs | 82 +++++++++++++ .../MovementStrategy/DrawningObjectBoat.cs | 34 ++++++ .../MovementStrategy/IMoveableObject.cs | 20 ++++ .../SailBoat/MovementStrategy/MoveToBorder.cs | 40 +++++++ .../SailBoat/MovementStrategy/MoveToCenter.cs | 48 ++++++++ .../MovementStrategy/ObjectParameters.cs | 39 +++++++ SailBoat/SailBoat/MovementStrategy/Status.cs | 17 +++ 11 files changed, 500 insertions(+) create mode 100644 SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs create mode 100644 SailBoat/SailBoat/DrawningObjects/DrawningSailBoat.cs create mode 100644 SailBoat/SailBoat/Entities/EntityBoat.cs create mode 100644 SailBoat/SailBoat/Entities/EntitySailBoat.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/AbstractStrategy.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/DrawningObjectBoat.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/IMoveableObject.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/MoveToBorder.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/MoveToCenter.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/ObjectParameters.cs create mode 100644 SailBoat/SailBoat/MovementStrategy/Status.cs diff --git a/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs b/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs new file mode 100644 index 0000000..24a14ca --- /dev/null +++ b/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs @@ -0,0 +1,110 @@ +using SailBoat.Entities; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SailBoat.DrawningObjects +{ + public class DrawningBoat + { + public EntityBoat? EntityBoat { get; protected set; } + + private int _pictureWidth; + + private int _pictureHeight; + + protected int _startPosX; + + protected int _startPosY; + + protected readonly int _boatWidth = 180; + + protected readonly int _boatHeight = 125; + + public int GetPosX => _startPosX; + + public int GetPosY => _startPosY; + + public int GetWidth => _boatWidth; + + public int GetHight => _boatHeight; + + 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 + _boatWidth < _pictureWidth, + DirectionType.Down => _startPosY + EntityBoat.Step + _boatHeight < _pictureHeight, + _ => false + }; + } + + public DrawningBoat(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < 200) { width = 900; } + if (height < 200) { height = 500; } + _pictureWidth = width; + _pictureHeight = height; + EntityBoat = new EntityBoat(speed, weight, bodyColor); + } + protected DrawningBoat(int speed, double weight, Color bodyColor, int width, int height, int boatWidth, int boatHeight) + { + if (width < _boatWidth) { return; } + if (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 = 0; } + else if (x > _pictureWidth) { x = _pictureWidth; } + if (y < 0) { y = 0; } + else if (y > _pictureHeight) { y = _pictureHeight; } + _startPosX = x; + _startPosY = y; + } + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityBoat == null) return; + switch (direction) + { + case DirectionType.Left: + _startPosX -= (int)EntityBoat.Step; + break; + case DirectionType.Right: + _startPosX += (int)EntityBoat.Step; + break; + case DirectionType.Up: + _startPosY -= (int)EntityBoat.Step; + break; + case DirectionType.Down: + _startPosY += (int)EntityBoat.Step; + break; + } + } + public virtual void DrawTransport(Graphics g) + { + if (EntityBoat == null) return; + Pen pen = new(Color.Black); + Brush BodyBrush = new SolidBrush(EntityBoat.BodyColor); + + g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60); + g.DrawLine(pen, _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90); + g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120); + g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120); + g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60); + g.FillEllipse(BodyBrush, _startPosX + 15, _startPosY + 65, 95, 50); + g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50); + } + } +} diff --git a/SailBoat/SailBoat/DrawningObjects/DrawningSailBoat.cs b/SailBoat/SailBoat/DrawningObjects/DrawningSailBoat.cs new file mode 100644 index 0000000..242f800 --- /dev/null +++ b/SailBoat/SailBoat/DrawningObjects/DrawningSailBoat.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SailBoat.Entities; + +namespace SailBoat.DrawningObjects +{ + internal class DrawningSailBoat : DrawningBoat + { + + public DrawningSailBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody, int width, int height) : base(speed, weight, bodyColor, width, height, 180,125) + { + if(EntityBoat != null) + { + EntityBoat = new EntitySailBoat(speed, weight, bodyColor, additionalColor, sail, rainforcedBody); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityBoat is not EntitySailBoat sailBoat) { return; } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(sailBoat.AdditionalColor); + + g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60); + g.DrawLine(pen, _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90); + g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120); + g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120); + g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60); + g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 65, 95, 50); + g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50); + + base.DrawTransport(g); + + if (sailBoat.RainforcedBody) + { + Brush rainforcedBody = new SolidBrush(Color.DarkGray); + g.FillRectangle(rainforcedBody, _startPosX + 2, _startPosY + 65, 10, 50); + g.DrawRectangle(pen, _startPosX + 2, _startPosY + 65, 10, 50); + g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 53, 90, 10); + g.DrawRectangle(pen, _startPosX + 15, _startPosY + 53, 90, 10); + g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 118, 90, 10); + g.DrawRectangle(pen, _startPosX + 15, _startPosY + 118, 90, 10); + } + + if (sailBoat.Sail) + { + Brush mast = new SolidBrush(Color.Brown); + g.FillRectangle(mast, _startPosX + 60, _startPosY, 4, 90); + g.DrawRectangle(pen, _startPosX + 60, _startPosY, 4, 90); + g.DrawLine(pen, _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY); + g.DrawLine(pen, _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50); + g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50); + g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20); + } + } + } +} diff --git a/SailBoat/SailBoat/Entities/EntityBoat.cs b/SailBoat/SailBoat/Entities/EntityBoat.cs new file mode 100644 index 0000000..c062152 --- /dev/null +++ b/SailBoat/SailBoat/Entities/EntityBoat.cs @@ -0,0 +1,27 @@ +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; + } + } +} \ No newline at end of file diff --git a/SailBoat/SailBoat/Entities/EntitySailBoat.cs b/SailBoat/SailBoat/Entities/EntitySailBoat.cs new file mode 100644 index 0000000..08318c9 --- /dev/null +++ b/SailBoat/SailBoat/Entities/EntitySailBoat.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SailBoat.Entities +{ + public class EntitySailBoat : EntityBoat + { + public Color AdditionalColor { get; private set; } + + public bool Sail { get; private set; } + + public bool RainforcedBody { get; private set; } + + public EntitySailBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody) : base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + Sail = sail; + RainforcedBody = rainforcedBody; + } + } +} diff --git a/SailBoat/SailBoat/MovementStrategy/AbstractStrategy.cs b/SailBoat/SailBoat/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..7d494a5 --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 (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 ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + + protected int? GetStep() + { + if(_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + protected abstract void MoveToTarget(); + + protected abstract bool IsTargetDestination(); + + 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/MovementStrategy/DrawningObjectBoat.cs b/SailBoat/SailBoat/MovementStrategy/DrawningObjectBoat.cs new file mode 100644 index 0000000..b31aa86 --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/DrawningObjectBoat.cs @@ -0,0 +1,34 @@ +using SailBoat.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SailBoat.MovementStrategy +{ + public class DrawningObjectBoat : IMoveableObject + { + private readonly DrawningBoat? _drawningBoat = null; + + public DrawningObjectBoat(DrawningBoat drawningBoat) + { + _drawningBoat = drawningBoat; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningBoat == null || _drawningBoat.EntityBoat == null) return null; + return new ObjectParameters(_drawningBoat.GetPosX, _drawningBoat.GetPosY, _drawningBoat.GetWidth, _drawningBoat.GetHight); + } + } + + public int GetStep => (int)(_drawningBoat?.EntityBoat?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) => _drawningBoat?.CanMove(direction) ?? false; + + public void MoveObject(DirectionType direction) => _drawningBoat?.MoveTransport(direction); + } +} diff --git a/SailBoat/SailBoat/MovementStrategy/IMoveableObject.cs b/SailBoat/SailBoat/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..fc9b86a --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Serialization; + +namespace SailBoat.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + + int GetStep { get; } + + bool CheckCanMove(DirectionType direction); + + void MoveObject(DirectionType direction); + } +} diff --git a/SailBoat/SailBoat/MovementStrategy/MoveToBorder.cs b/SailBoat/SailBoat/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..cdc818d --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,40 @@ +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 IsTargetDestination() + { + 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) + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if(Math.Abs(diffY) > GetStep()) + { + if(diffY < 0) + { + MoveDown(); + } + } + } + } +} diff --git a/SailBoat/SailBoat/MovementStrategy/MoveToCenter.cs b/SailBoat/SailBoat/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..06d4a90 --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,48 @@ +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 IsTargetDestination() + { + 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(); + } + } + } + } +} diff --git a/SailBoat/SailBoat/MovementStrategy/ObjectParameters.cs b/SailBoat/SailBoat/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..2f70a1d --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,39 @@ +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; + + public ObjectParameters(int x , int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/SailBoat/SailBoat/MovementStrategy/Status.cs b/SailBoat/SailBoat/MovementStrategy/Status.cs new file mode 100644 index 0000000..1feefd4 --- /dev/null +++ b/SailBoat/SailBoat/MovementStrategy/Status.cs @@ -0,0 +1,17 @@ +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 bdf0062316be6831df12d04d95a1dafe98be3bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sun, 8 Oct 2023 12:57:17 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SailBoat/SailBoat/FormSailBoat.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/SailBoat/SailBoat/FormSailBoat.cs b/SailBoat/SailBoat/FormSailBoat.cs index 3ef2e56..5028065 100644 --- a/SailBoat/SailBoat/FormSailBoat.cs +++ b/SailBoat/SailBoat/FormSailBoat.cs @@ -5,26 +5,27 @@ namespace SailBoat { public partial class FormSailBoat : Form { - private DrawningSailBoat _drawningSailBoat; + private DrawningBoat? _drawningBoat; + + private AbstractStrategy? _abstractStrategy; public FormSailBoat() { InitializeComponent(); } private void Draw() { - if (_drawningBoat == null) { return; } Bitmap bmp = new(pictureBoxSailBoat.Width, pictureBoxSailBoat.Height); Graphics gr = Graphics.FromImage(bmp); _drawningBoat.DrawTransport(gr); pictureBoxSailBoat.Image = bmp; + } private void buttonCreateSailBoat_Click(object sender, EventArgs e) { Random random = new(); - _drawningSailBoat = new DrawningSailBoat(); - _drawningSailBoat.Init(random.Next(300, 700), random.Next(2000, 4000), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailBoat.Width, pictureBoxSailBoat.Height); - _drawningSailBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawningBoat = new DrawningSailBoat(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); + _drawningBoat.SetPosition(random.Next(10, 100), random.Next(10,100)); Draw(); } -- 2.25.1 From 941e8dcfd08eedc3f5c2ce024c9f6d4375c09f2b Mon Sep 17 00:00:00 2001 From: YakovlevMaxim Date: Mon, 9 Oct 2023 18:22:01 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs b/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs index 24a14ca..e1ca743 100644 --- a/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs +++ b/SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs @@ -47,8 +47,8 @@ namespace SailBoat.DrawningObjects public DrawningBoat(int speed, double weight, Color bodyColor, int width, int height) { - if (width < 200) { width = 900; } - if (height < 200) { height = 500; } + if (width < _boatWidth) { width = 900; } + if (height < _boatHeight) { height = 500; } _pictureWidth = width; _pictureHeight = height; EntityBoat = new EntityBoat(speed, weight, bodyColor); -- 2.25.1