Лаба 2

This commit is contained in:
[USERNAME] 2023-11-16 23:54:30 +04:00
parent 0c0472e328
commit ddba502b60
14 changed files with 597 additions and 266 deletions

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bulldozer.DrawningObjects;
namespace Bulldozer.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _movebleObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_movebleObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(DirectionTypeBulldozer.Left);
protected bool MoveRight() => MoveTo(DirectionTypeBulldozer.Right);
protected bool MoveUp() => MoveTo(DirectionTypeBulldozer.Up);
protected bool MoveDown() => MoveTo(DirectionTypeBulldozer.Down);
protected ObjectParameters? GetObjectParametrs => _movebleObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _movebleObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestination();
private bool MoveTo(DirectionTypeBulldozer DirectionTypeBulldozer)
{
if (_state != Status.InProgress)
{
return false;
}
if (_movebleObject?.CheckCanMove(DirectionTypeBulldozer) ?? false)
{
_movebleObject.MoveObject(DirectionTypeBulldozer);
return true;
}
return false;
}
}
}

View File

@ -3,72 +3,45 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bulldozer.Entities;
namespace Bulldozer namespace Bulldozer.DrawningObjects
{ {
public class DrawningBulldozer public class DrawningBulldozer
{ {
/// <summary> public EntityBulldozer? EntityBulldozer { get; protected set; }
/// Класс-сущность
/// </summary>
public EntityBulldozer? EntityBulldozer { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth; private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight; private int _pictureHeight;
/// <summary> protected int _startPosX;
/// Левая координата прорисовки бульдозера protected int _startPosY;
/// </summary> protected readonly int _bulldozerWidth = 160;
private int _startPosX; protected readonly int _bulldozerHeight = 80;
/// <summary> public DrawningBulldozer(int speed, double weight, Color mainColor, int width, int heigth)
/// Верхняя кооридната прорисовки бульдозера
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки бульдозера
/// </summary>
private readonly int bulldozerWidth = 170;
/// <summary>
/// Высота прорисовки бульдозера
/// </summary>
private readonly int bulldozerHeight = 85;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bulldozerColor">Основной цвет</param>
/// <param name="cabinColor">Дополнительный цвет</param>
/// <param name="covshColor">Цвет для ковша</param>
/// <param name="hasMoldboardfront">Признак наличия переднего ковша</param>
/// <param name="hasRipper">Признак наличия заднего ковша</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена,
public bool Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, bool hasMoldboardfront, bool hasRipper, int width, int height)
{ {
if (width < _pictureWidth || height < _pictureHeight) if (width <= _bulldozerWidth || heigth <= _bulldozerHeight)
{ {
return false; return;
} }
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = heigth;
EntityBulldozer = new EntityBulldozer(); EntityBulldozer = new EntityBulldozer(speed, weight, mainColor);
EntityBulldozer.Init(speed, weight, bulldozerColor, cabinColor, covshColor, hasMoldboardfront, hasRipper); }
return true; protected DrawningBulldozer(int speed, double weight,
Color mainColor, int width, int heigth,
int bulldozerWidth, int bulldozerHeight)
{
if (width <= _bulldozerWidth || heigth <= _bulldozerHeight)
{
return;
}
_pictureHeight = heigth;
_pictureWidth = width;
_bulldozerHeight = bulldozerHeight;
_bulldozerWidth = bulldozerWidth;
EntityBulldozer = new EntityBulldozer(speed, weight, mainColor);
} }
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
if (x < 0 || y < 0 || x + bulldozerWidth > _pictureWidth || y + bulldozerHeight > _pictureHeight) if (x < 0 || y < 0 || x + _bulldozerWidth > _pictureWidth || y + _bulldozerHeight > _pictureHeight)
{ {
x = 10; x = 10;
y = 10; y = 10;
@ -76,84 +49,61 @@ namespace Bulldozer
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
/// <summary> public int GetPosX => _startPosX;
/// Изменение направления перемещения public int GetPosY => _startPosY;
/// </summary> public int GetWidth => _bulldozerWidth;
/// <param name="direction">Направление</param> public int GetHeight => _bulldozerHeight;
public void MoveTransport(DirectionTypeBulldozer direction) public bool CanMove(DirectionTypeBulldozer direction)
{ {
if (EntityBulldozer == null) if (EntityBulldozer == null)
{
return false;
}
return direction switch
{
DirectionTypeBulldozer.Left => _startPosX - EntityBulldozer.Step > 0,
DirectionTypeBulldozer.Up => _startPosY - EntityBulldozer.Step > 0,
DirectionTypeBulldozer.Right => _startPosX + EntityBulldozer.Step + _bulldozerWidth <= _pictureWidth,
DirectionTypeBulldozer.Down => _startPosY + EntityBulldozer.Step + _bulldozerHeight <= _pictureHeight,
_ => false,
};
}
public void MoveTransport(DirectionTypeBulldozer direction)
{
if (!CanMove(direction) || EntityBulldozer == null)
{ {
return; return;
} }
switch (direction) switch (direction)
{ {
//влево
case DirectionTypeBulldozer.Left: case DirectionTypeBulldozer.Left:
if (_startPosX - EntityBulldozer.Step > 0) _startPosX -= (int)EntityBulldozer.Step;
{
_startPosX -= (int)EntityBulldozer.Step;
}
if (_startPosX - EntityBulldozer.Step < 0)
{
_startPosX -= _startPosX - (int)EntityBulldozer.Step;
}
break; break;
//вверх
case DirectionTypeBulldozer.Up: case DirectionTypeBulldozer.Up:
if (_startPosY - EntityBulldozer.Step > 0) _startPosY -= (int)EntityBulldozer.Step;
{
_startPosY -= (int)EntityBulldozer.Step;
}
else if (_startPosY - EntityBulldozer.Step < 0)
{
_startPosY -= _startPosY - (int)EntityBulldozer.Step;
}
break; break;
// вправо
case DirectionTypeBulldozer.Right: case DirectionTypeBulldozer.Right:
if (_startPosX + EntityBulldozer.Step + bulldozerWidth < _pictureWidth) _startPosX += (int)EntityBulldozer.Step;
{
_startPosX += (int)EntityBulldozer.Step;
}
else if (_startPosX + EntityBulldozer.Step + bulldozerWidth > _pictureWidth)
{
_startPosX += _pictureWidth - _startPosX - bulldozerWidth;
}
break; break;
//вниз
case DirectionTypeBulldozer.Down: case DirectionTypeBulldozer.Down:
if (_startPosY + EntityBulldozer.Step + bulldozerHeight < _pictureHeight) _startPosY += (int)EntityBulldozer.Step;
{
_startPosY += (int)EntityBulldozer.Step;
}
else if (_startPosY + EntityBulldozer.Step + bulldozerHeight > _pictureHeight)
{
_startPosY += _pictureHeight - _startPosY - bulldozerHeight;
}
break; break;
} }
} }
/// <summary> public virtual void DrawTrasport(Graphics g)
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{ {
if (EntityBulldozer == null) if (EntityBulldozer == null)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityBulldozer.CabinColor); Brush mainBrush = new SolidBrush(EntityBulldozer.MainColor);
// Тело трактора // Тело трактора
Brush bulldozerColor = new SolidBrush(EntityBulldozer.BulldozerColor); Brush tractorColor = new SolidBrush(EntityBulldozer.MainColor);
g.FillRectangle(bulldozerColor, _startPosX + 25, _startPosY + 20, 110, 30); g.FillRectangle(tractorColor, _startPosX + 50, _startPosY + 20, 100, 30);
g.FillRectangle(bulldozerColor, _startPosX + 60, _startPosY, 10, 30); g.FillRectangle(tractorColor, _startPosX + 80, _startPosY, 10, 30);
//g.DrawEllipse(pen, _startPosX, _startPosY + 60, 90, 40);
int x = _startPosX + 30; // начальная позиция X int x = _startPosX + 50; // начальная позиция X
int y = _startPosY; // начальная позиция Y int y = _startPosY; // начальная позиция Y
int width = 110; // ширина прямоугольника int width = 110; // ширина прямоугольника
int height = 30; // высота прямоугольника int height = 30; // высота прямоугольника
@ -166,44 +116,15 @@ namespace Bulldozer
g.DrawArc(pen, x + width - radius * 2 - 5, y + height - radius * 2 + 50, radius * 2, radius * 2, 0, 90); // нижний правый угол g.DrawArc(pen, x + width - radius * 2 - 5, y + height - radius * 2 + 50, radius * 2, radius * 2, 0, 90); // нижний правый угол
g.DrawLine(pen, x + width - radius - 5, y + height + 50, x + radius - 5, y + height + 50); // нижняя горизонталь g.DrawLine(pen, x + width - radius - 5, y + height + 50, x + radius - 5, y + height + 50); // нижняя горизонталь
g.DrawArc(pen, x - 5, y + height - radius * 2 + 50, radius * 2, radius * 2, 90, 90); // нижний левый угол g.DrawArc(pen, x - 5, y + height - radius * 2 + 50, radius * 2, radius * 2, 90, 90); // нижний левый угол
int wheelRadius = 15; int wheelRadius = 15;
// Рисуем колеса трактора // Рисуем колеса трактора
g.DrawEllipse(pen, _startPosX + 30, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); g.DrawEllipse(pen, _startPosX + 50, _startPosY + 50, wheelRadius * 2, wheelRadius * 2);
g.FillEllipse(additionalBrush, _startPosX + 30, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); g.FillEllipse(mainBrush, _startPosX + 50, _startPosY + 50, wheelRadius * 2, wheelRadius * 2);
g.DrawEllipse(pen, _startPosX + 65, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); g.DrawEllipse(pen, _startPosX + 120, _startPosY + 50, wheelRadius * 2, wheelRadius * 2);
g.FillEllipse(additionalBrush, _startPosX + 65, _startPosY + 50, wheelRadius * 2, wheelRadius * 2); g.FillEllipse(mainBrush, _startPosX + 120, _startPosY + 50, wheelRadius * 2, wheelRadius * 2);
g.DrawEllipse(pen, _startPosX + 100, _startPosY + 50, wheelRadius * 2, wheelRadius * 2);
g.FillEllipse(additionalBrush, _startPosX + 100, _startPosY + 50, wheelRadius * 2, wheelRadius * 2);
// Кабина // Кабина
Brush cabinColor = new SolidBrush(EntityBulldozer.CabinColor); Brush cabinColor = new SolidBrush(EntityBulldozer.MainColor);
g.FillRectangle(cabinColor, _startPosX + 105, _startPosY, 30, 20); g.FillRectangle(cabinColor, _startPosX + 120, _startPosY, 30, 20);
// Рисуем ковш спереди
if (EntityBulldozer.HasMoldboardfront)
{
Point[] trianglePoints = new Point[]
{
new Point(_startPosX + 25, _startPosY + 30),
new Point(_startPosX + 25, _startPosY + 80),
new Point(_startPosX, _startPosY + 80),
};
g.DrawPolygon(pen, trianglePoints);
}
// Рисуем ковш сзади
if (EntityBulldozer.HasRipper)
{
Point[] trianglePoints2 = new Point[]
{
new Point(_startPosX + 130, _startPosY + 50),
new Point(_startPosX + 160, _startPosY + 50),
new Point(_startPosX + 160, _startPosY + 80)
};
g.DrawPolygon(pen, trianglePoints2);
}
} }
} }
} }

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bulldozer.Entities;
namespace Bulldozer.DrawningObjects
{
public class DrawningFastBulldozer : DrawningBulldozer
{
public DrawningFastBulldozer(int speed, double weight, Color mainColor, Color optionalColor, bool covsh, bool rearbucket, int width, int height) : base(speed, weight, mainColor, width, height, 200, 110)
{
if (EntityBulldozer != null)
{
EntityBulldozer = new EntityFastBulldozer(speed, weight, mainColor,
optionalColor, covsh, rearbucket);
}
}
public override void DrawTrasport(Graphics g)
{
if (EntityBulldozer is not EntityFastBulldozer fastBulldozer)
{
return;
}
Pen pen = new(Color.Black);
Brush optionalBrush = new SolidBrush(fastBulldozer.OptionalColor);
if (fastBulldozer.Covsh)
{
Point[] trianglePoints = new Point[]
{
new Point(_startPosX+50, _startPosY + 60),
new Point(_startPosX+50, _startPosY + 110),
new Point(_startPosX + 10, _startPosY + 110)
};
// Рисуем треугольник
g.DrawPolygon(pen, trianglePoints);
}
if (fastBulldozer.Rearbucket)
{
Point[] trianglePoints = new Point[]
{
new Point(_startPosX+150, _startPosY + 60),
new Point(_startPosX+200, _startPosY + 60),
new Point(_startPosX + 200, _startPosY + 110)
};
// Рисуем треугольник
g.DrawPolygon(pen, trianglePoints);
}
_startPosY += 30;
base.DrawTrasport(g);
_startPosY -= 30;
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bulldozer.DrawningObjects;
namespace Bulldozer.MovementStrategy
{
public class DrawningObjectBulldozer : IMoveableObject
{
private readonly DrawningBulldozer? _drawningBulldozer = null;
public DrawningObjectBulldozer(DrawningBulldozer drawningBulldozer)
{
_drawningBulldozer = drawningBulldozer;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningBulldozer == null || _drawningBulldozer.EntityBulldozer == null)
{
return null;
}
return new ObjectParameters(_drawningBulldozer.GetPosX,
_drawningBulldozer.GetPosY, _drawningBulldozer.GetWidth,
_drawningBulldozer.GetHeight);
}
}
public int GetStep => (int)(_drawningBulldozer?.EntityBulldozer?.Step ?? 0);
public bool CheckCanMove(DirectionTypeBulldozer direction) => _drawningBulldozer?.CanMove(direction) ?? false;
public void MoveObject(DirectionTypeBulldozer direction) => _drawningBulldozer?.MoveTransport(direction);
}
}

View File

@ -4,58 +4,19 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Bulldozer namespace Bulldozer.Entities
{ {
public class EntityBulldozer public class EntityBulldozer
{ {
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; } public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; } public double Weight { get; private set; }
/// <summary> public Color MainColor { get; private set; }
/// Основной цвет
/// </summary>
public Color BulldozerColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color CabinColor { get; private set; }
public Color CovshColor { get; private set; }
/// <summary>
/// Признак (опция) наличия переднего ковша
/// </summary>
public bool HasMoldboardfront { get; private set; }
/// <summary>
/// Признак (опция) наличия заднего ковша
/// </summary>
public bool HasRipper { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight; public double Step => (double)Speed * 100 / Weight;
/// <summary> public EntityBulldozer(int speed, double weight, Color mainColor)
/// Инициализация полей объекта-класса спортивного автомобиля
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bulldozerColor">Основной цвет</param>
/// <param name="cabinColor">Дополнительный цвет</param>
/// <param name="covshColor">Цвет для ковша</param>
/// <param name="hasMoldboardfront">Признак наличия переднего ковша</param>
/// <param name="hasRipper">Признак наличия заднего ковша</param>
public void Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, bool hasMoldboardfront, bool hasRipper)
{ {
Speed = speed; Speed = speed;
Weight = weight; Weight = weight;
BulldozerColor = bulldozerColor; MainColor = mainColor;
CabinColor = cabinColor;
CovshColor = covshColor;
HasMoldboardfront = hasMoldboardfront;
HasRipper = hasRipper;
} }
} }
} }

View File

@ -0,0 +1,22 @@
using Bulldozer.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldozer.Entities
{
public class EntityFastBulldozer : EntityBulldozer
{
public Color OptionalColor { get; private set; }
public bool Covsh { get; private set; }
public bool Rearbucket { get; private set; }
public EntityFastBulldozer(int speed, double weight, Color mainColor, Color optionalColor, bool covsh, bool rearbucket) : base(speed, weight, mainColor)
{
OptionalColor = optionalColor;
Covsh = covsh;
Rearbucket = rearbucket;
}
}
}

View File

@ -1,6 +1,6 @@
namespace Bulldozer namespace Bulldozer
{ {
partial class FormBulldozer partial class FastBulldozer
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
@ -28,53 +28,55 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBoxBulldozer = new PictureBox(); pictureBoxFastBulldozer = new PictureBox();
ButtonCreateBulldozer = new Button(); buttonCreateBulldozer = new Button();
buttonLeft = new Button(); buttonCreateFastBulldozer = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button();
buttonDown = new Button(); buttonDown = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).BeginInit(); buttonLeft = new Button();
buttonUp = new Button();
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxFastBulldozer).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// pictureBoxBulldozer // pictureBoxFastBulldozer
// //
pictureBoxBulldozer.Dock = DockStyle.Fill; pictureBoxFastBulldozer.Dock = DockStyle.Fill;
pictureBoxBulldozer.Location = new Point(0, 0); pictureBoxFastBulldozer.Location = new Point(0, 0);
pictureBoxBulldozer.Name = "pictureBoxBulldozer"; pictureBoxFastBulldozer.Name = "pictureBoxFastBulldozer";
pictureBoxBulldozer.Size = new Size(800, 450); pictureBoxFastBulldozer.Size = new Size(884, 461);
pictureBoxBulldozer.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxFastBulldozer.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxBulldozer.TabIndex = 0; pictureBoxFastBulldozer.TabIndex = 0;
pictureBoxBulldozer.TabStop = false; pictureBoxFastBulldozer.TabStop = false;
// //
// ButtonCreateBulldozer // buttonCreateBulldozer
// //
ButtonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateBulldozer.Location = new Point(54, 396); buttonCreateBulldozer.Location = new Point(12, 426);
ButtonCreateBulldozer.Name = "ButtonCreateBulldozer"; buttonCreateBulldozer.Name = "buttonCreateBulldozer";
ButtonCreateBulldozer.Size = new Size(75, 23); buttonCreateBulldozer.Size = new Size(119, 23);
ButtonCreateBulldozer.TabIndex = 1; buttonCreateBulldozer.TabIndex = 1;
ButtonCreateBulldozer.Text = "Создать"; buttonCreateBulldozer.Text = "Создать Трактор";
ButtonCreateBulldozer.UseVisualStyleBackColor = true; buttonCreateBulldozer.UseVisualStyleBackColor = true;
ButtonCreateBulldozer.Click += ButtonCreateBulldozer_Click; buttonCreateBulldozer.Click += ButtonCreateBulldozer_Click;
// //
// buttonLeft // buttonCreateFastBulldozer
// //
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonCreateFastBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; buttonCreateFastBulldozer.Location = new Point(137, 426);
buttonLeft.Location = new Point(660, 373); buttonCreateFastBulldozer.Name = "buttonCreateFastBulldozer";
buttonLeft.Name = "buttonLeft"; buttonCreateFastBulldozer.Size = new Size(162, 23);
buttonLeft.Size = new Size(30, 30); buttonCreateFastBulldozer.TabIndex = 2;
buttonLeft.TabIndex = 2; buttonCreateFastBulldozer.Text = "Создать быстрый трактор";
buttonLeft.Text = "<"; buttonCreateFastBulldozer.UseVisualStyleBackColor = true;
buttonLeft.UseVisualStyleBackColor = true; buttonCreateFastBulldozer.Click += ButtonCreateFastBulldozer_Click;
buttonLeft.Click += ButtonMove_Click;
// //
// buttonRight // buttonRight
// //
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom; buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(732, 373); buttonRight.Location = new Point(842, 419);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30); buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 3; buttonRight.TabIndex = 3;
@ -82,55 +84,95 @@
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click; buttonRight.Click += ButtonMove_Click;
// //
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(696, 342);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 4;
buttonUp.Text = "^";
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonDown // buttonDown
// //
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom; buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(696, 403); buttonDown.Location = new Point(806, 419);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30); buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 5; buttonDown.TabIndex = 4;
buttonDown.Text = "v"; buttonDown.Text = "v";
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click; buttonDown.Click += ButtonMove_Click;
// //
// FormBulldozer // buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(770, 419);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 5;
buttonLeft.Text = "<";
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(806, 383);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 6;
buttonUp.Text = "^";
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "Move to center", "Move to border" });
comboBoxStrategy.Location = new Point(751, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
//
// buttonStep
//
buttonStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonStep.Location = new Point(797, 41);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(75, 23);
buttonStep.TabIndex = 8;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += Buttonstep_Click;
//
// FastBulldozer
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(884, 461);
Controls.Add(buttonDown); Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(ButtonCreateBulldozer); Controls.Add(buttonDown);
Controls.Add(pictureBoxBulldozer); Controls.Add(buttonRight);
Name = "FormBulldozer"; Controls.Add(buttonCreateFastBulldozer);
Text = "Бульдозер"; Controls.Add(buttonCreateBulldozer);
((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).EndInit(); Controls.Add(pictureBoxFastBulldozer);
Name = "FastBulldozer";
StartPosition = FormStartPosition.CenterScreen;
Text = "FastBulldozer";
((System.ComponentModel.ISupportInitialize)pictureBoxFastBulldozer).EndInit();
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
#endregion #endregion
private PictureBox pictureBoxBulldozer; private PictureBox pictureBoxFastBulldozer;
private Button ButtonCreateBulldozer; private Button buttonCreateBulldozer;
private Button buttonLeft; private Button buttonCreateFastBulldozer;
private Button buttonRight; private Button buttonRight;
private Button buttonUp;
private Button buttonDown; private Button buttonDown;
private Button buttonLeft;
private Button buttonUp;
private ComboBox comboBoxStrategy;
private Button buttonStep;
} }
} }

View File

@ -1,11 +1,13 @@
using System.Windows.Forms; using Bulldozer.DrawningObjects;
using Bulldozer.MovementStrategy;
namespace Bulldozer namespace Bulldozer
{ {
public partial class FormBulldozer : Form public partial class FastBulldozer : Form
{ {
private DrawningBulldozer? _drawningBulldozer; private DrawningBulldozer? _drawningBulldozer;
public FormBulldozer() private AbstractStrategy? _abstractStrategy;
public FastBulldozer()
{ {
InitializeComponent(); InitializeComponent();
} }
@ -15,11 +17,37 @@ namespace Bulldozer
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxBulldozer.Width, Bitmap bmp = new(pictureBoxFastBulldozer.Width,
pictureBoxBulldozer.Height); pictureBoxFastBulldozer.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningBulldozer.DrawTransport(gr); _drawningBulldozer.DrawTrasport(gr);
pictureBoxBulldozer.Image = bmp; pictureBoxFastBulldozer.Image = bmp;
}
private void ButtonCreateFastBulldozer_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningBulldozer = new DrawningFastBulldozer(
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)),
pictureBoxFastBulldozer.Width,
pictureBoxFastBulldozer.Height);
_drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonCreateBulldozer_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawningBulldozer = new DrawningBulldozer(
random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxFastBulldozer.Width,
pictureBoxFastBulldozer.Height);
_drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
@ -45,19 +73,42 @@ namespace Bulldozer
} }
Draw(); Draw();
} }
private void Buttonstep_Click(object sender, EventArgs e)
private void ButtonCreateBulldozer_Click(object sender, EventArgs e)
{ {
Random random = new(); if (_drawningBulldozer == null)
_drawningBulldozer = new DrawningBulldozer(); {
_drawningBulldozer.Init(random.Next(100, 300), return;
random.Next(1000, 3000), }
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), if (comboBoxStrategy.Enabled)
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)), _abstractStrategy = comboBoxStrategy.SelectedIndex
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); switch
_drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); {
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(
new DrawningObjectBulldozer(_drawningBulldozer),
pictureBoxFastBulldozer.Width,
pictureBoxFastBulldozer.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw(); Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
} }
} }
} }

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bulldozer.DrawningObjects;
namespace Bulldozer.MovementStrategy
{
public interface IMoveableObject
{
/// <summary>
/// получение координаты
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// шаг
/// </summary>
int GetStep { get; }
/// <summary>
/// проверка можно ли инди в этом направлении
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionTypeBulldozer direction);
/// <summary>
/// изменение напрвления перемещения
/// </summary>
/// <param name="direction"></param>
void MoveObject(DirectionTypeBulldozer direction);
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldozer.MovementStrategy
{
internal class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestination()
{
var objParams = GetObjectParametrs;
if (objParams == null)
{
return false;
}
return objParams.RightBorder <= FieldWidth &&
objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder <= FieldHeight &&
objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParametrs;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
MoveRight();
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldozer.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
var objParams = GetObjectParametrs;
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 = GetObjectParametrs;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

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

@ -11,7 +11,7 @@ namespace Bulldozer
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormBulldozer()); Application.Run(new FastBulldozer());
} }
} }
} }

View File

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