Merge pull request 'Presnyakova V.V Lab_2' (#2) from Lab_2 into Lab_1

Reviewed-on: http://student.git.athene.tech/Victoria_Presnyakova/Pibd-22_Presnyakova.V.V_Catamaran_Base/pulls/2
This commit is contained in:
Евгений Эгов 2022-10-28 09:07:56 +04:00
commit 2fd0bcdf6b
18 changed files with 1092 additions and 184 deletions

164
Catamaran/AbstractMap.cs Normal file
View File

@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal abstract class AbstractMap
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected readonly Random _random = new Random();
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
bool isFree = true;
int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x);
int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y);
int boatWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x);
int boatHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y);
switch (direction)
{
// вправо
case Direction.Right:
for (int i = boatWidth; i <= boatWidth + (int)(_drawingObject.Step / _size_x); i++)
{
for (int j = startPosY; j <= boatHeight; j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
//влево
case Direction.Left:
for (int i = startPosX; i >= (int)(_drawingObject.Step / _size_x); i--)
{
for (int j = startPosY; j <= boatHeight; j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
//вверх
case Direction.Up:
for (int i = startPosX; i <= boatWidth; i++)
{
for (int j = startPosY; j >= (int)(_drawingObject.Step / _size_y); j--)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
//вниз
case Direction.Down:
for (int i = startPosX; i <= boatWidth; i++)
{
for (int j = boatHeight; j <= boatHeight + (int)(_drawingObject.Step / _size_y); j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
}
if (isFree)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
_drawingObject.SetObject(x, y, _width, _height);
int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x);
int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y);
int boatWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x);
int boatHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y);
for (int i = startPosX; i <= boatWidth; i++)
{
for (int j = startPosY; j <= boatHeight; j++)
{
if (_map[i, j] == _barrier)
{
return false;
}
}
}
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new Bitmap(_width, _height);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawingObject.DrawingObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}

View File

@ -46,19 +46,35 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Direction.cs" />
<Compile Include="AbstractMap.cs" />
<Compile Include="DrawingCatamaran.cs" />
<Compile Include="DrawingObjectBoat.cs" />
<Compile Include="EntityCatamaran.cs" />
<Compile Include="CatamaranForm.cs">
<Compile Include="Direction.cs" />
<Compile Include="DrawingBoat.cs" />
<Compile Include="EntityBoat.cs" />
<Compile Include="FormBoat.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="CatamaranForm.Designer.cs">
<DependentUpon>CatamaranForm.cs</DependentUpon>
<Compile Include="FormBoat.Designer.cs">
<DependentUpon>FormBoat.cs</DependentUpon>
</Compile>
<Compile Include="FormMap.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="FormMap.Designer.cs">
<DependentUpon>FormMap.cs</DependentUpon>
</Compile>
<Compile Include="IDrawingObject.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="CatamaranForm.resx">
<DependentUpon>CatamaranForm.cs</DependentUpon>
<Compile Include="SecondMap.cs" />
<Compile Include="SimpleMap.cs" />
<EmbeddedResource Include="FormBoat.resx">
<DependentUpon>FormBoat.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="FormMap.resx">
<DependentUpon>FormMap.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>

View File

@ -11,6 +11,7 @@ namespace Catamaran
/// </summary>
internal enum Direction
{
None = 0,
Up = 1,
Down = 2,
Left = 3,

191
Catamaran/DrawingBoat.cs Normal file
View File

@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBoat Catamaran { set; get; }
/// <summary>
/// Левая координата отрисовки лодки
/// </summary>
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки лодки
/// </summary>
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
protected int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
protected int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки лодки
/// </summary>
protected readonly int _catamaranWidth = 80;
/// <summary>
/// Высота отрисовки лодки
/// </summary>
protected readonly int _catamaranHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет лодки</param>
public DrawingBoat(int speed, float weight, Color bodyColor)
{
Catamaran = new EntityBoat(speed, weight, bodyColor);
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom)
GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _catamaranWidth, _startPosY +
_catamaranHeight);
}
/// <summary>
/// Установка позиции лодки
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || y < 0 || x + _catamaranWidth > width || y + _catamaranHeight > height) return;
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _catamaranWidth + Catamaran.Step < _pictureWidth)
{
_startPosX += Catamaran.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Catamaran.Step > 0)
{
_startPosX -= Catamaran.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Catamaran.Step > 0)
{
_startPosY -= Catamaran.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _catamaranHeight + Catamaran.Step < _pictureHeight)
{
_startPosY += Catamaran.Step;
}
break;
}
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="carWidth">Ширина отрисовки автомобиля</param>
/// <param name="carHeight">Высота отрисовки автомобиля</param>
protected DrawingBoat(int speed, float weight, Color bodyColor, int
catamaranWidth, int catamaranHeight) :
this(speed, weight, bodyColor)
{
_catamaranWidth = catamaranWidth;
_catamaranHeight = catamaranHeight;
}
/// <summary>
/// Отрисовка лодки
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
Pen pen = new Pen(Color.Black);
//границы
g.DrawRectangle(pen, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight);
Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)_startPosY);
Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2));
Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)(_startPosY + _catamaranHeight));
Point[] pointsArray = {point_1, point_2, point_3};
g.DrawPolygon(pen, pointsArray);
Brush br = new SolidBrush(Catamaran?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight);
g.FillPolygon(br, pointsArray);
// середина
g.DrawEllipse(pen, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 11);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillEllipse(brBlue, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 11);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _catamaranWidth || _pictureHeight <= _catamaranHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _catamaranWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _catamaranWidth;
}
if (_startPosY + _catamaranHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _catamaranHeight;
}
}
}
}

View File

@ -7,158 +7,56 @@ using System.Threading.Tasks;
namespace Catamaran
{
internal class DrawingCatamaran
internal class DrawingCatamaran : DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCatamaran Catamaran { private set; get; }
/// <summary>
/// Левая координата отрисовки лодки
/// </summary>
private float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки лодки
/// </summary>
private float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки лодки
/// </summary>
private readonly int _catamaranWidth = 80;
/// <summary>
/// Высота отрисовки лодки
/// </summary>
private readonly int _catamaranHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет лодки</param>
public void Init(int speed, float weight, Color bodyColor)
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="Floats">Признак наличия обвеса</param>
/// <param name="Sail">Признак наличия антикрыла</param>
public DrawingCatamaran(int speed, float weight, Color bodyColor, Color
dopColor, bool Floats, bool Sail) :
base(speed, weight, bodyColor, 110, 60)
{
Catamaran = new EntityCatamaran();
Catamaran.Init(speed, weight, bodyColor);
Catamaran = new EntityCatamaran(speed, weight, bodyColor, dopColor, Floats,
Sail);
}
/// <summary>
/// Установка позиции лодки
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
public override void DrawTransport(Graphics g)
{
if (x < 0 || y < 0 || x + _catamaranWidth > width || y + _catamaranHeight > height) return;
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _catamaranWidth + Catamaran.Step < _pictureWidth)
{
_startPosX += Catamaran.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Catamaran.Step > 0)
{
_startPosX -= Catamaran.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Catamaran.Step > 0)
{
_startPosY -= Catamaran.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _catamaranHeight + Catamaran.Step < _pictureHeight)
{
_startPosY += Catamaran.Step;
}
break;
}
}
/// <summary>
/// Отрисовка лодки
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
if (!(Catamaran is EntityCatamaran catamaranPro))
{
return;
}
Pen pen = new Pen(Color.Black);
//границы
g.DrawRectangle(pen, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight);
Brush dopBrush = new SolidBrush(catamaranPro.DopColor);
if (catamaranPro.Floats)
{
g.DrawEllipse(pen, _startPosX, _startPosY, (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2));
g.FillEllipse(dopBrush, _startPosX, _startPosY, (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2));
g.DrawEllipse(pen, _startPosX, 10 + _startPosY + (int)(_catamaranHeight / 2), (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2));
g.FillEllipse(dopBrush, _startPosX, 10 + _startPosY + (int)(_catamaranHeight / 2), (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2));
Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)_startPosY);
Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2));
Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)(_startPosY + _catamaranHeight));
Point[] pointsArray = {point_1, point_2, point_3};
g.DrawPolygon(pen, pointsArray);
Brush br = new SolidBrush(Catamaran?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight);
g.FillPolygon(br, pointsArray);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (catamaranPro.Sail)
{
Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 2 / 4), (int)_startPosY + 5);
Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2));
Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 2 / 4), (int)(_startPosY + _catamaranHeight));
Point[] pointsArray = { point_1, point_2, point_3 };
g.DrawPolygon(pen, pointsArray);
g.FillPolygon(dopBrush, pointsArray);
// середина
g.DrawEllipse(pen, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 15);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillEllipse(brBlue, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 15);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _catamaranWidth || _pictureHeight <= _catamaranHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _catamaranWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _catamaranWidth;
}
if (_startPosY + _catamaranHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _catamaranHeight;
}
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class DrawingObjectBoat : IDrawingObject
{
private DrawingBoat _catamaran = null;
public DrawingObjectBoat(DrawingBoat catamaran)
{
_catamaran = catamaran;
}
public float Step => _catamaran?.Catamaran?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom)
GetCurrentPosition()
{
return _catamaran?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_catamaran?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_catamaran.SetPosition(x, y, width, height);
}
public void DrawingObject(Graphics g)
{
_catamaran.DrawTransport(g);
}
}
}

43
Catamaran/EntityBoat.cs Normal file
View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class EntityBoat
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public float Weight { get; private set; }
/// <summary>
/// Цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения
/// </summary>
public float Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public EntityBoat(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
}
}

View File

@ -7,37 +7,33 @@ using System.Threading.Tasks;
namespace Catamaran
{
internal class EntityCatamaran
internal class EntityCatamaran : EntityBoat
{
public Color DopColor { get; private set; }
/// <summary>
/// Скорость
/// Признак наличия поплавков
/// </summary>
public int Speed { get; private set; }
public bool Floats { get; private set; }
/// <summary>
/// Вес
/// Признак наличия паруса
/// </summary>
public float Weight { get; private set; }
public bool Sail { get; private set; }
/// <summary>
/// Цвет
/// Инициализация свойств
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения
/// </summary>
public float Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public void Init(int speed, float weight, Color bodyColor)
/// /// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="Floats">Признак наличия поплавков</param>
/// <param name="Sail">Признак наличия паруса</param>
public EntityCatamaran(int speed, float weight, Color bodyColor, Color
dopColor, bool Floats, bool Sail) :
base(speed, weight, bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
DopColor = dopColor;
this.Floats = Floats;
this.Sail = Sail;
}
}
}

View File

@ -1,6 +1,6 @@
namespace Catamaran
{
partial class CatamaranForm
partial class FormBoat
{
/// <summary>
/// Обязательная переменная конструктора.
@ -72,7 +72,6 @@
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(55, 25);
this.toolStripStatusLabelColor.Text = "Color";
this.toolStripStatusLabelColor.Click += new System.EventHandler(this.toolStripStatusLabel3_Click);
//
// pictureBoxCatamaran
//
@ -158,7 +157,6 @@
this.Controls.Add(this.statusStrip1);
this.Name = "CatamaranForm";
this.Text = "Catamaran";
this.Load += new System.EventHandler(this.CatamaranForm_Load);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).EndInit();

View File

@ -10,10 +10,10 @@ using System.Windows.Forms;
namespace Catamaran
{
public partial class CatamaranForm : Form
public partial class FormBoat : Form
{
private DrawingCatamaran _catamaran;
public CatamaranForm()
private DrawingBoat _catamaran;
public FormBoat()
{
InitializeComponent();
}
@ -24,20 +24,11 @@ namespace Catamaran
_catamaran?.DrawTransport(gr);
pictureBoxCatamaran.Image = bmp;
}
private void CatamaranForm_Load(object sender, EventArgs e)
{
}
private void toolStripStatusLabel3_Click(object sender, EventArgs e)
{
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_catamaran = new DrawingCatamaran();
_catamaran.Init(rnd.Next(100, 300), rnd.Next(1000, 2000),
_catamaran = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_catamaran.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100),
pictureBoxCatamaran.Width, pictureBoxCatamaran.Height);

209
Catamaran/FormMap.Designer.cs generated Normal file
View File

@ -0,0 +1,209 @@
namespace Catamaran
{
partial class FormMap
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelColor = new System.Windows.Forms.ToolStripStatusLabel();
this.pictureBoxCatamaran = new System.Windows.Forms.PictureBox();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonCreate = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).BeginInit();
this.SuspendLayout();
//
// statusStrip1
//
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelColor});
this.statusStrip1.Location = new System.Drawing.Point(0, 436);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(826, 32);
this.statusStrip1.TabIndex = 0;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(62, 25);
this.toolStripStatusLabelSpeed.Text = "Speed";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(68, 25);
this.toolStripStatusLabelWeight.Text = "Weight";
//
// toolStripStatusLabelColor
//
this.toolStripStatusLabelColor.Name = "toolStripStatusLabelColor";
this.toolStripStatusLabelColor.Size = new System.Drawing.Size(55, 25);
this.toolStripStatusLabelColor.Text = "Color";
//
// pictureBoxCatamaran
//
this.pictureBoxCatamaran.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCatamaran.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCatamaran.Name = "pictureBoxCatamaran";
this.pictureBoxCatamaran.Size = new System.Drawing.Size(826, 436);
this.pictureBoxCatamaran.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCatamaran.TabIndex = 1;
this.pictureBoxCatamaran.TabStop = false;
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Catamaran.Properties.Resources.Up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(683, 344);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 2;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Catamaran.Properties.Resources.Down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(683, 380);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 3;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Catamaran.Properties.Resources.Left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(647, 372);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 4;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Catamaran.Properties.Resources.Right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(719, 372);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(14, 380);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(70, 30);
this.buttonCreate.TabIndex = 6;
this.buttonCreate.Text = "Cteate";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"SimpleMap",
"SecondMap"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 28);
this.comboBoxSelectorMap.TabIndex = 7;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged);
//
// buttonCreateModif
//
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(117, 380);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(102, 30);
this.buttonCreateModif.TabIndex = 8;
this.buttonCreateModif.Text = "Modification";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click);
//
// FormMap
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(826, 468);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.comboBoxSelectorMap);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.pictureBoxCatamaran);
this.Controls.Add(this.statusStrip1);
this.Name = "FormMap";
this.Text = "Map";
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCatamaran)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelWeight;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelColor;
private System.Windows.Forms.PictureBox pictureBoxCatamaran;
private System.Windows.Forms.Button buttonUp;
private System.Windows.Forms.Button buttonDown;
private System.Windows.Forms.Button buttonLeft;
private System.Windows.Forms.Button buttonRight;
private System.Windows.Forms.Button buttonCreate;
private System.Windows.Forms.ComboBox comboBoxSelectorMap;
private System.Windows.Forms.Button buttonCreateModif;
}
}

97
Catamaran/FormMap.cs Normal file
View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Catamaran
{
public partial class FormMap : Form
{
private AbstractMap _abstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
}
/// <summary>
/// Заполнение информации по объекту
/// </summary>
/// <param name="car"></param>
private void SetData(DrawingBoat catamaran)
{
toolStripStatusLabelSpeed.Text = $"Скорость: {catamaran.Catamaran.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {catamaran.Catamaran.Weight}";
toolStripStatusLabelColor.Text = $"Цвет: { catamaran.Catamaran.BodyColor.Name}";
pictureBoxCatamaran.Image = _abstractMap.CreateMap(pictureBoxCatamaran.Width,pictureBoxCatamaran.Height,new DrawingObjectBoat(catamaran));
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
var _catamaran = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
//_catamaran.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100),
//pictureBoxCatamaran.Width, pictureBoxCatamaran.Height);
SetData(_catamaran);
}
private void buttonMove_Click(object sender, EventArgs e)
{
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBoxCatamaran.Image = _abstractMap?.MoveObject(dir);
}
private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorMap.Text)
{
case "SimpleMap":
_abstractMap = new SimpleMap();
break;
case "SecondMap":
_abstractMap = new SecondMap();
break;
}
}
private void buttonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new Random();
var catamaran = new DrawingCatamaran(rnd.Next(100, 300), rnd.Next(1000,
2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,
256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,
256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,
2)));
SetData(catamaran);
}
}
}

123
Catamaran/FormMap.resx Normal file
View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal interface IDrawingObject
{
/// <summary>
/// Шаг перемещения объекта
/// </summary>
float Step { get; }
/// <summary>
/// Установка позиции объекта
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина полотна</param>
/// <param name="height">Высота полотна</param>
void SetObject(int x, int y, int width, int height);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(Direction direction);
/// <summary>
/// Отрисовка объекта
/// </summary>
/// <param name="g"></param>
void DrawingObject(Graphics g);
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
(float Left, float Right, float Top, float Bottom)
GetCurrentPosition();
}
}

View File

@ -16,7 +16,7 @@ namespace Catamaran
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CatamaranForm());
Application.Run(new FormMap());
}
}
}

48
Catamaran/SecondMap.cs Normal file
View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class SecondMap : AbstractMap
{
private readonly Brush barrierColor = new SolidBrush(Color.Black);
private readonly Brush roadColor = new SolidBrush(Color.DarkCyan);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 20)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

53
Catamaran/SimpleMap.cs Normal file
View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class SimpleMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Black);
/// <summary>
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.Gray);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}