diff --git a/Warship/Warship/AbstractMap.cs b/Warship/Warship/AbstractMap.cs index f94c819..d9a28b3 100644 --- a/Warship/Warship/AbstractMap.cs +++ b/Warship/Warship/AbstractMap.cs @@ -33,6 +33,12 @@ namespace Warship public Bitmap MoveObject(Direction direction) { + (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition(); + + if (CheckLand(Left, Right, Top, Bottom) != 0) + { + _drawingObject.MoveObject(SetOppositDirection(direction)); + } if (true) { @@ -50,10 +56,29 @@ namespace Warship int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawingObject.SetObject(x, y, _width, _height); - int i=(_width - x) / (int)_size_x; - int j = (_height - y) / (int)_size_y; - for - return true; + + (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition(); + + while (CheckLand(Left, Right, Top, Bottom) != 2) + { + int res; + do + { + res = CheckLand(Left, Right, Top, Bottom); + if (res == 0) + { + _drawingObject.SetObject((int)Left, (int)Right, _width, _height); + return true; + } + else + { + Left += _size_x; + } + } while (res != 2); + Left = x; + Right += _size_y; + } + return false; } private Bitmap DrawMapWithObject() @@ -81,8 +106,44 @@ namespace Warship _drawingObject.DrawingObject(gr); return bmp; } + private int CheckLand(float Left, float Right, float Top, float Bottom) + { + int RUi = (int)(Left / _size_x); + int RUj = (int)(Right / _size_y); + int LDi = (int)(Top / _size_x); + int LDj = (int)(Bottom / _size_y); - + if (RUi < 0 || RUj < 0 || LDi >= _map.GetLength(1) || LDj >= _map.GetLength(0)) + { + return -1; + } + for (int x = RUi; x <= LDi; x++) + { + for (int y = RUj; y <= LDj; y++) + { + if (_map[x, y] == _land) + { + return 1; + } + } + } + return 0; + } + private Direction SetOppositDirection(Direction dir) + { + switch (dir) + { + case Direction.Up: + return Direction.Down; + case Direction.Down: + return Direction.Up; + case Direction.Left: + return Direction.Right; + case Direction.Right: + return Direction.Left; + } + return Direction.None; + } protected abstract void GenerateMap(); protected abstract void DrawWaterPart(Graphics gr, int i, int j); diff --git a/Warship/Warship/DrawingAdvancedWarship.cs b/Warship/Warship/DrawingAdvancedWarship.cs index ed98ec3..e8c3fe7 100644 --- a/Warship/Warship/DrawingAdvancedWarship.cs +++ b/Warship/Warship/DrawingAdvancedWarship.cs @@ -12,6 +12,7 @@ namespace Warship { Warship = new EntityAdvancedWarship(speed, weight, bodyColor, dopColor, Helipad, Antenna, Missile); } + public override void DrawTransport(Graphics g) { if (Warship is not EntityAdvancedWarship advancedWarship) diff --git a/Warship/Warship/FormMap.Designer.cs b/Warship/Warship/FormMap.Designer.cs index 2f72657..5f3bf0f 100644 --- a/Warship/Warship/FormMap.Designer.cs +++ b/Warship/Warship/FormMap.Designer.cs @@ -161,8 +161,9 @@ this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); + "Простая карта", + "Вторая карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(667, 12); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); this.comboBoxSelectorMap.TabIndex = 8; diff --git a/Warship/Warship/FormMap.cs b/Warship/Warship/FormMap.cs index 181304b..7279993 100644 --- a/Warship/Warship/FormMap.cs +++ b/Warship/Warship/FormMap.cs @@ -69,6 +69,9 @@ namespace Warship case "Простая карта": _abstractMap = new SimpleMap(); break; + case "Вторая карта": + _abstractMap = new SecondMap(); + break; } } } diff --git a/Warship/Warship/SecondMap.cs b/Warship/Warship/SecondMap.cs new file mode 100644 index 0000000..46abb0e --- /dev/null +++ b/Warship/Warship/SecondMap.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Warship +{ + internal class SecondMap : AbstractMap + { + private readonly Brush waterColor = new SolidBrush(Color.White); + private readonly Brush landColor = new SolidBrush(Color.Black); + protected override void DrawLandPart(Graphics gr, int i, int j) + { + gr.FillRectangle(landColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + + protected override void DrawWaterPart(Graphics gr, int i, int j) + { + gr.FillRectangle(waterColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeWaterArea; + } + } + while (counter < 20) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeWaterArea) + { + _map[x, y] = _land; + counter++; + } + } + } + } +}