From 5b3f2c0ab161413d935df75184b5b9091d57a5d9 Mon Sep 17 00:00:00 2001 From: "yuliya.mavrina@internet.ru" Date: Sat, 14 Oct 2023 17:51:12 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DoubleDeckerBus/DirectionBus.cs | 2 +- .../DoubleDeckerBus.Designer.cs | 76 +++++++-- .../DoubleDeckerBus/DoubleDeckerBus.cs | 74 +++++++- .../DoubleDeckerBus/DrawningBus.cs | 130 -------------- .../DrawningObjects/DrawningBus.cs | 160 ++++++++++++++++++ .../DrawningDoubleDeckerBus.cs | 79 +++++++++ .../DoubleDeckerBus/Entities/EntityBus.cs | 24 +++ .../Entities/EntityDoubleDeckerBus.cs | 26 +++ DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs | 23 --- .../MovementStrategy/AbstractStrategy.cs | 72 ++++++++ .../MovementStrategy/DrawningObjectBus.cs | 40 +++++ .../MovementStrategy/IMoveableObject.cs | 16 ++ .../MovementStrategy/MoveToBorder.cs | 58 +++++++ .../MovementStrategy/MoveToCenter.cs | 57 +++++++ .../MovementStrategy/ObjectParameters.cs | 30 ++++ .../MovementStrategy/Status.cs | 15 ++ 16 files changed, 706 insertions(+), 176 deletions(-) delete mode 100644 DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs delete mode 100644 DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/DrawningObjectBus.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToBorder.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParameters.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/Status.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/DirectionBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DirectionBus.cs index 0ec093a..07db3c8 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DirectionBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DirectionBus.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace DoubleDeckerBus { - internal enum DirectionBus + public enum DirectionBus { None = 0, Up = 1, diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs index f5ce44d..0c2e372 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxBus = new PictureBox(); - buttonCreate = new Button(); + buttonCreateBus = new Button(); buttonUp = new Button(); buttonLeft = new Button(); buttonDown = new Button(); buttonRight = new Button(); + buttonCreatDoubleDeckerDus = new Button(); + buttonStep_Click = new Button(); + comboBoxStrategy = new ComboBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit(); SuspendLayout(); // @@ -46,17 +49,18 @@ pictureBoxBus.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxBus.TabIndex = 0; pictureBoxBus.TabStop = false; + // - // buttonCreate + // buttonCreateBus // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 426); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBus.Location = new Point(12, 409); + buttonCreateBus.Name = "buttonCreateBus"; + buttonCreateBus.Size = new Size(117, 40); + buttonCreateBus.TabIndex = 1; + buttonCreateBus.Text = "Создать автобус"; + buttonCreateBus.UseVisualStyleBackColor = true; + buttonCreateBus.Click += buttonCreateBus_Click; // // buttonUp // @@ -68,7 +72,8 @@ buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 2; buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += buttonMove_Click; + buttonUp.AutoSizeChanged += ButtonMove_Click; + buttonUp.Click += ButtonMove_Click; // // buttonLeft // @@ -80,7 +85,8 @@ buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 3; buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += buttonMove_Click; + buttonLeft.AutoSizeChanged += ButtonMove_Click; + buttonLeft.Click += ButtonMove_Click; // // buttonDown // @@ -92,7 +98,8 @@ buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; - buttonDown.Click += buttonMove_Click; + buttonDown.AutoSizeChanged += ButtonMove_Click; + buttonDown.Click += ButtonMove_Click; // // buttonRight // @@ -104,18 +111,52 @@ buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; - buttonRight.Click += buttonMove_Click; + buttonRight.AutoSizeChanged += ButtonMove_Click; + buttonRight.Click += ButtonMove_Click; + // + // buttonCreatDoubleDeckerDus + // + buttonCreatDoubleDeckerDus.Location = new Point(135, 409); + buttonCreatDoubleDeckerDus.Name = "buttonCreatDoubleDeckerDus"; + buttonCreatDoubleDeckerDus.Size = new Size(136, 40); + buttonCreatDoubleDeckerDus.TabIndex = 6; + buttonCreatDoubleDeckerDus.Text = "Создать двухэтажный автобус"; + buttonCreatDoubleDeckerDus.UseVisualStyleBackColor = true; + buttonCreatDoubleDeckerDus.Click += buttonCreatDoubleDeckerDus_Click; + // + // buttonStep_Click + // + buttonStep_Click.Location = new Point(797, 41); + buttonStep_Click.Name = "buttonStep_Click"; + buttonStep_Click.Size = new Size(75, 23); + buttonStep_Click.TabIndex = 7; + buttonStep_Click.Text = "Шаг"; + buttonStep_Click.UseVisualStyleBackColor = true; + buttonStep_Click.Click += buttonStep_Click_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" }); + comboBoxStrategy.Location = new Point(751, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 8; // // DoubleDeckerBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonStep_Click); + Controls.Add(buttonCreatDoubleDeckerDus); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonUp); - Controls.Add(buttonCreate); + Controls.Add(buttonCreateBus); Controls.Add(pictureBoxBus); Name = "DoubleDeckerBus"; StartPosition = FormStartPosition.CenterParent; @@ -128,10 +169,13 @@ #endregion private PictureBox pictureBoxBus; - private Button buttonCreate; + private Button buttonCreateBus; private Button buttonUp; private Button buttonLeft; private Button buttonDown; private Button buttonRight; + private Button buttonCreatDoubleDeckerDus; + private Button buttonStep_Click; + private ComboBox comboBoxStrategy; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs index c5f235b..e4e5994 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs @@ -1,8 +1,12 @@ +using DoubleDeckerBus.DrawningObjects; +using DoubleDeckerBus.MovementStrategy; + namespace DoubleDeckerBus { public partial class DoubleDeckerBus : Form { private DrawningBus? _drawningBus; + private AbstractStrategy? _abstractStrategy; public DoubleDeckerBus() { InitializeComponent(); @@ -20,14 +24,36 @@ namespace DoubleDeckerBus pictureBoxBus.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreateBus_Click(object sender, EventArgs e) { Random random = new(); - _drawningBus = new DrawningBus(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), -random.Next(0, 256)), pictureBoxBus.Width, pictureBoxBus.Height); + _drawningBus = new DrawningBus(random.Next(100, 300), + random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxBus.Width, pictureBoxBus.Height); + _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } - private void buttonMove_Click(object sender, EventArgs e) + + private void buttonCreatDoubleDeckerDus_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningBus = new DrawningDoubleDeckerBus(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)), + pictureBoxBus.Width, pictureBoxBus.Height); + _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningBus == null) { @@ -52,6 +78,42 @@ random.Next(0, 256)), pictureBoxBus.Width, pictureBoxBus.Height); Draw(); } - + private void buttonStep_Click_Click(object sender, EventArgs e) + { + if (_drawningBus == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectBus(_drawningBus), pictureBoxBus.Width, + pictureBoxBus.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/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs deleted file mode 100644 index 7571c88..0000000 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningBus.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DoubleDeckerBus -{ - internal class DrawningBus - { - public EntityBus? EntityBus { get; private set; } - private int? _pictureWidth = null; - private int? _pictureHeight = null; - private float _startPosX; - private float _startPosY; - private readonly int _busWidth = 100; - private readonly int _busHeight = 70; - public DrawningBus(int speed, float weight, Color bodyColor, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - EntityBus = new EntityBus(speed, weight, bodyColor); - } - public void SetPosition(int x, int y, int width, int height) - { - if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - _startPosX = x; - _startPosY = y; - _pictureWidth = width; - _pictureHeight = height; - } - public void MoveTransport(DirectionBus direction) - { - if (EntityBus == null) - { - return; - } - switch (direction) - { - case DirectionBus.Left: - if (_startPosX - EntityBus.Step > 0) - { - _startPosX -= (int)EntityBus.Step; - } - break; - case DirectionBus.Up: - if (_startPosY - EntityBus.Step > 0) - { - _startPosY -= (int)EntityBus.Step; - } - break; - case DirectionBus.Right: - if (_startPosX + _busWidth + EntityBus.Step < _pictureWidth) - { - _startPosX += EntityBus.Step; - } - break; - - case DirectionBus.Down: - if (_startPosY + _busHeight + EntityBus.Step < _pictureHeight) - { - _startPosY += EntityBus.Step; - } - break; - } - - } - public void DrawTransport(Graphics g) - { - if (EntityBus == null) - { - return; - } - Pen pen = new(Color.Black); - - g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 60); - Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); - g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 60); - - g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); - Brush brBlack = new SolidBrush(Color.Black); - g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); - - g.DrawEllipse(pen, _startPosX + 7, _startPosY + 55, 20, 20); - g.DrawEllipse(pen, _startPosX + 77, _startPosY + 55, 20, 20); - g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 55, 20, 20); - g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 55, 20, 20); - - Brush brBlue = new SolidBrush(Color.Blue); - g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); - g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); - g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); - g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); - - g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 35, 10, 15); - g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 35, 10, 15); - g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 35, 10, 15); - g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 35, 10, 15); - } - public void ChangeBorders(int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight) - { - _pictureWidth = null; - _pictureHeight = null; - return; - } - if (_startPosX + _busWidth > _pictureWidth) - { - _startPosX = _pictureWidth.Value - _busWidth; - } - if (_startPosY + _busHeight > _pictureHeight) - { - _startPosY = _pictureHeight.Value - _busHeight; - } - } - - public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() - { - return (_startPosX, _startPosX + _busWidth, _startPosY, _startPosY + _busHeight); - } - } -} diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs new file mode 100644 index 0000000..38d1741 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs @@ -0,0 +1,160 @@ +using DoubleDeckerBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.DrawningObjects +{ + public class DrawningBus + { + public EntityBus? EntityBus { get; protected set; } + private int? _pictureWidth; + private int? _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _busWidth = 110; + protected readonly int _busHeight = 70; + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _busWidth; + public int GetHeight => _busHeight; + public DrawningBus(int speed, float weight, Color bodyColor, int + width, int height) + { + if (width < _busWidth || height < _busHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityBus = new EntityBus(speed, weight, bodyColor); + } + protected DrawningBus(int speed, float weight, Color bodyColor, int width, int height, int busWidth, int busHeight) + { + if (width <= _busWidth || height <= _busHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _busWidth = busWidth; + _busHeight = busHeight; + EntityBus = new EntityBus(speed, weight, bodyColor); + } + public void SetPosition(int x, int y) + { + if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + _startPosX = x; + _startPosY = y; + } + public bool CanMove(DirectionBus direction) + { + if (EntityBus == null) + { + return false; + } + return direction switch + { + //влево + DirectionBus.Left => _startPosX - EntityBus.Step > 0, + //вверх + DirectionBus.Up => _startPosY - EntityBus.Step > 0, + //вправо + DirectionBus.Right => _startPosX + _busWidth + EntityBus.Step < _pictureWidth, + //вниз + DirectionBus.Down => _startPosY + _busHeight + EntityBus.Step < _pictureHeight, + _ => false, + }; + } + public void MoveTransport(DirectionBus direction) + { + if (!CanMove(direction) || EntityBus == null) + { + return; + } + switch (direction) + { + //влево + case DirectionBus.Left: + _startPosX -= (int)EntityBus.Step; + break; + //вверх + case DirectionBus.Up: + _startPosY -= (int)EntityBus.Step; + break; + // вправо + case DirectionBus.Right: + _startPosX += (int)EntityBus.Step; + break; + //вниз + case DirectionBus.Down: + _startPosY += (int)EntityBus.Step; + break; + } + } + + public virtual void DrawTransport(Graphics g) + { + if (EntityBus == null) + { + return; + } + if (EntityBus == null) + { + return; + } + + Pen pen = new(Color.Black); + + //границы автобсуса + g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 30); + Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); + g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 30); + + //Дверь + g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); + Brush brBlack = new SolidBrush(Color.Black); + g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); + + //Колеса + g.DrawEllipse(pen, _startPosX + 7, _startPosY + 35, 10, 10); + g.DrawEllipse(pen, _startPosX + 77, _startPosY + 35, 10, 10); + g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 35, 10, 10); + g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 35, 10, 10); + + //окна + Brush brBlue = new SolidBrush(Color.Blue); + g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); + + //g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 30); + //Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); + //g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 30); + + //g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); + //Brush brBlack = new SolidBrush(Color.Black); + //g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); + + //g.DrawEllipse(pen, _startPosX + 7, _startPosY + 35, 10, 10); + //g.DrawEllipse(pen, _startPosX + 77, _startPosY + 35, 10, 10); + //g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 35, 10, 10); + //g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 35, 10, 10); + + //Brush brBlue = new SolidBrush(Color.Blue); + //g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); + //g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); + //g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); + //g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); + } + + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs new file mode 100644 index 0000000..38a86a2 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs @@ -0,0 +1,79 @@ +using DoubleDeckerBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.NetworkInformation; + +namespace DoubleDeckerBus.DrawningObjects +{ + public class DrawningDoubleDeckerBus : DrawningBus + { + public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, + Color additionalColor, bool secondFloor, bool ladder, bool lineBetweenFloor, int width, int height) : + base(speed, weight, bodyColor, width, height, 120, 85) + { + + if (EntityBus != null) + { + EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, + additionalColor, secondFloor, ladder, lineBetweenFloor); + } + } + + public override void DrawTransport(Graphics g) + { + if (EntityBus is not EntityDoubleDeckerBus doubleDeckerBus) + { + return; + } + + Pen pen = new(Color.Black); + Brush brAdditionalColor = new SolidBrush(doubleDeckerBus.AdditionalColor); + Brush brBlue = new SolidBrush(Color.Blue); + Brush brBlack = new SolidBrush(Color.Black); + + if (doubleDeckerBus.SecondFloor) + { + g.FillRectangle(brAdditionalColor, _startPosX, _startPosY + 10, 100, 30); + + g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); + g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); + + + g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); + } + + _startPosY += 30; + base.DrawTransport(g); + _startPosY -= 30; + + if (doubleDeckerBus.Ladder) + { + if (doubleDeckerBus.SecondFloor == true) + { + //Вертикальные прямые + g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 70)), new Point((int)(_startPosX), (int)(_startPosY + 10))); + g.DrawLine(pen, new Point((int)(_startPosX + 10), (int)(_startPosY + 70)), new Point((int)(_startPosX + 10), (int)(_startPosY + 10))); + + //Горизонтальные прямые + g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 20)), new Point((int)(_startPosX + 10), (int)(_startPosY + 20))); + g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 30)), new Point((int)(_startPosX + 10), (int)(_startPosY + 30))); + g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 40)), new Point((int)(_startPosX + 10), (int)(_startPosY + 40))); + g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 50)), new Point((int)(_startPosX + 10), (int)(_startPosY + 50))); + g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 60)), new Point((int)(_startPosX + 10), (int)(_startPosY + 60))); + } + } + + + } + } +} + + + + diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs new file mode 100644 index 0000000..6e75501 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityBus.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Entities +{ + public class EntityBus + { + 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 EntityBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } + +} + diff --git a/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs new file mode 100644 index 0000000..a762a85 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Entities/EntityDoubleDeckerBus.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Entities +{ + public class EntityDoubleDeckerBus : EntityBus + { + public Color AdditionalColor { get; private set; } + public bool SecondFloor { get; private set; } + public bool Ladder { get; private set; } + public bool LineBetweenFloor { get; private set; } + public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color + additionalColor, bool secondFloor, bool ladder, bool lineBetweenFloor) : + base(speed, weight, bodyColor) + { + AdditionalColor = additionalColor; + SecondFloor = secondFloor; + Ladder = ladder; + LineBetweenFloor = lineBetweenFloor; + } + } + +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs b/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs deleted file mode 100644 index 1ab74f9..0000000 --- a/DoubleDeckerBus/DoubleDeckerBus/EntityBus.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DoubleDeckerBus -{ - internal class EntityBus - { - public int Speed { get; private set; } - public float Weight { get; private set; } - public Color BodyColor { get; private set; } - public float Step => (float)Speed * 100 / Weight; - public EntityBus(int speed, float weight, Color bodyColor) - { - Random rnd = new Random(); - Speed = (speed <= 0) ? rnd.Next(250, 300) : speed; - Weight = (weight <= 0) ? rnd.Next(50, 70) : weight; - BodyColor = bodyColor; - } - } -} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..c2613cf --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.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(); + } + protected bool MoveLeft() => MoveTo(DirectionBus.Left); + protected bool MoveRight() => MoveTo(DirectionBus.Right); + protected bool MoveUp() => MoveTo(DirectionBus.Up); + protected bool MoveDown() => MoveTo(DirectionBus.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(); + private bool MoveTo(DirectionBus directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/DrawningObjectBus.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/DrawningObjectBus.cs new file mode 100644 index 0000000..d81df87 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/DrawningObjectBus.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using DoubleDeckerBus.DrawningObjects; + +namespace DoubleDeckerBus.MovementStrategy +{ + public class DrawningObjectBus : IMoveableObject + { + private readonly DrawningBus? _drawningBus = null; + public DrawningObjectBus(DrawningBus drawningBus) + { + _drawningBus = drawningBus; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningBus == null || _drawningBus.EntityBus == null) + { + return null; + } + return new ObjectParameters(_drawningBus.GetPosX, + _drawningBus.GetPosY, _drawningBus.GetWidth, _drawningBus.GetHeight); + } + } + + public int GetStep => (int)(_drawningBus?.EntityBus?.Step ?? 0); + + public bool CheckCanMove(DirectionBus direction) => + _drawningBus?.CanMove(direction) ?? false; + + public void MoveObject(DirectionBus direction) => + _drawningBus?.MoveTransport(direction); + } +} + diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..d116928 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool CheckCanMove(DirectionBus direction); + void MoveObject(DirectionBus direction); + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToBorder.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..8115183 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.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/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..609c7bf --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.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(); + } + } + } + } + +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParameters.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..e7136da --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.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/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/Status.cs b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/Status.cs new file mode 100644 index 0000000..c7168d3 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/MovementStrategy/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +} \ No newline at end of file -- 2.25.1 From 41f1398da6ebafb57f6c475c082607794686803c Mon Sep 17 00:00:00 2001 From: "yuliya.mavrina@internet.ru" Date: Sat, 14 Oct 2023 18:26:14 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=E2=84=962.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningObjects/DrawningBus.cs | 29 +++---------------- .../DrawningDoubleDeckerBus.cs | 1 - 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs index 38d1741..7940935 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs @@ -113,48 +113,27 @@ namespace DoubleDeckerBus.DrawningObjects Pen pen = new(Color.Black); - //границы автобсуса + g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 30); Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 30); - //Дверь + g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); Brush brBlack = new SolidBrush(Color.Black); g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); - //Колеса + g.DrawEllipse(pen, _startPosX + 7, _startPosY + 35, 10, 10); g.DrawEllipse(pen, _startPosX + 77, _startPosY + 35, 10, 10); g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 35, 10, 10); g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 35, 10, 10); - //окна Brush brBlue = new SolidBrush(Color.Blue); g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); - g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); - - //g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 30); - //Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); - //g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 30); - - //g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); - //Brush brBlack = new SolidBrush(Color.Black); - //g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); - - //g.DrawEllipse(pen, _startPosX + 7, _startPosY + 35, 10, 10); - //g.DrawEllipse(pen, _startPosX + 77, _startPosY + 35, 10, 10); - //g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 35, 10, 10); - //g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 35, 10, 10); - - //Brush brBlue = new SolidBrush(Color.Blue); - //g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); - //g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); - //g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); - //g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); + g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15); } - } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs index 38a86a2..6a7cd3c 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.cs @@ -41,7 +41,6 @@ namespace DoubleDeckerBus.DrawningObjects g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20); g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20); - g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15); g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15); g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15); -- 2.25.1