Лабораторная 2

This commit is contained in:
nikbel2004@outlook.com 2023-10-04 03:06:05 +04:00
parent 72a71f346b
commit e0874c2924
17 changed files with 651 additions and 210 deletions

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.6.33801.468 VisualStudioVersion = 17.6.33801.468
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{394C34D3-98AF-48A5-B765-763149963292}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tank", "Tank\Tank.csproj", "{19BA0FA1-372B-490A-AA7F-757B0B8F322B}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -11,15 +11,15 @@ Global
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{394C34D3-98AF-48A5-B765-763149963292}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{394C34D3-98AF-48A5-B765-763149963292}.Debug|Any CPU.Build.0 = Debug|Any CPU {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{394C34D3-98AF-48A5-B765-763149963292}.Release|Any CPU.ActiveCfg = Release|Any CPU {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{394C34D3-98AF-48A5-B765-763149963292}.Release|Any CPU.Build.0 = Release|Any CPU {19BA0FA1-372B-490A-AA7F-757B0B8F322B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {29179072-02D8-4625-BA39-9548F5C3A015} SolutionGuid = {5332F2FC-D1C2-4B42-B32E-53ECC094BBB7}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldHeight = height;
FieldWidth = width;
}
public void MakeStep()
{
if (_state != Status.InProgress)
return;
if (IsTargetDestination())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(Direction.Left);
protected bool MoveRight() => MoveTo(Direction.Right);
protected bool MoveUp() => MoveTo(Direction.Up);
protected bool MoveDown() => MoveTo(Direction.Down);
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectParameters;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestination();
private bool MoveTo(Direction direction)
{
if (_state != Status.InProgress)
return false;
if (_moveableObject?.CheckCanMove(direction) ?? false)
{
_moveableObject.MoveObject(direction);
return true;
}
return false;
}
}
}

23
Tank/Tank/AddBase.cs Normal file
View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.Entities
{
public class AddBase : Base
{
public Color AdditionalColor { get; private set; }
public bool BodyKit { get; private set; }
public bool Caterpillar { get; private set; }
public bool Rinks { get; private set; }
public AddBase(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool caterpillar, bool rinks) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Caterpillar = caterpillar;
Rinks = rinks;
}
}
}

22
Tank/Tank/Base.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.Entities
{
public class Base
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public Base(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.Pkcs;
using System.Text;
using System.Threading.Tasks;
using Tank.Entities;
namespace Tank
{
public class DrawArmoVehicle
{
public Base? Tank { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _Width = 160;
protected readonly int _Height = 90;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _Width;
public int GetHeight => _Height;
//==========ДОРАБОТКА================//
public bool CanStopObject(Direction direction)
{
return false;
}
//==================================//
public bool CanMove(Direction direction)
{
if (Tank == null)
return false;
return direction switch
{
Direction.Left => _startPosX - Tank.Step > 0,
Direction.Up => _startPosY - Tank.Step > 0,
Direction.Right => _startPosX + _Width + Tank.Step < _pictureWidth,
Direction.Down => _startPosY + _Height + Tank.Step < _pictureHeight,
_ => false
};
}
// Конструктор класса
public DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureHeight = height;
_pictureWidth = width;
Tank = new Base(speed, weight, bodyColor);
}
public DrawArmoVehicle(int speed, double weight, Color bodyColor, int width, int height, int tankWidth, int tankHeight)
{
_pictureHeight = height;
_pictureWidth = width;
_Height = tankHeight;
_Width = tankWidth;
Tank = new Base(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || Tank == null)
return;
switch (direction)
{
case Direction.Left:
{
if (_startPosX - Tank.Step > 0)
{
_startPosX -= (int)Tank.Step;
}
}
break;
case Direction.Up:
{
if (_startPosY - Tank.Step > 0)
{
_startPosY -= (int)Tank.Step;
}
}
break;
case Direction.Right:
{
if (_startPosX + _Width + Tank.Step < _pictureWidth)
{
_startPosX += (int)Tank.Step;
}
}
break;
case Direction.Down:
{
if (_startPosY + Tank.Step + _Height < _pictureHeight)
{
_startPosY += (int)Tank.Step;
}
}
break;
}
}
public virtual void DrawTransport(Graphics g) // ПЕРЕПИСАТЬ В НОРМАЛЬНОЕ СОСТОЯНИЕ
{
if (Tank == null)
return;
Brush BrushRandom = new SolidBrush(Tank?.BodyColor ?? Color.Black);
//Корпус
Brush WhiteColor = new SolidBrush(Color.White);
g.FillRectangle(WhiteColor, _startPosX, _startPosY, 155, 30);
Point[] pointsbody1 = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 110, _startPosY + 30),
new Point(_startPosX + 142, _startPosY + 30), new Point(_startPosX + 120, _startPosY + 45), new Point(_startPosX + 12, _startPosY + 45) };
g.FillPolygon(BrushRandom, pointsbody1);
// Гусеница
g.FillRectangle(BrushRandom, 28 + _startPosX, 50 + _startPosY, 10, 3);
g.FillRectangle(BrushRandom, 53 + _startPosX, 50 + _startPosY, 10, 3);
g.FillRectangle(BrushRandom, 78 + _startPosX, 50 + _startPosY, 10, 3);
g.FillRectangle(BrushRandom, 103 + _startPosX, 50 + _startPosY, 10, 3);
// Колеса, катки
Brush ColorBlack = new SolidBrush(Color.Black);
g.FillEllipse(ColorBlack, 10 + _startPosX, 42 + _startPosY, 20, 20);
g.FillEllipse(ColorBlack, 35 + _startPosX, 42 + _startPosY, 20, 20);
g.FillEllipse(ColorBlack, 60 + _startPosX, 42 + _startPosY, 20, 20);
g.FillEllipse(ColorBlack, 85 + _startPosX, 42 + _startPosY, 20, 20);
g.FillEllipse(ColorBlack, 110 + _startPosX, 42 + _startPosY, 20, 20);
}
}
}

72
Tank/Tank/DrawTank.cs Normal file
View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.Entities;
namespace Tank.DrawningObjects
{
public class DrawTank : DrawArmoVehicle
{
public DrawTank(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height) : base(speed, weight, bodyColor, width, height)
{
if (Tank != null)
{
Tank = new AddBase(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
}
}
public override void DrawTransport(Graphics g)
{
if (Tank is not AddBase ArmoVehicle)
return;
base.DrawTransport(g);
if (ArmoVehicle.BodyKit)
{
Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor);
// Корпус танка
Point[] pointsbody = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 140, _startPosY + 30),
new Point(_startPosX + 130, _startPosY + 42), new Point(_startPosX + 12, _startPosY + 42) };
g.FillPolygon(bodyBrush, pointsbody);
Point[] pointtower = { new Point(_startPosX + 52, _startPosY + 30), new Point(_startPosX + 52, _startPosY + 27), new Point(_startPosX + 40, _startPosY + 23),
new Point(_startPosX + 15, _startPosY + 18), new Point(_startPosX + 15,_startPosY + 15), new Point(_startPosX + 60, _startPosY + 11), new Point(_startPosX + 90, _startPosY + 11),
new Point(_startPosX + 120, _startPosY + 20), new Point(_startPosX + 100,_startPosY + 25), new Point(_startPosX + 95, _startPosY + 27), new Point(_startPosX + 90, _startPosY + 30)};
g.FillPolygon(bodyBrush, pointtower);
}
if (ArmoVehicle.Rinks)
{
Pen pen = new(Color.Black);
Brush lineBrush = new SolidBrush(Color.Gray);
// Гусеница
Brush BrushGray = new SolidBrush(Color.DarkGray);
//g.DrawEllipse(BrushGray, _startPosX + 10, _startPosY + 30, 120, 30);
// Отрисовка танковых катков
Brush BrushBlack = new SolidBrush(Color.Black);
g.FillEllipse(BrushBlack, _startPosX + 113, _startPosY + 41, 11, 11);
g.FillEllipse(BrushBlack, _startPosX + 13, _startPosY + 40, 11, 11);
}
if (ArmoVehicle.Caterpillar)
{
Brush bodyBrush = new SolidBrush(ArmoVehicle.AdditionalColor);
// Орудие
g.FillRectangle(bodyBrush, _startPosX + 111, _startPosY + 17, 70, 5);
// Зенитное орудие
Point[] pointgun = { new Point(_startPosX + 44, _startPosY + 13), new Point(_startPosX + 45, _startPosY + 12), new Point(_startPosX + 41, _startPosY + 8), new Point(_startPosX + 41, _startPosY + 7),
new Point(_startPosX + 42, _startPosY + 5), new Point(_startPosX + 41, _startPosY + 4), new Point(_startPosX + 44, _startPosY + 3), new Point(_startPosX + 50, _startPosY + 3),
new Point(_startPosX + 52, _startPosY + 5), new Point(_startPosX + 53, _startPosY + 7), new Point(_startPosX + 58, _startPosY + 11)};
g.FillPolygon(bodyBrush, pointgun);
g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY + 5, 20, 2);
}
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tank.DrawningObjects;
namespace Tank.MovementStrategy
{
public class DrawingObjectTank : IMoveableObject
{
private readonly DrawArmoVehicle? _drawTank = null;
public DrawingObjectTank(DrawArmoVehicle drawTank)
{
_drawTank = drawTank;
}
public ObjectParameters? GetObjectParameters
{
get
{
if (_drawTank == null || _drawTank.Tank == null)
return null;
return new ObjectParameters(_drawTank.GetPosX, _drawTank.GetPosY, _drawTank.GetWidth, _drawTank.GetHeight);
}
}
public int GetStep => (int)(_drawTank?.Tank?.Step ?? 0);
public bool CheckCanMove(Direction direction) => _drawTank?.CanMove(direction) ?? false;
public void MoveObject(Direction direction) => _drawTank?.MoveTransport(direction);
// Доработка
public bool CanStopObject(Direction direction) => _drawTank?.CanStopObject(direction) ?? false;
}
}

View File

@ -1,139 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace Tank
{
public class DrawingTank
{
public EntityTank Tank { get; set; }
private int _startPosX;
private int _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
private readonly int _TankWidth = 160;
public readonly int _TankHeight = 90;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, int Step, int width, int height, bool bodykit, bool wing, bool roadline)
{
_pictureWidth = width;
_pictureHeight = height;
Tank = new EntityTank();
Tank.Init(speed, weight, bodyColor, additionalColor, Step, width, height, bodykit, wing, roadline);
}
public void SetPosition(int x, int y, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (x >= 0 && x + _TankWidth <= width && y >= 0 && y + _TankHeight <= height)
{
_startPosX = x;
_startPosY = y;
}
}
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _TankWidth + Tank.Step < _pictureWidth)
{
_startPosX += Tank.Step;
}
break;
// влево
case Direction.Left:
if (_startPosX - Tank.Step > 0)
{
_startPosX -= Tank.Step;
}
break;
// вверх
case Direction.Up:
if (_startPosY - Tank.Step > 0)
{
_startPosY -= Tank.Step;
}
break;
// вниз
case Direction.Down:
if (_startPosY + _TankHeight + Tank.Step < _pictureHeight)
{
_startPosY += Tank.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
Pen pen = new(Color.Black);
// Отрисовка танковых катков
Brush BrushBlack = new SolidBrush(Color.Black);
g.FillEllipse(BrushBlack, _startPosX + 113, _startPosY + 41, 11, 11);
g.FillEllipse(BrushBlack, _startPosX + 13, _startPosY + 40, 11, 11);
// Гусеница
Brush BrushGray = new SolidBrush(Color.DarkGray);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 30, 120, 30);
// Корпус танка
Point[] pointsbody = { new Point(_startPosX + 5, _startPosY + 30), new Point(_startPosX + 140, _startPosY + 30),
new Point(_startPosX + 130, _startPosY + 42), new Point(_startPosX + 12, _startPosY + 42) };
g.FillPolygon(BrushGray, pointsbody);
Point[] pointtower = { new Point(_startPosX + 52, _startPosY + 30), new Point(_startPosX + 52, _startPosY + 27), new Point(_startPosX + 40, _startPosY + 23),
new Point(_startPosX + 15, _startPosY + 18), new Point(_startPosX + 15,_startPosY + 15), new Point(_startPosX + 60, _startPosY + 11), new Point(_startPosX + 90, _startPosY + 11),
new Point(_startPosX + 120, _startPosY + 20), new Point(_startPosX + 100,_startPosY + 25), new Point(_startPosX + 95, _startPosY + 27), new Point(_startPosX + 90, _startPosY + 30)};
g.FillPolygon(BrushGray, pointtower);
// Орудие
g.FillRectangle(BrushGray, _startPosX + 111, _startPosY + 17, 55, 5);
// Зенитное орудие
Brush BrushRandom = new SolidBrush(Tank?.AdditionalColor ?? Color.Black);
Point[] pointgun = { new Point(_startPosX + 44, _startPosY + 13), new Point(_startPosX + 45, _startPosY + 12), new Point(_startPosX + 41, _startPosY + 8), new Point(_startPosX + 41, _startPosY + 7),
new Point(_startPosX + 42, _startPosY + 5), new Point(_startPosX + 41, _startPosY + 4), new Point(_startPosX + 44, _startPosY + 3), new Point(_startPosX + 50, _startPosY + 3),
new Point(_startPosX + 52, _startPosY + 5), new Point(_startPosX + 53, _startPosY + 7), new Point(_startPosX + 58, _startPosY + 11)};
g.FillPolygon(BrushRandom, pointgun);
g.FillRectangle(BrushRandom, _startPosX + 50, _startPosY + 5, 20, 2);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _TankWidth || _pictureHeight <= _TankHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _TankWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _TankWidth;
}
if (_startPosY + _TankHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _TankHeight;
}
}
}
}

View File

@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace Tank
{
public class EntityTank
{
public int Speed { get; private set; }
public float Weight { get; private set; }
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; }
public int Step => Speed * 100 / (int)Weight;
public bool BodyKit { get; private set; }
public bool Wing { get; private set; }
public bool RoadLine { get; private set; }
public void Init(int speed, float weight, Color bodyColor, Color additionalcolor, int step, int width, int height, bool bodyKit, bool wing, bool roadline)
{
Random random = new Random();
Speed = speed <= 0 ? random.Next(50, 100) : speed;
Weight = weight <= 0 ? random.Next(30,60) : weight;
BodyColor = bodyColor;
AdditionalColor = additionalcolor;
BodyKit = bodyKit;
Wing = wing;
RoadLine = roadline;
}
}
}

View File

@ -34,6 +34,9 @@
keyUp = new Button(); keyUp = new Button();
keyLeft = new Button(); keyLeft = new Button();
keyRight = new Button(); keyRight = new Button();
buttonArmVechicle = new Button();
buttonStep = new Button();
comboBoxStrategy = new ComboBox();
((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -48,12 +51,12 @@
// //
// ButtonCreate // ButtonCreate
// //
ButtonCreate.Location = new Point(14, 523); ButtonCreate.Location = new Point(14, 500);
ButtonCreate.Margin = new Padding(3, 4, 3, 4); ButtonCreate.Margin = new Padding(3, 4, 3, 4);
ButtonCreate.Name = "ButtonCreate"; ButtonCreate.Name = "ButtonCreate";
ButtonCreate.Size = new Size(90, 31); ButtonCreate.Size = new Size(145, 53);
ButtonCreate.TabIndex = 1; ButtonCreate.TabIndex = 1;
ButtonCreate.Text = "button"; ButtonCreate.Text = "Создание танка";
ButtonCreate.UseVisualStyleBackColor = true; ButtonCreate.UseVisualStyleBackColor = true;
ButtonCreate.Click += ButtonCreate_Click; ButtonCreate.Click += ButtonCreate_Click;
// //
@ -62,7 +65,7 @@
keyDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
keyDown.BackgroundImage = Properties.Resources.KeyDown; keyDown.BackgroundImage = Properties.Resources.KeyDown;
keyDown.BackgroundImageLayout = ImageLayout.Stretch; keyDown.BackgroundImageLayout = ImageLayout.Stretch;
keyDown.Location = new Point(820, 514); keyDown.Location = new Point(821, 515);
keyDown.Margin = new Padding(3, 4, 3, 4); keyDown.Margin = new Padding(3, 4, 3, 4);
keyDown.Name = "keyDown"; keyDown.Name = "keyDown";
keyDown.Size = new Size(34, 40); keyDown.Size = new Size(34, 40);
@ -75,7 +78,7 @@
keyUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
keyUp.BackgroundImage = Properties.Resources.KeyUp; keyUp.BackgroundImage = Properties.Resources.KeyUp;
keyUp.BackgroundImageLayout = ImageLayout.Stretch; keyUp.BackgroundImageLayout = ImageLayout.Stretch;
keyUp.Location = new Point(820, 466); keyUp.Location = new Point(821, 467);
keyUp.Margin = new Padding(3, 4, 3, 4); keyUp.Margin = new Padding(3, 4, 3, 4);
keyUp.Name = "keyUp"; keyUp.Name = "keyUp";
keyUp.Size = new Size(34, 40); keyUp.Size = new Size(34, 40);
@ -88,7 +91,7 @@
keyLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
keyLeft.BackgroundImage = Properties.Resources.KeyLeft; keyLeft.BackgroundImage = Properties.Resources.KeyLeft;
keyLeft.BackgroundImageLayout = ImageLayout.Stretch; keyLeft.BackgroundImageLayout = ImageLayout.Stretch;
keyLeft.Location = new Point(779, 514); keyLeft.Location = new Point(779, 515);
keyLeft.Margin = new Padding(3, 4, 3, 4); keyLeft.Margin = new Padding(3, 4, 3, 4);
keyLeft.Name = "keyLeft"; keyLeft.Name = "keyLeft";
keyLeft.Size = new Size(34, 40); keyLeft.Size = new Size(34, 40);
@ -101,7 +104,7 @@
keyRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; keyRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
keyRight.BackgroundImage = Properties.Resources.KeyRight; keyRight.BackgroundImage = Properties.Resources.KeyRight;
keyRight.BackgroundImageLayout = ImageLayout.Stretch; keyRight.BackgroundImageLayout = ImageLayout.Stretch;
keyRight.Location = new Point(861, 514); keyRight.Location = new Point(861, 515);
keyRight.Margin = new Padding(3, 4, 3, 4); keyRight.Margin = new Padding(3, 4, 3, 4);
keyRight.Name = "keyRight"; keyRight.Name = "keyRight";
keyRight.Size = new Size(34, 40); keyRight.Size = new Size(34, 40);
@ -109,11 +112,46 @@
keyRight.UseVisualStyleBackColor = true; keyRight.UseVisualStyleBackColor = true;
keyRight.Click += ButtonMove_Click; keyRight.Click += ButtonMove_Click;
// //
// buttonArmVechicle
//
buttonArmVechicle.Location = new Point(176, 501);
buttonArmVechicle.Margin = new Padding(3, 4, 3, 4);
buttonArmVechicle.Name = "buttonArmVechicle";
buttonArmVechicle.Size = new Size(145, 53);
buttonArmVechicle.TabIndex = 15;
buttonArmVechicle.Text = "Создание бронеавтомобиля";
buttonArmVechicle.UseVisualStyleBackColor = true;
buttonArmVechicle.Click += CreateButtonArmoVehicle_Click;
//
// buttonStep
//
buttonStep.Location = new Point(815, 71);
buttonStep.Margin = new Padding(3, 4, 3, 4);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(86, 31);
buttonStep.TabIndex = 17;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += ButtonStep_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "0", "1" });
comboBoxStrategy.Location = new Point(749, 24);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 19;
//
// FormTank // FormTank
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ButtonHighlight;
ClientSize = new Size(914, 568); ClientSize = new Size(914, 568);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep);
Controls.Add(buttonArmVechicle);
Controls.Add(keyRight); Controls.Add(keyRight);
Controls.Add(keyLeft); Controls.Add(keyLeft);
Controls.Add(keyUp); Controls.Add(keyUp);
@ -123,6 +161,7 @@
Margin = new Padding(3, 4, 3, 4); Margin = new Padding(3, 4, 3, 4);
Name = "FormTank"; Name = "FormTank";
Text = "FormTank"; Text = "FormTank";
Load += FormTank_Load;
((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -135,5 +174,8 @@
private Button keyLeft; private Button keyLeft;
private Button keyUp; private Button keyUp;
private Button keyDown; private Button keyDown;
private Button buttonArmVechicle;
private Button buttonStep;
private ComboBox comboBoxStrategy;
} }
} }

View File

@ -1,8 +1,13 @@
using Tank.DrawningObjects;
using Tank.MovementStrategy;
namespace Tank namespace Tank
{ {
public partial class FormTank : Form public partial class FormTank : Form
{ {
private DrawingTank _Tank; private DrawArmoVehicle? _Tank;
private AbstractStrategy? _abstractStrategy;
public FormTank() public FormTank()
{ {
InitializeComponent(); InitializeComponent();
@ -10,29 +15,34 @@ namespace Tank
private void Draw() private void Draw()
{ {
if (_Tank == null)
return;
Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height); Bitmap bmp = new(pictureBoxTank.Width, pictureBoxTank.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_Tank?.DrawTransport(gr); _Tank?.DrawTransport(gr);
pictureBoxTank.Image = bmp; pictureBoxTank.Image = bmp;
} }
private void PictureBoxTank_Resize(object sender, EventArgs e)
{
_Tank?.ChangeBorders(pictureBoxTank.Width, pictureBoxTank.Height);
Draw();
}
private void ButtonCreate_Click(object sender, EventArgs e) private void ButtonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new(); Random rnd = new();
_Tank = new DrawingTank(); _Tank = new DrawTank(rnd.Next(100, 200), rnd.Next(2000, 4000),
_Tank.Init(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)), 0, pictureBoxTank.Width, pictureBoxTank.Height, true, true, true); Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
_Tank.SetPosition(rnd.Next(10, 50), rnd.Next(30, 70), pictureBoxTank.Width, pictureBoxTank.Height); Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(1, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
pictureBoxTank.Width, pictureBoxTank.Height);
_Tank.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw(); Draw();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
//ïîëó÷àåì èìÿ êíîïêè //ïîëó÷àåì èìÿ êíîïêè
if (_Tank == null)
return;
string name = ((Button)sender)?.Name ?? string.Empty; string name = ((Button)sender)?.Name ?? string.Empty;
switch (name) switch (name)
{ {
@ -51,5 +61,51 @@ namespace Tank
} }
Draw(); Draw();
} }
private void CreateButtonArmoVehicle_Click(object sender, EventArgs e)
{
Random rnd = new();
_Tank = new DrawArmoVehicle(rnd.Next(100, 200), rnd.Next(2000, 4000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
pictureBoxTank.Width, pictureBoxTank.Height);
_Tank.SetPosition(rnd.Next(10, 50), rnd.Next(30, 70));
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_Tank == null)
return;
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null
};
if (_abstractStrategy == null)
return;
_abstractStrategy.SetData(new DrawingObjectTank(_Tank), pictureBoxTank.Width, pictureBoxTank.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
return;
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void FormTank_Load(object sender, EventArgs e)
{
}
} }
} }

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectParameters { get; }
int GetStep { get; }
bool CheckCanMove(Direction direction);
void MoveObject(Direction direction);
// Доп задание
bool CanStopObject(Direction direction);
}
}

47
Tank/Tank/MoveToBorder.cs Normal file
View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestination()
{
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;
var diffY = objParams.DownBorder - FieldHeight;
if (diffX >= 0)
{
MoveDown();
}
else if (diffY >= 0)
{
MoveRight();
}
else if (Math.Abs(diffX) > Math.Abs(diffY))
{
MoveRight();
}
else
{
MoveDown();
}
}
}
}

54
Tank/Tank/MoveToCenter.cs Normal file
View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
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();
}
}
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank.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 => _width + _x; // Правая граница
public int DownBorder => _height + _y; // Нижняя граница
public int ObjectMiddleHorizontal => _x + _width / 2; // Середина по горизонтали
public int ObjectMiddleVertical => _y + _height / 2; // Середина по вертикали
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

15
Tank/Tank/Status.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tank
{
public enum Status
{
NotInit = 0,
InProgress = 1,
Finish = 2
}
}

View File

@ -8,19 +8,4 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project> </Project>