From e1db93a2e7ddaa0b035a4ae4396ce5ee3f28b97d Mon Sep 17 00:00:00 2001 From: Daniya_Youdakova Date: Thu, 15 Dec 2022 21:56:58 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftCarrier/AbstractMap.cs | 196 +++++++++++++----- AircraftCarrier/AircraftCarrier/ComplexMap.cs | 3 +- AircraftCarrier/AircraftCarrier/Direction.cs | 2 +- .../DrawningAircraftCarrier.cs | 29 ++- 4 files changed, 162 insertions(+), 68 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/AbstractMap.cs b/AircraftCarrier/AircraftCarrier/AbstractMap.cs index ffea273..a7ac427 100644 --- a/AircraftCarrier/AircraftCarrier/AbstractMap.cs +++ b/AircraftCarrier/AircraftCarrier/AbstractMap.cs @@ -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); - } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/ComplexMap.cs b/AircraftCarrier/AircraftCarrier/ComplexMap.cs index ab8ec95..f3d4bae 100644 --- a/AircraftCarrier/AircraftCarrier/ComplexMap.cs +++ b/AircraftCarrier/AircraftCarrier/ComplexMap.cs @@ -16,7 +16,6 @@ namespace AircraftCarrier /// Цвет участка открытого /// 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); diff --git a/AircraftCarrier/AircraftCarrier/Direction.cs b/AircraftCarrier/AircraftCarrier/Direction.cs index 3253596..9879da6 100644 --- a/AircraftCarrier/AircraftCarrier/Direction.cs +++ b/AircraftCarrier/AircraftCarrier/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace AircraftCarrier { - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs index 742e646..d0edec2 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs @@ -10,7 +10,7 @@ namespace AircraftCarrier /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawningAircraftCarrier + public class DrawningAircraftCarrier { /// /// Класс-сущность @@ -36,8 +36,8 @@ namespace AircraftCarrier /// Ширина отрисовки самолета /// - 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) { -- 2.25.1 From ef7807d6fdc252237ac180d616d8a409c58540e7 Mon Sep 17 00:00:00 2001 From: Daniya_Youdakova Date: Thu, 15 Dec 2022 21:57:40 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningModernAircraftCarrier.cs | 36 +++++++------- .../AircraftCarrier/EntityAircraftCarrier.cs | 7 ++- .../AircraftCarrier/FormAircraftCarrier.cs | 47 ++++++++++++++----- 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs index d48e925..fbc7d11 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs @@ -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); } } } diff --git a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs index a03d40d..3ae43c3 100644 --- a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs @@ -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; } + } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index 0ec9b56..99d1e9e 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -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; + } } } \ No newline at end of file -- 2.25.1 From 274bfc284a57bc1c96db5aeb79f1f8697696bc97 Mon Sep 17 00:00:00 2001 From: Daniya_Youdakova Date: Thu, 15 Dec 2022 21:58:38 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormAircraftCarrier.Designer.cs | 14 ++ ...FormMapWithSetAircraftCarriers.Designer.cs | 208 ++++++++++++++++++ .../FormMapWithSetAircraftCarriers.cs | 134 +++++++++++ .../FormMapWithSetAircraftCarriers.resx | 60 +++++ 4 files changed, 416 insertions(+) create mode 100644 AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs create mode 100644 AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs create mode 100644 AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.resx diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs index 918d3ea..37a1a63 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs new file mode 100644 index 0000000..e8d1b4d --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs @@ -0,0 +1,208 @@ +namespace AircraftCarrier +{ + partial class FormMapWithSetAircraftCarriers + { + /// + /// Required designer variable. + /// + + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs new file mode 100644 index 0000000..37d36e2 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs @@ -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 +_mapAircraftCarriersCollectionGeneric; + /// + /// Конструктор + /// + 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( + 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); + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.resx b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file -- 2.25.1 From b0b97b53e05e79366333f417fc35ed6df3cab7fe Mon Sep 17 00:00:00 2001 From: Daniya_Youdakova Date: Thu, 15 Dec 2022 21:59:21 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=20-=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MapWithSetAircraftCarriersGeneric.cs | 190 ++++++++++++++++++ AircraftCarrier/AircraftCarrier/Program.cs | 2 +- .../SetAircraftCarriersGeneric.cs | 99 +++++++++ AircraftCarrier/AircraftCarrier/SimpleMap.cs | 3 +- 4 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs create mode 100644 AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs new file mode 100644 index 0000000..379f1c9 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs @@ -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 + where T : class, IDrawningObject + where U : AbstractMap + { + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 250; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 100; + /// + /// Набор объектов + /// + private readonly SetAircraftCarriersGeneric _setAircraftCarriers; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetAircraftCarriersGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setAircraftCarriers = new SetAircraftCarriersGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(MapWithSetAircraftCarriersGeneric map, T AircraftCarrier) + { + return map._setAircraftCarriers.Insert(AircraftCarrier); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetAircraftCarriersGeneric map, int + position) + { + return map._setAircraftCarriers.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawAircraftCarriers(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + 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); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + 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; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + 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); + } + } + /// + /// Метод прорисовки объектов + /// + /// + 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++; + } + } + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/Program.cs b/AircraftCarrier/AircraftCarrier/Program.cs index 4080ebd..8aecab1 100644 --- a/AircraftCarrier/AircraftCarrier/Program.cs +++ b/AircraftCarrier/AircraftCarrier/Program.cs @@ -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()); } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs new file mode 100644 index 0000000..017e06b --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs @@ -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 + where T : class + { + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetAircraftCarriersGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T AircraftCarrier) + { + return Insert(AircraftCarrier, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + 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; + } + + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + 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; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position >= _places.Length || position < 0) + return null; + return _places[position]; + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SimpleMap.cs b/AircraftCarrier/AircraftCarrier/SimpleMap.cs index c9cc6b8..fdd003f 100644 --- a/AircraftCarrier/AircraftCarrier/SimpleMap.cs +++ b/AircraftCarrier/AircraftCarrier/SimpleMap.cs @@ -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 } } } + } } \ No newline at end of file -- 2.25.1