PIbd-21. Shanygin A.V. Lab work 02 #2
97
AircraftCarrier/AircraftCarrier/AbstractStrategy.cs
Normal file
97
AircraftCarrier/AircraftCarrier/AbstractStrategy.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AircraftCarrier.DrawningObjects;
|
||||
namespace AircraftCarrier.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; }
|
||||
/// Установка данных
|
||||
/// <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;
|
||||
}
|
||||
/// Шаг перемещения
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// Перемещение влево
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false -неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(Direction.Left);
|
||||
/// Перемещение вправо
|
||||
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(Direction.Right);
|
||||
/// Перемещение вверх
|
||||
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(Direction.Up);
|
||||
/// Перемещение вниз
|
||||
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(Direction.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();
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// <param name="directionType">Направление</param>
|
||||
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||
private bool MoveTo(Direction directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
153
AircraftCarrier/AircraftCarrier/DrawningAircraft.cs
Normal file
153
AircraftCarrier/AircraftCarrier/DrawningAircraft.cs
Normal file
@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AircraftCarrier.Entities;
|
||||
namespace AircraftCarrier.DrawningObjects
|
||||
{
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
public class DrawningAircraft
|
||||
{
|
||||
/// Класс-сущность
|
||||
public EntityAircraft? EntityAircraft { get; protected set; }
|
||||
/// Ширина окна
|
||||
private int _pictureWidth;
|
||||
/// Высота окна
|
||||
private int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected readonly int _AircraftWidth = 164;
|
||||
protected readonly int _AircraftHeight = 40;
|
||||
/// Координата X объекта
|
||||
public int GetPosX => _startPosX;
|
||||
/// Координата Y объекта
|
||||
public int GetPosY => _startPosY;
|
||||
/// Ширина объекта
|
||||
public int GetWidth => _AircraftWidth;
|
||||
/// Высота объекта
|
||||
public int GetHeight => _AircraftHeight;
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
public bool CanMove(Direction direction)
|
||||
{
|
||||
if (EntityAircraft == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
Direction.Left => _startPosX - EntityAircraft.Step > 0,
|
||||
//вверх
|
||||
Direction.Up => _startPosY - EntityAircraft.Step > 0,
|
||||
// вправо
|
||||
Direction.Right => _startPosX + EntityAircraft.Step + _AircraftWidth < _pictureWidth,
|
||||
//вниз
|
||||
Direction.Down => _startPosY + EntityAircraft.Step + _AircraftHeight < _pictureHeight,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
/// Изменение направления перемещения
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityAircraft == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case Direction.Left:
|
||||
_startPosX -= (int)EntityAircraft.Step;
|
||||
break;
|
||||
//вверх
|
||||
case Direction.Up:
|
||||
_startPosY -= (int)EntityAircraft.Step;
|
||||
break;
|
||||
// вправо
|
||||
case Direction.Right:
|
||||
_startPosX += (int)EntityAircraft.Step;
|
||||
break;
|
||||
//вниз
|
||||
case Direction.Down:
|
||||
_startPosY += (int)EntityAircraft.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// Конструктор
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
public DrawningAircraft(int speed, double weight, Color bodyColor, int
|
||||
width, int height)
|
||||
{
|
||||
if (width < _AircraftWidth || height < _AircraftHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityAircraft = new EntityAircraft(speed, weight, bodyColor);
|
||||
}
|
||||
/// Конструктор
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <param name="carWidth">Ширина прорисовки автомобиля</param>
|
||||
/// <param name="carHeight">Высота прорисовки автомобиля</param>
|
||||
protected DrawningAircraft(int speed, double weight, Color bodyColor,int
|
||||
width, int height, int AircraftWidth, int AircraftHeight)
|
||||
{
|
||||
if (width < _AircraftWidth || height < _AircraftHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_AircraftWidth = AircraftWidth;
|
||||
_AircraftHeight = AircraftHeight;
|
||||
EntityAircraft = new EntityAircraft(speed, weight, bodyColor);
|
||||
|
||||
}
|
||||
/// Установка позиции
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _AircraftWidth) || _startPosY > (_pictureHeight - _AircraftHeight))
|
||||
{
|
||||
_startPosX = 50;
|
||||
_startPosY = 50;
|
||||
}
|
||||
}
|
||||
/// Изменение направления перемещения
|
||||
/// Прорисовка объекта
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityAircraft == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush budyBrush = new SolidBrush(EntityAircraft.BodyColor);
|
||||
g.DrawLine(pen, _startPosX + 4, _startPosY, _startPosX + 124, _startPosY);
|
||||
g.DrawLine(pen, _startPosX + 124, _startPosY, _startPosX + 164, _startPosY + 20);
|
||||
g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
|
||||
g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
|
||||
g.DrawLine(pen, _startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
|
||||
g.DrawLine(pen, _startPosX + 4, _startPosY + 40, _startPosX + 4, _startPosY);
|
||||
g.FillRectangle(budyBrush, _startPosX, _startPosY + 6, 4, 12);
|
||||
g.FillRectangle(budyBrush, _startPosX, _startPosY + 24, 4, 12);
|
||||
g.DrawEllipse(pen, _startPosX + 115, _startPosY + 13, 14, 14);
|
||||
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 13, 27, 14);
|
||||
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 9, 14, 22);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,111 +3,41 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace AircraftCarrier
|
||||
using AircraftCarrier.Entities;
|
||||
namespace AircraftCarrier.DrawningObjects
|
||||
{
|
||||
internal class DrawningAircraftCarrier
|
||||
public class DrawningAircraftCarrier : DrawningAircraft
|
||||
{
|
||||
public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; }
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private readonly int _AircraftCarrierWidth = 164;
|
||||
private readonly int _AircraftCarrierHeight = 40;
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height)
|
||||
public DrawningAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool cabin, bool runway, int width, int height) :
|
||||
base(speed, weight, bodyColor, width, height, 164, 40)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if(_AircraftCarrierWidth <_pictureWidth || _AircraftCarrierHeight <_pictureHeight)
|
||||
if (EntityAircraft != null)
|
||||
{
|
||||
EntityAircraftCarrier = new EntityAircraftCarrier();
|
||||
EntityAircraftCarrier.Init(speed, weight, bodyColor, additionalColor,
|
||||
bodyKit, wing, sportLine);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _AircraftCarrierWidth) || _startPosY>(_pictureHeight - _AircraftCarrierHeight))
|
||||
{
|
||||
_startPosX = 50;
|
||||
_startPosY = 50;
|
||||
EntityAircraft = new EntityAircraftCarrier(speed, weight, bodyColor,
|
||||
additionalColor, cabin, runway);
|
||||
}
|
||||
}
|
||||
public void MoveTransport(Direction direction)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityAircraftCarrier == null)
|
||||
if (EntityAircraft is not EntityAircraftCarrier aircraftCarrie)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case Direction.Left:
|
||||
if (_startPosX - EntityAircraftCarrier.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityAircraftCarrier.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Direction.Up:
|
||||
if (_startPosY - EntityAircraftCarrier.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityAircraftCarrier.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case Direction.Right:
|
||||
if (_startPosX + EntityAircraftCarrier.Step + _AircraftCarrierWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityAircraftCarrier.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Direction.Down:
|
||||
if(_startPosY + EntityAircraftCarrier.Step + _AircraftCarrierHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityAircraftCarrier.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityAircraftCarrier == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityAircraftCarrier.AdditionalColor);
|
||||
g.DrawLine(pen, _startPosX+4, _startPosY, _startPosX + 124, _startPosY);
|
||||
g.DrawLine(pen, _startPosX+124, _startPosY, _startPosX + 164, _startPosY+20);
|
||||
g.DrawLine(pen, _startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40);
|
||||
g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
|
||||
g.DrawLine(pen, _startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
|
||||
g.DrawLine(pen, _startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY);
|
||||
g.FillRectangle(additionalBrush, _startPosX, _startPosY + 6, 4, 12);
|
||||
g.FillRectangle(additionalBrush, _startPosX, _startPosY + 24, 4, 12);
|
||||
g.DrawEllipse(pen, _startPosX + 115, _startPosY + 13, 14, 14);
|
||||
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 13, 27, 14);
|
||||
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 9, 14, 22);
|
||||
Pen penWhite = new(Color.White);
|
||||
if (EntityAircraftCarrier.BodyKit)
|
||||
Brush additionalBrush = new SolidBrush(aircraftCarrie.AdditionalColor);
|
||||
Pen pen = new(Color.White);
|
||||
if (aircraftCarrie.Cabin)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX + 4, _startPosY + 13, 59, 14);
|
||||
g.DrawLine(penWhite, _startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19);
|
||||
g.DrawLine(penWhite, _startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19);
|
||||
g.DrawLine(penWhite, _startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19);
|
||||
g.DrawLine(penWhite, _startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19);
|
||||
g.DrawLine(pen, _startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19);
|
||||
g.DrawLine(pen, _startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19);
|
||||
g.DrawLine(pen, _startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19);
|
||||
g.DrawLine(pen, _startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19);
|
||||
}
|
||||
if (EntityAircraftCarrier.Wing)
|
||||
if (aircraftCarrie.Runway)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9);
|
||||
}
|
||||
base.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
35
AircraftCarrier/AircraftCarrier/DrawningObjectAircraft.cs
Normal file
35
AircraftCarrier/AircraftCarrier/DrawningObjectAircraft.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AircraftCarrier.DrawningObjects;
|
||||
namespace AircraftCarrier.MovementStrategy
|
||||
{
|
||||
public class DrawningObjectAircraft : IMoveableObject
|
||||
{
|
||||
private readonly DrawningAircraft? _drawningAircraft = null;
|
||||
public DrawningObjectAircraft(DrawningAircraft drawningAircraft)
|
||||
{
|
||||
_drawningAircraft = drawningAircraft;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawningAircraft == null || _drawningAircraft.EntityAircraft ==
|
||||
null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningAircraft.GetPosX,
|
||||
_drawningAircraft.GetPosY, _drawningAircraft.GetWidth, _drawningAircraft.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawningAircraft?.EntityAircraft?.Step ?? 0);
|
||||
public bool CheckCanMove(Direction direction) =>
|
||||
_drawningAircraft?.CanMove(direction) ?? false;
|
||||
public void MoveObject(Direction direction) =>
|
||||
_drawningAircraft?.MoveTransport(direction);
|
||||
}
|
||||
}
|
29
AircraftCarrier/AircraftCarrier/EntityAircraft.cs
Normal file
29
AircraftCarrier/AircraftCarrier/EntityAircraft.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace AircraftCarrier.Entities
|
||||
{
|
||||
/// Класс-сущность "Автомобиль"
|
||||
public class EntityAircraft
|
||||
{
|
||||
/// Скорость
|
||||
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;
|
||||
/// Конструктор с параметрами
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityAircraft(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,53 +3,23 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AircraftCarrier
|
||||
namespace AircraftCarrier.Entities
|
||||
{
|
||||
public class EntityAircraftCarrier
|
||||
/// Класс-сущность "Спортивный автомобиль"
|
||||
public class EntityAircraftCarrier : EntityAircraft
|
||||
{
|
||||
/// <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 BodyKit { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия антикрыла
|
||||
/// </summary>
|
||||
public bool Wing { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия гоночной полосы
|
||||
/// </summary>
|
||||
public bool SportLine { get; private set; }
|
||||
/// <summary>
|
||||
/// Шаг перемещения автомобиля
|
||||
/// </summary>
|
||||
/// Признак (опция) наличия кабины
|
||||
public bool Cabin { get; private set; }
|
||||
/// Признак (опция) наличия взлетной полосы
|
||||
public bool Runway { get; private set; }
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool bodyKit, bool wing, bool sportLine)
|
||||
public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool cabin, bool runway) : base(speed, weight, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
BodyKit = bodyKit;
|
||||
Wing = wing;
|
||||
SportLine = sportLine;
|
||||
Cabin = cabin;
|
||||
Runway = runway;
|
||||
}
|
||||
}
|
||||
}
|
@ -29,11 +29,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBox = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
ButtonCreateAircraftCarrier = new Button();
|
||||
ButtonCreateAircraft = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -48,17 +51,6 @@
|
||||
pictureBox.TabStop = false;
|
||||
pictureBox.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreate
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(23, 403);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(94, 29);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
//
|
||||
// buttonUp
|
||||
//
|
||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
@ -107,16 +99,63 @@
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonMove_Click;
|
||||
//
|
||||
// ButtonCreateAircraftCarrier
|
||||
//
|
||||
ButtonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
ButtonCreateAircraftCarrier.Location = new Point(234, 393);
|
||||
ButtonCreateAircraftCarrier.Name = "ButtonCreateAircraftCarrier";
|
||||
ButtonCreateAircraftCarrier.Size = new Size(204, 39);
|
||||
ButtonCreateAircraftCarrier.TabIndex = 6;
|
||||
ButtonCreateAircraftCarrier.Text = "Создать военный корабль";
|
||||
ButtonCreateAircraftCarrier.UseVisualStyleBackColor = true;
|
||||
ButtonCreateAircraftCarrier.Click += ButtonCreateAircraftCarrier_Click;
|
||||
//
|
||||
// ButtonCreateAircraft
|
||||
//
|
||||
ButtonCreateAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
ButtonCreateAircraft.Location = new Point(12, 393);
|
||||
ButtonCreateAircraft.Name = "ButtonCreateAircraft";
|
||||
ButtonCreateAircraft.Size = new Size(204, 39);
|
||||
ButtonCreateAircraft.TabIndex = 7;
|
||||
ButtonCreateAircraft.Text = "Создать корабль";
|
||||
ButtonCreateAircraft.UseVisualStyleBackColor = true;
|
||||
ButtonCreateAircraft.Click += ButtonCreateAircraft_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
|
||||
comboBoxStrategy.Location = new Point(706, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 28);
|
||||
comboBoxStrategy.TabIndex = 8;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStep.Location = new Point(763, 46);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(94, 29);
|
||||
buttonStep.TabIndex = 9;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// FormAircraftCarrier
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(882, 453);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(ButtonCreateAircraft);
|
||||
Controls.Add(ButtonCreateAircraftCarrier);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(pictureBox);
|
||||
Name = "FormAircraftCarrier";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
@ -129,10 +168,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBox;
|
||||
private Button buttonCreate;
|
||||
private Button buttonUp;
|
||||
private Button buttonLeft;
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button ButtonCreateAircraft;
|
||||
private Button ButtonCreateAircraftCarrier;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,43 +1,32 @@
|
||||
using AircraftCarrier.DrawningObjects;
|
||||
using AircraftCarrier.MovementStrategy;
|
||||
using System;
|
||||
|
||||
namespace AircraftCarrier
|
||||
{
|
||||
public partial class FormAircraftCarrier : System.Windows.Forms.Form
|
||||
public partial class FormAircraftCarrier : Form
|
||||
{
|
||||
private DrawningAircraftCarrier? _drawningAircraftCarrier;
|
||||
private DrawningAircraft? _drawingAircraft;
|
||||
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
public FormAircraftCarrier()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningAircraftCarrier == null)
|
||||
if (_drawingAircraft == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBox.Width, pictureBox.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningAircraftCarrier.DrawTransport(gr);
|
||||
_drawingAircraft.DrawTransport(gr);
|
||||
pictureBox.Image = bmp;
|
||||
}
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningAircraftCarrier = new DrawningAircraftCarrier();
|
||||
_drawningAircraftCarrier.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)), Convert.ToBoolean(random.Next(0, 2)),
|
||||
pictureBox.Width, pictureBox.Height);
|
||||
_drawningAircraftCarrier.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningAircraftCarrier == null)
|
||||
if (_drawingAircraft == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -45,19 +34,82 @@ namespace AircraftCarrier
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawningAircraftCarrier.MoveTransport(Direction.Up);
|
||||
_drawingAircraft.MoveTransport(Direction.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawningAircraftCarrier.MoveTransport(Direction.Down);
|
||||
_drawingAircraft.MoveTransport(Direction.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawningAircraftCarrier.MoveTransport(Direction.Left);
|
||||
_drawingAircraft.MoveTransport(Direction.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawningAircraftCarrier.MoveTransport(Direction.Right);
|
||||
_drawingAircraft.MoveTransport(Direction.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateAircraft_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingAircraft = new DrawningAircraft(random.Next(100, 300),
|
||||
random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
||||
random.Next(0, 256)),
|
||||
pictureBox.Width, pictureBox.Height);
|
||||
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingAircraft = new DrawningAircraftCarrier(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)), pictureBox.Width, pictureBox.Height);
|
||||
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingAircraft == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawningObjectAircraft(_drawingAircraft), pictureBox.Width,
|
||||
pictureBox.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
22
AircraftCarrier/AircraftCarrier/IMoveableObject.cs
Normal file
22
AircraftCarrier/AircraftCarrier/IMoveableObject.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using AircraftCarrier.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AircraftCarrier.DrawningObjects;
|
||||
namespace AircraftCarrier.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// Получение координаты X объекта
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// Шаг объекта
|
||||
int GetStep { get; }
|
||||
/// Проверка, можно ли переместиться по нужному направлению
|
||||
bool CheckCanMove(Direction direction);
|
||||
/// Изменение направления пермещения объекта
|
||||
/// <param name="direction">Направление</param>
|
||||
void MoveObject(Direction direction);
|
||||
}
|
||||
}
|
25
AircraftCarrier/AircraftCarrier/MoveToBorder.cs
Normal file
25
AircraftCarrier/AircraftCarrier/MoveToBorder.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using AircraftCarrier.MovementStrategy;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace AircraftCarrier.MovementStrategy
|
||||
{
|
||||
internal class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null) return false;
|
||||
return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null) return;
|
||||
if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight();
|
||||
if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
55
AircraftCarrier/AircraftCarrier/MoveToCenter.cs
Normal file
55
AircraftCarrier/AircraftCarrier/MoveToCenter.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace AircraftCarrier.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
AircraftCarrier/AircraftCarrier/ObjectParameters.cs
Normal file
39
AircraftCarrier/AircraftCarrier/ObjectParameters.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace AircraftCarrier.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;
|
||||
/// Конструктор
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,7 @@ namespace AircraftCarrier
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
|
14
AircraftCarrier/AircraftCarrier/Status.cs
Normal file
14
AircraftCarrier/AircraftCarrier/Status.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace AircraftCarrier.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user