From 761cfdbdcc676bb0f40c345fe39410ab3f2412ec Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Sat, 16 Sep 2023 22:51:47 +0400 Subject: [PATCH 1/9] Lab_2_Kryukov_AI_Excavator --- .../DrawningExcavarorBodyKits.cs | 62 +++++++++++++++ .../ProjectExcavator/DrawningExcavator.cs | 76 +++++++++---------- .../ProjectExcavator/EntityExcavator.cs | 31 ++------ .../EntityExcavatorBodyKits.cs | 47 ++++++++++++ 4 files changed, 151 insertions(+), 65 deletions(-) create mode 100644 ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs create mode 100644 ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs new file mode 100644 index 0000000..7271fda --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectExcavator.Entities; + +namespace ProjectExcavator.DrawningObjects +{ + public class DrawningExcavarorBodyKits : DrawningExcavator + { + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия антикрыла + /// Ширина картинки + /// Высота картинки + public DrawningExcavarorBodyKits(int speed, double weight, + Color bodyColor, Color additionalColor, + bool bodyKit, bool bucket, + int width, int height) : + base(speed, weight, bodyColor, width, height, 110, 60) + { + if(EntityExcavator != null) + { + EntityExcavator = new EntityExcavatorBodyKits(speed, weight, + bodyColor, additionalColor, bodyKit, bucket); + } + } + public override void DrawTransport(Graphics g) + { + if(EntityExcavator is not EntityExcavatorBodyKits excavator) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(excavator.AdditionalColor); + //ыспомогательные опоры + if (excavator.BodyKit) + { + + } + base.DrawTransport(g); + //ковш + if (excavator.Bucket) + { + Point[] pointsBacket = { + new Point(_startPosX + 150, _startPosY + 75), + new Point(_startPosX + 150, _startPosY + 135), + new Point(_startPosX + 195, _startPosY + 135), + }; + g.FillPolygon(additionalBrush, pointsBacket); + g.DrawPolygon(pen, pointsBacket); + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index 6a25801..93f9f08 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -1,17 +1,18 @@ -using System; +using ProjectExcavator.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectExcavator +namespace ProjectExcavator.DrawningObjects { - internal class DrawningExcavator + public class DrawningExcavator { /// /// Класс-сущность /// - public EntityExcavator? EntityExcavator { get; private set; } + public EntityExcavator? EntityExcavator { get; protected set; } /// /// Ширина окна /// @@ -31,32 +32,48 @@ namespace ProjectExcavator /// /// Ширина прорисовки автомобиля /// - private readonly int _excavatorWidth = 190; + private readonly int _excavatorWidth = 100; /// /// Высота прорисовки автомобиля /// - private readonly int _excavatorHeight = 170; + private readonly int _excavatorHeight = 55; /// - /// Инициализация свойств + /// Конструктор /// /// Скорость /// Вес - /// Цвет кузова - /// Дополнительный цвет - /// Признак наличия вспомогательных опор - /// Признак наличия ковша + /// Основной цвет /// Ширина картинки /// Высота картинки - /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool bodyKit, bool backet, int width, int height) + public DrawningExcavator(int speed, double weight, Color bodyColor, int + width, int height) { // TODO: Продумать проверки _pictureWidth = width; _pictureHeight = height; - EntityExcavator = new EntityExcavator(); - EntityExcavator.Init(speed, weight, bodyColor, additionalColor, bodyKit, backet); - return true; + EntityExcavator = new EntityExcavator(speed, weight, bodyColor); + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningExcavator(int speed, double weight, + Color bodyColor, + int width, int height, + int excavatorWidth, int excavatorHeight) + { + // TODO: Продумать проверки + _pictureWidth = width; + _pictureHeight = height; + _excavatorWidth = excavatorWidth; + _excavatorHeight = excavatorHeight; + EntityExcavator = new EntityExcavator(speed, weight, bodyColor); } /// /// Установка позиции @@ -68,8 +85,6 @@ namespace ProjectExcavator // TODO: Изменение x, y _startPosX = x; _startPosY = y; - - } /// /// Изменение направления перемещения @@ -92,7 +107,7 @@ namespace ProjectExcavator break; //вверх case DirectionType.Up: - if (_startPosY - EntityExcavator.Step > -50) + if (_startPosY - EntityExcavator.Step > 0) { _startPosY -= (int)EntityExcavator.Step; } @@ -117,20 +132,13 @@ namespace ProjectExcavator /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (EntityExcavator == null) { return; } Pen pen = new(Color.Black); - - Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor); - if (EntityExcavator.BodyKit) - { - g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 120, 130, 10); - g.DrawLine(pen, _startPosX + 20, _startPosY + 120, _startPosX + 150, _startPosY + 120); - } //корпус Brush bodyBrush = new SolidBrush(Color.Red); g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 110, 130, 20); @@ -156,17 +164,7 @@ namespace ProjectExcavator g.DrawEllipse(pen, _startPosX + 80, _startPosY + 130, 20, 20); g.DrawEllipse(pen, _startPosX + 100, _startPosY + 130, 20, 20); g.DrawEllipse(pen, _startPosX + 120, _startPosY + 130, 20, 20); - //ковш - if (EntityExcavator.Backet) - { - Point[] pointsBacket = { - new Point(_startPosX + 150, _startPosY + 75), - new Point(_startPosX + 150, _startPosY + 135), - new Point(_startPosX + 195, _startPosY + 135), - }; - g.FillPolygon(additionalBrush, pointsBacket); - g.DrawPolygon(pen, pointsBacket); - } + } } } diff --git a/ProjectExcavator/ProjectExcavator/EntityExcavator.cs b/ProjectExcavator/ProjectExcavator/EntityExcavator.cs index 6189140..a6fa8de 100644 --- a/ProjectExcavator/ProjectExcavator/EntityExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/EntityExcavator.cs @@ -4,11 +4,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectExcavator +namespace ProjectExcavator.Entities { - internal class EntityExcavator + public class EntityExcavator { - // + /// /// Скорость /// public int Speed { get; private set; } @@ -20,42 +20,21 @@ namespace ProjectExcavator /// Основной цвет /// public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } - /// - /// Признак (опция) наличия вспомогательных опор - /// - public bool BodyKit { get; private set; } - /// - /// Признак (опция) наличия ковша - /// - public bool Backet { get; private set; } - /// /// Шаг перемещения автомобиля /// public double Step => (double)Speed * 100 / Weight; /// - /// Инициализация полей объекта-класса спортивного автомобиля + /// Конструктор с параметрами /// /// Скорость /// Вес автомобиля /// Основной цвет - /// Дополнительный цвет - /// Признак наличия обвеса - /// Признак наличия антикрыла - - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool bodyKit, bool backet) + public EntityExcavator(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; - BodyKit = bodyKit; - Backet = backet; } } } diff --git a/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs b/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs new file mode 100644 index 0000000..a93f8ba --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator.Entities +{ + /// + /// Класс-сущность "Спортивный автомобиль" + /// + public class EntityExcavatorBodyKits : EntityExcavator + { + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor { get; private set; } + /// + /// признак наличия вспомогательных опор + /// + public bool BodyKit { get; private set; } + /// + /// Признак (опция) наличия ковша + /// + public bool Bucket { get; private set; } + /// + /// Инициализация полей объекта-класса экскаватора + /// + /// Скорость + /// Вес экскаватора + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия ковша + public EntityExcavatorBodyKits(int speed, double weight, + Color bodyColor, Color additionalColor, + bool bodyKit, bool bucket) + { + + AdditionalColor = additionalColor; + BodyKit = bodyKit; + Bucket = bucket; + } + + } +} -- 2.25.1 From ce251e6792778ce04416e8a25eac81e7f470370c Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Sat, 16 Sep 2023 23:46:58 +0400 Subject: [PATCH 2/9] Lab_2_Kryukov_AI_Excavator --- ProjectExcavator/ProjectExcavator/DrawningExcavator.cs | 8 ++++---- .../ProjectExcavator/EntityExcavatorBodyKits.cs | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index 93f9f08..e5e90d3 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -24,19 +24,19 @@ namespace ProjectExcavator.DrawningObjects /// /// Левая координата прорисовки автомобиля /// - private int _startPosX; + protected int _startPosX; /// /// Верхняя кооридната прорисовки автомобиля /// - private int _startPosY; + protected int _startPosY; /// /// Ширина прорисовки автомобиля /// - private readonly int _excavatorWidth = 100; + protected readonly int _excavatorWidth = 100; /// /// Высота прорисовки автомобиля /// - private readonly int _excavatorHeight = 55; + protected readonly int _excavatorHeight = 55; /// /// Конструктор /// diff --git a/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs b/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs index a93f8ba..d9f7403 100644 --- a/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs +++ b/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs @@ -33,11 +33,13 @@ namespace ProjectExcavator.Entities /// Дополнительный цвет /// Признак наличия обвеса /// Признак наличия ковша - public EntityExcavatorBodyKits(int speed, double weight, - Color bodyColor, Color additionalColor, - bool bodyKit, bool bucket) + public EntityExcavatorBodyKits(int speed, + double weight, + Color bodyColor, + Color additionalColor, + bool bodyKit, + bool bucket) { - AdditionalColor = additionalColor; BodyKit = bodyKit; Bucket = bucket; -- 2.25.1 From c3f9cee1af8e7a8360512455993a49c905ec5b10 Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Wed, 20 Sep 2023 21:30:15 +0400 Subject: [PATCH 3/9] Lab_2_Kryukov_AI_Excavator --- .../ProjectExcavator/AbstractStrategy.cs | 134 ++++++++++++++++++ .../DrawningExcavarorBodyKits.cs | 3 +- .../ProjectExcavator/DrawningExcavator.cs | 65 ++++++--- .../DrawningObjectExcavator.cs | 40 ++++++ .../EntityExcavatorBodyKits.cs | 4 +- .../ExcavatorForm.Designer.cs | 45 +++++- .../ProjectExcavator/ExcavatorForm.cs | 67 ++++++++- .../ProjectExcavator/IMoveableObject.cs | 34 +++++ .../ProjectExcavator/MoveToBorder.cs | 49 +++++++ .../ProjectExcavator/MoveToCenter.cs | 57 ++++++++ .../ProjectExcavator/ObjectParameters.cs | 54 +++++++ ProjectExcavator/ProjectExcavator/Status.cs | 15 ++ 12 files changed, 536 insertions(+), 31 deletions(-) create mode 100644 ProjectExcavator/ProjectExcavator/AbstractStrategy.cs create mode 100644 ProjectExcavator/ProjectExcavator/DrawningObjectExcavator.cs create mode 100644 ProjectExcavator/ProjectExcavator/IMoveableObject.cs create mode 100644 ProjectExcavator/ProjectExcavator/MoveToBorder.cs create mode 100644 ProjectExcavator/ProjectExcavator/MoveToCenter.cs create mode 100644 ProjectExcavator/ProjectExcavator/ObjectParameters.cs create mode 100644 ProjectExcavator/ProjectExcavator/Status.cs diff --git a/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs b/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs new file mode 100644 index 0000000..f33ed01 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/AbstractStrategy.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace ProjectExcavator.MovementStrategy +{ + /// + /// Класс-стратегия перемещения объекта + /// + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private Status _state = Status.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public Status GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestination()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + 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 IsTargetDestination(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + + } +} diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs index 7271fda..79f4a78 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs @@ -43,8 +43,9 @@ namespace ProjectExcavator.DrawningObjects //ыспомогательные опоры if (excavator.BodyKit) { - + g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 100, 110, 10); } + //отрисовка базы экскаватора base.DrawTransport(g); //ковш if (excavator.Bucket) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index e5e90d3..76870f1 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -32,11 +32,26 @@ namespace ProjectExcavator.DrawningObjects /// /// Ширина прорисовки автомобиля /// - protected readonly int _excavatorWidth = 100; + protected readonly int _excavatorWidth = 150; /// /// Высота прорисовки автомобиля /// - protected readonly int _excavatorHeight = 55; + protected readonly int _excavatorHeight = 155; + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// Ширина объекта + /// + public int GetWidth => _excavatorWidth; + /// + /// Высота объекта + /// + public int GetHeight => _excavatorHeight; /// /// Конструктор /// @@ -87,12 +102,36 @@ namespace ProjectExcavator.DrawningObjects _startPosY = y; } /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if(EntityExcavator == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityExcavator.Step >0, + //вверх + DirectionType.Up => _startPosY - EntityExcavator.Step > -50, + //вправо + DirectionType.Right => _startPosX + EntityExcavator.Step < _pictureWidth - _excavatorWidth, + //вниз + DirectionType.Down => _startPosY + EntityExcavator.Step < _pictureHeight - _excavatorHeight, + _ => false, + }; + } + /// /// Изменение направления перемещения /// /// Направление public void MoveTransport(DirectionType direction) { - if (EntityExcavator == null) + if (!CanMove(direction) || EntityExcavator == null) { return; } @@ -100,31 +139,19 @@ namespace ProjectExcavator.DrawningObjects { //влево case DirectionType.Left: - if (_startPosX - EntityExcavator.Step > 0) - { - _startPosX -= (int)EntityExcavator.Step; - } + _startPosX -= (int)EntityExcavator.Step; break; //вверх case DirectionType.Up: - if (_startPosY - EntityExcavator.Step > 0) - { - _startPosY -= (int)EntityExcavator.Step; - } + _startPosY -= (int)EntityExcavator.Step; break; // вправо case DirectionType.Right: - if (_startPosX + EntityExcavator.Step < _pictureWidth - _excavatorWidth) - { - _startPosX += (int)EntityExcavator.Step; - } + _startPosX += (int)EntityExcavator.Step; break; //вниз case DirectionType.Down: - if (_startPosY + EntityExcavator.Step < _pictureHeight - _excavatorHeight) - { - _startPosY += (int)EntityExcavator.Step; - } + _startPosY += (int)EntityExcavator.Step; break; } } diff --git a/ProjectExcavator/ProjectExcavator/DrawningObjectExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningObjectExcavator.cs new file mode 100644 index 0000000..5465b87 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/DrawningObjectExcavator.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectExcavator.DrawningObjects; + +namespace ProjectExcavator.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter) + /// + public class DrawningObjectExcavator : IMoveableObject + { + private readonly DrawningExcavator? _drawningExcavator = null; + public DrawningObjectExcavator(DrawningExcavator drawningExcavator) + { + _drawningExcavator = drawningExcavator; + } + public ObjectParameters? GetObjectPosition + { + get + { + if(_drawningExcavator == null || _drawningExcavator.EntityExcavator == null) + { + return null; + } + return new ObjectParameters(_drawningExcavator.GetPosX, + _drawningExcavator.GetPosY, + _drawningExcavator.GetWidth, + _drawningExcavator.GetHeight); + } + } + public int GetStep => (int)(_drawningExcavator?.EntityExcavator?.Step ?? 0); + + public bool CheckCanMove(DirectionType direction) =>_drawningExcavator?.CanMove(direction) ?? false; + + public void MoveObject(DirectionType direction) => _drawningExcavator?.MoveTransport(direction); + } +} diff --git a/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs b/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs index d9f7403..2994917 100644 --- a/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs +++ b/ProjectExcavator/ProjectExcavator/EntityExcavatorBodyKits.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; +using ProjectExcavator.Entities; + namespace ProjectExcavator.Entities { @@ -38,7 +40,7 @@ namespace ProjectExcavator.Entities Color bodyColor, Color additionalColor, bool bodyKit, - bool bucket) + bool bucket) : base(speed, weight, bodyColor) { AdditionalColor = additionalColor; BodyKit = bodyKit; diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs b/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs index e7b1f13..39c5858 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs @@ -34,6 +34,9 @@ buttonUp = new Button(); buttonLeft = new Button(); buttonRight = new Button(); + comboBoxStrategy = new ComboBox(); + buttonCreateExcavatorBodyKits = new Button(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit(); SuspendLayout(); // @@ -50,11 +53,11 @@ // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 415); + buttonCreate.Location = new Point(12, 397); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); + buttonCreate.Size = new Size(155, 41); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать простой экскаватор"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreate_Click; // @@ -106,11 +109,44 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); + comboBoxStrategy.Location = new Point(667, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 6; + // + // buttonCreateExcavatorBodyKits + // + buttonCreateExcavatorBodyKits.Location = new Point(173, 397); + buttonCreateExcavatorBodyKits.Name = "buttonCreateExcavatorBodyKits"; + buttonCreateExcavatorBodyKits.Size = new Size(145, 41); + buttonCreateExcavatorBodyKits.TabIndex = 7; + buttonCreateExcavatorBodyKits.Text = "Создать Экскаватор с обвесами"; + buttonCreateExcavatorBodyKits.UseVisualStyleBackColor = true; + buttonCreateExcavatorBodyKits.Click += buttonCreateExcavatorBodyKits_Click; + // + // buttonStep + // + buttonStep.Location = new Point(713, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // // ExcavatorForm // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonStep); + Controls.Add(buttonCreateExcavatorBodyKits); + Controls.Add(comboBoxStrategy); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonUp); @@ -133,5 +169,8 @@ private Button buttonUp; private Button buttonLeft; private Button buttonRight; + private ComboBox comboBoxStrategy; + private Button buttonCreateExcavatorBodyKits; + private Button buttonStep; } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorForm.cs b/ProjectExcavator/ProjectExcavator/ExcavatorForm.cs index 6b7455f..c8a16f1 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorForm.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorForm.cs @@ -1,3 +1,6 @@ +using ProjectExcavator.DrawningObjects; +using ProjectExcavator.MovementStrategy; + namespace ProjectExcavator { public partial class ExcavatorForm : Form @@ -7,6 +10,10 @@ namespace ProjectExcavator /// private DrawningExcavator? _drawnigExcavator; /// + /// + /// + private AbstractStrategy? _abstractStrategy; + /// /// /// public ExcavatorForm() @@ -41,17 +48,12 @@ namespace ProjectExcavator private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawnigExcavator = new DrawningExcavator(); - _drawnigExcavator.Init(random.Next(100, 300), + _drawnigExcavator = new DrawningExcavator( + 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)), pictureBoxExcavator.Width, pictureBoxExcavator.Height); - _drawnigExcavator.SetPosition(random.Next(10, 100), - random.Next(10, 100)); Draw(); } private void buttonMove_Click(object sender, EventArgs e) @@ -78,5 +80,56 @@ namespace ProjectExcavator } Draw(); } + + private void buttonCreateExcavatorBodyKits_Click(object sender, EventArgs e) + { + Random random = new(); + _drawnigExcavator = new DrawningExcavarorBodyKits(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)), + pictureBoxExcavator.Width, + pictureBoxExcavator.Height); + _drawnigExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawnigExcavator == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawningObjectExcavator(_drawnigExcavator), + pictureBoxExcavator.Width, + pictureBoxExcavator.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/ProjectExcavator/ProjectExcavator/IMoveableObject.cs b/ProjectExcavator/ProjectExcavator/IMoveableObject.cs new file mode 100644 index 0000000..fc48d19 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/IMoveableObject.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/ProjectExcavator/ProjectExcavator/MoveToBorder.cs b/ProjectExcavator/ProjectExcavator/MoveToBorder.cs new file mode 100644 index 0000000..7fbe879 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MoveToBorder.cs @@ -0,0 +1,49 @@ +using ProjectExcavator.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParameters; + if(objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth && objParams.DownBorder + + GetStep() <= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if(objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth - 150; + var diffY = objParams.DownBorder - FieldHeight - 150; + if(diffX >= 0) + { + MoveDown(); + } + else if (diffY >= 0) + { + MoveRight(); + } + else if(Math.Abs(diffX) > Math.Abs(diffY)) + { + MoveRight(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/MoveToCenter.cs b/ProjectExcavator/ProjectExcavator/MoveToCenter.cs new file mode 100644 index 0000000..8c32d3f --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestination() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/ObjectParameters.cs b/ProjectExcavator/ProjectExcavator/ObjectParameters.cs new file mode 100644 index 0000000..c78c2e1 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/ObjectParameters.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator.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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/ProjectExcavator/ProjectExcavator/Status.cs b/ProjectExcavator/ProjectExcavator/Status.cs new file mode 100644 index 0000000..8f61c75 --- /dev/null +++ b/ProjectExcavator/ProjectExcavator/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectExcavator +{ + public enum Status + { + NotInit = 0, + InProgress = 1, + Finish = 2 + } +} -- 2.25.1 From c42834e908f0b160ee65921f7a361964bbc9174d Mon Sep 17 00:00:00 2001 From: SooNooClose Date: Tue, 26 Sep 2023 13:50:29 +0400 Subject: [PATCH 4/9] Lab_2_Kryukov_AI_Excavator --- .../DrawningExcavarorBodyKits.cs | 11 ++-- .../ProjectExcavator/DrawningExcavator.cs | 56 ++++++++++--------- .../ExcavatorForm.Designer.cs | 50 ++++++++++------- .../ProjectExcavator/MoveToCenter.cs | 4 +- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs index 79f4a78..5e47dad 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavarorBodyKits.cs @@ -24,7 +24,7 @@ namespace ProjectExcavator.DrawningObjects Color bodyColor, Color additionalColor, bool bodyKit, bool bucket, int width, int height) : - base(speed, weight, bodyColor, width, height, 110, 60) + base(speed, weight, bodyColor, width, height, 170, 100) { if(EntityExcavator != null) { @@ -43,7 +43,8 @@ namespace ProjectExcavator.DrawningObjects //ыспомогательные опоры if (excavator.BodyKit) { - g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 100, 110, 10); + g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 50, 110, 10); + g.DrawRectangle(pen, _startPosX + 20, _startPosY + 50, 110, 10); } //отрисовка базы экскаватора base.DrawTransport(g); @@ -51,9 +52,9 @@ namespace ProjectExcavator.DrawningObjects if (excavator.Bucket) { Point[] pointsBacket = { - new Point(_startPosX + 150, _startPosY + 75), - new Point(_startPosX + 150, _startPosY + 135), - new Point(_startPosX + 195, _startPosY + 135), + new Point(_startPosX + 150, _startPosY + 25), + new Point(_startPosX + 150, _startPosY + 85), + new Point(_startPosX + 195, _startPosY + 85), }; g.FillPolygon(additionalBrush, pointsBacket); g.DrawPolygon(pen, pointsBacket); diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index 76870f1..f5c74f8 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -32,11 +32,11 @@ namespace ProjectExcavator.DrawningObjects /// /// Ширина прорисовки автомобиля /// - protected readonly int _excavatorWidth = 150; + protected readonly int _excavatorWidth = 135; /// /// Высота прорисовки автомобиля /// - protected readonly int _excavatorHeight = 155; + protected readonly int _excavatorHeight = 80; /// /// Координата X объекта /// @@ -83,11 +83,14 @@ namespace ProjectExcavator.DrawningObjects int width, int height, int excavatorWidth, int excavatorHeight) { - // TODO: Продумать проверки _pictureWidth = width; _pictureHeight = height; _excavatorWidth = excavatorWidth; _excavatorHeight = excavatorHeight; + if (_pictureHeight < _excavatorHeight || _pictureWidth < _excavatorWidth) + { + return; + } EntityExcavator = new EntityExcavator(speed, weight, bodyColor); } /// @@ -97,9 +100,8 @@ namespace ProjectExcavator.DrawningObjects /// Координата Y public void SetPosition(int x, int y) { - // TODO: Изменение x, y - _startPosX = x; - _startPosY = y; + _startPosX = Math.Min(x,_pictureWidth - _excavatorWidth); + _startPosY = Math.Min(y,_pictureHeight - _excavatorHeight); } /// /// Проверка, что объект может переместится по указанному направлению @@ -117,7 +119,7 @@ namespace ProjectExcavator.DrawningObjects //влево DirectionType.Left => _startPosX - EntityExcavator.Step >0, //вверх - DirectionType.Up => _startPosY - EntityExcavator.Step > -50, + DirectionType.Up => _startPosY - EntityExcavator.Step > 0, //вправо DirectionType.Right => _startPosX + EntityExcavator.Step < _pictureWidth - _excavatorWidth, //вниз @@ -168,29 +170,29 @@ namespace ProjectExcavator.DrawningObjects Pen pen = new(Color.Black); //корпус Brush bodyBrush = new SolidBrush(Color.Red); - g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 110, 130, 20); - g.FillRectangle(bodyBrush, _startPosX + 100, _startPosY + 70, 30, 40); - g.FillRectangle(bodyBrush, _startPosX + 30, _startPosY + 90, 10, 20); - g.DrawRectangle(pen, _startPosX + 20, _startPosY + 110, 130, 20); - g.DrawRectangle(pen, _startPosX + 100, _startPosY + 70, 30, 40); - g.DrawRectangle(pen, _startPosX + 30, _startPosY + 90, 10, 20); + g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 60, 130, 20); + g.FillRectangle(bodyBrush, _startPosX + 100, _startPosY + 20, 30, 40); + g.FillRectangle(bodyBrush, _startPosX + 30, _startPosY + 40, 10, 20); + g.DrawRectangle(pen, _startPosX + 20, _startPosY + 60, 130, 20); + g.DrawRectangle(pen, _startPosX + 100, _startPosY + 20, 30, 40); + g.DrawRectangle(pen, _startPosX + 30, _startPosY + 40, 10, 20); //гусеница Point[] points = { - new Point(_startPosX + 20, _startPosY + 130), - new Point(_startPosX + 15, _startPosY+ 135), - new Point(_startPosX + 15, _startPosY + 145), - new Point(_startPosX + 20, _startPosY + 150), - new Point(_startPosX + 145, _startPosY + 150), - new Point(_startPosX + 150, _startPosY + 145), - new Point(_startPosX + 150, _startPosY + 135), - new Point(_startPosX + 145, _startPosY + 130)}; + new Point(_startPosX + 20, _startPosY + 80), + new Point(_startPosX + 15, _startPosY+ 85), + new Point(_startPosX + 15, _startPosY + 95), + new Point(_startPosX + 20, _startPosY + 100), + new Point(_startPosX + 145, _startPosY + 100), + new Point(_startPosX + 150, _startPosY + 95), + new Point(_startPosX + 150, _startPosY + 85), + new Point(_startPosX + 145, _startPosY + 80)}; g.DrawPolygon(pen, points); - g.DrawEllipse(pen, _startPosX + 20, _startPosY + 130, 20, 20); - g.DrawEllipse(pen, _startPosX + 40, _startPosY + 130, 20, 20); - g.DrawEllipse(pen, _startPosX + 60, _startPosY + 130, 20, 20); - g.DrawEllipse(pen, _startPosX + 80, _startPosY + 130, 20, 20); - g.DrawEllipse(pen, _startPosX + 100, _startPosY + 130, 20, 20); - g.DrawEllipse(pen, _startPosX + 120, _startPosY + 130, 20, 20); + g.DrawEllipse(pen, _startPosX + 20, _startPosY + 80, 20, 20); + g.DrawEllipse(pen, _startPosX + 40, _startPosY + 80, 20, 20); + g.DrawEllipse(pen, _startPosX + 60, _startPosY + 80, 20, 20); + g.DrawEllipse(pen, _startPosX + 80, _startPosY + 80, 20, 20); + g.DrawEllipse(pen, _startPosX + 100, _startPosY + 80, 20, 20); + g.DrawEllipse(pen, _startPosX + 120, _startPosY + 80, 20, 20); } } diff --git a/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs b/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs index 39c5858..6352b5c 100644 --- a/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs +++ b/ProjectExcavator/ProjectExcavator/ExcavatorForm.Designer.cs @@ -44,8 +44,9 @@ // pictureBoxExcavator.Dock = DockStyle.Fill; pictureBoxExcavator.Location = new Point(0, 0); + pictureBoxExcavator.Margin = new Padding(3, 4, 3, 4); pictureBoxExcavator.Name = "pictureBoxExcavator"; - pictureBoxExcavator.Size = new Size(800, 450); + pictureBoxExcavator.Size = new Size(914, 600); pictureBoxExcavator.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxExcavator.TabIndex = 0; pictureBoxExcavator.TabStop = false; @@ -53,9 +54,10 @@ // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 397); + buttonCreate.Location = new Point(14, 529); + buttonCreate.Margin = new Padding(3, 4, 3, 4); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(155, 41); + buttonCreate.Size = new Size(177, 55); buttonCreate.TabIndex = 1; buttonCreate.Text = "Создать простой экскаватор"; buttonCreate.UseVisualStyleBackColor = true; @@ -66,9 +68,10 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.down; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(722, 415); + buttonDown.Location = new Point(825, 553); + buttonDown.Margin = new Padding(3, 4, 3, 4); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); + buttonDown.Size = new Size(34, 40); buttonDown.TabIndex = 2; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += buttonMove_Click; @@ -78,9 +81,10 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.up; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(722, 379); + buttonUp.Location = new Point(825, 505); + buttonUp.Margin = new Padding(3, 4, 3, 4); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); + buttonUp.Size = new Size(34, 40); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += buttonMove_Click; @@ -90,9 +94,10 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.left; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(686, 415); + buttonLeft.Location = new Point(784, 553); + buttonLeft.Margin = new Padding(3, 4, 3, 4); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); + buttonLeft.Size = new Size(34, 40); buttonLeft.TabIndex = 4; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += buttonMove_Click; @@ -102,9 +107,10 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.right; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(758, 415); + buttonRight.Location = new Point(866, 553); + buttonRight.Margin = new Padding(3, 4, 3, 4); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); + buttonRight.Size = new Size(34, 40); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; @@ -113,17 +119,19 @@ // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; - comboBoxStrategy.Items.AddRange(new object[] { "0", "1" }); - comboBoxStrategy.Location = new Point(667, 12); + comboBoxStrategy.Items.AddRange(new object[] { "Двигаться в центр", "Двигаться к краю экрана" }); + comboBoxStrategy.Location = new Point(762, 16); + comboBoxStrategy.Margin = new Padding(3, 4, 3, 4); comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.Size = new Size(138, 28); comboBoxStrategy.TabIndex = 6; // // buttonCreateExcavatorBodyKits // - buttonCreateExcavatorBodyKits.Location = new Point(173, 397); + buttonCreateExcavatorBodyKits.Location = new Point(198, 529); + buttonCreateExcavatorBodyKits.Margin = new Padding(3, 4, 3, 4); buttonCreateExcavatorBodyKits.Name = "buttonCreateExcavatorBodyKits"; - buttonCreateExcavatorBodyKits.Size = new Size(145, 41); + buttonCreateExcavatorBodyKits.Size = new Size(166, 55); buttonCreateExcavatorBodyKits.TabIndex = 7; buttonCreateExcavatorBodyKits.Text = "Создать Экскаватор с обвесами"; buttonCreateExcavatorBodyKits.UseVisualStyleBackColor = true; @@ -131,9 +139,10 @@ // // buttonStep // - buttonStep.Location = new Point(713, 41); + buttonStep.Location = new Point(815, 55); + buttonStep.Margin = new Padding(3, 4, 3, 4); buttonStep.Name = "buttonStep"; - buttonStep.Size = new Size(75, 23); + buttonStep.Size = new Size(86, 31); buttonStep.TabIndex = 8; buttonStep.Text = "Шаг"; buttonStep.UseVisualStyleBackColor = true; @@ -141,9 +150,9 @@ // // ExcavatorForm // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(914, 600); Controls.Add(buttonStep); Controls.Add(buttonCreateExcavatorBodyKits); Controls.Add(comboBoxStrategy); @@ -153,6 +162,7 @@ Controls.Add(buttonDown); Controls.Add(buttonCreate); Controls.Add(pictureBoxExcavator); + Margin = new Padding(3, 4, 3, 4); Name = "ExcavatorForm"; Text = "Excavator"; Click += buttonMove_Click; diff --git a/ProjectExcavator/ProjectExcavator/MoveToCenter.cs b/ProjectExcavator/ProjectExcavator/MoveToCenter.cs index 8c32d3f..92af0d0 100644 --- a/ProjectExcavator/ProjectExcavator/MoveToCenter.cs +++ b/ProjectExcavator/ProjectExcavator/MoveToCenter.cs @@ -28,7 +28,7 @@ namespace ProjectExcavator.MovementStrategy { return; } - var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2 ; if (Math.Abs(diffX) > GetStep()) { if (diffX > 0) @@ -40,7 +40,7 @@ namespace ProjectExcavator.MovementStrategy MoveRight(); } } - var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2 ; if (Math.Abs(diffY) > GetStep()) { if (diffY > 0) -- 2.25.1 From e8437e26f61d2b1c12a67ddddb5cd9544c961d25 Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Tue, 10 Oct 2023 23:02:29 +0400 Subject: [PATCH 5/9] Lab_2_Kryukov_AI_Excavator --- ProjectExcavator/ProjectExcavator/DrawningExcavator.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index f5c74f8..40e8ec3 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -63,7 +63,10 @@ namespace ProjectExcavator.DrawningObjects public DrawningExcavator(int speed, double weight, Color bodyColor, int width, int height) { - // TODO: Продумать проверки + if (_pictureHeight < _excavatorHeight || _pictureWidth < _excavatorWidth) + { + return; + } _pictureWidth = width; _pictureHeight = height; EntityExcavator = new EntityExcavator(speed, weight, bodyColor); -- 2.25.1 From 7b6f4285e5a304073060446fb7cf01b5efac504c Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Tue, 10 Oct 2023 23:07:56 +0400 Subject: [PATCH 6/9] Lab_2_Kryukov_AI_Excavator --- ProjectExcavator/ProjectExcavator/DrawningExcavator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index 40e8ec3..d2b4947 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -172,7 +172,7 @@ namespace ProjectExcavator.DrawningObjects } Pen pen = new(Color.Black); //корпус - Brush bodyBrush = new SolidBrush(Color.Red); + Brush bodyBrush = new SolidBrush(EntityExcavator.BodyColor); g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 60, 130, 20); g.FillRectangle(bodyBrush, _startPosX + 100, _startPosY + 20, 30, 40); g.FillRectangle(bodyBrush, _startPosX + 30, _startPosY + 40, 10, 20); -- 2.25.1 From 27ec0eb38f0d7530d3180103e1d0d706f9b896c5 Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Tue, 10 Oct 2023 23:19:19 +0400 Subject: [PATCH 7/9] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectExcavator/DrawningExcavator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index d2b4947..1e4298e 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -22,19 +22,19 @@ namespace ProjectExcavator.DrawningObjects /// private int _pictureHeight; /// - /// Левая координата прорисовки автомобиля + /// Левая координата прорисовки экскаватора /// protected int _startPosX; /// - /// Верхняя кооридната прорисовки автомобиля + /// Верхняя кооридната прорисовки экскаватора /// protected int _startPosY; /// - /// Ширина прорисовки автомобиля + /// Ширина прорисовки экскаватора /// protected readonly int _excavatorWidth = 135; /// - /// Высота прорисовки автомобиля + /// Высота прорисовки экскаватора /// protected readonly int _excavatorHeight = 80; /// @@ -79,8 +79,8 @@ namespace ProjectExcavator.DrawningObjects /// Основной цвет /// Ширина картинки /// Высота картинки - /// Ширина прорисовки автомобиля - /// Высота прорисовки автомобиля + /// Ширина прорисовки экскаватора + /// Высота прорисовки экскаватора protected DrawningExcavator(int speed, double weight, Color bodyColor, int width, int height, -- 2.25.1 From cd4daffc3ed93aee45edf963eb8800f7ea8700bd Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Tue, 10 Oct 2023 23:28:58 +0400 Subject: [PATCH 8/9] =?UTF-8?q?=D0=BF=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectExcavator/ProjectExcavator/EntityExcavator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ProjectExcavator/ProjectExcavator/EntityExcavator.cs b/ProjectExcavator/ProjectExcavator/EntityExcavator.cs index a6fa8de..4a0a157 100644 --- a/ProjectExcavator/ProjectExcavator/EntityExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/EntityExcavator.cs @@ -21,14 +21,14 @@ namespace ProjectExcavator.Entities /// public Color BodyColor { get; private set; } /// - /// Шаг перемещения автомобиля + /// Шаг перемещения экскаватора /// public double Step => (double)Speed * 100 / Weight; /// /// Конструктор с параметрами /// /// Скорость - /// Вес автомобиля + /// Вес экскаватора /// Основной цвет public EntityExcavator(int speed, double weight, Color bodyColor) { -- 2.25.1 From e96e7debf3a398d78a942082c7cd35d98963b033 Mon Sep 17 00:00:00 2001 From: 1SooNoo1 Date: Sun, 22 Oct 2023 15:51:47 +0400 Subject: [PATCH 9/9] =?UTF-8?q?=D0=BF=D0=BE=D1=84=D0=B8=D0=BA=D1=81=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=BD=D1=84=D0=BB=D0=B8=D0=BA=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectExcavator/DrawningExcavator.cs | 95 ++++++++++++++----- .../ProjectExcavator/EntityExcavator.cs | 6 +- 2 files changed, 71 insertions(+), 30 deletions(-) diff --git a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs index 96bc3fc..7dd58fa 100644 --- a/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/DrawningExcavator.cs @@ -38,6 +38,43 @@ namespace ProjectExcavator.DrawningObjects /// protected readonly int _excavatorHeight = 80; /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// Ширина объекта + /// + public int GetWidth => _excavatorWidth; + /// + /// Высота объекта + /// + public int GetHeight => _excavatorHeight; + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningExcavator(int speed, double weight, + Color bodyColor, + int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if(_pictureHeight < _excavatorHeight || _pictureWidth < _excavatorWidth) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityExcavator = new EntityExcavator(speed,weight,bodyColor); + } + /// /// Конструктор /// /// Скорость @@ -47,20 +84,20 @@ namespace ProjectExcavator.DrawningObjects /// Высота картинки /// Ширина прорисовки экскаватора /// Высота прорисовки экскаватора - protected DrawningExcavator(int speed, double weight, - Color bodyColor, - int width, int height, + protected DrawningExcavator(int speed, double weight, + Color bodyColor, + int width, int height, int excavatorWidth, int excavatorHeight) { _pictureWidth = width; _pictureHeight = height; - if(_pictureHeight < _excavatorHeight || _pictureWidth < _excavatorWidth) + _excavatorWidth = excavatorWidth; + _excavatorHeight = excavatorHeight; + if (_pictureHeight < _excavatorHeight || _pictureWidth < _excavatorWidth) { - return false; + return; } - EntityExcavator = new EntityExcavator(); - EntityExcavator.Init(speed, weight, bodyColor, additionalColor, bodyKit, backet); - return true; + EntityExcavator = new EntityExcavator(speed, weight, bodyColor); } /// /// Установка позиции @@ -74,6 +111,30 @@ namespace ProjectExcavator.DrawningObjects _startPosY = Math.Min(y,_pictureHeight - _excavatorHeight); + } + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityExcavator == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityExcavator.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityExcavator.Step > 0, + //вправо + DirectionType.Right => _startPosX + EntityExcavator.Step < _pictureWidth - _excavatorWidth, + //вниз + DirectionType.Down => _startPosY + EntityExcavator.Step < _pictureHeight - _excavatorHeight, + _ => false, + }; } /// /// Изменение направления перемещения @@ -122,12 +183,6 @@ namespace ProjectExcavator.DrawningObjects return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor); - if (EntityExcavator.BodyKit) - { - g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 70, 130, 10); - g.DrawLine(pen, _startPosX + 20, _startPosY + 70, _startPosX + 150, _startPosY + 70); - } //корпус Brush bodyBrush = new SolidBrush(EntityExcavator.BodyColor); g.FillRectangle(bodyBrush, _startPosX + 20, _startPosY + 60, 130, 20); @@ -153,17 +208,7 @@ namespace ProjectExcavator.DrawningObjects g.DrawEllipse(pen, _startPosX + 80, _startPosY + 80, 20, 20); g.DrawEllipse(pen, _startPosX + 100, _startPosY + 80, 20, 20); g.DrawEllipse(pen, _startPosX + 120, _startPosY + 80, 20, 20); - //ковш - if (EntityExcavator.Backet) - { - Point[] pointsBacket = { - new Point(_startPosX + 150, _startPosY + 25), - new Point(_startPosX + 150, _startPosY + 85), - new Point(_startPosX + 195, _startPosY + 85), - }; - g.FillPolygon(additionalBrush, pointsBacket); - g.DrawPolygon(pen, pointsBacket); - } + } } } diff --git a/ProjectExcavator/ProjectExcavator/EntityExcavator.cs b/ProjectExcavator/ProjectExcavator/EntityExcavator.cs index c2acec2..4a0a157 100644 --- a/ProjectExcavator/ProjectExcavator/EntityExcavator.cs +++ b/ProjectExcavator/ProjectExcavator/EntityExcavator.cs @@ -30,11 +30,7 @@ namespace ProjectExcavator.Entities /// Скорость /// Вес экскаватора /// Основной цвет - /// Дополнительный цвет - /// Признак наличия обвеса - /// Признак наличия антикрыла - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool bodyKit, bool backet) + public EntityExcavator(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; -- 2.25.1