Лаба 2 #2

Closed
Stepan wants to merge 1 commits from Lab2 into Lab1
14 changed files with 597 additions and 266 deletions
Showing only changes of commit ddba502b60 - Show all commits

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

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.Threading.Tasks;
namespace Bulldozer
namespace Bulldozer.Entities
{
public class EntityBulldozer
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </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 Color MainColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
/// <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>
public void Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, bool hasMoldboardfront, bool hasRipper)
public EntityBulldozer(int speed, double weight, Color mainColor)
{
Speed = speed;
Weight = weight;
BulldozerColor = bulldozerColor;
CabinColor = cabinColor;
CovshColor = covshColor;
HasMoldboardfront = hasMoldboardfront;
HasRipper = hasRipper;
MainColor = mainColor;
}
}
}

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
{
partial class FormBulldozer
partial class FastBulldozer
{
/// <summary>
/// Required designer variable.
@ -28,53 +28,55 @@
/// </summary>
private void InitializeComponent()
{
pictureBoxBulldozer = new PictureBox();
ButtonCreateBulldozer = new Button();
buttonLeft = new Button();
pictureBoxFastBulldozer = new PictureBox();
buttonCreateBulldozer = new Button();
buttonCreateFastBulldozer = new Button();
buttonRight = new Button();
buttonUp = 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();
//
// pictureBoxBulldozer
// pictureBoxFastBulldozer
//
pictureBoxBulldozer.Dock = DockStyle.Fill;
pictureBoxBulldozer.Location = new Point(0, 0);
pictureBoxBulldozer.Name = "pictureBoxBulldozer";
pictureBoxBulldozer.Size = new Size(800, 450);
pictureBoxBulldozer.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxBulldozer.TabIndex = 0;
pictureBoxBulldozer.TabStop = false;
pictureBoxFastBulldozer.Dock = DockStyle.Fill;
pictureBoxFastBulldozer.Location = new Point(0, 0);
pictureBoxFastBulldozer.Name = "pictureBoxFastBulldozer";
pictureBoxFastBulldozer.Size = new Size(884, 461);
pictureBoxFastBulldozer.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxFastBulldozer.TabIndex = 0;
pictureBoxFastBulldozer.TabStop = false;
//
// ButtonCreateBulldozer
// buttonCreateBulldozer
//
ButtonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateBulldozer.Location = new Point(54, 396);
ButtonCreateBulldozer.Name = "ButtonCreateBulldozer";
ButtonCreateBulldozer.Size = new Size(75, 23);
ButtonCreateBulldozer.TabIndex = 1;
ButtonCreateBulldozer.Text = "Создать";
ButtonCreateBulldozer.UseVisualStyleBackColor = true;
ButtonCreateBulldozer.Click += ButtonCreateBulldozer_Click;
buttonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateBulldozer.Location = new Point(12, 426);
buttonCreateBulldozer.Name = "buttonCreateBulldozer";
buttonCreateBulldozer.Size = new Size(119, 23);
buttonCreateBulldozer.TabIndex = 1;
buttonCreateBulldozer.Text = "Создать Трактор";
buttonCreateBulldozer.UseVisualStyleBackColor = true;
buttonCreateBulldozer.Click += ButtonCreateBulldozer_Click;
//
// buttonLeft
// buttonCreateFastBulldozer
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(660, 373);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 2;
buttonLeft.Text = "<";
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
buttonCreateFastBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateFastBulldozer.Location = new Point(137, 426);
buttonCreateFastBulldozer.Name = "buttonCreateFastBulldozer";
buttonCreateFastBulldozer.Size = new Size(162, 23);
buttonCreateFastBulldozer.TabIndex = 2;
buttonCreateFastBulldozer.Text = "Создать быстрый трактор";
buttonCreateFastBulldozer.UseVisualStyleBackColor = true;
buttonCreateFastBulldozer.Click += ButtonCreateFastBulldozer_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(732, 373);
buttonRight.Location = new Point(842, 419);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 3;
@ -82,55 +84,95 @@
buttonRight.UseVisualStyleBackColor = true;
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.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(696, 403);
buttonDown.Location = new Point(806, 419);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 5;
buttonDown.TabIndex = 4;
buttonDown.Text = "v";
buttonDown.UseVisualStyleBackColor = true;
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);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonDown);
ClientSize = new Size(884, 461);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(ButtonCreateBulldozer);
Controls.Add(pictureBoxBulldozer);
Name = "FormBulldozer";
Text = "Бульдозер";
((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).EndInit();
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonCreateFastBulldozer);
Controls.Add(buttonCreateBulldozer);
Controls.Add(pictureBoxFastBulldozer);
Name = "FastBulldozer";
StartPosition = FormStartPosition.CenterScreen;
Text = "FastBulldozer";
((System.ComponentModel.ISupportInitialize)pictureBoxFastBulldozer).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private PictureBox pictureBoxBulldozer;
private Button ButtonCreateBulldozer;
private Button buttonLeft;
private PictureBox pictureBoxFastBulldozer;
private Button buttonCreateBulldozer;
private Button buttonCreateFastBulldozer;
private Button buttonRight;
private Button buttonUp;
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
{
public partial class FormBulldozer : Form
public partial class FastBulldozer : Form
{
private DrawningBulldozer? _drawningBulldozer;
public FormBulldozer()
private AbstractStrategy? _abstractStrategy;
public FastBulldozer()
{
InitializeComponent();
}
@ -15,11 +17,37 @@ namespace Bulldozer
{
return;
}
Bitmap bmp = new(pictureBoxBulldozer.Width,
pictureBoxBulldozer.Height);
Bitmap bmp = new(pictureBoxFastBulldozer.Width,
pictureBoxFastBulldozer.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningBulldozer.DrawTransport(gr);
pictureBoxBulldozer.Image = bmp;
_drawningBulldozer.DrawTrasport(gr);
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)
{
@ -45,19 +73,42 @@ namespace Bulldozer
}
Draw();
}
private void ButtonCreateBulldozer_Click(object sender, EventArgs e)
private void Buttonstep_Click(object sender, EventArgs e)
{
Random random = new();
_drawningBulldozer = new DrawningBulldozer();
_drawningBulldozer.Init(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)),
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)), pictureBoxBulldozer.Width, pictureBoxBulldozer.Height);
_drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100));
if (_drawningBulldozer == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
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();
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,
// see https://aka.ms/applicationconfiguration.
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
}
}