PIbd22_NikiforovaMV_lab2
This commit is contained in:
parent
a0083358a9
commit
5a3b9d61d8
71
ContainerShip/AbstractStategy.cs
Normal file
71
ContainerShip/AbstractStategy.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ContainerShip.MovementStrategy;
|
||||
|
||||
namespace ContainerShip.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 (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
protected bool MoveLeft() => MoveTo(DirectionType.Left);
|
||||
protected bool MoveRight() => MoveTo(DirectionType.Right);
|
||||
protected bool MoveUp() => MoveTo(DirectionType.Up);
|
||||
protected bool MoveDown() => MoveTo(DirectionType.Down);
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
private bool MoveTo(DirectionType directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,117 +3,37 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ContainerShip.Entities;
|
||||
|
||||
namespace ContainerShip
|
||||
namespace ContainerShip.DrawningObjects
|
||||
{
|
||||
public class DrawingContainerShip
|
||||
public class DrawingContainerShip : DrawningShip
|
||||
{
|
||||
public EntityContainerShip? EntityContainerShip { get; private set;}
|
||||
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private readonly int _shipWidth = 140;
|
||||
private readonly int _shipHeight = 90;
|
||||
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool load, bool crane, int width, int height)
|
||||
public DrawingContainerShip(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool load, bool crane, int width, int height) :
|
||||
base(speed, weight, bodyColor, width, height, 140, 90)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if ((_pictureWidth < _shipWidth) || (_pictureHeight < _shipHeight))
|
||||
if (EntityShip != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
EntityContainerShip = new EntityContainerShip();
|
||||
EntityContainerShip.Init(speed, weight, bodyColor, additionalColor, load, crane);
|
||||
return true;
|
||||
|
||||
}
|
||||
public void SetPosition (int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if (_startPosX + _shipWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _shipWidth;
|
||||
}
|
||||
|
||||
if (_startPosX < 0)
|
||||
{
|
||||
_startPosX = 0;
|
||||
}
|
||||
|
||||
if (_startPosY + _shipHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _shipHeight;
|
||||
}
|
||||
|
||||
if (_startPosY < 0)
|
||||
{
|
||||
_startPosY = 0;
|
||||
EntityShip = new EntityContainerShip(speed, weight, bodyColor,
|
||||
additionalColor, load, crane);
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveTransport (DirectionType direction)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityContainerShip == null) return;
|
||||
switch (direction)
|
||||
if (EntityShip is not EntityContainerShip containerShip)
|
||||
{
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityContainerShip.Step > 0)
|
||||
{
|
||||
_startPosX-= (int)EntityContainerShip.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityContainerShip.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityContainerShip.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + EntityContainerShip.Step + _shipWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityContainerShip.Step;
|
||||
}
|
||||
break;
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + EntityContainerShip.Step + _shipHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityContainerShip.Step;
|
||||
}
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport (Graphics g)
|
||||
{
|
||||
if (EntityContainerShip == null) { return; }
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodyBrush = new SolidBrush(EntityContainerShip.BodyColor);
|
||||
Point point1 = new Point(_startPosX, _startPosY + 52);
|
||||
Point point2 = new Point(_startPosX + 137, _startPosY + 52);
|
||||
Point point3 = new Point(_startPosX + 137 - 20, _startPosY + 52 + 46 - 10);
|
||||
Point point4 = new Point(_startPosX + 20, _startPosY + 52 + 46 - 10);
|
||||
|
||||
Point[] p = { point1, point2, point3, point4 };
|
||||
|
||||
g.FillPolygon(bodyBrush, p); //корпус
|
||||
|
||||
Brush additionalBrush = new SolidBrush(EntityContainerShip.AdditionalColor);
|
||||
|
||||
g.FillRectangle(additionalBrush, _startPosX + 15, _startPosY + 32, 80, 20); //прямоугольник
|
||||
|
||||
//груз
|
||||
|
||||
if (EntityContainerShip.Load)
|
||||
Brush additionalBrush = new
|
||||
SolidBrush(containerShip.AdditionalColor);
|
||||
if (containerShip.Load)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20); //большой прямоугольник
|
||||
}
|
||||
|
||||
// кран
|
||||
if (EntityContainerShip.Crane)
|
||||
base.DrawTransport(g);
|
||||
if (containerShip.Crane)
|
||||
{
|
||||
g.DrawLine(pen, _startPosX + 90, _startPosY + 32, _startPosX + 120, _startPosY);
|
||||
g.DrawLine(pen, _startPosX + 80, _startPosY + 32, _startPosX + 120, _startPosY);
|
||||
|
32
ContainerShip/DrawningObjectShip.cs
Normal file
32
ContainerShip/DrawningObjectShip.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using ContainerShip.DrawningObjects;
|
||||
namespace ContainerShip.MovementStrategy
|
||||
{
|
||||
public class DrawingObjectShip : IMoveableObject
|
||||
{
|
||||
private readonly DrawningShip? _drawingBus = null;
|
||||
public DrawingObjectShip(DrawningShip drawingBus)
|
||||
{
|
||||
_drawingBus = drawingBus;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawingBus == null || _drawingBus.EntityShip == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingBus.GetPosX, _drawingBus.GetPosY, _drawingBus.GetWidth, _drawingBus.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawingBus?.EntityShip?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionType direction) => _drawingBus?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) => _drawingBus?.MoveTransport(direction);
|
||||
}
|
||||
}
|
133
ContainerShip/DrawningShip.cs
Normal file
133
ContainerShip/DrawningShip.cs
Normal file
@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using ContainerShip.Entities;
|
||||
|
||||
namespace ContainerShip.DrawningObjects
|
||||
{
|
||||
|
||||
public class DrawningShip
|
||||
{
|
||||
public EntityShip? EntityShip { get; protected set; }
|
||||
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
|
||||
protected int _startPosY;
|
||||
|
||||
protected readonly int _busWidth = 150;
|
||||
|
||||
protected readonly int _busHeight = 65;
|
||||
|
||||
public int GetPosX => _startPosX;
|
||||
public int GetPosY => _startPosY;
|
||||
public int GetWidth => _busWidth;
|
||||
public int GetHeight => _busHeight;
|
||||
|
||||
public DrawningShip(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < _busWidth || height < _busHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityShip = new EntityShip(speed, weight, bodyColor);
|
||||
}
|
||||
protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight)
|
||||
{
|
||||
if (width < _busWidth || height < _busHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_busWidth = busWidth;
|
||||
_busHeight = busHeight;
|
||||
EntityShip = new EntityShip(speed, weight, bodyColor);
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x > _pictureWidth || y > _pictureHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityShip.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityShip.Step;
|
||||
}
|
||||
break;
|
||||
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityShip.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityShip.Step;
|
||||
}
|
||||
break;
|
||||
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _busWidth + EntityShip.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityShip.Step;
|
||||
}
|
||||
break;
|
||||
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _busHeight + EntityShip.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityShip.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodyBrush = new SolidBrush(EntityShip.BodyColor);
|
||||
Point point1 = new Point(_startPosX, _startPosY + 52);
|
||||
Point point2 = new Point(_startPosX + 137, _startPosY + 52);
|
||||
Point point3 = new Point(_startPosX + 137 - 20, _startPosY + 52 + 46 - 10);
|
||||
Point point4 = new Point(_startPosX + 20, _startPosY + 52 + 46 - 10);
|
||||
|
||||
Point[] p = { point1, point2, point3, point4 };
|
||||
|
||||
g.FillPolygon(bodyBrush, p);
|
||||
|
||||
g.FillRectangle(bodyBrush, _startPosX + 15, _startPosY + 32, 80, 20);
|
||||
}
|
||||
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityShip == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
DirectionType.Left => _startPosX - EntityShip.Step > 0,
|
||||
DirectionType.Up => _startPosY - EntityShip.Step > 0,
|
||||
DirectionType.Right => _startPosX + _busWidth + EntityShip.Step < _pictureWidth,
|
||||
DirectionType.Down => _startPosY + _busHeight + EntityShip.Step < _pictureHeight,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -5,24 +5,17 @@ using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ContainerShip
|
||||
namespace ContainerShip.Entities
|
||||
{
|
||||
public class EntityContainerShip
|
||||
public class EntityContainerShip : EntityShip
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
public double Weight { get; private set; }
|
||||
public Color BodyColor { get; private set; }
|
||||
public Color AdditionalColor { get; private set; }
|
||||
public bool Load { get; private set; }
|
||||
public bool Crane { get; private set; }
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool load, bool crane)
|
||||
public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, bool load, bool crane) : base(speed, weight, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Load = load;
|
||||
Crane = crane;
|
||||
|
22
ContainerShip/EntityShip.cs
Normal file
22
ContainerShip/EntityShip.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ContainerShip.Entities
|
||||
{
|
||||
public class EntityShip
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
public double Weight { get; private set; }
|
||||
public Color BodyColor { get; private set; }
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
public EntityShip(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
||||
}
|
63
ContainerShip/FormShips.Designer.cs
generated
63
ContainerShip/FormShips.Designer.cs
generated
@ -30,11 +30,14 @@
|
||||
{
|
||||
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormShips));
|
||||
pictureBoxShips = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateContainerShip = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonCreateShip = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxShips).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -48,16 +51,16 @@
|
||||
pictureBoxShips.TabIndex = 0;
|
||||
pictureBoxShips.TabStop = false;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateContainerShip
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 391);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(100, 50);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreateContainerShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateContainerShip.Location = new Point(12, 364);
|
||||
buttonCreateContainerShip.Name = "buttonCreateContainerShip";
|
||||
buttonCreateContainerShip.Size = new Size(131, 77);
|
||||
buttonCreateContainerShip.TabIndex = 1;
|
||||
buttonCreateContainerShip.Text = "Создать контейнеровоз";
|
||||
buttonCreateContainerShip.UseVisualStyleBackColor = true;
|
||||
buttonCreateContainerShip.Click += ButtonCreateContainerShip_Click;
|
||||
//
|
||||
// buttonRight
|
||||
//
|
||||
@ -107,16 +110,49 @@
|
||||
buttonUp.UseVisualStyleBackColor = true;
|
||||
buttonUp.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreateShip
|
||||
//
|
||||
buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateShip.Location = new Point(149, 364);
|
||||
buttonCreateShip.Name = "buttonCreateShip";
|
||||
buttonCreateShip.Size = new Size(131, 77);
|
||||
buttonCreateShip.TabIndex = 6;
|
||||
buttonCreateShip.Text = "Создать корабль";
|
||||
buttonCreateShip.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К правому нижнему краю" });
|
||||
comboBoxStrategy.Location = new Point(711, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(159, 28);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Location = new Point(729, 46);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(128, 31);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += ButtonStep_Click;
|
||||
//
|
||||
// FormShips
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(882, 453);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateShip);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateContainerShip);
|
||||
Controls.Add(pictureBoxShips);
|
||||
Name = "FormShips";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
@ -129,10 +165,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxShips;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateContainerShip;
|
||||
private Button buttonRight;
|
||||
private Button buttonDown;
|
||||
private Button buttonLeft;
|
||||
private Button buttonUp;
|
||||
private Button buttonCreateShip;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,8 +1,13 @@
|
||||
using ContainerShip.DrawningObjects;
|
||||
using ContainerShip.MovementStrategy;
|
||||
|
||||
namespace ContainerShip
|
||||
{
|
||||
public partial class FormShips : Form
|
||||
{
|
||||
private DrawingContainerShip? _drawningShip;
|
||||
private DrawningShip? _drawningShip;
|
||||
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
|
||||
public FormShips()
|
||||
{
|
||||
@ -21,6 +26,36 @@ namespace ContainerShip
|
||||
pictureBoxShips.Image = bmp;
|
||||
}
|
||||
|
||||
private void ButtonCreateContainerShip_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningShip = new DrawingContainerShip(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)),
|
||||
pictureBoxShips.Width, pictureBoxShips.Height);
|
||||
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateShip_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningShip = new DrawningShip(random.Next(100, 300),
|
||||
random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
||||
random.Next(0, 256)),
|
||||
pictureBoxShips.Width, pictureBoxShips.Height);
|
||||
_drawningShip.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningShip == null)
|
||||
@ -46,23 +81,41 @@ namespace ContainerShip
|
||||
Draw();
|
||||
}
|
||||
|
||||
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
private void ButtonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningShip = new DrawingContainerShip();
|
||||
_drawningShip.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)),
|
||||
Convert.ToBoolean(random.Next(0, 2)),
|
||||
Convert.ToBoolean(random.Next(0, 2)),
|
||||
pictureBoxShips.Width, pictureBoxShips.Height);
|
||||
_drawningShip.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
if (_drawningShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawingObjectShip(_drawningShip), pictureBoxShips.Width,
|
||||
pictureBoxShips.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,7 +121,7 @@
|
||||
<data name="buttonRight.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
|
||||
vgAADr4B6kKxwAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAA21SURBVHhe7d3f
|
||||
vAAADrwBlbxySQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAA21SURBVHhe7d3f
|
||||
75/lXcfxtlCgHe3qCHSAZawr1hIIBINpxEBkASGyWIUEkxpHAuNAXTyZIdFknukO9GALmrAZScCIGYIR
|
||||
AnEGY2cJBC0jY2BdU1m6BggrHdCOtvQH+roSOFneBif39b0+/X4ej+T5J9x5td/7+tzXEgAAAAAAAAAA
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||
@ -186,7 +186,7 @@
|
||||
<data name="buttonDown.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
|
||||
vgAADr4B6kKxwAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABOSSURBVHhe7d1t
|
||||
vAAADrwBlbxySQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABOSSURBVHhe7d1t
|
||||
yPZ3WcDxbW66OZ1L0fnQ1KZmiqIYxshQMjRHSisFAyMFH170QG8MocDe1V7UC8UCLRI0MjSNJoqG0XSi
|
||||
WJpkmimmqKj4sHTOh6lzdRzoatwd1+7rvu7jd57HeZ6fD3zf3Luu8//7P/A/7l33/zrPcwAAAAAAAAAA
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||
@ -276,7 +276,7 @@
|
||||
<data name="buttonLeft.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
|
||||
vgAADr4B6kKxwAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAA2lSURBVHhe7d3f
|
||||
vAAADrwBlbxySQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAA2lSURBVHhe7d3f
|
||||
y5/1fcdxkxhNUpOmlZiqi7VpbBZRIhmOMIcyi05pyrIpWMhYBW0O1paddAgbdGdbD7aDFjewHRN0zFGn
|
||||
Y4rSFkvTRRTb2FCrSxsySxpUbEyjSc3vuL0+4JG8S2u8rvvzve/v4wHPP+HildzX53t9zgIAAAAAAAAA
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||
@ -341,7 +341,7 @@
|
||||
<data name="buttonUp.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>
|
||||
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
|
||||
vgAADr4B6kKxwAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABOUSURBVHhe7d1b
|
||||
vAAADrwBlbxySQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABOUSURBVHhe7d1b
|
||||
rG1nVcDxtrTQUigVAuViAQuIEAgEg2nEQMSAECFWIcEEIyRcHrzEFwyJJvimfdAHCJqARhIwYkAwlkDA
|
||||
YCyUQFCQiCBCEAIECJcKpVwKlOoYgYa6Gadnn7PHt9ZYa/1+yf/lnL3XnN+cM3O0Z8+91jkAAAAAAAAA
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||
|
20
ContainerShip/IMovableObject.cs
Normal file
20
ContainerShip/IMovableObject.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace ContainerShip.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
int GetStep { get; }
|
||||
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
55
ContainerShip/MoveToBorder.cs
Normal file
55
ContainerShip/MoveToBorder.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ContainerShip.MovementStrategy;
|
||||
|
||||
namespace ContainerShip.MovementStrategy
|
||||
{
|
||||
internal class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
|
||||
if (GetObjectParameters == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return GetObjectParameters.RightBorder <= FieldWidth &&
|
||||
GetObjectParameters.RightBorder + GetStep() >= FieldWidth &&
|
||||
GetObjectParameters.DownBorder <= FieldHeight &&
|
||||
GetObjectParameters.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
if (GetObjectParameters == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (Math.Abs(GetObjectParameters.ObjectMiddleHorizontal - FieldWidth) > GetStep())
|
||||
{
|
||||
if (GetObjectParameters.ObjectMiddleHorizontal - FieldWidth > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
}
|
||||
if (Math.Abs(GetObjectParameters.ObjectMiddleVertical - FieldHeight) > GetStep())
|
||||
{
|
||||
if (GetObjectParameters.ObjectMiddleVertical - FieldHeight > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
ContainerShip/MoveToCenter.cs
Normal file
58
ContainerShip/MoveToCenter.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ContainerShip.MovementStrategy;
|
||||
|
||||
namespace ContainerShip.MovementStrategy
|
||||
{
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
ContainerShip/ObjectParameters.cs
Normal file
35
ContainerShip/ObjectParameters.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ContainerShip.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;
|
||||
}
|
||||
}
|
||||
}
|
15
ContainerShip/Status.cs
Normal file
15
ContainerShip/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ContainerShip.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user