From e0e745cd3a78f4394951c34778429b37e0d3273a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=93=D0=BE=D1=80=D0=B4?=
=?UTF-8?q?=D0=B5=D0=B5=D0=B2?= <89176335310x@gmail.com>
Date: Fri, 6 Oct 2023 12:58:55 +0400
Subject: [PATCH] laba is done
---
.../ElectricLocomotive/AbstractStrategy.cs | 87 +++++++
.../ElectricLocomotive/DirectionType.cs | 3 -
.../DrawningElectricLocomotive.cs | 176 ++-------------
.../ElectricLocomotive/DrawningLocomotive.cs | 212 ++++++++++++++++++
.../DrawningObjectLocomotive.cs | 35 +++
.../ElectricLocomotive.Designer.cs | 70 +++++-
.../ElectricLocomotive/ElectricLocomotive.cs | 91 ++++++--
.../EntityElectricLocomotive.cs | 41 +---
.../ElectricLocomotive/EntityLocomotive.cs | 46 ++++
.../ElectricLocomotive/IMoveableObject.cs | 18 ++
.../ElectricLocomotive/MoveToCenter.cs | 57 +++++
.../ElectricLocomotive/MoveToRightCorner.cs | 30 +++
.../ElectricLocomotive/ObjectParameters.cs | 55 +++++
.../ElectricLocomotive/Status.cs | 15 ++
14 files changed, 708 insertions(+), 228 deletions(-)
create mode 100644 ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/MoveToRightCorner.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
create mode 100644 ElectricLocomotive/ElectricLocomotive/Status.cs
diff --git a/ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs b/ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
new file mode 100644
index 0000000..527448d
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/AbstractStrategy.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using ElectricLocomotive;
+using ElectricLocomotive.MovementStrategy;
+
+namespace ProjectElectricLocomotive.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(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 IsTargetDestinaion();
+
+ private bool MoveTo(DirectionType directionType)
+ {
+ if (_state != Status.InProgress)
+ {
+ return false;
+ }
+ if (_moveableObject?.CheckCanMove(directionType) ?? false)
+ {
+ _moveableObject.MoveObject(directionType);
+ return true;
+ }
+ return false;
+ }
+ }
+}
\ No newline at end of file
diff --git a/ElectricLocomotive/ElectricLocomotive/DirectionType.cs b/ElectricLocomotive/ElectricLocomotive/DirectionType.cs
index ab57b68..88dcdec 100644
--- a/ElectricLocomotive/ElectricLocomotive/DirectionType.cs
+++ b/ElectricLocomotive/ElectricLocomotive/DirectionType.cs
@@ -9,11 +9,8 @@ namespace ElectricLocomotive
public enum DirectionType
{
Up = 1,
-
Down = 2,
-
Left = 3,
-
Right = 4
}
}
diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs
index 5ea4c7c..e34a7b8 100644
--- a/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs
+++ b/ElectricLocomotive/ElectricLocomotive/DrawningElectricLocomotive.cs
@@ -1,175 +1,35 @@
-using System;
+using ElectricLocomotive.DrawningObject;
+using ElectricLocomotive.Entities;
+using System;
using System.Collections.Generic;
-using System.Drawing;
using System.Linq;
-using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
-using System.Windows.Forms.VisualStyles;
namespace ElectricLocomotive
{
- public class DrawningElectricLocomotive
+ public class DrawningElectricLocomotive : DrawningLocomotive
{
- public EntityElectricLocomotive? _ElectricLocomotive { get; private set; }
-
- private int _pictureWidth;
-
- private int _pictureHeight;
-
-
- private int _startPosX;
-
- private int _startPosY;
-
- private readonly int _LocomotiveWidth = 100;
-
- private readonly int _LocomoriveHeight =50;
-
-
- public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, int width, int height)
+ public DrawningElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool Horns, int width, int height)
+ : base (speed, weight, bodyColor, additionalColor, width , height)
{
- _pictureWidth = width;
- _pictureHeight = height;
- if ((_pictureHeight < _LocomoriveHeight) || (_pictureWidth < _LocomotiveWidth))
+ if (EntityLocomotive != null)
{
- return false;
- }
- _ElectricLocomotive = new EntityElectricLocomotive();
- _ElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns);
- return true;
- }
-
- public void SetPosition(int x, int y)
- {
- if (x < 0 || y < 0 || x + _LocomotiveWidth > _pictureWidth || y + _LocomoriveHeight > _pictureHeight)
- {
- return ;
- }
- else
- {
- _startPosX = x;
-
- _startPosY = y;
+ EntityLocomotive = new EntityElectroLocomotive(speed, weight, bodyColor,
+ additionalColor, Horns);
}
}
- public void MoveTransport(DirectionType direction)
+ public override void DrawTransport(Graphics g)
{
- if (_ElectricLocomotive == null)
+ if (EntityLocomotive is not EntityElectroLocomotive electroLocomotive)
{
return;
}
-
- switch (direction)
- {
- //влево
- case DirectionType.Left:
- if (_startPosX - _ElectricLocomotive.Step > 0)
- {
- _startPosX -= (int)_ElectricLocomotive.Step;
- }
- break;
-
- //вверх
- case DirectionType.Up:
- if (_startPosY - _ElectricLocomotive.Step > 0)
- {
- _startPosY -= (int)_ElectricLocomotive.Step;
- }
- break;
-
- // вправо
- case DirectionType.Right: // TODO: Продумать логику
- if (_startPosX+ _LocomotiveWidth + (int)_ElectricLocomotive.Step < _pictureWidth)
- {
- _startPosX += (int)_ElectricLocomotive.Step;
- }
- break;
-
- //вниз
- case DirectionType.Down: // TODO: Продумать логику
- if (_startPosY + _LocomoriveHeight + (int)_ElectricLocomotive.Step < _pictureHeight)
- {
- _startPosY += (int)_ElectricLocomotive.Step;
- }
- break;
- }
- }
-
-
-
-
- public void DrawTransport(Graphics g)
- {
- if (_ElectricLocomotive == null)
- {
- return;
- }
-
- Pen pen = new(_ElectricLocomotive.AdditionalColor);
- Brush brush = new SolidBrush(_ElectricLocomotive.BodyColor);
-
- ///ВЛ60к-1595
-
- ///ходовая
- g.FillRectangle(brush, new Rectangle(_startPosX + 20, _startPosY + 45, 15, 5));
- g.FillRectangle(brush, new Rectangle(_startPosX + 65, _startPosY + 45, 15, 5));
-
- g.FillEllipse(brush, _startPosX + 15, _startPosY + 45, 10, 10);
- g.FillEllipse(brush, _startPosX + 30, _startPosY + 45, 10, 10);
- g.FillEllipse(brush, _startPosX + 60, _startPosY + 45, 10, 10);
- g.FillEllipse(brush, _startPosX + 75, _startPosY + 45, 10, 10);
- PointF[] chassis =
- {
- new Point(_startPosX+10, _startPosY+50),
- new Point(_startPosX, _startPosY+50),
- new Point(_startPosX+10, _startPosY +40),
- new Point(_startPosX + 90, _startPosY+40),
- new Point(_startPosX+100, _startPosY+50),
- new Point(_startPosX+90,_startPosY+ 50),
- new Point(_startPosX+85, _startPosY+45),
- new Point(_startPosX+15, _startPosY+45),
- };
- g.FillPolygon(brush, chassis);
-
- ///обводка ходовой
- Point[] chassisCont =
- {
- new Point(_startPosX+10, _startPosY+50),
- new Point(_startPosX, _startPosY+50),
- new Point(_startPosX+10, _startPosY +40),
- new Point(_startPosX + 90, _startPosY+40),
- new Point(_startPosX+100, _startPosY+50),
- new Point(_startPosX+90,_startPosY+ 50),
- new Point(_startPosX+85, _startPosY+45),
- new Point(_startPosX+15, _startPosY+45),
- };
- g.DrawPolygon(pen, chassisCont);
-
- ///кабина
- Point[] cabin =
- {
- new Point(_startPosX+20, _startPosY+10),
- new Point(_startPosX+90,_startPosY+10),
- new Point(_startPosX+90,_startPosY + 40),
- new Point(_startPosX+10,_startPosY+40),
- new Point(_startPosX+10,_startPosY+25)
- };
- g.DrawPolygon(pen, cabin);
- g.DrawLine(pen, _startPosX + 10, _startPosY + 25, _startPosX + 90, _startPosY + 25);
-
- ///окна
- g.FillRectangle(brush, _startPosX + 20, _startPosY + 15, 10, 10);
- g.FillRectangle(brush, _startPosX + 35, _startPosY + 15, 10, 10);
-
- g.FillRectangle(brush, _startPosX + 75, _startPosY + 15, 10, 10);
-
- ///дверь
- g.FillRectangle(brush, _startPosX + 50, _startPosY + 15, 10, 25);
-
- ///рога
- if (_ElectricLocomotive.Horns == true)
+ Pen pen = new(electroLocomotive.BodyColor) ;
+ Brush brush = new SolidBrush(electroLocomotive.AdditionalColor);
+ // обвесы
+ if (electroLocomotive.Horns)
{
g.FillRectangle(brush, _startPosX + 25, _startPosY + 5, 15, 5);
@@ -198,9 +58,11 @@ namespace ElectricLocomotive
};
g.DrawPolygon(pen, horns2);
}
- ///Батарея
- g.FillRectangle(brush, _startPosX + 45, _startPosY + 5, 15, 5);
+ base.DrawTransport(g);
+
}
+
}
}
+
diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs
new file mode 100644
index 0000000..fbef2bb
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/DrawningLocomotive.cs
@@ -0,0 +1,212 @@
+using ElectricLocomotive.Entities;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive.DrawningObject
+{
+ ///
+ /// Класс, отвечающий за прорисовку и перемещение объекта-сущности
+ ///
+ public class DrawningLocomotive
+ {
+ public EntityLocomotive? EntityLocomotive { get; protected set; }
+
+ protected int _pictureWidth;
+
+ protected int _pictureHeight;
+
+ protected int _startPosX;
+
+ protected int _startPosY;
+
+ protected readonly int _locoWidth = 95;
+
+ protected readonly int _locoHeight = 50;
+ public int GetPosX => _startPosX;
+ public int GetPosY => _startPosY;
+ public int GetWidth => _locoWidth;
+ public int GetHeight => _locoHeight;
+
+
+ public DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width, int heigth)
+ {
+ if (width < _locoWidth || heigth < _locoHeight)
+ {
+ return;
+ }
+ _pictureWidth = width;
+ _pictureHeight = heigth;
+ EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor, additionalColor);
+ }
+
+ protected DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width,
+ int height, int locoWidth, int locoHeight)
+ {
+ if (width < _locoWidth || height < _locoHeight)
+ {
+ return;
+ }
+ _pictureWidth = width;
+ _pictureHeight = height;
+ _locoWidth = locoWidth;
+ _locoHeight = locoHeight;
+ EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor, additionalColor);
+ }
+ ///
+ /// Установка позиции
+ ///
+ /// Координата X
+ /// Координата Y _pictureWidth)
+ {
+ x = _pictureWidth - _locoWidth;
+ }
+ if (y < 0 || y + _locoHeight > _pictureHeight)
+ {
+ y = _pictureHeight - _locoHeight;
+ }
+ _startPosX = x;
+ _startPosY = y;
+ }
+ ///
+ /// Изменение направления перемещения
+ ///
+ /// Направление
+
+ public void MoveTransport(DirectionType direction)
+ {
+ if (EntityLocomotive == null)
+ {
+ return;
+ }
+ switch (direction)
+ {
+ case DirectionType.Left:
+ if (_startPosX - EntityLocomotive.Step > 0)
+ {
+ _startPosX -= (int)EntityLocomotive.Step;
+ }
+ break;
+ case DirectionType.Up:
+ if (_startPosY - EntityLocomotive.Step > 0)
+ {
+ _startPosY -= (int)EntityLocomotive.Step;
+ }
+ break;
+ case DirectionType.Right:
+ if (_startPosX + EntityLocomotive.Step + _locoWidth < _pictureWidth)
+ {
+ _startPosX += (int)EntityLocomotive.Step;
+ }
+ break;
+ case DirectionType.Down:
+ if (_startPosY + EntityLocomotive.Step + _locoHeight < _pictureHeight)
+ {
+ _startPosY += (int)EntityLocomotive.Step;
+ }
+ break;
+ }
+ }
+
+ ///
+ /// Прорисовка объекта
+ ///
+ ///
+ public virtual void DrawTransport(Graphics g)
+ {
+ if (EntityLocomotive == null)
+ {
+ return;
+ }
+ Pen pen = new(EntityLocomotive.BodyColor);
+ Brush brush = new SolidBrush(EntityLocomotive.AdditionalColor);
+ ///ВЛ60к-1595
+
+ ///ходовая
+ g.FillRectangle(brush, new Rectangle(_startPosX + 20, _startPosY + 45, 15, 5));
+ g.FillRectangle(brush, new Rectangle(_startPosX + 65, _startPosY + 45, 15, 5));
+
+ g.FillEllipse(brush, _startPosX + 15, _startPosY + 45, 10, 10);
+ g.FillEllipse(brush, _startPosX + 30, _startPosY + 45, 10, 10);
+ g.FillEllipse(brush, _startPosX + 60, _startPosY + 45, 10, 10);
+ g.FillEllipse(brush, _startPosX + 75, _startPosY + 45, 10, 10);
+ PointF[] chassis =
+ {
+ new Point(_startPosX+10, _startPosY+50),
+ new Point(_startPosX, _startPosY+50),
+ new Point(_startPosX+10, _startPosY +40),
+ new Point(_startPosX + 90, _startPosY+40),
+ new Point(_startPosX+100, _startPosY+50),
+ new Point(_startPosX+90,_startPosY+ 50),
+ new Point(_startPosX+85, _startPosY+45),
+ new Point(_startPosX+15, _startPosY+45),
+ };
+ g.FillPolygon(brush, chassis);
+
+ ///обводка ходовой
+ Point[] chassisCont =
+ {
+ new Point(_startPosX+10, _startPosY+50),
+ new Point(_startPosX, _startPosY+50),
+ new Point(_startPosX+10, _startPosY +40),
+ new Point(_startPosX + 90, _startPosY+40),
+ new Point(_startPosX+100, _startPosY+50),
+ new Point(_startPosX+90,_startPosY+ 50),
+ new Point(_startPosX+85, _startPosY+45),
+ new Point(_startPosX+15, _startPosY+45),
+ };
+ g.DrawPolygon(pen, chassisCont);
+
+ ///кабина
+ Point[] cabin =
+ {
+ new Point(_startPosX+20, _startPosY+10),
+ new Point(_startPosX+90,_startPosY+10),
+ new Point(_startPosX+90,_startPosY + 40),
+ new Point(_startPosX+10,_startPosY+40),
+ new Point(_startPosX+10,_startPosY+25)
+ };
+ g.DrawPolygon(pen, cabin);
+ g.DrawLine(pen, _startPosX + 10, _startPosY + 25, _startPosX + 90, _startPosY + 25);
+
+ ///окна
+ g.FillRectangle(brush, _startPosX + 20, _startPosY + 15, 10, 10);
+ g.FillRectangle(brush, _startPosX + 35, _startPosY + 15, 10, 10);
+
+ g.FillRectangle(brush, _startPosX + 75, _startPosY + 15, 10, 10);
+
+ ///дверь
+ g.FillRectangle(brush, _startPosX + 50, _startPosY + 15, 10, 25);
+
+
+ ///Батарея
+ g.FillRectangle(brush, _startPosX + 45, _startPosY + 5, 15, 5);
+
+ }
+
+ public bool CanMove(DirectionType direction)
+ {
+ if (EntityLocomotive == null)
+ {
+ return false;
+ }
+ return direction switch
+ {
+ //влево
+ DirectionType.Left => _startPosX - EntityLocomotive.Step > 0,
+ //вверх
+ DirectionType.Up => _startPosY - EntityLocomotive.Step > 0,
+ // вправо
+ DirectionType.Right => _startPosX + EntityLocomotive.Step < _pictureWidth,
+ //вниз
+ DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
+ };
+ }
+ }
+}
diff --git a/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs
new file mode 100644
index 0000000..7604f4d
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/DrawningObjectLocomotive.cs
@@ -0,0 +1,35 @@
+using ElectricLocomotive.DrawningObject;
+using ElectricLocomotive.MovementStrategy;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive
+{
+ public class DrawningObjectLocomotive : IMoveableObject
+ {
+ private readonly DrawningLocomotive? _drawningLocomotive = null;
+ public DrawningObjectLocomotive(DrawningLocomotive drawningLocomotive)
+ {
+ _drawningLocomotive = drawningLocomotive;
+ }
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_drawningLocomotive == null || _drawningLocomotive.EntityLocomotive ==
+ null)
+ {
+ return null;
+ }
+ return new ObjectParameters(_drawningLocomotive.GetPosX,_drawningLocomotive.GetPosY, _drawningLocomotive.GetWidth, _drawningLocomotive.GetHeight);
+ }
+ }
+ public int GetStep => (int)(_drawningLocomotive?.EntityLocomotive?.Step ?? 0);
+ public bool CheckCanMove(DirectionType direction) =>_drawningLocomotive?.CanMove(direction) ?? false;
+ public void MoveObject(DirectionType direction) =>_drawningLocomotive?.MoveTransport(direction);
+ }
+}
+
diff --git a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.Designer.cs
index f765a9b..38e688c 100644
--- a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.Designer.cs
+++ b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.Designer.cs
@@ -34,7 +34,10 @@
buttonRight = new Button();
buttonDown = new Button();
buttonLeft = new Button();
- buttonCreate = new Button();
+ buttonCreateElectricLocomotive = new Button();
+ buttonCreateLocomotive = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxElectroLocomotiv).BeginInit();
SuspendLayout();
//
@@ -48,6 +51,7 @@
pictureBoxElectroLocomotiv.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxElectroLocomotiv.TabIndex = 5;
pictureBoxElectroLocomotiv.TabStop = false;
+
//
// buttonUp
//
@@ -97,27 +101,66 @@
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click;
//
- // buttonCreate
+ // buttonCreateElectricLocomotive
//
- buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
- buttonCreate.Location = new Point(13, 425);
- buttonCreate.Name = "buttonCreate";
- buttonCreate.Size = new Size(108, 34);
- buttonCreate.TabIndex = 6;
- buttonCreate.Text = "Создать";
- buttonCreate.UseVisualStyleBackColor = true;
- buttonCreate.Click += buttonCreate_Click;
+ buttonCreateElectricLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
+ buttonCreateElectricLocomotive.Location = new Point(13, 425);
+ buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
+ buttonCreateElectricLocomotive.Size = new Size(208, 34);
+ buttonCreateElectricLocomotive.TabIndex = 6;
+ buttonCreateElectricLocomotive.Text = "Создать Электролокомотив";
+ buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
+ buttonCreateElectricLocomotive.Click += buttonCreateElectricLocomotive_Click;
+ //
+ // buttonCreateLocomotive
+ //
+ buttonCreateLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
+ buttonCreateLocomotive.Location = new Point(227, 425);
+ buttonCreateLocomotive.Name = "buttonCreateLocomotive";
+ buttonCreateLocomotive.Size = new Size(197, 34);
+ buttonCreateLocomotive.TabIndex = 11;
+ buttonCreateLocomotive.Text = "Создать локомотив";
+ buttonCreateLocomotive.UseVisualStyleBackColor = true;
+ buttonCreateLocomotive.Click += buttonCreateLocomotive_Click;
+ //
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToRightCorner" });
+ comboBoxStrategy.Location = new Point(730, 11);
+ comboBoxStrategy.Margin = new Padding(2);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(113, 23);
+ comboBoxStrategy.TabIndex = 12;
+ //
+ // buttonStep
+ //
+ buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
+ buttonStep.Font = new Font("Candara Light", 10F, FontStyle.Regular, GraphicsUnit.Point);
+ buttonStep.Location = new Point(741, 49);
+ buttonStep.Margin = new Padding(2);
+ buttonStep.Name = "buttonStep";
+ buttonStep.Size = new Size(88, 34);
+ buttonStep.TabIndex = 13;
+ buttonStep.Text = "Шаг";
+ buttonStep.UseVisualStyleBackColor = true;
+ buttonStep.Click += buttonStep_Click;
//
// ElectricLocomotive
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(854, 467);
+ Controls.Add(buttonStep);
+ Controls.Add(comboBoxStrategy);
+ Controls.Add(buttonCreateLocomotive);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonLeft);
- Controls.Add(buttonCreate);
+ Controls.Add(buttonCreateElectricLocomotive);
Controls.Add(pictureBoxElectroLocomotiv);
Name = "ElectricLocomotive";
Text = "ElectricLocomotive";
@@ -133,6 +176,9 @@
private Button buttonRight;
private Button buttonDown;
private Button buttonLeft;
- private Button buttonCreate;
+ private Button buttonCreateElectricLocomotive;
+ private Button buttonCreateLocomotive;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStep;
}
}
\ No newline at end of file
diff --git a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.cs
index 744b4dc..01fadb1 100644
--- a/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.cs
+++ b/ElectricLocomotive/ElectricLocomotive/ElectricLocomotive.cs
@@ -1,8 +1,18 @@
+using ElectricLocomotive.DrawningObject;
+using ElectricLocomotive.MovementStrategy;
+using ProjectElectricLocomotive;
+using ProjectElectricLocomotive.MovementStrategy;
+using System;
+
namespace ElectricLocomotive
{
public partial class ElectricLocomotive : Form
{
- private DrawningElectricLocomotive? _drawningElectricLocomotive;
+
+ private DrawningLocomotive? _drawningLocomotive;
+
+ private AbstractStrategy? _abstractStrategy;
+
public ElectricLocomotive()
{
InitializeComponent();
@@ -10,57 +20,100 @@ namespace ElectricLocomotive
private void Draw()
{
- if (_drawningElectricLocomotive == null)
+ if (_drawningLocomotive == null)
{
return;
}
-
Bitmap bmp = new(pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
Graphics gr = Graphics.FromImage(bmp);
- _drawningElectricLocomotive.DrawTransport(gr);
+ _drawningLocomotive.DrawTransport(gr);
pictureBoxElectroLocomotiv.Image = bmp;
-
}
- private void buttonCreate_Click(object sender, EventArgs e)
+ private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e)
{
- Random random = new();
- _drawningElectricLocomotive = new DrawningElectricLocomotive();
- _drawningElectricLocomotive.Init(random.Next(100, 300), random.Next(1000, 3000),
+ Random random = new Random();
+ _drawningLocomotive = new DrawningElectricLocomotive(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)), pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
- _drawningElectricLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
-
+ _drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
+
+ private void buttonCreateLocomotive_Click(object sender, EventArgs e)
+ {
+ Random rnd = new Random();
+ _drawningLocomotive = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 3000),
+ Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
+ Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
+ pictureBoxElectroLocomotiv.Width, pictureBoxElectroLocomotiv.Height);
+ _drawningLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
+ Draw();
+ }
+
private void buttonMove_Click(object sender, EventArgs e)
{
- if (_drawningElectricLocomotive == null)
+ if (_drawningLocomotive == null)
{
return;
}
-
string name = ((Button)sender)?.Name ?? string.Empty;
-
switch (name)
{
case "buttonUp":
- _drawningElectricLocomotive.MoveTransport(DirectionType.Up);
+ _drawningLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
- _drawningElectricLocomotive.MoveTransport(DirectionType.Down);
+ _drawningLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
- _drawningElectricLocomotive.MoveTransport(DirectionType.Left);
+ _drawningLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
- _drawningElectricLocomotive.MoveTransport(DirectionType.Right);
+ _drawningLocomotive.MoveTransport(DirectionType.Right);
break;
}
-
Draw();
}
+
+ private void buttonStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningLocomotive == null)
+ {
+ return;
+ }
+ if (comboBoxStrategy.Enabled)
+ {
+ _abstractStrategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToRightCorner(),
+ _ => null,
+ };
+ if (_abstractStrategy == null)
+ {
+ return;
+ }
+ _abstractStrategy.SetData(new
+ DrawningObjectLocomotive(_drawningLocomotive), pictureBoxElectroLocomotiv.Width,
+ pictureBoxElectroLocomotiv.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/ElectricLocomotive/ElectricLocomotive/EntityElectricLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/EntityElectricLocomotive.cs
index 779c84e..a340c0f 100644
--- a/ElectricLocomotive/ElectricLocomotive/EntityElectricLocomotive.cs
+++ b/ElectricLocomotive/ElectricLocomotive/EntityElectricLocomotive.cs
@@ -3,51 +3,18 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using ElectricLocomotive.Entities;
-namespace ElectricLocomotive
+namespace ElectricLocomotive.Entities
{
- public class EntityElectricLocomotive
+ public class EntityElectroLocomotive : EntityLocomotive
{
- ///
- /// Скорость
- ///
- 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 Horns { get; private set; }
///
/// Признак (опция) roga
///
-
- public double Step => (double)Speed * 100 / Weight; ///свойство с лямбда выражением
- ///
- /// Инициализация полей объекта-класса электролокоматива
- ///
- /// Скорость
- /// Вес
- /// Основной цвет
- /// Дополнительный цвет
- /// Признак наличия рогов
-
- public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns)
+ public EntityElectroLocomotive(int Speed, double weight, Color bodyColor, Color additionalColor, bool horns) : base(Speed,weight,bodyColor, additionalColor)
{
- Speed = speed;
- Weight = weight;
- BodyColor = bodyColor;
- AdditionalColor = additionalColor;
Horns = horns;
}
}
diff --git a/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs
new file mode 100644
index 0000000..f80e673
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/EntityLocomotive.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive.Entities
+{
+ public class EntityLocomotive
+
+ {
+ ///
+ /// Скорость
+ ///
+ 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 double Step => (double)Speed * 100 / Weight;
+ ///
+ /// Конструктор с параметрами
+ ///
+ /// Скорость
+ /// Вес автомобиля
+ /// Основной цвет
+ public EntityLocomotive(int speed, double weight, Color bodyColor, Color additionalColor)
+ {
+ Speed = speed;
+ Weight = weight;
+ BodyColor = bodyColor;
+ AdditionalColor = additionalColor;
+ }
+ }
+}
diff --git a/ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs b/ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
new file mode 100644
index 0000000..93d9d2c
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/IMoveableObject.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive.MovementStrategy
+{
+ public interface IMoveableObject
+ {
+ ObjectParameters? GetObjectPosition { get; }
+
+ int GetStep { get; }
+
+ bool CheckCanMove(DirectionType direction);
+ void MoveObject(DirectionType direction);
+ }
+}
diff --git a/ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs b/ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
new file mode 100644
index 0000000..88c78a0
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/MoveToCenter.cs
@@ -0,0 +1,57 @@
+using ElectricLocomotive.MovementStrategy;
+using ProjectElectricLocomotive.MovementStrategy;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive.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/ElectricLocomotive/ElectricLocomotive/MoveToRightCorner.cs b/ElectricLocomotive/ElectricLocomotive/MoveToRightCorner.cs
new file mode 100644
index 0000000..08b5261
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/MoveToRightCorner.cs
@@ -0,0 +1,30 @@
+using ProjectElectricLocomotive.MovementStrategy;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive.MovementStrategy
+{
+ public class MoveToRightCorner : AbstractStrategy
+ {
+ protected override bool IsTargetDestinaion()
+ {
+ var objParams = GetObjectParameters;
+ if (objParams == null) return false;
+
+ return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
+ }
+
+ protected override void MoveToTarget()
+ {
+ var objParams = GetObjectParameters;
+ if (objParams == null) return;
+
+ if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight();
+ if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown();
+
+ }
+ }
+}
diff --git a/ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs b/ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
new file mode 100644
index 0000000..64ce1e8
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/ObjectParameters.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive
+{
+ public class ObjectParameters
+ {
+ private readonly int _x;
+ private readonly int _y;
+ private readonly int _width;
+ private readonly int _height;
+ ///
+ /// Левая граница
+ ///
+ public int LeftBorder => _x;
+ ///
+ /// Верхняя граница
+ ///
+ public int TopBorder => _y;
+ ///
+ /// Правая граница
+ ///
+ public int RightBorder => _x + _width;
+ ///
+ /// Нижняя граница
+ ///
+ public int DownBorder => _y + _height;
+ ///
+ /// Середина объекта
+ ///
+ public int ObjectMiddleHorizontal => _x + _width / 2;
+ ///
+ /// Середина объекта
+ ///
+ public int ObjectMiddleVertical => _y + _height / 2;
+ ///
+ /// Конструктор
+ ///
+ /// Координата X
+ /// Координата Y
+ /// Ширина
+ /// Высота
+ public ObjectParameters(int x, int y, int width, int height)
+ {
+ _x = x;
+ _y = y;
+ _width = width;
+ _height = height;
+ }
+
+ }
+}
diff --git a/ElectricLocomotive/ElectricLocomotive/Status.cs b/ElectricLocomotive/ElectricLocomotive/Status.cs
new file mode 100644
index 0000000..3724795
--- /dev/null
+++ b/ElectricLocomotive/ElectricLocomotive/Status.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ElectricLocomotive.MovementStrategy
+{
+ public enum Status
+ {
+ NotInit,
+ InProgress,
+ Finish
+ }
+}