файлы лабораторной

This commit is contained in:
Ivan_Starostin 2023-12-08 20:11:31 +04:00
parent cfa3253cf1
commit c8eed1e7df
5 changed files with 197 additions and 29 deletions

View File

@ -0,0 +1,16 @@
namespace ProjectLainer.Entities
{
public class EntitySuperLainer : EntityLainer
{
public Color AdditionalColor { get; private set; }
public bool Pools { get; private set; }
public bool Decks { get; private set; }
public EntitySuperLainer(int speed, double weight, Color bodyColor, Color
additionalColor, bool pools, bool decks) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Pools = pools;
Decks = decks;
}
}
}

View File

@ -0,0 +1,11 @@
//using ProjectLainer.Drawings;
namespace ProjectLainer.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -29,11 +29,14 @@
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBoxLainer = new PictureBox(); pictureBoxLainer = new PictureBox();
buttonCreate = new Button(); ButtonCreate = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonUp = new Button(); buttonUp = new Button();
ButtonCreateSuperLainer = new Button();
comboBoxStrategy = new ComboBox();
ButtonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxLainer).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxLainer).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -47,16 +50,16 @@
pictureBoxLainer.TabIndex = 0; pictureBoxLainer.TabIndex = 0;
pictureBoxLainer.TabStop = false; pictureBoxLainer.TabStop = false;
// //
// buttonCreate // ButtonCreate
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; ButtonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 356); ButtonCreate.Location = new Point(12, 356);
buttonCreate.Name = "buttonCreate"; ButtonCreate.Name = "ButtonCreate";
buttonCreate.Size = new Size(94, 29); ButtonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1; ButtonCreate.TabIndex = 1;
buttonCreate.Text = "создать"; ButtonCreate.Text = "создать";
buttonCreate.UseVisualStyleBackColor = true; ButtonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; ButtonCreate.Click += ButtonCreate_Click;
// //
// buttonRight // buttonRight
// //
@ -68,7 +71,7 @@
buttonRight.Size = new Size(30, 30); buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 2; buttonRight.TabIndex = 2;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click; buttonRight.Click += ButtonMove_Click;
// //
// buttonLeft // buttonLeft
// //
@ -80,7 +83,7 @@
buttonLeft.Size = new Size(30, 30); buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 3; buttonLeft.TabIndex = 3;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click; buttonLeft.Click += ButtonMove_Click;
// //
// buttonDown // buttonDown
// //
@ -92,7 +95,7 @@
buttonDown.Size = new Size(30, 30); buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 4; buttonDown.TabIndex = 4;
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click; buttonDown.Click += ButtonMove_Click;
// //
// buttonUp // buttonUp
// //
@ -104,18 +107,50 @@
buttonUp.Size = new Size(30, 30); buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 5; buttonUp.TabIndex = 5;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click; buttonUp.Click += ButtonMove_Click;
//
// ButtonCreateSuperLainer
//
ButtonCreateSuperLainer.Location = new Point(137, 356);
ButtonCreateSuperLainer.Name = "ButtonCreateSuperLainer";
ButtonCreateSuperLainer.Size = new Size(192, 29);
ButtonCreateSuperLainer.TabIndex = 6;
ButtonCreateSuperLainer.Text = "создать супер лайнер";
ButtonCreateSuperLainer.UseVisualStyleBackColor = true;
ButtonCreateSuperLainer.Click += ButtonCreateSuperLainer_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "центр", "край"});
comboBoxStrategy.Location = new Point(566, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 7;
//
// ButtonStep
//
ButtonStep.Location = new Point(623, 66);
ButtonStep.Name = "ButtonStep";
ButtonStep.Size = new Size(94, 29);
ButtonStep.TabIndex = 8;
ButtonStep.Text = "шаг";
ButtonStep.UseVisualStyleBackColor = true;
ButtonStep.Click += ButtonStep_Click;
// //
// LainerForm // LainerForm
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(729, 397); ClientSize = new Size(729, 397);
Controls.Add(ButtonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(ButtonCreateSuperLainer);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonCreate); Controls.Add(ButtonCreate);
Controls.Add(pictureBoxLainer); Controls.Add(pictureBoxLainer);
Name = "LainerForm"; Name = "LainerForm";
Text = "LainerField"; Text = "LainerField";
@ -124,13 +159,17 @@
PerformLayout(); PerformLayout();
} }
#endregion #endregion
private PictureBox pictureBoxLainer; private PictureBox pictureBoxLainer;
private Button buttonCreate; private Button ButtonCreate;
private Button buttonRight; private Button buttonRight;
private Button buttonLeft; private Button buttonLeft;
private Button buttonDown; private Button buttonDown;
private Button buttonUp; private Button buttonUp;
private Button ButtonCreateSuperLainer;
private ComboBox comboBoxStrategy;
private Button ButtonStep;
} }
} }

View File

@ -1,16 +1,15 @@
using ProjectLainer.DrawningObjects;
using ProjectLainer.MovementStrategy;
namespace ProjectLainer namespace ProjectLainer
{ {
public partial class LainerForm : Form public partial class LainerForm : Form
{ {
private DrawingLainer? _drawningLainer;
private DrawningLainer? _drawningLainer; private AbstractStrategy? _abstractStrategy;
public LainerForm() public LainerForm()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw() private void Draw()
{ {
if (_drawningLainer == null) if (_drawningLainer == null)
@ -23,12 +22,10 @@ namespace ProjectLainer
_drawningLainer.DrawTransport(gr); _drawningLainer.DrawTransport(gr);
pictureBoxLainer.Image = bmp; pictureBoxLainer.Image = bmp;
} }
private void ButtonCreateSuperLainer_Click(object sender, EventArgs e)
private void buttonCreate_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningLainer = new DrawningLainer(); _drawningLainer = new DrawningSuperLainer(random.Next(100, 300),
_drawningLainer.Init(random.Next(100, 300),
random.Next(1000, 3000), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)), random.Next(0, 256)),
@ -37,12 +34,23 @@ namespace ProjectLainer
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
pictureBoxLainer.Width, pictureBoxLainer.Height); pictureBoxLainer.Width, pictureBoxLainer.Height);
_drawningLainer.SetPosition(random.Next(10, 100), _drawningLainer.SetPosition(random.Next(10, 100), random.Next(10,
random.Next(10, 100)); 100));
Draw(); Draw();
} }
private void ButtonCreate_Click(object sender, EventArgs e)
private void buttonMove_Click(object sender, EventArgs e) {
Random random = new();
_drawningLainer = new DrawingLainer(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxLainer.Width, pictureBoxLainer.Height);
_drawningLainer.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawningLainer == null) if (_drawningLainer == null)
{ {
@ -66,5 +74,41 @@ namespace ProjectLainer
} }
Draw(); Draw();
} }
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningLainer == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectLainer(_drawningLainer), pictureBoxLainer.Width,
pictureBoxLainer.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
} }
} }

View File

@ -0,0 +1,58 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLainer.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.ObjectMiddleHorizontal - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}