PIbd-21 Bakalskaya E.D. LabWork02 BASE #2

Closed
ekallin wants to merge 7 commits from LabWork02 into LabWork01
13 changed files with 657 additions and 230 deletions

View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectElectricLocomotive.MovementStrategy
{
/// <summary>
/// Абстрактный класс стратегии
/// </summary>
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;
}
}
}

View File

@ -1,88 +1,25 @@
using System;
using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
namespace ProjectElectricLocomotive.DrawingObjects
{
public class DrawingElectricLocomotive
public class DrawingElectricLocomotive : DrawingLocomotive
{
public EntityElectricLocomotive? EntityElectricLocomotive;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _locoWidth = 150;
private readonly int _locoHeight = 50;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, bool seifbatteries,
int width, int height)
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
bool horns, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 85, 50)
{
if (width < _locoWidth || height < _locoHeight)
if (EntityLocomotive != null)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityElectricLocomotive = new EntityElectricLocomotive();
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, seifbatteries);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _locoWidth > _pictureWidth)
{
x = 20;
}
if (y < 0 || y + _locoHeight > _pictureHeight)
{
y = 20;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityElectricLocomotive == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityElectricLocomotive.Step > 0)
{
_startPosX -= (int)EntityElectricLocomotive.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntityElectricLocomotive.Step > 0)
{
_startPosY -= (int)EntityElectricLocomotive.Step;
}
break;
case DirectionType.Right:
if (_startPosX + EntityElectricLocomotive.Step + _locoWidth < _pictureWidth)
{
_startPosX += (int)EntityElectricLocomotive.Step;
}
break;
case DirectionType.Down:
if (_startPosY + EntityElectricLocomotive.Step + _locoHeight < _pictureHeight)
{
_startPosY += (int)EntityElectricLocomotive.Step;
}
break;
EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries);
}
}
public void DrawTransport(Graphics g)
public override void DrawTransport(Graphics g)
{
if (EntityElectricLocomotive == null)
if (EntityLocomotive is not EntityElectricLocomotive electricLocomotive)
{
return;
}
@ -90,10 +27,10 @@ namespace ElectricLocomotive
Pen pen = new(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);
Brush windows = new SolidBrush(Color.LightBlue);
Brush bodyColor = new SolidBrush(EntityElectricLocomotive.BodyColor);
Brush additionalBrush = new SolidBrush(EntityElectricLocomotive.AdditionalColor);
Brush bodyColor = new SolidBrush(electricLocomotive.BodyColor);
Brush additionalBrush = new SolidBrush(electricLocomotive.AdditionalColor);
if (EntityElectricLocomotive.Horns)
if (electricLocomotive.Horns)
{
//horns
g.FillRectangle(blackBrush, _startPosX + 30, _startPosY + 15, 20, 5);
@ -103,115 +40,11 @@ namespace ElectricLocomotive
g.DrawLine(pen, _startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY);
}
//локомотив
g.FillPolygon(bodyColor, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 30),
new Point(_startPosX +80, _startPosY + 40),
new Point(_startPosX +75, _startPosY + 45),
new Point(_startPosX +5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
if (EntityElectricLocomotive.Seifbatteries)
if (electricLocomotive.SeifBatteries)
{
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 40, _startPosY + 25),
new Point(_startPosX + 65, _startPosY + 25),
new Point(_startPosX + 65, _startPosY + 35),
new Point(_startPosX + 40, _startPosY + 35),
new Point(_startPosX + 40, _startPosY + 25),
}
);
g.FillRectangle(blackBrush, _startPosX + 80, _startPosY + 30, 5, 10);
}
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 30),
new Point(_startPosX +80, _startPosY + 40),
new Point(_startPosX +75, _startPosY + 45),
new Point(_startPosX +5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
//окошки
g.FillPolygon(windows, new Point[]
{
new Point(_startPosX + 10, _startPosY + 30),
new Point(_startPosX +15, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX +10, _startPosY + 30),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 10, _startPosY + 30),
new Point(_startPosX +15, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX +10, _startPosY + 30),
}
);
g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5);
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5);
//обязательные колеса
//loco
g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 5, 5);
//telega
g.FillEllipse(blackBrush, _startPosX + 95, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 140, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 130, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 105, _startPosY + 45, 5, 5);
//telejka
g.FillPolygon(additionalBrush, new Point[]
{
new Point(_startPosX + 90, _startPosY + 25),
new Point(_startPosX + 95, _startPosY + 20),
new Point(_startPosX + 145, _startPosY + 20),
new Point(_startPosX + 150, _startPosY + 25),
new Point(_startPosX + 150, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 25),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 90, _startPosY + 25),
new Point(_startPosX + 95, _startPosY + 20),
new Point(_startPosX + 145, _startPosY + 20),
new Point(_startPosX + 150, _startPosY + 25),
new Point(_startPosX + 150, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 25),
}
);
//телега окна
g.DrawLine(pen, _startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40);
g.FillRectangle(windows, _startPosX + 95, _startPosY + 30, 10, 5);
g.FillRectangle(windows, _startPosX + 115, _startPosY + 30, 10, 5);
g.FillRectangle(windows, _startPosX + 135, _startPosY + 30, 10, 5);
base.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,198 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectricLocomotive;
using ProjectElectricLocomotive.Entities;
using ProjectElectricLocomotive.Properties;
namespace ProjectElectricLocomotive.DrawingObjects
{
public class DrawingLocomotive
{
public EntityLocomotive? EntityLocomotive { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _locoWidth = 85;
protected readonly int _locoHeight = 50;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _locoWidth;
public int GetHeight => _locoHeight;
public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
{
if (width < _locoWidth || heigth < _locoHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = heigth;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
protected DrawingLocomotive(int speed, double weight, Color bodyColor, 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);
}
//Установка позиции
public void SetPosition(int x, int y)
{
if (x < 0 || x + _locoWidth > _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(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);
Brush windows = new SolidBrush(Color.LightBlue);
Brush bodyColor = new SolidBrush(EntityLocomotive.BodyColor);
//локомотив
g.FillPolygon(bodyColor, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 30),
new Point(_startPosX +80, _startPosY + 40),
new Point(_startPosX +75, _startPosY + 45),
new Point(_startPosX +5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 30),
new Point(_startPosX +80, _startPosY + 40),
new Point(_startPosX +75, _startPosY + 45),
new Point(_startPosX +5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
//окошки
g.FillPolygon(windows, new Point[]
{
new Point(_startPosX + 10, _startPosY + 30),
new Point(_startPosX +15, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX +10, _startPosY + 30),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 10, _startPosY + 30),
new Point(_startPosX +15, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX +10, _startPosY + 30),
}
);
g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5);
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5);
//обязательные колеса
//loco
g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 5, 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,
};
}
}
}

View File

@ -0,0 +1,35 @@
using ElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.MovementStrategy
{
public class DrawingObjectLocomotive : IMoveableObject
{
private readonly DrawingLocomotive? _drawningLocomotive = null;
public DrawingObjectLocomotive(DrawingLocomotive 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);
}
}

View File

@ -1,29 +1,23 @@
using System;
using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
namespace ProjectElectricLocomotive.Entities
{
public class EntityElectricLocomotive
public class EntityElectricLocomotive : 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; }
public bool Seifbatteries { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, bool seifbatteries)
public bool SeifBatteries { get; private set; }
public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool horns,
bool seifBatteries) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Horns = horns;
Seifbatteries = seifbatteries;
SeifBatteries = seifBatteries;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Entities
{
public class EntityLocomotive
{
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 EntityLocomotive(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -29,11 +29,14 @@
private void InitializeComponent()
{
this.pictureBoxElectricLocomotive = new System.Windows.Forms.PictureBox();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonCreateElectricLocomotive = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.buttonCreateLocomotive = new System.Windows.Forms.Button();
this.buttonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxElectricLocomotive)).BeginInit();
this.SuspendLayout();
//
@ -42,29 +45,29 @@
this.pictureBoxElectricLocomotive.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxElectricLocomotive.Location = new System.Drawing.Point(0, 0);
this.pictureBoxElectricLocomotive.Name = "pictureBoxElectricLocomotive";
this.pictureBoxElectricLocomotive.Size = new System.Drawing.Size(533, 465);
this.pictureBoxElectricLocomotive.Size = new System.Drawing.Size(1242, 512);
this.pictureBoxElectricLocomotive.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxElectricLocomotive.TabIndex = 0;
this.pictureBoxElectricLocomotive.TabStop = false;
//
// buttonCreate
// buttonCreateElectricLocomotive
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreate.Location = new System.Drawing.Point(16, 395);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(111, 52);
this.buttonCreate.TabIndex = 1;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
this.buttonCreateElectricLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateElectricLocomotive.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreateElectricLocomotive.Location = new System.Drawing.Point(16, 442);
this.buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
this.buttonCreateElectricLocomotive.Size = new System.Drawing.Size(272, 52);
this.buttonCreateElectricLocomotive.TabIndex = 1;
this.buttonCreateElectricLocomotive.Text = "Создать электролокомотив";
this.buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
this.buttonCreateElectricLocomotive.Click += new System.EventHandler(this.buttonCreateElectricLocomotive_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(389, 417);
this.buttonLeft.Location = new System.Drawing.Point(1098, 464);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 2;
@ -76,7 +79,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowUP;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(435, 368);
this.buttonUp.Location = new System.Drawing.Point(1144, 415);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 3;
@ -88,7 +91,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(479, 417);
this.buttonRight.Location = new System.Drawing.Point(1188, 464);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 4;
@ -100,23 +103,63 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(435, 417);
this.buttonDown.Location = new System.Drawing.Point(1144, 464);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 5;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
//
// comboBoxStrategy
//
this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"MoveToCenter",
"MoveToRightCorner"});
this.comboBoxStrategy.Location = new System.Drawing.Point(1058, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(160, 33);
this.comboBoxStrategy.TabIndex = 6;
//
// buttonCreateLocomotive
//
this.buttonCreateLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateLocomotive.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreateLocomotive.Location = new System.Drawing.Point(333, 442);
this.buttonCreateLocomotive.Name = "buttonCreateLocomotive";
this.buttonCreateLocomotive.Size = new System.Drawing.Size(272, 52);
this.buttonCreateLocomotive.TabIndex = 7;
this.buttonCreateLocomotive.Text = "Создать локомотив";
this.buttonCreateLocomotive.UseVisualStyleBackColor = true;
this.buttonCreateLocomotive.Click += new System.EventHandler(this.buttonCreateLocomotive_Click);
//
// buttonStep
//
this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonStep.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonStep.Location = new System.Drawing.Point(1079, 65);
this.buttonStep.Name = "buttonStep";
this.buttonStep.Size = new System.Drawing.Size(125, 56);
this.buttonStep.TabIndex = 8;
this.buttonStep.Text = "Шаг";
this.buttonStep.UseVisualStyleBackColor = true;
this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click);
//
// ElectricLocomotive
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(533, 465);
this.ClientSize = new System.Drawing.Size(1242, 512);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.buttonCreateLocomotive);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.buttonCreateElectricLocomotive);
this.Controls.Add(this.pictureBoxElectricLocomotive);
this.Name = "ElectricLocomotive";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
@ -130,10 +173,13 @@
#endregion
private PictureBox pictureBoxElectricLocomotive;
private Button buttonCreate;
private Button buttonCreateElectricLocomotive;
private Button buttonLeft;
private Button buttonUp;
private Button buttonRight;
private Button buttonDown;
private ComboBox comboBoxStrategy;
private Button buttonCreateLocomotive;
private Button buttonStep;
}
}

View File

@ -1,8 +1,17 @@
using ProjectElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.MovementStrategy;
using System;
namespace ElectricLocomotive
{
public partial class ElectricLocomotive : Form
{
private DrawingElectricLocomotive? _drawingElectricLocomotive;
private DrawingLocomotive? _drawingLocomotive;
private AbstractStrategy? _abstractStrategy;
public ElectricLocomotive()
{
InitializeComponent();
@ -10,32 +19,41 @@ namespace ElectricLocomotive
private void Draw()
{
if (_drawingElectricLocomotive == null)
if (_drawingLocomotive == null)
{
return;
}
Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingElectricLocomotive.DrawTransport(gr);
_drawingLocomotive.DrawTransport(gr);
pictureBoxElectricLocomotive.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawingLocomotive = new DrawingElectricLocomotive(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)),
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
_drawingLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateLocomotive_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_drawingElectricLocomotive = new DrawingElectricLocomotive();
_drawingElectricLocomotive.Init(rnd.Next(100, 300), rnd.Next(1000, 3000),
_drawingLocomotive = new DrawingLocomotive(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)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
_drawingElectricLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
_drawingLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingElectricLocomotive == null)
if (_drawingLocomotive == null)
{
return;
}
@ -43,19 +61,55 @@ namespace ElectricLocomotive
switch (name)
{
case "buttonUp":
_drawingElectricLocomotive.MoveTransport(DirectionType.Up);
_drawingLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingElectricLocomotive.MoveTransport(DirectionType.Down);
_drawingLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingElectricLocomotive.MoveTransport(DirectionType.Left);
_drawingLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingElectricLocomotive.MoveTransport(DirectionType.Right);
_drawingLocomotive.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingLocomotive == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToRightCorner(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectLocomotive(_drawingLocomotive), pictureBoxElectricLocomotive.Width,
pictureBoxElectricLocomotive.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
namespace ProjectElectricLocomotive.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.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();
}
}
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.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();
}
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.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;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}