Youdakova D.R. PIbd-21 LabWork03 #3

Closed
Daniya_Youdakova wants to merge 4 commits from LabWork03 into LabWork02
15 changed files with 928 additions and 102 deletions

View File

@ -32,52 +32,124 @@ namespace AircraftCarrier
}
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
bool roadIsClear = true;
(float Left, float Top, float Right, float Bottom) = _drawningObject.GetCurrentPosition();
int xNumOfCells;
int yNumOfCells;
int xObjOffset;
int yObjOffset;
float aircraftcarrierWidth = rightX - leftX;
float aircraftcarrierHeight = bottomY - topY;
for (int i = 0; i < _map.GetLength(0); i++)
switch (direction)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
case Direction.Up:
xNumOfCells = (int)Math.Ceiling((Right - Left) / _size_x);
yNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_y);
xObjOffset = (int)(Left / _size_x);
yObjOffset = (int)Math.Floor(Top / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
switch (direction)
if (!roadIsClear)
{
case Direction.Up:
if (_size_y * (j + 1) >= topY - _drawningObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
}
break;
}
for (int j = 0; j < xNumOfCells; j++)
{
if (yObjOffset - i < 0 || xObjOffset + j >= _map.GetLength(1))
{
break;
case Direction.Down:
if (_size_y * j <= bottomY + _drawningObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX
&& _size_x * (i + 1) <= rightX)
{
return DrawMapWithObject();
}
break;
case Direction.Left:
if (_size_x * (i + 1) >= leftX - _drawningObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
break;
case Direction.Right:
if (_size_x * i <= rightX + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY
&& _size_y * (j + 1) >= topY)
{
return DrawMapWithObject();
}
}
if (_map[xObjOffset + j, yObjOffset - i] == _barrier)
{
roadIsClear = false;
break;
}
}
}
}
break;
case Direction.Down:
xNumOfCells = (int)Math.Ceiling((Right - Left) / _size_x);
yNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_y);
xObjOffset = (int)(Left / _size_x);
yObjOffset = (int)Math.Ceiling(Bottom / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
if (!roadIsClear)
{
break;
}
for (int j = 0; j < xNumOfCells; j++)
{
if (yObjOffset + i >= _map.GetLength(0) || xObjOffset + j >= _map.GetLength(1))
{
break;
}
if (_map[xObjOffset + j, yObjOffset + i] == _barrier)
{
roadIsClear = false;
break;
}
}
}
break;
case Direction.Left:
xNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_x);
yNumOfCells = (int)Math.Ceiling((Bottom - Top) / _size_y);
xObjOffset = (int)Math.Floor(Left / _size_x);
yObjOffset = (int)(Top / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
if (!roadIsClear)
{
break;
}
for (int j = 0; j < xNumOfCells; j++)
{
if (yObjOffset + i >= _map.GetLength(0) || xObjOffset - j < 0)
{
break;
}
if (_map[xObjOffset - j, yObjOffset + i] == _barrier)
{
roadIsClear = false;
break;
}
}
}
break;
case Direction.Right:
xNumOfCells = (int)Math.Ceiling(_drawningObject.Step / _size_x);
yNumOfCells = (int)Math.Ceiling((Bottom - Top) / _size_y);
xObjOffset = (int)(Right / _size_x);
yObjOffset = (int)Math.Ceiling(Top / _size_y);
for (int i = 0; i < yNumOfCells; i++)
{
if (!roadIsClear)
{
break;
}
for (int j = 0; j < xNumOfCells; j++)
{
if (yObjOffset + i >= _map.GetLength(0) || xObjOffset + j >= _map.GetLength(1))
{
break;
}
if (_map[xObjOffset + j, yObjOffset + i] == _barrier)
{
roadIsClear = false;
break;
}
}
}
break;
}
if (true)
if (roadIsClear)
{
_drawningObject.MoveObject(direction);
}
@ -85,31 +157,56 @@ namespace AircraftCarrier
}
private bool SetObjectOnMap()
{
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
if (_drawningObject == null || _map == null)
{
return false;
}
float aircraftcarrierWidth = rightX - leftX;
float aircraftcarrierHeight = bottomY - topY;
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
for (int i = 0; i < _map.GetLength(0); ++i)
int y = _random.Next(50, 100);
(float Left, float Top, float Right, float Bottom) = _drawningObject.GetCurrentPosition();
int xNumOfCells = (int)Math.Ceiling(Right / _size_x) - (int)Math.Floor(Left / _size_x);
int yNumOfCells = (int)Math.Ceiling(Bottom / _size_y) - (int)Math.Floor(Top / _size_y);
int xObjOffset = (int)(x / _size_x);
int yObjOffset = (int)(y / _size_y);
while (y < _height - (Bottom - Top))
{
for (int j = 0; j < _map.GetLength(1); ++j)
while (x < _width - (Right - Left))
{
if (_map[i, j] == _barrier)
if (AreaIsFree(xNumOfCells, yNumOfCells, xObjOffset, yObjOffset))
{
if (x + aircraftcarrierWidth >= _size_x * i && x <= _size_x * i && y + aircraftcarrierHeight > _size_y * j && y <= _size_y * j)
{
return false;
}
_drawningObject.SetObject(x, y, _width, _height);
return true;
}
x += (int)_size_x;
xObjOffset = (int)(x / _size_x);
}
x = 0;
y += (int)_size_y;
yObjOffset = (int)(y / _size_y);
}
return false;
}
private bool AreaIsFree(int xNumOfCells, int yNumOfCells, int xObjOffset, int yObjOffset)
{
for (int i = 0; i <= yNumOfCells; i++)
{
for (int j = 0; j <= xNumOfCells; j++)
{
if (yObjOffset + i >= _map.GetLength(0) || xObjOffset + j >= _map.GetLength(1))
{
return false;
}
if (_map[xObjOffset + j, yObjOffset + i] == _barrier)
{
return false;
}
}
}
_drawningObject.SetObject(x, y, _width, _height);
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
@ -138,6 +235,5 @@ namespace AircraftCarrier
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}

View File

@ -16,7 +16,6 @@ namespace AircraftCarrier
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.LightBlue);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillEllipse(barrierColor, i * (_size_x - 1), j * (_size_y - 1), 40, 15);
@ -38,7 +37,7 @@ namespace AircraftCarrier
_map[i, j] = _freeRoad;
}
}
while (counter < 5)
while (counter < 7)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AircraftCarrier
{
internal enum Direction
public enum Direction
{
None = 0,
Up = 1,

View File

@ -10,7 +10,7 @@ namespace AircraftCarrier
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawningAircraftCarrier
public class DrawningAircraftCarrier
{
/// <summary>
/// Класс-сущность
@ -36,8 +36,8 @@ namespace AircraftCarrier
/// Ширина отрисовки самолета
/// </summary>
protected readonly int _aircraftcarrierWidth = 270;
protected readonly int _aircraftcarrierHeight = 120;
protected readonly int _aircraftcarrierWidth = 230;
protected readonly int _aircraftcarrierHeight = 80;
public DrawningAircraftCarrier(int speed, float weight, Color bodyColor)
{
AircraftCarrier = new EntityAircraftCarrier(speed, weight, bodyColor);
@ -53,7 +53,6 @@ namespace AircraftCarrier
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeigth = height;
}
@ -99,20 +98,20 @@ namespace AircraftCarrier
return;
}
Pen pen = new(Color.Black);
Point PointFront_1 = new Point((int)_startPosX, (int)_startPosY);//основа
Point PointFront_2 = new Point((int)_startPosX + 160, (int)_startPosY);
Point PointFront_3 = new Point((int)_startPosX + 220, (int)_startPosY + 40);
Point PointFront_4 = new Point((int)_startPosX + 160, (int)_startPosY + 80);
Point PointFront_5 = new Point((int)_startPosX, (int)_startPosY + 80);
Point PointFront_1 = new Point((int)_startPosX + 10, (int)_startPosY);//основа
Point PointFront_2 = new Point((int)_startPosX + 170, (int)_startPosY);
Point PointFront_3 = new Point((int)_startPosX + 230, (int)_startPosY + 40);
Point PointFront_4 = new Point((int)_startPosX + 170, (int)_startPosY + 80);
Point PointFront_5 = new Point((int)_startPosX + 10, (int)_startPosY + 80);
Point[] PointsFront = { PointFront_1, PointFront_2, PointFront_3, PointFront_4, PointFront_5 };
g.DrawPolygon(pen, PointsFront);
Brush brBlack = new SolidBrush(Color.Black);//наружние блоки
g.FillRectangle(brBlack, _startPosX - 9, _startPosY + 20, 10, 15);
g.FillRectangle(brBlack, _startPosX - 9, _startPosY + 55, 10, 15);
Brush br = new SolidBrush(Color.Red);//внутренние блоки
g.FillRectangle(br, _startPosX + 110, _startPosY + 24, 20, 30);
g.FillRectangle(br, _startPosX + 90, _startPosY + 36, 20, 10);
g.FillEllipse(br, _startPosX + 130, _startPosY + 27, 32, 30);
g.FillRectangle(brBlack, _startPosX, _startPosY + 20, 10, 15);
g.FillRectangle(brBlack, _startPosX, _startPosY + 55, 10, 15);
Brush br = new SolidBrush(AircraftCarrier?.BodyColor ?? Color.Black);//внутренние блоки
g.FillRectangle(br, _startPosX + 130, _startPosY + 24, 20, 30);
g.FillRectangle(br, _startPosX + 110, _startPosY + 36, 20, 10);
g.FillEllipse(br, _startPosX + 150, _startPosY + 27, 32, 30);
}
public void ChangeBorders(int width, int height)
{

View File

@ -10,42 +10,38 @@ namespace AircraftCarrier
{
public DrawningModernAircraftCarrier(int speed, float weight, Color bodyColor, Color
dopColor, bool flightDeck, bool hangarDeck, bool route) :
base(speed, weight, bodyColor, 270, 150)
base(speed, weight, bodyColor, 230, 80)
{
AircraftCarrier = new EntityModernAircraftCarrier(speed, weight, bodyColor, dopColor, flightDeck, hangarDeck, route);
}
public override void DrawTransport(Graphics g)
{
if (AircraftCarrier is not EntityModernAircraftCarrier ModernAircraftCarrier)
if (AircraftCarrier is not EntityModernAircraftCarrier modernAircraftCarrier)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(ModernAircraftCarrier.DopColor);
Brush dopBrush = new SolidBrush(modernAircraftCarrier.DopColor);
if (ModernAircraftCarrier.FlightDeck)
if (modernAircraftCarrier.FlightDeck)
{
g.DrawRectangle(pen, _startPosX+10, _startPosY+5, 160, 10);
g.DrawRectangle(pen, _startPosX+10, _startPosY + 75, 160, 10);
g.FillRectangle(dopBrush, _startPosX+10, _startPosY + 5, 160, 10);
g.FillRectangle(dopBrush, _startPosX+10, _startPosY + 75, 160, 10);
g.DrawRectangle(pen, _startPosX + 10, _startPosY, 160, 10);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 70, 160, 10);
g.FillRectangle(dopBrush, _startPosX + 10, _startPosY, 160, 10);
g.FillRectangle(dopBrush, _startPosX + 10, _startPosY + 70, 160, 10);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (ModernAircraftCarrier.HangarDeck)
if (modernAircraftCarrier.HangarDeck)
{
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 29, 20, 30);
g.FillRectangle(dopBrush, _startPosX + 20, _startPosY + 29, 20, 30);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 24, 20, 30);
g.FillRectangle(dopBrush, _startPosX + 20, _startPosY + 24, 20, 30);
}
if (ModernAircraftCarrier.Route)
if (modernAircraftCarrier.Route)
{
g.DrawRectangle(pen, _startPosX, _startPosY + 19, 10, 5);
g.DrawRectangle(pen, _startPosX, _startPosY + 74, 10, 5);
g.FillRectangle(dopBrush, _startPosX, _startPosY + 19, 10, 5);
g.FillRectangle(dopBrush, _startPosX , _startPosY + 74, 10, 5);
g.DrawRectangle(pen, _startPosX, _startPosY + 14, 10, 5);
g.DrawRectangle(pen, _startPosX, _startPosY + 69, 10, 5);
g.FillRectangle(dopBrush, _startPosX, _startPosY + 14, 10, 5);
g.FillRectangle(dopBrush, _startPosX, _startPosY + 69, 10, 5);
}
}
}

View File

@ -6,12 +6,16 @@ using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class EntityAircraftCarrier
public class EntityAircraftCarrier
{
public int Speed { get; private set; }
public float Weight { get; private set; }
public Color BodyColor { get; private set; }
public float Step => Speed * 100 / Weight;
public EntityAircraftCarrier(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
@ -19,5 +23,6 @@ namespace AircraftCarrier
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
}
}

View File

@ -39,6 +39,7 @@
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.buttonSelectAircraftCarrier = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAircraftCarrier)).BeginInit();
this.statusStrip.SuspendLayout();
this.SuspendLayout();
@ -52,6 +53,7 @@
this.pictureBoxAircraftCarrier.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxAircraftCarrier.TabIndex = 0;
this.pictureBoxAircraftCarrier.TabStop = false;
//this.pictureBoxAircraftCarrier.Click += new System.EventHandler(this.PictureBoxAircraftCarrier_Click);
this.pictureBoxAircraftCarrier.Resize += new System.EventHandler(this.PictureBoxAircraftCarrier_Resize);
//
// statusStrip
@ -153,11 +155,22 @@
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// buttonSelectAircraftCarrier
//
this.buttonSelectAircraftCarrier.Location = new System.Drawing.Point(520, 380);
this.buttonSelectAircraftCarrier.Name = "buttonSelectAircraftCarrier";
this.buttonSelectAircraftCarrier.Size = new System.Drawing.Size(94, 29);
this.buttonSelectAircraftCarrier.TabIndex = 8;
this.buttonSelectAircraftCarrier.Text = "Выбрать";
this.buttonSelectAircraftCarrier.UseVisualStyleBackColor = true;
this.buttonSelectAircraftCarrier.Click += new System.EventHandler(this.ButtonSelectAircraftCarrier_Click);
//
// FormAircraftCarrier
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonSelectAircraftCarrier);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonUp);
@ -190,5 +203,6 @@
private Button buttonUp;
private Button buttonDown;
private Button buttonCreateModif;
private Button buttonSelectAircraftCarrier;
}
}

View File

@ -13,6 +13,7 @@ namespace AircraftCarrier
public partial class FormAircraftCarrier : Form
{
private DrawningAircraftCarrier _aircraftcarrier;
public DrawningAircraftCarrier SelectedAircraftCarrier { get; private set; }
public FormAircraftCarrier()
{
InitializeComponent();
@ -24,6 +25,7 @@ namespace AircraftCarrier
_aircraftcarrier?.DrawTransport(gr);
pictureBoxAircraftCarrier.Image = bmp;
}
private void SetData()
{
Random rnd = new();
@ -35,8 +37,14 @@ namespace AircraftCarrier
}
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_aircraftcarrier = new DrawningAircraftCarrier(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
Random rnd = new Random();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_aircraftcarrier = new DrawningAircraftCarrier(rnd.Next(100, 300), rnd.Next(4000, 5000), color);
SetData();
Draw();
}
@ -60,19 +68,36 @@ namespace AircraftCarrier
}
Draw();
}
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_aircraftcarrier = new DrawningAircraftCarrier(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), Color.FromArgb(rnd.Next(0, 256),rnd.Next(0, 256), rnd.Next(0, 256))));
SetData();
Draw();
}
private void PictureBoxAircraftCarrier_Resize(object sender, EventArgs e)
{
/// Изменение размеров формы
_aircraftcarrier?.ChangeBorders(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
Draw();
}
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new Random();
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialogDop = new();
if (dialogDop.ShowDialog() == DialogResult.OK)
{
dopColor = dialogDop.Color;
}
_aircraftcarrier = new DrawningModernAircraftCarrier(rnd.Next(100, 300), rnd.Next(1000, 2000), color, dopColor,
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
private void ButtonSelectAircraftCarrier_Click(object sender, EventArgs e)
{
SelectedAircraftCarrier = _aircraftcarrier;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,208 @@
namespace AircraftCarrier
{
partial class FormMapWithSetAircraftCarriers
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveAircraftCarrier = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddAircraftCarrier = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonLeft);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonUp);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonShowOnMap);
this.groupBox1.Controls.Add(this.buttonShowStorage);
this.groupBox1.Controls.Add(this.buttonRemoveAircraftCarrier);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddAircraftCarrier);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(550, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 450);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// buttonLeft
//
//this.buttonLeft.BackgroundImage = global::AircraftCarrier.Properties.Resources.Left;
this.buttonLeft.Location = new System.Drawing.Point(61, 402);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 11;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
//this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.Down;
this.buttonDown.Location = new System.Drawing.Point(97, 402);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
this.buttonUp.Location = new System.Drawing.Point(97, 366);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 9;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.Location = new System.Drawing.Point(133, 402);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 8;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(25, 321);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(184, 29);
this.buttonShowOnMap.TabIndex = 6;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.buttonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(25, 280);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(184, 29);
this.buttonShowStorage.TabIndex = 5;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.buttonShowStorage_Click);
//
// buttonRemoveAircraftCarrier
//
this.buttonRemoveAircraftCarrier.Location = new System.Drawing.Point(25, 230);
this.buttonRemoveAircraftCarrier.Name = "buttonRemoveAircraftCarrier";
this.buttonRemoveAircraftCarrier.Size = new System.Drawing.Size(184, 29);
this.buttonRemoveAircraftCarrier.TabIndex = 4;
this.buttonRemoveAircraftCarrier.Text = "Удалить авианосец";
this.buttonRemoveAircraftCarrier.UseVisualStyleBackColor = true;
this.buttonRemoveAircraftCarrier.Click += new System.EventHandler(this.buttonRemoveAircraftCarrier_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 168);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(184, 27);
this.maskedTextBoxPosition.TabIndex = 3;
//
// buttonAddAircraftCarrier
//
this.buttonAddAircraftCarrier.Location = new System.Drawing.Point(25, 110);
this.buttonAddAircraftCarrier.Name = "buttonAddAircraftCarrier";
this.buttonAddAircraftCarrier.Size = new System.Drawing.Size(184, 29);
this.buttonAddAircraftCarrier.TabIndex = 2;
this.buttonAddAircraftCarrier.Text = "Добавить авианосец";
this.buttonAddAircraftCarrier.UseVisualStyleBackColor = true;
this.buttonAddAircraftCarrier.Click += new System.EventHandler(this.ButtonAddAircraftCarrier_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Сложная карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(25, 61);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(184, 28);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
this.comboBoxSelectorMap.Click += new System.EventHandler(this.ButtonMove_Click);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(550, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//this.pictureBox.Click += new System.EventHandler(this.pictureBox1_Click);
//
// FormMapWithSetAircraftCarriers
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetAircraftCarriers";
this.Text = "Карта с набором объектов";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
private Button buttonLeft;
private Button buttonDown;
private Button buttonUp;
private Button buttonRight;
private Button buttonShowOnMap;
private Button buttonShowStorage;
private Button buttonRemoveAircraftCarrier;
private MaskedTextBox maskedTextBoxPosition;
private Button buttonAddAircraftCarrier;
private ComboBox comboBoxSelectorMap;
private PictureBox pictureBox;
}
}

View File

@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace AircraftCarrier
{
public partial class FormMapWithSetAircraftCarriers : Form
{
private MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap>
_mapAircraftCarriersCollectionGeneric;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetAircraftCarriers()
{
InitializeComponent();
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Сложная карта":
map = new ComplexMap();
break;
}
if (map != null)
{
_mapAircraftCarriersCollectionGeneric = new
MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapAircraftCarriersCollectionGeneric = null;
}
}
private void ButtonAddAircraftCarrier_Click(object sender, EventArgs e)
{
if (_mapAircraftCarriersCollectionGeneric == null)
{
return;
}
FormAircraftCarrier form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectAircraftCarrier aircraftcarrier = new(form.SelectedAircraftCarrier);
if ((_mapAircraftCarriersCollectionGeneric + aircraftcarrier) != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapAircraftCarriersCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void buttonRemoveAircraftCarrier_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapAircraftCarriersCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapAircraftCarriersCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonShowStorage_Click(object sender, EventArgs e)
{
if (_mapAircraftCarriersCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapAircraftCarriersCollectionGeneric.ShowSet();
}
private void buttonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapAircraftCarriersCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapAircraftCarriersCollectionGeneric.ShowOnMap();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapAircraftCarriersCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapAircraftCarriersCollectionGeneric.MoveObject(dir);
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,190 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class MapWithSetAircraftCarriersGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 250;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 100;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetAircraftCarriersGeneric<T> _setAircraftCarriers;
/// <summary>
/// Карта
/// </summary>
private readonly U _map;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
public MapWithSetAircraftCarriersGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAircraftCarriers = new SetAircraftCarriersGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
/// <param name="car"></param>
/// <returns></returns>
public static int operator +(MapWithSetAircraftCarriersGeneric<T, U> map, T AircraftCarrier)
{
return map._setAircraftCarriers.Insert(AircraftCarrier);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
public static T operator -(MapWithSetAircraftCarriersGeneric<T, U> map, int
position)
{
return map._setAircraftCarriers.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawAircraftCarriers(gr);
return bmp;
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>
/// <returns></returns>
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setAircraftCarriers.Count; i++)
{
var aircraftcarrier = _setAircraftCarriers.Get(i);
if (aircraftcarrier != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, aircraftcarrier);
}
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Перемещение объекта по крате
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()
{
int j = _setAircraftCarriers.Count - 1;
for (int i = 0; i < _setAircraftCarriers.Count; i++)
{
if (_setAircraftCarriers.Get(i) == null)
{
for (; j > i; j--)
{
var aircraftcarrier = _setAircraftCarriers.Get(j);
if (aircraftcarrier != null)
{
_setAircraftCarriers.Insert(aircraftcarrier, i);
_setAircraftCarriers.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawAircraftCarriers(Graphics g)
{
int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
int rowNum = yNumOfPlaces - 1;
int columnNum = 0;
for (int i = 0; i < _setAircraftCarriers.Count; i++)
{
if (_setAircraftCarriers.Get(i) != null)
{
(float Left, float Top, float Right, float Bottom) = _setAircraftCarriers.Get(i).GetCurrentPosition();
_setAircraftCarriers.Get(i).SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight);
_setAircraftCarriers.Get(i).DrawningObject(g);
}
if (columnNum == xNumOfPlaces - 1)
{
columnNum = 0;
rowNum--;
}
else
{
columnNum++;
}
}
}
}
}

View File

@ -11,7 +11,7 @@ namespace AircraftCarrier
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormMap());
Application.Run(new FormMapWithSetAircraftCarriers());
}
}
}

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class SetAircraftCarriersGeneric<T>
where T : class
{
private readonly T[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetAircraftCarriersGeneric(int count)
{
_places = new T[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T AircraftCarrier)
{
return Insert(AircraftCarrier, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T AircraftCarrier, int position)
{
// TODO проверка позиции
if (position >= _places.Length || position < 0)
return -1;
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
if (_places[position] == null)
{
_places[position] = AircraftCarrier;
return position;
}
// проверка, что после вставляемого элемента в массиве есть пустой элемент
int findEmptyPos = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
findEmptyPos = i;
break;
}
}
if (findEmptyPos < 0) return -1;
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
for (int i = findEmptyPos; i > position; i--)
{
_places[i] = _places[i - 1];
}
// TODO вставка по позиции
_places[position] = AircraftCarrier;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Remove(int position)
{
// TODO проверка позиции
if (position >= _places.Length || position < 0) return null;
// TODO удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places[position] = null;
return temp;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
if (position >= _places.Length || position < 0)
return null;
return _places[position];
}
}
}

View File

@ -36,7 +36,7 @@ namespace AircraftCarrier
_map[i, j] = _freeRoad;
}
}
while (counter < 10)
while (counter < 5)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
@ -47,5 +47,6 @@ namespace AircraftCarrier
}
}
}
}
}