lab2 готово
This commit is contained in:
parent
6b3a0ed91b
commit
8cb412d042
133
ProjectBattleship/ProjectBattleship/AbstractStrategy.cs
Normal file
133
ProjectBattleship/ProjectBattleship/AbstractStrategy.cs
Normal file
@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectBattleship;
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,44 +3,17 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectBattleship.Entities;
|
||||
|
||||
namespace ProjectBattleship
|
||||
namespace ProjectBattleship.DrawingObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingBattleship
|
||||
public class DrawingBattleship : DrawingShip
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityBattleship? EntityBattleship { get; private set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки
|
||||
/// </summary>
|
||||
private int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки
|
||||
/// </summary>
|
||||
private int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки
|
||||
/// </summary>
|
||||
private readonly int _shipWidth = 150;
|
||||
/// <summary>
|
||||
/// Высота прорисовки
|
||||
/// </summary>
|
||||
private readonly int _shipHeight = 50;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// Конструктор и инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
@ -50,105 +23,24 @@ namespace ProjectBattleship
|
||||
/// <param name="rocketLauncher">Признак наличия контейнера для ракет</param>
|
||||
/// <param name="width">Ширина окна</param>
|
||||
/// <param name="height">Высота окна</param>
|
||||
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color
|
||||
public DrawingBattleship(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool turret, bool rocketLauncher, int width, int height)
|
||||
: base(speed, weight, bodyColor, width, height)
|
||||
{
|
||||
if (width < _shipWidth || height < _shipHeight)
|
||||
return false;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityBattleship = new EntityBattleship();
|
||||
EntityBattleship.Init(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
|
||||
return true;
|
||||
if (EntityShip != null)
|
||||
EntityShip = new EntityBattleship(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (x < 0 || y < 0 || x + _shipWidth > _pictureWidth || y + _shipHeight > _pictureHeight)
|
||||
{
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение объекта
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityBattleship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityBattleship.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityBattleship.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityBattleship.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityBattleship.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _shipWidth + (int)EntityBattleship.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityBattleship.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _shipHeight + (int)EntityBattleship.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityBattleship.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBattleship == null)
|
||||
if (EntityShip is not EntityBattleship battleship)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityBattleship.AdditionalColor);
|
||||
Brush bodyBrush = new SolidBrush(EntityBattleship.BodyColor);
|
||||
//корпус
|
||||
Point[] hull = new Point[] {new Point(_startPosX + 5, _startPosY), new Point(_startPosX + 100, _startPosY),
|
||||
new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 100, _startPosY + 50), new Point(_startPosX + 5, _startPosY + 50)};
|
||||
g.FillPolygon(bodyBrush, hull);
|
||||
g.DrawPolygon(pen, hull);
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
g.FillRectangle(brBlack, _startPosX, _startPosY + 6, 5, 13);
|
||||
g.FillRectangle(brBlack, _startPosX, _startPosY + 31, 5, 13);
|
||||
//надстройки
|
||||
Brush brDark = new SolidBrush(Color.DarkGray);
|
||||
g.FillRectangle(brDark, _startPosX + 39, _startPosY + 20, 40, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 39, _startPosY + 20, 40, 10);
|
||||
g.FillRectangle(brDark, _startPosX + 70, _startPosY + 12, 18, 26);
|
||||
g.DrawRectangle(pen, _startPosX + 70, _startPosY + 12, 18, 26);
|
||||
g.FillEllipse(brBlack, _startPosX + 94, _startPosY + 19, 12, 12);
|
||||
Brush additionalBrush = new SolidBrush(battleship.AdditionalColor);
|
||||
base.DrawTransport(g);
|
||||
//орудийная башня
|
||||
if (EntityBattleship.Turret)
|
||||
if (battleship.Turret)
|
||||
{
|
||||
Point[] shield = new Point[] {new Point(_startPosX + 112, _startPosY + 19), new Point(_startPosX + 112, _startPosY + 31),
|
||||
new Point(_startPosX + 119, _startPosY + 28), new Point(_startPosX + 119, _startPosY + 22)};
|
||||
@ -158,7 +50,7 @@ namespace ProjectBattleship
|
||||
g.DrawRectangle(pen, _startPosX + 119, _startPosY + 24, 12, 2);
|
||||
}
|
||||
//ячейки для ракет
|
||||
if (EntityBattleship.RocketLauncher)
|
||||
if (battleship.RocketLauncher)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX + 14, _startPosY + 14, 10, 10);
|
||||
g.FillRectangle(additionalBrush, _startPosX + 26, _startPosY + 14, 10, 10);
|
||||
|
39
ProjectBattleship/ProjectBattleship/DrawingObjectShip.cs
Normal file
39
ProjectBattleship/ProjectBattleship/DrawingObjectShip.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectBattleship;
|
||||
using ProjectBattleship.DrawingObjects;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningShip (паттерн Adapter)
|
||||
/// </summary>
|
||||
public class DrawingObjectShip : IMoveableObject
|
||||
{
|
||||
private readonly DrawingShip? _drawingShip = null;
|
||||
public DrawingObjectShip(DrawingShip drawingShip)
|
||||
{
|
||||
_drawingShip = drawingShip;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawingShip == null || _drawingShip.EntityShip == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingShip.GetPosX,
|
||||
_drawingShip.GetPosY, _drawingShip.GetWidth, _drawingShip.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawingShip?.EntityShip?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionType direction) =>
|
||||
_drawingShip?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) =>
|
||||
_drawingShip?.MoveTransport(direction);
|
||||
}
|
||||
}
|
189
ProjectBattleship/ProjectBattleship/DrawingShip.cs
Normal file
189
ProjectBattleship/ProjectBattleship/DrawingShip.cs
Normal file
@ -0,0 +1,189 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectBattleship.Entities;
|
||||
|
||||
namespace ProjectBattleship.DrawingObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawingShip
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityShip? EntityShip { get; protected set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки
|
||||
/// </summary>
|
||||
protected int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки
|
||||
/// </summary>
|
||||
protected int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки
|
||||
/// </summary>
|
||||
private readonly int _shipWidth = 150;
|
||||
/// <summary>
|
||||
/// Высота прорисовки
|
||||
/// </summary>
|
||||
private readonly int _shipHeight = 50;
|
||||
/// <summary>
|
||||
/// Конструктор и инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет корпуса</param>
|
||||
/// <param name="width">Ширина окна</param>
|
||||
/// <param name="height">Высота окна</param>
|
||||
public DrawingShip(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < _shipWidth || height < _shipHeight)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityShip = new EntityShip(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор и инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет корпуса</param>
|
||||
/// <param name="width">Ширина окна</param>
|
||||
/// <param name="height">Высота окна</param>
|
||||
/// <param name="shipWidth">Ширина прорисовки</param>
|
||||
/// <param name="shipHeight">Высота прорисовки</param>
|
||||
protected DrawingShip(int speed, double weight, Color bodyColor,
|
||||
int width, int height, int shipWidth, int shipHeight)
|
||||
{
|
||||
if (width < _shipWidth || height < _shipHeight)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_shipWidth = shipWidth;
|
||||
_shipHeight = shipHeight;
|
||||
EntityShip = new EntityShip(speed, weight, bodyColor);
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x + _shipWidth > _pictureWidth || y + _shipHeight > _pictureHeight)
|
||||
{
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush bodyBrush = new SolidBrush(EntityShip.BodyColor);
|
||||
//корпус
|
||||
Point[] hull = new Point[] {new Point(_startPosX + 5, _startPosY), new Point(_startPosX + 100, _startPosY),
|
||||
new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 100, _startPosY + 50), new Point(_startPosX + 5, _startPosY + 50)};
|
||||
g.FillPolygon(bodyBrush, hull);
|
||||
g.DrawPolygon(pen, hull);
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
g.FillRectangle(brBlack, _startPosX, _startPosY + 6, 5, 13);
|
||||
g.FillRectangle(brBlack, _startPosX, _startPosY + 31, 5, 13);
|
||||
//надстройки
|
||||
Brush brDark = new SolidBrush(Color.DarkGray);
|
||||
g.FillRectangle(brDark, _startPosX + 39, _startPosY + 20, 40, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 39, _startPosY + 20, 40, 10);
|
||||
g.FillRectangle(brDark, _startPosX + 70, _startPosY + 12, 18, 26);
|
||||
g.DrawRectangle(pen, _startPosX + 70, _startPosY + 12, 18, 26);
|
||||
g.FillEllipse(brBlack, _startPosX + 94, _startPosY + 19, 12, 12);
|
||||
}
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _shipWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _shipHeight;
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
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 + EntityShip.Step + _shipWidth < _pictureWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityShip.Step + _shipHeight < _pictureHeight,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
_startPosX -= (int)EntityShip.Step;
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
_startPosY -= (int)EntityShip.Step;
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
_startPosX += (int)EntityShip.Step;
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
_startPosY += (int)EntityShip.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,25 +4,13 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectBattleship
|
||||
namespace ProjectBattleship.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс - сущность "Линкор"
|
||||
/// Класс-сущность "Линкор"
|
||||
/// </summary>
|
||||
public class EntityBattleship
|
||||
public class EntityBattleship : EntityShip
|
||||
{
|
||||
/// <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>
|
||||
@ -36,11 +24,7 @@ namespace ProjectBattleship
|
||||
/// </summary>
|
||||
public bool RocketLauncher { get; private set; }
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса линкора
|
||||
/// Конструктор и инициализация полей объекта-класса линкора
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
@ -48,12 +32,10 @@ namespace ProjectBattleship
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="turret">Признак наличия орудийной башни</param>
|
||||
/// <param name="rocketLauncher">Признак наличия контейнера для ракет</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
public EntityBattleship(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool turret, bool rocketLauncher)
|
||||
: base(speed, weight, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Turret = turret;
|
||||
RocketLauncher = rocketLauncher;
|
||||
|
43
ProjectBattleship/ProjectBattleship/EntityShip.cs
Normal file
43
ProjectBattleship/ProjectBattleship/EntityShip.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectBattleship.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность "Корабль"
|
||||
/// </summary>
|
||||
public class EntityShip
|
||||
{
|
||||
/// <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 double Step => (double)Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Конструктор и инициализация полей (простого) объекта-класса линкора
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityShip(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
||||
}
|
@ -28,25 +28,28 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
buttonCreate = new Button();
|
||||
buttonCreateShip = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonRight = new Button();
|
||||
pictureBoxBattleship = new PictureBox();
|
||||
buttonCreateBattleship = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateShip
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 421);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(98, 28);
|
||||
buttonCreate.TabIndex = 0;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += ButtonCreate_Click;
|
||||
buttonCreateShip.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateShip.Location = new Point(128, 419);
|
||||
buttonCreateShip.Name = "buttonCreateShip";
|
||||
buttonCreateShip.Size = new Size(110, 30);
|
||||
buttonCreateShip.TabIndex = 0;
|
||||
buttonCreateShip.Text = "Создать корабль";
|
||||
buttonCreateShip.UseVisualStyleBackColor = true;
|
||||
buttonCreateShip.Click += ButtonCreateShip_Click;
|
||||
//
|
||||
// buttonDown
|
||||
//
|
||||
@ -106,16 +109,52 @@
|
||||
pictureBoxBattleship.TabIndex = 5;
|
||||
pictureBoxBattleship.TabStop = false;
|
||||
//
|
||||
// buttonCreateBattleship
|
||||
//
|
||||
buttonCreateBattleship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateBattleship.Location = new Point(12, 419);
|
||||
buttonCreateBattleship.Name = "buttonCreateBattleship";
|
||||
buttonCreateBattleship.Size = new Size(110, 30);
|
||||
buttonCreateBattleship.TabIndex = 6;
|
||||
buttonCreateBattleship.Text = "Создать линкор";
|
||||
buttonCreateBattleship.UseVisualStyleBackColor = true;
|
||||
buttonCreateBattleship.Click += ButtonCreateBattleship_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "к центру", "к границе" });
|
||||
comboBoxStrategy.Location = new Point(751, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(110, 23);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Location = new Point(751, 41);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(110, 30);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += ButtonStep_Click;
|
||||
//
|
||||
// FormBattleship
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(884, 461);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateBattleship);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateShip);
|
||||
Controls.Add(pictureBoxBattleship);
|
||||
Name = "FormBattleship";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
@ -128,11 +167,14 @@
|
||||
|
||||
#endregion
|
||||
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateShip;
|
||||
private Button buttonDown;
|
||||
private Button buttonUp;
|
||||
private Button buttonLeft;
|
||||
private Button buttonRight;
|
||||
private PictureBox pictureBoxBattleship;
|
||||
private Button buttonCreateBattleship;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
using ProjectBattleship.DrawingObjects;
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
|
||||
namespace ProjectBattleship
|
||||
{
|
||||
/// <summary>
|
||||
@ -8,7 +11,11 @@ namespace ProjectBattleship
|
||||
/// <summary>
|
||||
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
||||
/// </summary>
|
||||
private DrawingBattleship? _drawingBattleship;
|
||||
private DrawingShip? _drawingShip;
|
||||
/// <summary>
|
||||
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
/// <summary>
|
||||
/// Èíèöèàëèçàöèÿ ôîðìû
|
||||
/// </summary>
|
||||
@ -17,27 +24,53 @@ namespace ProjectBattleship
|
||||
InitializeComponent();
|
||||
}
|
||||
/// <summary>
|
||||
/// Ìåòîä ïðîðèñîâêè
|
||||
/// Ìåòîä ïðîðèñîâêè ìàøèíû
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingBattleship == null)
|
||||
if (_drawingShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new Bitmap(pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawingBattleship.DrawTransport(gr);
|
||||
_drawingShip.DrawTransport(gr);
|
||||
pictureBoxBattleship.Image = bmp;
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïîê äâèæåíèÿ
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ëèíêîð"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateBattleship_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new Random();
|
||||
_drawingShip = new DrawingBattleship(random.Next(100, 300), (double)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)), pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ëèíêîð"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateShip_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new Random();
|
||||
_drawingShip = new DrawingShip(random.Next(100, 300), (double)random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||
_drawingShip.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Ïåðåìåùåíèå îáúåêòà
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingBattleship == null)
|
||||
if (_drawingShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -45,33 +78,60 @@ namespace ProjectBattleship
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawingBattleship.MoveTransport(DirectionType.Up);
|
||||
_drawingShip.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawingBattleship.MoveTransport(DirectionType.Down);
|
||||
_drawingShip.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawingBattleship.MoveTransport(DirectionType.Left);
|
||||
_drawingShip.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawingBattleship.MoveTransport(DirectionType.Right);
|
||||
_drawingShip.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
private void ButtonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new Random();
|
||||
_drawingBattleship = new DrawingBattleship();
|
||||
_drawingBattleship.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)), pictureBoxBattleship.Width, pictureBoxBattleship.Height);
|
||||
_drawingBattleship.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
if (_drawingShip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawingObjectShip(_drawingShip), pictureBoxBattleship.Width,
|
||||
pictureBoxBattleship.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
ProjectBattleship/ProjectBattleship/IMoveableObject.cs
Normal file
35
ProjectBattleship/ProjectBattleship/IMoveableObject.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectBattleship;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
42
ProjectBattleship/ProjectBattleship/MoveToBorders.cs
Normal file
42
ProjectBattleship/ProjectBattleship/MoveToBorders.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
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;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
60
ProjectBattleship/ProjectBattleship/MoveToCenter.cs
Normal file
60
ProjectBattleship/ProjectBattleship/MoveToCenter.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using ProjectBattleship.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
ProjectBattleship/ProjectBattleship/ObjectParameters.cs
Normal file
57
ProjectBattleship/ProjectBattleship/ObjectParameters.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
18
ProjectBattleship/ProjectBattleship/Status.cs
Normal file
18
ProjectBattleship/ProjectBattleship/Status.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectBattleship.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Статус выполнения операции перемещения
|
||||
/// </summary>
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user