This commit is contained in:
Extrimal 2023-10-18 02:22:03 +03:00
parent 321144f895
commit 7603d99eb9
4 changed files with 160 additions and 40 deletions

View File

@ -37,6 +37,7 @@ namespace AircraftCarrier.DrawningObjects
{ {
g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9); g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9);
} }
base.DrawTransport(g);
} }
} }
} }

View File

@ -29,11 +29,14 @@
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBox = new PictureBox(); pictureBox = new PictureBox();
buttonCreate = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonRight = new Button(); buttonRight = new Button();
ButtonCreateAircraftCarrier = new Button();
ButtonCreateAircraft = new Button();
comboBoxStrategy = new ComboBox();
buttonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -48,17 +51,6 @@
pictureBox.TabStop = false; pictureBox.TabStop = false;
pictureBox.Click += buttonMove_Click; pictureBox.Click += buttonMove_Click;
// //
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(23, 403);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
//
// buttonUp // buttonUp
// //
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
@ -107,16 +99,63 @@
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click; buttonRight.Click += buttonMove_Click;
// //
// ButtonCreateAircraftCarrier
//
ButtonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAircraftCarrier.Location = new Point(26, 381);
ButtonCreateAircraftCarrier.Name = "ButtonCreateAircraftCarrier";
ButtonCreateAircraftCarrier.Size = new Size(204, 39);
ButtonCreateAircraftCarrier.TabIndex = 6;
ButtonCreateAircraftCarrier.Text = "Создать военный корабль";
ButtonCreateAircraftCarrier.UseVisualStyleBackColor = true;
ButtonCreateAircraftCarrier.Click += ButtonCreateAircraftCarrier_Click;
//
// ButtonCreateAircraft
//
ButtonCreateAircraft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
ButtonCreateAircraft.Location = new Point(246, 381);
ButtonCreateAircraft.Name = "ButtonCreateAircraft";
ButtonCreateAircraft.Size = new Size(204, 39);
ButtonCreateAircraft.TabIndex = 7;
ButtonCreateAircraft.Text = "Создать корабль";
ButtonCreateAircraft.UseVisualStyleBackColor = true;
ButtonCreateAircraft.Click += ButtonCreateAircraft_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
comboBoxStrategy.Location = new Point(706, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 8;
//
// buttonStep
//
buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStep.Location = new Point(763, 46);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(94, 29);
buttonStep.TabIndex = 9;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// FormAircraftCarrier // FormAircraftCarrier
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 453); ClientSize = new Size(882, 453);
Controls.Add(buttonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(ButtonCreateAircraft);
Controls.Add(ButtonCreateAircraftCarrier);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonCreate);
Controls.Add(pictureBox); Controls.Add(pictureBox);
Name = "FormAircraftCarrier"; Name = "FormAircraftCarrier";
StartPosition = FormStartPosition.CenterScreen; StartPosition = FormStartPosition.CenterScreen;
@ -129,10 +168,13 @@
#endregion #endregion
private PictureBox pictureBox; private PictureBox pictureBox;
private Button buttonCreate;
private Button buttonUp; private Button buttonUp;
private Button buttonLeft; private Button buttonLeft;
private Button buttonDown; private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private Button ButtonCreateAircraft;
private Button ButtonCreateAircraftCarrier;
private ComboBox comboBoxStrategy;
private Button buttonStep;
} }
} }

View File

@ -1,43 +1,31 @@
using AircraftCarrier.DrawningObjects;
using AircraftCarrier.MovementStrategy;
namespace AircraftCarrier namespace AircraftCarrier
{ {
public partial class FormAircraftCarrier : System.Windows.Forms.Form public partial class FormAircraftCarrier : Form
{ {
private DrawningAircraftCarrier? _drawningAircraftCarrier; private DrawningAircraft? _drawingAircraft;
private AbstractStrategy? _abstractStrategy;
public FormAircraftCarrier() public FormAircraftCarrier()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw() private void Draw()
{ {
if (_drawningAircraftCarrier == null) if (_drawingAircraft == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBox.Width, pictureBox.Height); Bitmap bmp = new(pictureBox.Width, pictureBox.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningAircraftCarrier.DrawTransport(gr); _drawingAircraft.DrawTransport(gr);
pictureBox.Image = bmp; pictureBox.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAircraftCarrier = new DrawningAircraftCarrier();
_drawningAircraftCarrier.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)),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
pictureBox.Width, pictureBox.Height);
_drawningAircraftCarrier.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawningAircraftCarrier == null) if (_drawingAircraft == null)
{ {
return; return;
} }
@ -45,19 +33,79 @@ namespace AircraftCarrier
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawningAircraftCarrier.MoveTransport(Direction.Up); _drawingAircraft.MoveTransport(Direction.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawningAircraftCarrier.MoveTransport(Direction.Down); _drawingAircraft.MoveTransport(Direction.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawningAircraftCarrier.MoveTransport(Direction.Left); _drawingAircraft.MoveTransport(Direction.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawningAircraftCarrier.MoveTransport(Direction.Right); _drawingAircraft.MoveTransport(Direction.Right);
break; break;
} }
Draw(); Draw();
} }
private void ButtonCreateAircraft_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawingAircraft = new DrawningAircraftCarrier(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)),
pictureBox.Width, pictureBox.Height);
_drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_drawingAircraft = new DrawningAircraft(rnd.Next(100, 300), rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
pictureBox.Width, pictureBox.Height);
_drawingAircraft.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingAircraft == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectAircraft(_drawingAircraft), pictureBox.Width,
pictureBox.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,29 @@
using AircraftCarrier.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier.MovementStrategy
{
internal class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null) return false;
return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null) return;
if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight();
if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown();
}
}
}