Готовая лаба 2

This commit is contained in:
Полина Чубыкина 2023-10-10 17:27:58 +04:00
parent 20300279e7
commit e68ab7b607
6 changed files with 286 additions and 29 deletions

View File

@ -17,6 +17,10 @@ namespace Sailboat.DrawingObjects
protected int _startPosY; protected int _startPosY;
private readonly int _boatWidth = 160; private readonly int _boatWidth = 160;
private readonly int _boatHeight = 160; private readonly int _boatHeight = 160;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _boatWidth;
public int GetHeight => _boatHeight;
public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height) public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height)
{ {
if (width < _boatWidth || height < _boatHeight) if (width < _boatWidth || height < _boatHeight)
@ -53,9 +57,28 @@ namespace Sailboat.DrawingObjects
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
public void MoveTransport(DirectionType direction) public bool CanMove(DirectionType direction)
{ {
if (EntityBoat == null) if (EntityBoat == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityBoat.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityBoat.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityBoat.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityBoat.Step < _pictureHeight,
_ => false
};
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityBoat == null)
{ {
return; return;
} }
@ -93,7 +116,7 @@ namespace Sailboat.DrawingObjects
{ {
return; return;
} }
Pen pen = new(Color.Black, 4); Pen pen = new(Color.Black);
Brush additionalBrush = new Brush additionalBrush = new
SolidBrush(EntityBoat.BodyColor); SolidBrush(EntityBoat.BodyColor);

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sailboat.DrawingObjects;
namespace Sailboat.MovementStrategy
{
public class DrawingObjectBoat : IMoveableObject
{
private readonly DrawingBoat? _drawingBoat = null;
public DrawingObjectBoat(DrawingBoat drawingBoat)
{
_drawingBoat = drawingBoat;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawingBoat == null || _drawingBoat.EntityBoat ==
null)
{
return null;
}
return new ObjectParameters(_drawingBoat.GetPosX,
_drawingBoat.GetPosY, _drawingBoat.GetWidth, _drawingBoat.GetHeight);
}
}
public int GetStep => (int)(_drawingBoat?.EntityBoat?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawingBoat?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawingBoat?.MoveTransport(direction);
}
}

View File

@ -30,11 +30,14 @@
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSailboat)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSailboat));
this.pictureBoxSailboat = new System.Windows.Forms.PictureBox(); this.pictureBoxSailboat = new System.Windows.Forms.PictureBox();
this.buttonCreate = new System.Windows.Forms.Button(); this.buttonCreateBoat = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button();
this.buttonCreateSailboat = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.buttonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxSailboat)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxSailboat)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -47,18 +50,17 @@
this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxSailboat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxSailboat.TabIndex = 0; this.pictureBoxSailboat.TabIndex = 0;
this.pictureBoxSailboat.TabStop = false; this.pictureBoxSailboat.TabStop = false;
this.pictureBoxSailboat.Click += new System.EventHandler(this.pictureBoxSailboat_Click);
// //
// buttonCreate // buttonCreateBoat
// //
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonCreateBoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(12, 395); this.buttonCreateBoat.Location = new System.Drawing.Point(12, 385);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreateBoat.Name = "buttonCreateBoat";
this.buttonCreate.Size = new System.Drawing.Size(127, 46); this.buttonCreateBoat.Size = new System.Drawing.Size(127, 56);
this.buttonCreate.TabIndex = 1; this.buttonCreateBoat.TabIndex = 1;
this.buttonCreate.Text = "Создать"; this.buttonCreateBoat.Text = "Создать лодку";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreateBoat.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); this.buttonCreateBoat.Click += new System.EventHandler(this.buttonCreateBoat_Click);
// //
// buttonLeft // buttonLeft
// //
@ -108,16 +110,53 @@
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
// //
// buttonCreateSailboat
//
this.buttonCreateSailboat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateSailboat.Location = new System.Drawing.Point(175, 385);
this.buttonCreateSailboat.Name = "buttonCreateSailboat";
this.buttonCreateSailboat.Size = new System.Drawing.Size(127, 56);
this.buttonCreateSailboat.TabIndex = 6;
this.buttonCreateSailboat.Text = "Создать парусник";
this.buttonCreateSailboat.UseVisualStyleBackColor = true;
this.buttonCreateSailboat.Click += new System.EventHandler(this.buttonCreateSailboat_Click);
//
// comboBoxStrategy
//
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"До центра",
"До края"});
this.comboBoxStrategy.Location = new System.Drawing.Point(688, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28);
this.comboBoxStrategy.TabIndex = 7;
//
// buttonStep
//
this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonStep.Location = new System.Drawing.Point(688, 46);
this.buttonStep.Name = "buttonStep";
this.buttonStep.Size = new System.Drawing.Size(149, 32);
this.buttonStep.TabIndex = 8;
this.buttonStep.Text = "Шаг";
this.buttonStep.UseVisualStyleBackColor = true;
this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click);
//
// FormSailboat // FormSailboat
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(882, 453); this.ClientSize = new System.Drawing.Size(882, 453);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonCreateSailboat);
this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonCreateBoat);
this.Controls.Add(this.pictureBoxSailboat); this.Controls.Add(this.pictureBoxSailboat);
this.Name = "FormSailboat"; this.Name = "FormSailboat";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
@ -131,10 +170,13 @@
#endregion #endregion
private PictureBox pictureBoxSailboat; private PictureBox pictureBoxSailboat;
private Button buttonCreate; private Button buttonCreateBoat;
private Button buttonLeft; private Button buttonLeft;
private Button buttonUp; private Button buttonUp;
private Button buttonRight; private Button buttonRight;
private Button buttonDown; private Button buttonDown;
private Button buttonCreateSailboat;
private ComboBox comboBoxStrategy;
private Button buttonStep;
} }
} }

View File

@ -1,39 +1,49 @@
using Sailboat.Entities; using Sailboat.Entities;
using Sailboat.DrawingObjects; using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy;
namespace Sailboat namespace Sailboat
{ {
public partial class FormSailboat : Form public partial class FormSailboat : Form
{ {
private DrawingSailboat? _drawingSailboat; private DrawingBoat? _drawingBoat;
private AbstractStrategy? _abstractStrategy;
public FormSailboat() public FormSailboat()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw() private void Draw()
{ {
if (_drawingSailboat == null) if (_drawingBoat == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxSailboat.Width, Bitmap bmp = new(pictureBoxSailboat.Width,
pictureBoxSailboat.Height); pictureBoxSailboat.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawingSailboat.DrawTransport(gr); _drawingBoat.DrawTransport(gr);
pictureBoxSailboat.Image = bmp; pictureBoxSailboat.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreateBoat_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawingSailboat = new DrawingSailboat(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)), _drawingBoat = new DrawingBoat(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSailboat.Width, pictureBoxSailboat.Height);
_drawingBoat.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void buttonCreateSailboat_Click(object sender, EventArgs e)
{
Random random = new();
_drawingBoat = new DrawingSailboat(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailboat.Width, pictureBoxSailboat.Height);
_drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100)); _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
} }
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawingSailboat == null) if (_drawingBoat == null)
{ {
return; return;
} }
@ -41,25 +51,56 @@ namespace Sailboat
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawingSailboat.MoveTransport(DirectionType.Up); _drawingBoat.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawingSailboat.MoveTransport(DirectionType.Down); _drawingBoat.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawingSailboat.MoveTransport(DirectionType.Left); _drawingBoat.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawingSailboat.MoveTransport(DirectionType.Right); _drawingBoat.MoveTransport(DirectionType.Right);
break; break;
} }
Draw(); Draw();
} }
private void pictureBoxSailboat_Click(object sender, EventArgs e) private void buttonStep_Click(object sender, EventArgs e)
{ {
if (_drawingBoat == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectBoat(_drawingBoat), pictureBoxSailboat.Width,
pictureBoxSailboat.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,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.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,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat.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();
}
}
}
}
}