создала новые классы по примеру

This commit is contained in:
Полина Чубыкина 2023-10-08 18:48:39 +04:00
parent dbbef18263
commit 20300279e7
10 changed files with 418 additions and 136 deletions

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
namespace Sailboat.MovementStrategy
{
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
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;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(DirectionType.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(DirectionType.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(DirectionType.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveDown() => MoveTo(DirectionType.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.Entities;
namespace Sailboat.DrawingObjects
{
public class DrawingBoat
{
public EntityBoat? EntityBoat { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private readonly int _boatWidth = 160;
private readonly int _boatHeight = 160;
public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _boatWidth || height < _boatHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityBoat = new EntityBoat(speed, weight, bodyColor);
}
protected DrawingBoat(int speed, double weight, Color bodyColor, int width, int height, int boatWidth, int boatHeight)
{
if (width < _boatWidth || height < _boatHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_boatWidth = boatWidth;
_boatHeight = boatHeight;
EntityBoat = new EntityBoat(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _boatWidth > _pictureWidth)
{
x = 0;
}
if (y < 0 || y + _boatHeight > _pictureHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntityBoat == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityBoat.Step > 0)
{
_startPosX -= (int)EntityBoat.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntityBoat.Step > 0)
{
_startPosY -= (int)EntityBoat.Step;
}
break;
case DirectionType.Right:
if (_startPosX + EntityBoat.Step + _boatWidth < _pictureWidth)
{
_startPosX += (int)EntityBoat.Step;
}
break;
case DirectionType.Down:
if (_startPosY + EntityBoat.Step + _boatHeight < _pictureHeight)
{
_startPosY += (int)EntityBoat.Step;
}
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityBoat == null)
{
return;
}
Pen pen = new(Color.Black, 4);
Brush additionalBrush = new
SolidBrush(EntityBoat.BodyColor);
//основной корпус парусника
Brush Brush = new
SolidBrush(EntityBoat.BodyColor);
Point[] hull = new Point[]
{
new Point(_startPosX + 10, _startPosY + 90),
new Point(_startPosX + 110, _startPosY + 90),
new Point(_startPosX + 140, _startPosY + 120),
new Point(_startPosX + 110, _startPosY + 150),
new Point(_startPosX + 10, _startPosY + 150)
};
g.FillPolygon(Brush, hull);
g.DrawPolygon(pen, hull);
Brush addBrush = new
SolidBrush(Color.Green);
g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 90, 40);
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40);
}
}
}

View File

@ -1,91 +1,38 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
using Sailboat.Entities;
namespace Sailboat.DrawingObjects
{
public class DrawingSailboat
public class DrawingSailboat : DrawingBoat
{
public EntitySailboat? EntitySailboat { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _boatWidth = 160;
private readonly int _boatHeight = 160;
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height)
public DrawingSailboat(int speed, double weight, Color bodyColor, Color
additionalColor, bool hull, bool sail, int width, int height) :
base(speed, weight, bodyColor, width, height, 160, 160)
{
if (width < _boatWidth || height < _boatHeight)
if (EntityBoat != null)
{
return false;
EntityBoat = new EntitySailboat(speed, weight, bodyColor,
additionalColor, hull, sail);
}
_pictureWidth = width;
_pictureHeight = height;
EntitySailboat = new EntitySailboat();
EntitySailboat.Init(speed, weight, bodyColor, additionalColor, hull, sail);
return true;
}
public void SetPosition(int x, int y)
public override void DrawTransport(Graphics g)
{
if (x < 0 || x + _boatWidth > _pictureWidth)
{
x = 0;
}
if (y < 0 || y + _boatHeight > _pictureHeight)
{
y = 0;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (EntitySailboat == null)
if (EntityBoat is not EntitySailboat sailboat)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntitySailboat.Step > 0)
{
_startPosX -= (int)EntitySailboat.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntitySailboat.Step > 0)
{
_startPosY -= (int)EntitySailboat.Step;
}
break;
case DirectionType.Right:
if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth)
{
_startPosX += (int)EntitySailboat.Step;
}
break;
case DirectionType.Down:
if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight)
{
_startPosY += (int)EntitySailboat.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (EntitySailboat == null)
{
return;
}
Pen pen = new(Color.Black, 4);
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(EntitySailboat.AdditionalColor);
SolidBrush(sailboat.AdditionalColor);
//усиленный корпус парусника
if (EntitySailboat.Hull)
if (sailboat.Hull)
{
Point[] hullCooler = new Point[]
{
@ -99,33 +46,13 @@ namespace Sailboat
g.DrawPolygon(pen, hullCooler);
}
//основной корпус парусника
Brush Brush = new
SolidBrush(EntitySailboat.BodyColor);
Point[] hull = new Point[]
{
new Point(_startPosX + 10, _startPosY + 90),
new Point(_startPosX + 110, _startPosY + 90),
new Point(_startPosX + 140, _startPosY + 120),
new Point(_startPosX + 110, _startPosY + 150),
new Point(_startPosX + 10, _startPosY + 150)
};
g.FillPolygon(Brush, hull);
g.DrawPolygon(pen, hull);
Brush addBrush = new
SolidBrush(Color.Green);
g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 90, 40);
g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 90, 40);
base.DrawTransport(g);
//парус
if (EntitySailboat.Sail)
if (sailboat.Sail)
{
Brush sailBrush = new
SolidBrush(EntitySailboat.AdditionalColor);
SolidBrush(sailboat.AdditionalColor);
Point[] sail = new Point[]
{

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.Entities
{
public class EntityBoat
{
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 EntityBoat(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -4,53 +4,16 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
namespace Sailboat.Entities
{
public class EntitySailboat
public class EntitySailboat : EntityBoat
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия усиленного корпуса
/// </summary>
public bool Hull { get; private set; }
/// <summary>
/// Признак (опция) наличия паруса
/// </summary>
public bool Sail { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="hull">Признак наличия усиленного корпуса</param>
/// <param name="sail">Признак наличия паруса</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool hull, bool sail)
public EntitySailboat(int speed, double weight, Color bodyColor, Color
additionalColor, bool hull, bool sail) : base (speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Hull = hull;
Sail = sail;

View File

@ -47,6 +47,7 @@
this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxSailboat.TabIndex = 0;
this.pictureBoxSailboat.TabStop = false;
this.pictureBoxSailboat.Click += new System.EventHandler(this.pictureBoxSailboat_Click);
//
// buttonCreate
//

View File

@ -1,3 +1,6 @@
using Sailboat.Entities;
using Sailboat.DrawingObjects;
namespace Sailboat
{
public partial class FormSailboat : Form
@ -22,8 +25,7 @@ namespace Sailboat
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawingSailboat = new DrawingSailboat();
_drawingSailboat.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)),
_drawingSailboat = new DrawingSailboat(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)), pictureBoxSailboat.Width, pictureBoxSailboat.Height);
_drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100));
@ -54,5 +56,10 @@ namespace Sailboat
Draw();
}
private void pictureBoxSailboat_Click(object sender, EventArgs e)
{
}
}
}

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
namespace Sailboat.MovementStrategy
{
public interface IMoveableObject
{
/// <summary>
/// Получение координаты X объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionType direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.MovementStrategy
{
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.MovementStrategy
{
/// <summary>
/// Статус выполнения операции перемещения
/// </summary>
public enum Status
{
NotInit,
InProgress,
Finish
}
}