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

This commit is contained in:
DavidMakarov 2023-10-15 21:35:45 +04:00
parent ff6f059599
commit 18d5f5f466
20 changed files with 695 additions and 136 deletions

View File

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.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;
this.moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(Movement.Left);
protected bool MoveRight() => MoveTo(Movement.Right);
protected bool MoveUp() => MoveTo(Movement.Up);
protected bool MoveDown() => MoveTo(Movement.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();
private bool MoveTo(Movement directionType)
{
if (state != Status.InProgress)
{
return false;
}
if (moveableObject?.CheckCanMove(directionType) ?? false)
{
moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

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

View File

@ -8,4 +8,10 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="AirplanesGenericCollection.cs" />
<Compile Remove="Class1.cs" />
<Compile Remove="SetGeneric.cs" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.Entities
{
public class AirplaneWithRadarEntity : AirplaneEntity
{
public Color AdditColor { get; private set; }
public bool RadarOnBoard { get; private set; }
public bool AdditFuelPod { get; private set; }
public AirplaneWithRadarEntity(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod)
: base (speed, weight, mainColor)
{
AdditColor = additColor;
RadarOnBoard = radarOnBoard;
AdditFuelPod = additFuelPod;
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirplaneWithRadar.PaintObjects;
using AirplaneWithRadar.MovementStrategy;
namespace AirplaneWithRadar.Generics;
internal class AirplanesGenericCollection<T,U>
where T : PaintAirplane
where U : IMoveableObject
{
private readonly int pictWidth;
private readonly int pictHeight;
private readonly int placeSizeWidth = 210;
private readonly int placeSizeHeight = 90;
private readonly SetGeneric<T> collection;
public AirplanesGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / placeSizeWidth;
int height = picHeight / placeSizeHeight;
pictWidth = picWidth;
pictHeight = picHeight;
collection = new SetGeneric<T>(width * height);
}
public static bool operator +(AirplanesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return false;
}
return collect?.collection.Insert(obj) ?? false;
}
public static T? operator -(AirplanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect.collection.Get(pos);
if (obj != null)
{
collect.collection.Remove(pos);
}
return obj;
}
}

View File

@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Plane
{
public class Class1
{
private int x;
public int y { private get; set; }
public int Sum(int num1, int num2)
{
return num1 + num2;
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(Movement direction);
void MoveObject(Movement direction);
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,59 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.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();
}
}
}
}
}

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Plane namespace AirplaneWithRadar
{ {
public enum Movement public enum Movement
{ {

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.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;
public ObjectParameters(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
}

View File

@ -2,112 +2,124 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Linq; using System.Linq;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using AirplaneWithRadar.Entities;
namespace Plane namespace AirplaneWithRadar.PaintObjects
{ {
public class Paint public class PaintAirplane
{ {
public PlaneEntity? PlaneEntity { get; private set; } public AirplaneEntity? AirplaneEntity { get; protected set; }
private int pictWidth; private int pictWidth;
private int pictHeight; private int pictHeight;
private int startPosX; protected int startPosX;
private int startPosY; protected int startPosY;
private readonly int planeWidth = 140; protected readonly int planeWidth = 140;
private readonly int planeHeight = 60; protected readonly int planeHeight = 60;
public bool Init(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod, int width, int height) public int GetPosX => startPosX;
public int GetPosY => startPosY;
public int GetWidth => planeWidth;
public int GetHeight => planeHeight;
public bool CanMove(Movement dir)
{ {
if (planeWidth > width || planeHeight > height) { return false; } if (AirplaneEntity == null)
{
return false;
}
return dir switch
{
//влево
Movement.Left => startPosX - AirplaneEntity.Step > 0,
//вверх
Movement.Up => startPosY - AirplaneEntity.Step > 0,
// вправо
Movement.Right => startPosX + planeWidth + AirplaneEntity.Step < pictWidth,
//вниз
Movement.Down => startPosY + planeHeight + AirplaneEntity.Step < pictHeight,
_ => false,
};
}
public PaintAirplane(int speed, double weight, Color bodyColor, int width, int height)
{
if (planeWidth > width || planeHeight > height) { return; }
pictHeight = height; pictHeight = height;
pictWidth = width; pictWidth = width;
PlaneEntity = new PlaneEntity(); AirplaneEntity = new AirplaneEntity(speed, weight, bodyColor);
PlaneEntity.Init(speed, weight, mainColor, additColor, radarOnBoard, additFuelPod); }
return true; protected PaintAirplane(int speed, double weight, Color bodyColor, int width, int height, int planeWidth, int planeHeight)
{
if (this.planeWidth > width || this.planeHeight > height) { return; }
pictHeight = height;
pictWidth = width;
this.planeWidth = planeWidth;
this.planeHeight = planeHeight;
AirplaneEntity = new AirplaneEntity(speed, weight, bodyColor);
} }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
if (x + planeWidth < pictWidth && y + planeHeight < pictHeight) if ((x >= 0 && y >= 0)&&(x + planeHeight <= pictHeight || y + planeWidth <= pictWidth)) {
{
startPosX = x; startPosX = x;
startPosY = y; startPosY = y;
} }
else
{
if (x > pictWidth)
{
while (x + planeWidth > pictWidth)
{
x -= 1;
}
}
if (y > pictWidth)
{
while (y + planeHeight > pictHeight)
{
y -= 1;
}
}
}
} }
public void Move(Movement dir) public void Move(Movement dir)
{ {
if (PlaneEntity == null) if (!CanMove(dir) || AirplaneEntity == null)
{ {
return; return;
} }
switch (dir) switch (dir)
{ {
//влево
case Movement.Left: case Movement.Left:
if (startPosX - PlaneEntity.Step > 0) startPosX -= (int)AirplaneEntity.Step;
{
startPosX -= (int) PlaneEntity.Step;
}
break; break;
//вверх
case Movement.Up: case Movement.Up:
if (startPosY - PlaneEntity.Step > 0) startPosY -= (int)AirplaneEntity.Step;
{
startPosY -= (int) PlaneEntity.Step;
}
break; break;
// вправо
case Movement.Right: case Movement.Right:
if (startPosX + planeWidth + PlaneEntity.Step < pictWidth) startPosX += (int)AirplaneEntity.Step;
{
startPosX += (int)PlaneEntity.Step;
}
break; break;
//вниз
case Movement.Down: case Movement.Down:
if (startPosY + planeHeight + PlaneEntity.Step < pictHeight) startPosY += (int)AirplaneEntity.Step;
{
startPosY += (int)PlaneEntity.Step;
}
break; break;
} }
} }
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{ {
if (PlaneEntity == null) if (AirplaneEntity == null)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush mainBrush = new SolidBrush(PlaneEntity.MainColor); Brush brush = new SolidBrush(AirplaneEntity.BodyColor);
Brush additBrush = new SolidBrush(PlaneEntity.AdditColor);
//основа //основа
g.FillRectangle(brush, startPosX + planeWidth / 44, startPosY + planeHeight / 2, planeWidth - 2* planeWidth/11, planeHeight / 2 - planeHeight / 8);
g.FillEllipse(brush, new Rectangle(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight / 2 - planeHeight / 8));
g.DrawLine(pen, new Point(startPosX + planeWidth / 44, startPosY + planeHeight / 2), new Point(startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2)); g.DrawLine(pen, new Point(startPosX + planeWidth / 44, startPosY + planeHeight / 2), new Point(startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2));
g.DrawLine(pen, new Point(startPosX + planeWidth / 44, startPosY + planeHeight - planeHeight / 8), new Point(startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight - planeHeight / 8)); g.DrawLine(pen, new Point(startPosX + planeWidth / 44, startPosY + planeHeight - planeHeight / 8), new Point(startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight - planeHeight / 8));
g.DrawArc(pen, new Rectangle(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight / 2 - planeHeight / 8), 90.0f, 180.0f); g.DrawArc(pen, new Rectangle(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight / 2 - planeHeight / 8), 90.0f, 180.0f);
DrawTriangle(g, pen, DrawTriangle(g, pen,
startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2, // 1 startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2, // 1
startPosX + planeWidth, startPosY + 5 * planeHeight / 7, // 2 startPosX + planeWidth, startPosY + 5 * planeHeight / 7, // 2
@ -135,17 +147,9 @@ namespace Plane
FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX + 2 * planeWidth / 9, startPosY + planeHeight / 2 + planeHeight / 7, planeWidth / 2, planeHeight / 7), 5); FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX + 2 * planeWidth / 9, startPosY + planeHeight / 2 + planeHeight / 7, planeWidth / 2, planeHeight / 7), 5);
FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX, startPosY + planeHeight / 2 - planeHeight / 9, planeWidth / 5, planeHeight / 6), 5); FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX, startPosY + planeHeight / 2 - planeHeight / 9, planeWidth / 5, planeHeight / 6), 5);
if (PlaneEntity.RadarOnBoard) {
g.DrawLine(pen, new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 2), new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 3));
g.FillPie(additBrush, new Rectangle(startPosX + planeWidth / 4, startPosY + planeHeight / 10, planeWidth / 6, planeHeight / 4), 30.0f, 180.0f);
}
if (PlaneEntity.AdditFuelPod)
{
FillRoundedRectangle(g, additBrush, new Rectangle(startPosX, startPosY + planeHeight - 2*planeHeight / 5, planeWidth / 5, planeHeight / 6), 5);
}
} }
private void DrawTriangle(Graphics g, Pen pen, int x1, int y1, int x2, int y2, int x3, int y3) protected void DrawTriangle(Graphics g, Pen pen, int x1, int y1, int x2, int y2, int x3, int y3)
{ {
Point p1 = new Point(x1, y1); Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2); Point p2 = new Point(x2, y2);
@ -155,7 +159,7 @@ namespace Plane
g.DrawLine(pen, p3, p1); g.DrawLine(pen, p3, p1);
} }
private static GraphicsPath RoundedRect(Rectangle bounds, int radius) protected static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{ {
int diameter = radius * 2; int diameter = radius * 2;
Size size = new Size(diameter, diameter); Size size = new Size(diameter, diameter);
@ -187,7 +191,7 @@ namespace Plane
return path; return path;
} }
private static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) protected static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius)
{ {
if (graphics == null) if (graphics == null)
throw new ArgumentNullException(nameof(graphics)); throw new ArgumentNullException(nameof(graphics));
@ -200,7 +204,7 @@ namespace Plane
} }
} }
private static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) protected static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius)
{ {
if (graphics == null) if (graphics == null)
throw new ArgumentNullException(nameof(graphics)); throw new ArgumentNullException(nameof(graphics));
@ -212,6 +216,5 @@ namespace Plane
graphics.FillPath(brush, path); graphics.FillPath(brush, path);
} }
} }
} }
} }

View File

@ -0,0 +1,48 @@

using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using AirplaneWithRadar.Entities;
namespace AirplaneWithRadar.PaintObjects
{
public class PaintAirplaneWithRadar : PaintAirplane
{
public AirplaneWithRadarEntity? PlaneEntity { get; private set; }
public PaintAirplaneWithRadar(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod, int width, int height)
: base(speed, weight, mainColor, width, height, 140, 60)
{
if (AirplaneEntity != null)
{
AirplaneEntity = new AirplaneWithRadarEntity(speed, weight, mainColor, additColor, radarOnBoard, additFuelPod);
}
}
public override void DrawTransport(Graphics g)
{
if (AirplaneEntity is not AirplaneWithRadarEntity AirplaneWithRadar)
{
return;
}
Pen pen = new(Color.Black);
Brush additBrush = new SolidBrush(AirplaneWithRadar.AdditColor);
base.DrawTransport(g);
if (AirplaneWithRadar.RadarOnBoard) {
g.DrawLine(pen, new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 2), new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 3));
g.FillPie(additBrush, new Rectangle(startPosX + planeWidth / 4, startPosY + planeHeight / 10, planeWidth / 6, planeHeight / 4), 30.0f, 180.0f);
}
if (AirplaneWithRadar.AdditFuelPod)
{
FillRoundedRectangle(g, additBrush, new Rectangle(startPosX, startPosY + planeHeight - 2*planeHeight / 5, planeWidth / 5, planeHeight / 6), 5);
}
}
}
}

View File

@ -0,0 +1,37 @@
using AirplaneWithRadar.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirplaneWithRadar.PaintObjects;
namespace AirplaneWithRadar.MovementStrategy
{
public class PaintObjectAirplane : IMoveableObject
{
private readonly PaintAirplane? paintAirplane = null;
public PaintObjectAirplane(PaintAirplane paintAirplane)
{
this.paintAirplane = paintAirplane;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (paintAirplane == null || paintAirplane.AirplaneEntity == null) { return null; }
return new ObjectParameters(paintAirplane.GetPosX, paintAirplane.GetPosY, paintAirplane.GetWidth, paintAirplane.GetHeight);
}
}
public int GetStep => (int)(paintAirplane?.AirplaneEntity?.Step ?? 0);
public bool CheckCanMove(Movement dir) =>
paintAirplane?.CanMove(dir) ?? false;
public void MoveObject(Movement dir) =>
paintAirplane?.Move(dir);
}
}

View File

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Plane
{
public class PlaneEntity
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color MainColor { get; private set; }
public Color AdditColor { get; private set; }
public bool RadarOnBoard { get; private set; }
public bool AdditFuelPod { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod)
{
Speed = speed;
Weight = weight;
MainColor = mainColor;
AdditColor = additColor;
RadarOnBoard = radarOnBoard;
AdditFuelPod = additFuelPod;
}
}
}

View File

@ -1,4 +1,4 @@
namespace Plane namespace AirplaneWithRadar
{ {
partial class PlaneVisual partial class PlaneVisual
{ {
@ -34,6 +34,9 @@
DownButton = new Button(); DownButton = new Button();
RightButton = new Button(); RightButton = new Button();
PlanesPictureBox = new PictureBox(); PlanesPictureBox = new PictureBox();
StrategyComboBox = new ComboBox();
StepButton = new Button();
CreateAirplaneButton = new Button();
((System.ComponentModel.ISupportInitialize)PlanesPictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)PlanesPictureBox).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -43,7 +46,7 @@
CreateButton.Name = "CreateButton"; CreateButton.Name = "CreateButton";
CreateButton.Size = new Size(120, 40); CreateButton.Size = new Size(120, 40);
CreateButton.TabIndex = 0; CreateButton.TabIndex = 0;
CreateButton.Text = "Создать"; CreateButton.Text = "Создать самолёт с радаром";
CreateButton.UseVisualStyleBackColor = true; CreateButton.UseVisualStyleBackColor = true;
CreateButton.Click += CreateButton_Click; CreateButton.Click += CreateButton_Click;
// //
@ -103,11 +106,44 @@
PlanesPictureBox.TabIndex = 5; PlanesPictureBox.TabIndex = 5;
PlanesPictureBox.TabStop = false; PlanesPictureBox.TabStop = false;
// //
// StrategyComboBox
//
StrategyComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
StrategyComboBox.FormattingEnabled = true;
StrategyComboBox.Items.AddRange(new object[] { "Центр", "Граница" });
StrategyComboBox.Location = new Point(667, 12);
StrategyComboBox.Name = "StrategyComboBox";
StrategyComboBox.Size = new Size(121, 23);
StrategyComboBox.TabIndex = 6;
//
// StepButton
//
StepButton.Location = new Point(713, 41);
StepButton.Name = "StepButton";
StepButton.Size = new Size(75, 23);
StepButton.TabIndex = 7;
StepButton.Text = "Шаг";
StepButton.UseVisualStyleBackColor = true;
StepButton.Click += StepButton_Click;
//
// CreateAirplaneButton
//
CreateAirplaneButton.Location = new Point(156, 391);
CreateAirplaneButton.Name = "CreateAirplaneButton";
CreateAirplaneButton.Size = new Size(120, 40);
CreateAirplaneButton.TabIndex = 8;
CreateAirplaneButton.Text = "Создать самолёт";
CreateAirplaneButton.UseVisualStyleBackColor = true;
CreateAirplaneButton.Click += CreateAirplaneButton_Click;
//
// PlaneVisual // PlaneVisual
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(CreateAirplaneButton);
Controls.Add(StepButton);
Controls.Add(StrategyComboBox);
Controls.Add(RightButton); Controls.Add(RightButton);
Controls.Add(DownButton); Controls.Add(DownButton);
Controls.Add(LeftButton); Controls.Add(LeftButton);
@ -115,8 +151,7 @@
Controls.Add(CreateButton); Controls.Add(CreateButton);
Controls.Add(PlanesPictureBox); Controls.Add(PlanesPictureBox);
Name = "PlaneVisual"; Name = "PlaneVisual";
Text = "Flying Plane"; Text = "Самолёт";
Load += Form1_Load;
((System.ComponentModel.ISupportInitialize)PlanesPictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)PlanesPictureBox).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -129,5 +164,8 @@
private Button DownButton; private Button DownButton;
private Button RightButton; private Button RightButton;
private PictureBox PlanesPictureBox; private PictureBox PlanesPictureBox;
private ComboBox StrategyComboBox;
private Button StepButton;
private Button CreateAirplaneButton;
} }
} }

View File

@ -1,10 +1,13 @@
using System; using System;
using AirplaneWithRadar.MovementStrategy;
using AirplaneWithRadar.PaintObjects;
namespace Plane namespace AirplaneWithRadar
{ {
public partial class PlaneVisual : System.Windows.Forms.Form public partial class PlaneVisual : System.Windows.Forms.Form
{ {
private Paint? PaintPlanes; private PaintAirplane? PaintPlanes;
private AbstractStrategy? abstractStrategy;
private void Draw() private void Draw()
{ {
@ -23,10 +26,6 @@ namespace Plane
} }
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (PaintPlanes == null) { return; } if (PaintPlanes == null) { return; }
@ -55,12 +54,54 @@ namespace Plane
{ {
Random random = new Random(); Random random = new Random();
PaintPlanes = new Paint(); PaintPlanes = new PaintAirplaneWithRadar(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
PaintPlanes.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)), true,
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)), PlanesPictureBox.Width, PlanesPictureBox.Height); Convert.ToBoolean(random.Next(0, 2)), PlanesPictureBox.Width, PlanesPictureBox.Height);
PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100)); PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
} }
private void CreateAirplaneButton_Click(object sender, EventArgs e)
{
Random random = new Random();
PaintPlanes = new PaintAirplane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
PlanesPictureBox.Width, PlanesPictureBox.Height);
PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void StepButton_Click(object sender, EventArgs e)
{
if (PaintPlanes == null)
{
return;
}
if (StrategyComboBox.Enabled)
{
abstractStrategy = StrategyComboBox.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.SetData(new PaintObjectAirplane(PaintPlanes), PlanesPictureBox.Width, PlanesPictureBox.Height);
StrategyComboBox.Enabled = false;
}
if (abstractStrategy == null)
{
return;
}
abstractStrategy.MakeStep();
Draw();
if (abstractStrategy.GetStatus() == Status.Finish)
{
StrategyComboBox.Enabled = true;
abstractStrategy = null;
}
}
} }
} }

View File

@ -1,4 +1,4 @@
namespace Plane namespace AirplaneWithRadar
{ {
internal static class Program internal static class Program
{ {

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirplaneWithRadar.Generics;
internal class SetGeneric<T>
where T : class
{
private readonly T?[] places;
public int Count => places.Length;
public SetGeneric(int count)
{
places = new T?[count];
}
public bool Insert(T airplane)
{
if (places[0] == null)
{
places[0] = airplane;
}
else
{
for (int i = Count - 1; i < 0; i--)
{
places[i] = places[i - 1];
}
places[0] = airplane;
}
return true;
}
public bool Insert(T airplane, int position)
{
if (position > Count || position < 0)
{
return false;
}
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
places[position] = airplane;
return true;
}
public bool Remove(int position)
{
if (position > Count || position < 0)
{
return false;
}
places[position] = null;
return true;
}
public T? Get(int position)
{
if (position > Count || position < 0)
{
return null;
}
return places[position];
}
}

View File

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