diff --git a/Bulldozer/Bulldozer/AbstractStrategy.cs b/Bulldozer/Bulldozer/AbstractStrategy.cs index abab1a7..956a732 100644 --- a/Bulldozer/Bulldozer/AbstractStrategy.cs +++ b/Bulldozer/Bulldozer/AbstractStrategy.cs @@ -3,26 +3,26 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using Bulldozer.DrawningObjects; +using Bulldozer.Drawnings; namespace Bulldozer.MovementStrategy { public abstract class AbstractStrategy { - private IMoveableObject? _movebleObject; + 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) + if (moveableObject == null) { _state = Status.NotInit; return; } _state = Status.InProgress; - _movebleObject = moveableObject; + _moveableObject = moveableObject; FieldWidth = width; FieldHeight = height; } @@ -43,26 +43,26 @@ namespace Bulldozer.MovementStrategy protected bool MoveRight() => MoveTo(DirectionTypeBulldozer.Right); protected bool MoveUp() => MoveTo(DirectionTypeBulldozer.Up); protected bool MoveDown() => MoveTo(DirectionTypeBulldozer.Down); - protected ObjectParameters? GetObjectParametrs => _movebleObject?.GetObjectPosition; + protected ObjectParameters? GetObjectParametrs => _moveableObject?.GetObjectPosition; protected int? GetStep() { if (_state != Status.InProgress) { return null; } - return _movebleObject?.GetStep; + return _moveableObject?.GetStep; } protected abstract void MoveToTarget(); protected abstract bool IsTargetDestination(); - private bool MoveTo(DirectionTypeBulldozer DirectionTypeBulldozer) + private bool MoveTo(DirectionTypeBulldozer directionType) { if (_state != Status.InProgress) { return false; } - if (_movebleObject?.CheckCanMove(DirectionTypeBulldozer) ?? false) + if (_moveableObject?.CheckCanMove(directionType) ?? false) { - _movebleObject.MoveObject(DirectionTypeBulldozer); + _moveableObject.MoveObject(directionType); return true; } return false; diff --git a/Bulldozer/Bulldozer/BulldozerGenericCollection.cs b/Bulldozer/Bulldozer/BulldozerGenericCollection.cs new file mode 100644 index 0000000..ad36459 --- /dev/null +++ b/Bulldozer/Bulldozer/BulldozerGenericCollection.cs @@ -0,0 +1,160 @@ +using Bulldozer.Drawnings; +using Bulldozer.MovementStrategy; +using Bulldozer.DrawningObjects; + +namespace Bulldozer.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningBulldozer + /// + /// + /// + internal class BulldozerGenericCollection + where T : DrawningBulldozer + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 200; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 110; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public BulldozerGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int? operator +(BulldozerGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(BulldozerGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowBulldozer() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / (_placeSizeWidth); i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) + {//линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 8, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int c = 11; + int k = 3; + for (int i = 0; i < _collection.Count; i++) + { + if (i % 3 == 0 && i != 0) + { + c = c - 3; + } + + if (k != 0) + { + k--; + } + else + { + k = 2; + } + T? obj = _collection.Get(i); + if (obj != null) + { + // Получение объекта + IMoveableObject moveableObject = obj.GetMoveableObject; + // Установка позиции + int x = k % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth; + int y = c / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight + 10; + moveableObject.SetPosition(x, y); + // Прорисовка объекта + moveableObject.Draw(g); + } + } + } + } +} + diff --git a/Bulldozer/Bulldozer/DirectionBulldozer.cs b/Bulldozer/Bulldozer/DirectionBulldozer.cs index 1704ed9..b2a3671 100644 --- a/Bulldozer/Bulldozer/DirectionBulldozer.cs +++ b/Bulldozer/Bulldozer/DirectionBulldozer.cs @@ -4,16 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Bulldozer +namespace Bulldozer.Drawnings { public enum DirectionTypeBulldozer { Up = 1, - Down = 2, - Left = 3, - - Right = 4 + Right = 4, } } diff --git a/Bulldozer/Bulldozer/DrawningBulldozer.cs b/Bulldozer/Bulldozer/DrawningBulldozer.cs index 0dcda1c..e740e85 100644 --- a/Bulldozer/Bulldozer/DrawningBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawningBulldozer.cs @@ -4,44 +4,47 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Bulldozer.Entities; +using Bulldozer.Drawnings; +using Bulldozer.MovementStrategy; + namespace Bulldozer.DrawningObjects { public class DrawningBulldozer { - public EntityBulldozer? EntityBulldozer { get; protected set; } + public EntityBulldozer? EntityTractor { get; protected set; } private int _pictureWidth; private int _pictureHeight; protected int _startPosX; protected int _startPosY; - protected readonly int _bulldozerWidth = 160; - protected readonly int _bulldozerHeight = 80; + protected readonly int _tractorWidth = 160; + protected readonly int _tractorHeight = 80; public DrawningBulldozer(int speed, double weight, Color mainColor, int width, int heigth) { - if (width <= _bulldozerWidth || heigth <= _bulldozerHeight) + if (width <= _tractorWidth || heigth <= _tractorHeight) { return; } _pictureWidth = width; _pictureHeight = heigth; - EntityBulldozer = new EntityBulldozer(speed, weight, mainColor); + EntityTractor = new EntityBulldozer(speed, weight, mainColor); } protected DrawningBulldozer(int speed, double weight, Color mainColor, int width, int heigth, - int bulldozerWidth, int bulldozerHeight) + int tractorWidth, int tractorHeight) { - if (width <= _bulldozerWidth || heigth <= _bulldozerHeight) + if (width <= tractorWidth || heigth <= tractorHeight) { return; } _pictureHeight = heigth; _pictureWidth = width; - _bulldozerHeight = bulldozerHeight; - _bulldozerWidth = bulldozerWidth; - EntityBulldozer = new EntityBulldozer(speed, weight, mainColor); + _tractorHeight = tractorHeight; + _tractorWidth = tractorWidth; + EntityTractor = new EntityBulldozer(speed, weight, mainColor); } public void SetPosition(int x, int y) { - if (x < 0 || y < 0 || x + _bulldozerWidth > _pictureWidth || y + _bulldozerHeight > _pictureHeight) + if (x < 0 || y < 0 || x + _tractorWidth > _pictureWidth || y + _tractorHeight > _pictureHeight) { x = 10; y = 10; @@ -49,66 +52,65 @@ namespace Bulldozer.DrawningObjects _startPosX = x; _startPosY = y; } + public IMoveableObject GetMoveableObject => new DrawningObjectBulldozer(this); public int GetPosX => _startPosX; public int GetPosY => _startPosY; - public int GetWidth => _bulldozerWidth; - public int GetHeight => _bulldozerHeight; + public int GetWidth => _tractorWidth; + public int GetHeight => _tractorHeight; public bool CanMove(DirectionTypeBulldozer direction) { - if (EntityBulldozer == null) + if (EntityTractor == 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, + DirectionTypeBulldozer.Left => _startPosX - EntityTractor.Step > 0, + DirectionTypeBulldozer.Up => _startPosY - EntityTractor.Step > 0, + DirectionTypeBulldozer.Right => _startPosX + EntityTractor.Step + _tractorWidth <= _pictureWidth, + DirectionTypeBulldozer.Down => _startPosY + EntityTractor.Step + _tractorHeight <= _pictureHeight, _ => false, }; } public void MoveTransport(DirectionTypeBulldozer direction) { - if (!CanMove(direction) || EntityBulldozer == null) + if (!CanMove(direction) || EntityTractor == null) { return; } switch (direction) { case DirectionTypeBulldozer.Left: - _startPosX -= (int)EntityBulldozer.Step; + _startPosX -= (int)EntityTractor.Step; break; case DirectionTypeBulldozer.Up: - _startPosY -= (int)EntityBulldozer.Step; + _startPosY -= (int)EntityTractor.Step; break; case DirectionTypeBulldozer.Right: - _startPosX += (int)EntityBulldozer.Step; + _startPosX += (int)EntityTractor.Step; break; case DirectionTypeBulldozer.Down: - _startPosY += (int)EntityBulldozer.Step; + _startPosY += (int)EntityTractor.Step; break; } } public virtual void DrawTrasport(Graphics g) { - if (EntityBulldozer == null) + if (EntityTractor == null) { return; } Pen pen = new(Color.Black); - Brush mainBrush = new SolidBrush(EntityBulldozer.MainColor); + Brush mainBrush = new SolidBrush(EntityTractor.MainColor); // Тело трактора - Brush tractorColor = new SolidBrush(EntityBulldozer.MainColor); + Brush tractorColor = new SolidBrush(EntityTractor.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; // высота прямоугольника int radius = 20; // радиус закругления углов - // Рисуем закругленный прямоугольник g.DrawArc(pen, x - 5, y + 50, radius * 2, radius * 2, 180, 90); // верхний левый угол g.DrawLine(pen, x + radius - 5, y + 50, x + width - radius - 5, y + 50); // верхняя горизонталь @@ -123,7 +125,7 @@ namespace Bulldozer.DrawningObjects 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.MainColor); + Brush cabinColor = new SolidBrush(EntityTractor.MainColor); g.FillRectangle(cabinColor, _startPosX + 120, _startPosY, 30, 20); } } diff --git a/Bulldozer/Bulldozer/DrawningFastBulldozer.cs b/Bulldozer/Bulldozer/DrawningFastBulldozer.cs index eb31d2d..d528ad1 100644 --- a/Bulldozer/Bulldozer/DrawningFastBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawningFastBulldozer.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Drawing.Drawing2D; using System.Linq; +using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using Bulldozer.Entities; @@ -9,47 +11,48 @@ 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) + 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) + if (EntityTractor != null) { - EntityBulldozer = new EntityFastBulldozer(speed, weight, mainColor, + EntityTractor = new EntityFastBulldozer(speed, weight, mainColor, optionalColor, covsh, rearbucket); } } public override void DrawTrasport(Graphics g) { - if (EntityBulldozer is not EntityFastBulldozer fastBulldozer) + if (EntityTractor is not EntityFastBulldozer fastTractor) { return; } - Pen pen = new(Color.Black); - Brush optionalBrush = new SolidBrush(fastBulldozer.OptionalColor); - if (fastBulldozer.Covsh) + Pen pen = new(fastTractor.OptionalColor, 2); + + if (fastTractor.Covsh) { Point[] trianglePoints = new Point[] { - new Point(_startPosX+50, _startPosY + 60), - new Point(_startPosX+50, _startPosY + 110), - new Point(_startPosX + 10, _startPosY + 110) + new Point(_startPosX+50, _startPosY + 30), + new Point(_startPosX+50, _startPosY + 80), + new Point(_startPosX + 10, _startPosY + 80) }; // Рисуем треугольник g.DrawPolygon(pen, trianglePoints); } - if (fastBulldozer.Rearbucket) + if (fastTractor.Rearbucket) { Point[] trianglePoints = new Point[] { - new Point(_startPosX+150, _startPosY + 60), - new Point(_startPosX+200, _startPosY + 60), - new Point(_startPosX + 200, _startPosY + 110) + new Point(_startPosX+150, _startPosY + 30), + new Point(_startPosX+180, _startPosY + 30), + new Point(_startPosX + 180, _startPosY + 80) }; // Рисуем треугольник g.DrawPolygon(pen, trianglePoints); } - _startPosY += 30; base.DrawTrasport(g); - _startPosY -= 30; } } } diff --git a/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs b/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs index 761eafc..2af1de5 100644 --- a/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawningObjectBulldozer.cs @@ -4,31 +4,48 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Bulldozer.DrawningObjects; +using Bulldozer.Drawnings; namespace Bulldozer.MovementStrategy { public class DrawningObjectBulldozer : IMoveableObject { - private readonly DrawningBulldozer? _drawningBulldozer = null; - public DrawningObjectBulldozer(DrawningBulldozer drawningBulldozer) + private readonly DrawningBulldozer? _drawningTractor = null; + public DrawningObjectBulldozer(DrawningBulldozer drawingTractor) { - _drawningBulldozer = drawningBulldozer; + _drawningTractor = drawingTractor; } public ObjectParameters? GetObjectPosition { get { - if (_drawningBulldozer == null || _drawningBulldozer.EntityBulldozer == null) + if (_drawningTractor == null || _drawningTractor.EntityTractor == null) { return null; } - return new ObjectParameters(_drawningBulldozer.GetPosX, - _drawningBulldozer.GetPosY, _drawningBulldozer.GetWidth, - _drawningBulldozer.GetHeight); + return new ObjectParameters(_drawningTractor.GetPosX, + _drawningTractor.GetPosY, _drawningTractor.GetWidth, + _drawningTractor.GetHeight); + } + } + public int GetStep => (int)(_drawningTractor?.EntityTractor?.Step ?? 0); + public bool CheckCanMove(DirectionTypeBulldozer direction) => + _drawningTractor?.CanMove(direction) ?? false; + public void MoveObject(DirectionTypeBulldozer direction) => + _drawningTractor?.MoveTransport(direction); + public void SetPosition(int x, int y) + { + if (_drawningTractor != null) + { + _drawningTractor.SetPosition(x, y); + } + } + public void Draw(Graphics g) + { + if (_drawningTractor != null) + { + _drawningTractor.DrawTrasport(g); } } - 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); } } diff --git a/Bulldozer/Bulldozer/EntityBulldozer.cs b/Bulldozer/Bulldozer/EntityBulldozer.cs index c205e4f..5f00c1f 100644 --- a/Bulldozer/Bulldozer/EntityBulldozer.cs +++ b/Bulldozer/Bulldozer/EntityBulldozer.cs @@ -10,7 +10,7 @@ namespace Bulldozer.Entities { public int Speed { get; private set; } public double Weight { get; private set; } - public Color MainColor { get; private set; } + public Color MainColor{ get; private set; } public double Step => (double)Speed * 100 / Weight; public EntityBulldozer(int speed, double weight, Color mainColor) { diff --git a/Bulldozer/Bulldozer/EntityFastBulldozer.cs b/Bulldozer/Bulldozer/EntityFastBulldozer.cs index ae7499f..5a40129 100644 --- a/Bulldozer/Bulldozer/EntityFastBulldozer.cs +++ b/Bulldozer/Bulldozer/EntityFastBulldozer.cs @@ -1,5 +1,4 @@ -using Bulldozer.Entities; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,7 +11,9 @@ namespace Bulldozer.Entities 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) + public EntityFastBulldozer(int speed, double weight, + Color mainColor, Color optionalColor, + bool covsh, bool rearbucket) : base (speed, weight, mainColor) { OptionalColor = optionalColor; Covsh = covsh; diff --git a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs index 8a836ec..7196367 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs @@ -37,6 +37,7 @@ buttonUp = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + ButtonSelectBulldozer = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxFastBulldozer).BeginInit(); SuspendLayout(); // @@ -141,11 +142,23 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += Buttonstep_Click; // + // ButtonSelectBulldozer + // + ButtonSelectBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonSelectBulldozer.Location = new Point(318, 426); + ButtonSelectBulldozer.Name = "ButtonSelectBulldozer"; + ButtonSelectBulldozer.Size = new Size(162, 23); + ButtonSelectBulldozer.TabIndex = 9; + ButtonSelectBulldozer.Text = "Выбор"; + ButtonSelectBulldozer.UseVisualStyleBackColor = true; + ButtonSelectBulldozer.Click += ButtonSelectBulldozer_Click; + // // FastBulldozer // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(ButtonSelectBulldozer); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonUp); @@ -174,5 +187,6 @@ private Button buttonUp; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button ButtonSelectBulldozer; } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozer.cs b/Bulldozer/Bulldozer/FormBulldozer.cs index 796d4ad..10d488c 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.cs @@ -1,57 +1,79 @@ using Bulldozer.DrawningObjects; using Bulldozer.MovementStrategy; +using Bulldozer.Drawnings; namespace Bulldozer { public partial class FastBulldozer : Form { - private DrawningBulldozer? _drawningBulldozer; + private DrawningBulldozer? _drawningTractor; private AbstractStrategy? _abstractStrategy; + public DrawningBulldozer? SelectedTractor { get; private set; } public FastBulldozer() { InitializeComponent(); + _abstractStrategy = null; + SelectedTractor = null; } private void Draw() { - if (_drawningBulldozer == null) + if (_drawningTractor == null) { return; } Bitmap bmp = new(pictureBoxFastBulldozer.Width, pictureBoxFastBulldozer.Height); Graphics gr = Graphics.FromImage(bmp); - _drawningBulldozer.DrawTrasport(gr); + _drawningTractor.DrawTrasport(gr); pictureBoxFastBulldozer.Image = bmp; } private void ButtonCreateFastBulldozer_Click(object sender, EventArgs e) { Random random = new Random(); - _drawningBulldozer = new DrawningFastBulldozer( + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog_dop = new(); + if (dialog_dop.ShowDialog() == DialogResult.OK) + { + dopColor = dialog_dop.Color; + } + Color dopColor2 = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog_dop2 = new(); + if (dialog_dop2.ShowDialog() == DialogResult.OK) + { + dopColor2 = dialog_dop2.Color; + } + _drawningTractor = 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)), + dopColor, + dopColor2, 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)); + _drawningTractor.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( + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog_dop = new(); + if (dialog_dop.ShowDialog() == DialogResult.OK) + { + dopColor = dialog_dop.Color; + } + _drawningTractor = new DrawningBulldozer( random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + dopColor, pictureBoxFastBulldozer.Width, pictureBoxFastBulldozer.Height); - _drawningBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawningTractor.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawningBulldozer == null) + if (_drawningTractor == null) { return; } @@ -59,23 +81,24 @@ namespace Bulldozer switch (name) { case "buttonUp": - _drawningBulldozer.MoveTransport(DirectionTypeBulldozer.Up); + _drawningTractor.MoveTransport(DirectionTypeBulldozer.Up); break; case "buttonDown": - _drawningBulldozer.MoveTransport(DirectionTypeBulldozer.Down); + _drawningTractor.MoveTransport(DirectionTypeBulldozer.Down); break; case "buttonLeft": - _drawningBulldozer.MoveTransport(DirectionTypeBulldozer.Left); + _drawningTractor.MoveTransport(DirectionTypeBulldozer.Left); break; case "buttonRight": - _drawningBulldozer.MoveTransport(DirectionTypeBulldozer.Right); + _drawningTractor.MoveTransport(DirectionTypeBulldozer.Right); break; } Draw(); } + private void Buttonstep_Click(object sender, EventArgs e) { - if (_drawningBulldozer == null) + if (_drawningTractor == null) { return; } @@ -93,7 +116,7 @@ namespace Bulldozer return; } _abstractStrategy.SetData( - new DrawningObjectBulldozer(_drawningBulldozer), + new DrawningObjectBulldozer(_drawningTractor), pictureBoxFastBulldozer.Width, pictureBoxFastBulldozer.Height); comboBoxStrategy.Enabled = false; @@ -110,5 +133,11 @@ namespace Bulldozer _abstractStrategy = null; } } + + private void ButtonSelectBulldozer_Click(object sender, EventArgs e) + { + SelectedTractor = _drawningTractor; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs new file mode 100644 index 0000000..859fc68 --- /dev/null +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs @@ -0,0 +1,125 @@ +namespace Bulldozer +{ + partial class FormBulldozerCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + ButtonAddBulldozer = new Button(); + ButtonRemoveBulldozer = new Button(); + ButtonRefreshCollection = new Button(); + pictureBoxCollection = new PictureBox(); + maskedTextBoxNumber = new MaskedTextBox(); + groupBox1 = new GroupBox(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // ButtonAddBulldozer + // + ButtonAddBulldozer.Location = new Point(668, 45); + ButtonAddBulldozer.Name = "ButtonAddBulldozer"; + ButtonAddBulldozer.Size = new Size(166, 31); + ButtonAddBulldozer.TabIndex = 0; + ButtonAddBulldozer.Text = "Добавить трактор"; + ButtonAddBulldozer.UseVisualStyleBackColor = true; + ButtonAddBulldozer.Click += ButtonAddBulldozer_Click; + // + // ButtonRemoveBulldozer + // + ButtonRemoveBulldozer.Location = new Point(668, 129); + ButtonRemoveBulldozer.Name = "ButtonRemoveBulldozer"; + ButtonRemoveBulldozer.Size = new Size(166, 31); + ButtonRemoveBulldozer.TabIndex = 1; + ButtonRemoveBulldozer.Text = "Удалить трактор"; + ButtonRemoveBulldozer.UseVisualStyleBackColor = true; + ButtonRemoveBulldozer.Click += ButtonRemoveBulldozer_Click; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(668, 215); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(166, 31); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(624, 457); + pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; + pictureBoxCollection.TabIndex = 3; + pictureBoxCollection.TabStop = false; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point); + maskedTextBoxNumber.Location = new Point(698, 100); + maskedTextBoxNumber.Mask = "00"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(100, 22); + maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.ValidatingType = typeof(int); + // + // groupBox1 + // + groupBox1.Location = new Point(646, 12); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(209, 412); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // FormBulldozerCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(877, 469); + Controls.Add(ButtonRefreshCollection); + Controls.Add(ButtonRemoveBulldozer); + Controls.Add(maskedTextBoxNumber); + Controls.Add(ButtonAddBulldozer); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormBulldozerCollection"; + Text = "Набор трактористов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button ButtonAddBulldozer; + private Button ButtonRemoveBulldozer; + private Button ButtonRefreshCollection; + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBox1; + } +} \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs new file mode 100644 index 0000000..4f077bf --- /dev/null +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Bulldozer.DrawningObjects; +using Bulldozer.Drawnings; +using Bulldozer.Generics; +using Bulldozer.MovementStrategy; + + +namespace Bulldozer +{ + public partial class FormBulldozerCollection : Form + { + private readonly BulldozerGenericCollection _tractor; + public FormBulldozerCollection() + { + InitializeComponent(); + _tractor = new BulldozerGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void ButtonAddBulldozer_Click(object sender, EventArgs e) + { + FastBulldozer form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_tractor + form.SelectedTractor > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _tractor.ShowBulldozer(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveBulldozer_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_tractor - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _tractor.ShowBulldozer(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _tractor.ShowBulldozer(); + } + } +} diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.resx b/Bulldozer/Bulldozer/FormBulldozerCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Bulldozer/Bulldozer/IMoveableObject.cs b/Bulldozer/Bulldozer/IMoveableObject.cs index a5fbcff..ab16122 100644 --- a/Bulldozer/Bulldozer/IMoveableObject.cs +++ b/Bulldozer/Bulldozer/IMoveableObject.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Bulldozer.DrawningObjects; +using Bulldozer.Drawnings; namespace Bulldozer.MovementStrategy { @@ -28,5 +29,7 @@ namespace Bulldozer.MovementStrategy /// /// void MoveObject(DirectionTypeBulldozer direction); + void SetPosition(int x, int y); + void Draw(Graphics g); } } diff --git a/Bulldozer/Bulldozer/MoveToBorder.cs b/Bulldozer/Bulldozer/MoveToBorder.cs index 7315b6d..44db834 100644 --- a/Bulldozer/Bulldozer/MoveToBorder.cs +++ b/Bulldozer/Bulldozer/MoveToBorder.cs @@ -16,7 +16,7 @@ namespace Bulldozer.MovementStrategy return false; } return objParams.RightBorder <= FieldWidth && - objParams.RightBorder + GetStep() >= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth&& objParams.DownBorder <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; } diff --git a/Bulldozer/Bulldozer/MoveToCenter.cs b/Bulldozer/Bulldozer/MoveToCenter.cs index 783c718..5c6157d 100644 --- a/Bulldozer/Bulldozer/MoveToCenter.cs +++ b/Bulldozer/Bulldozer/MoveToCenter.cs @@ -11,7 +11,7 @@ namespace Bulldozer.MovementStrategy protected override bool IsTargetDestination() { var objParams = GetObjectParametrs; - if (objParams == null) + if (objParams == null ) { return false; } @@ -23,7 +23,7 @@ namespace Bulldozer.MovementStrategy protected override void MoveToTarget() { var objParams = GetObjectParametrs; - if (objParams == null) + if(objParams == null) { return; } diff --git a/Bulldozer/Bulldozer/ObjectParameters.cs b/Bulldozer/Bulldozer/ObjectParameters.cs index b495ded..5955b3d 100644 --- a/Bulldozer/Bulldozer/ObjectParameters.cs +++ b/Bulldozer/Bulldozer/ObjectParameters.cs @@ -16,8 +16,8 @@ namespace Bulldozer.MovementStrategy 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 int ObjectMiddleHorizontal => _x + _width/2; + public int ObjectMiddleVertical => _y + _height/2; public ObjectParameters(int x, int y, int width, int height) { _x = x; diff --git a/Bulldozer/Bulldozer/Program.cs b/Bulldozer/Bulldozer/Program.cs index b7362fc..3671b7b 100644 --- a/Bulldozer/Bulldozer/Program.cs +++ b/Bulldozer/Bulldozer/Program.cs @@ -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 FastBulldozer()); + Application.Run(new FormBulldozerCollection()); } } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/SetGeneric.cs b/Bulldozer/Bulldozer/SetGeneric.cs new file mode 100644 index 0000000..0236517 --- /dev/null +++ b/Bulldozer/Bulldozer/SetGeneric.cs @@ -0,0 +1,108 @@ +namespace Bulldozer.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемая установка + /// + public int Insert(T tractor) + { + return Insert(tractor, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемая установкаь + /// Позиция + /// + public int Insert(T tractor, int position) + { + // TODO проверка позиции + if (position < 0 || position >= Count) + { + // Позиция недопустима + return -1; + } + if (_places[position] != null) + { + int d = 0; + for(int j = 1; j < Count; j++) + { + if (_places[position + j] == null) + { + d = position + j; + break; + } + } + if(d == 0) + { + return -1; + } + for(int j = d; j > position; j--) + { + _places[j] = _places[j - 1]; + } + } + _places[position] = tractor; + return position; + + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // Проверка позиции + if (position < 0 || position >= _places.Length) + { + // Позиция недопустима + return false; + } + // TODO удаление объекта из массива, присвоив элементу массива значение null + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // TODO проверка позиции + if (position < 0 || position >= _places.Length) + { + // Позиция недопустима + return null; + } + return _places[position]; + } + } +} +