diff --git a/WarmlyShip/WarmlyShip/AbstractMap.cs b/WarmlyShip/WarmlyShip/AbstractMap.cs index 76b877b..a28a2b6 100644 --- a/WarmlyShip/WarmlyShip/AbstractMap.cs +++ b/WarmlyShip/WarmlyShip/AbstractMap.cs @@ -32,28 +32,13 @@ namespace WarmlyShip return DrawMapWithObject(); } - private bool CanMove(Direction direction) + private bool CanMove(Direction direction, (float Left, float Right, float Top, float Bottom) position) { - for (int i = 0; i < _map.GetLength(0); ++i) + for (int i = (int)(position.Top / _size_y); i <= (int)(position.Bottom / _size_y); ++i) { - for (int j = 0; j < _map.GetLength(1); ++j) + for (int j = (int)(position.Left / _size_x); j <= (int)(position.Right / _size_x); ++j) { - if (direction == Direction.Left && _drawningObject.GetCurrentPosition().Left - _drawningObject.Step < i * _size_x && _drawningObject.GetCurrentPosition().Left > i * _size_x + _size_x && _map[i,j] == _barrier) - { - return false; - } - else if (direction == Direction.Up && _drawningObject.GetCurrentPosition().Top - _drawningObject.Step < j * _size_y && _drawningObject.GetCurrentPosition().Top > j * _size_y + _size_y && _map[i, j] == _barrier) - { - return false; - } - else if (direction == Direction.Right && _drawningObject.GetCurrentPosition().Right < i * _size_x && _drawningObject.GetCurrentPosition().Right + _drawningObject.Step > i * _size_x + _size_x && _map[i, j] == _barrier) - { - return false; - } - else if (direction == Direction.Down && _drawningObject.GetCurrentPosition().Bottom < j * _size_y && _drawningObject.GetCurrentPosition().Bottom + _drawningObject.Step > j * _size_y + _size_y && _map[i, j] == _barrier) - { - return false; - } + if (i >= 0 && j >= 0 && _map[i, j] == _barrier) return false; } } return true; @@ -61,13 +46,31 @@ namespace WarmlyShip public Bitmap MoveObject(Direction direction) { - // TODO проверка, что объект может переместится в требуемомнаправлении - if (CanMove(direction)) + (float Left, float Right, float Top, float Bottom) position = _drawningObject.GetCurrentPosition(); + if (direction == Direction.Left) + { + position.Left -= _drawningObject.Step; + } + else if (direction == Direction.Right) + { + position.Right += _drawningObject.Step; + } + else if (direction == Direction.Up) + { + position.Top -= _drawningObject.Step; + } + else if (direction == Direction.Down) + { + position.Bottom += _drawningObject.Step; + } + + if (CanMove(direction, position)) { _drawningObject.MoveObject(direction); } return DrawMapWithObject(); } + private bool SetObjectOnMap() { if (_drawningObject == null || _map == null) @@ -77,7 +80,13 @@ namespace WarmlyShip int x = _random.Next(0, 10); int y = _random.Next(0, 10); _drawningObject.SetObject(x, y, _width, _height); - // TODO првоерка, что объект не "накладывается" на закрытые участки + for (int i = (int)(_drawningObject.GetCurrentPosition().Top / _size_y); i <= (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y); ++i) + { + for (int j = (int)(_drawningObject.GetCurrentPosition().Left / _size_x); j <= (int)(_drawningObject.GetCurrentPosition().Right / _size_x); ++j) + { + if (_map[i, j] == _barrier) return false; + } + } return true; } private Bitmap DrawMapWithObject() diff --git a/WarmlyShip/WarmlyShip/DrawningWarmlyShip.cs b/WarmlyShip/WarmlyShip/DrawningWarmlyShip.cs index d6624ae..b6df0c9 100644 --- a/WarmlyShip/WarmlyShip/DrawningWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/DrawningWarmlyShip.cs @@ -117,7 +117,7 @@ namespace WarmlyShip public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { - return (_startPosX, _startPosY, _startPosX + _warmlyShipWidth, _startPosY + _warmlyShipHeight); + return (_startPosX, _startPosX + _warmlyShipWidth, _startPosY, _startPosY + _warmlyShipHeight); } } } diff --git a/WarmlyShip/WarmlyShip/FormMap.Designer.cs b/WarmlyShip/WarmlyShip/FormMap.Designer.cs index 2b82e57..f89a930 100644 --- a/WarmlyShip/WarmlyShip/FormMap.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMap.Designer.cs @@ -159,7 +159,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.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); diff --git a/WarmlyShip/WarmlyShip/FormMap.cs b/WarmlyShip/WarmlyShip/FormMap.cs index 7dd8e94..57b6071 100644 --- a/WarmlyShip/WarmlyShip/FormMap.cs +++ b/WarmlyShip/WarmlyShip/FormMap.cs @@ -75,6 +75,12 @@ namespace WarmlyShip case "Простая карта": _abstractMap = new SimpleMap(); break; + case "Вторая карта": + _abstractMap = new SecondMap(); + break; + case "Последняя карта": + _abstractMap = new LastMap(); + break; } } diff --git a/WarmlyShip/WarmlyShip/LastMap.cs b/WarmlyShip/WarmlyShip/LastMap.cs new file mode 100644 index 0000000..9cede84 --- /dev/null +++ b/WarmlyShip/WarmlyShip/LastMap.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal class LastMap : AbstractMap + { + private readonly Brush barrierColor = new SolidBrush(Color.Red); + + private readonly Brush roadColor = new SolidBrush(Color.Green); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y); + } + 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] = _freeRoad; + } + } + while (counter < 45) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + if (x > 0 && y > 0 && x < _map.GetLength(0) - 1 && y < _map.GetLength(1) - 1) + { + _map[x - 1, y] = _barrier; + _map[x + 1, y] = _barrier; + _map[x, y - 1] = _barrier; + _map[x, y + 1] = _barrier; + } + counter++; + } + } + } + } +} diff --git a/WarmlyShip/WarmlyShip/SecondMap.cs b/WarmlyShip/WarmlyShip/SecondMap.cs new file mode 100644 index 0000000..e3674f0 --- /dev/null +++ b/WarmlyShip/WarmlyShip/SecondMap.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + internal class SecondMap : AbstractMap + { + private readonly Brush barrierColor = new SolidBrush(Color.Gold); + + private readonly Brush roadColor = new SolidBrush(Color.Black); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y); + } + 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] = _freeRoad; + } + } + for (int i = 0; i < _map.GetLength(0); ++i) + { + _map[i, _map.GetLength(1) / 2] = _barrier; + _map[i, _map.GetLength(1) - 1] = _barrier; + } + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[_map.GetLength(0) - 1, j] = _barrier; + } + while (counter < 45) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +} diff --git a/WarmlyShip/WarmlyShip/SimpleMap.cs b/WarmlyShip/WarmlyShip/SimpleMap.cs index 9d9cd7d..88ac895 100644 --- a/WarmlyShip/WarmlyShip/SimpleMap.cs +++ b/WarmlyShip/WarmlyShip/SimpleMap.cs @@ -14,11 +14,11 @@ namespace WarmlyShip protected override void DrawBarrierPart(Graphics g, int i, int j) { - g.FillRectangle(barrierColor, i * _size_x, j * _size_y, _size_x, _size_y); + g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y); } protected override void DrawRoadPart(Graphics g, int i, int j) { - g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y); + g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y); } protected override void GenerateMap() {