Полностью доделал лабу 2.

This commit is contained in:
Артём Алейкин 2022-10-18 20:56:42 +04:00
parent 9d01e9ae66
commit 4ec1797948
7 changed files with 148 additions and 58 deletions

View File

@ -24,66 +24,21 @@ namespace AirBomber
_width = width; _width = width;
_height = heigth; _height = heigth;
_drawningObject = drawningObject; _drawningObject = drawningObject;
GenerateMap(); GenerateMap();
while (!SetObjectOnMap()) while (!SetObjectOnMap())
{ {
GenerateMap(); GenerateMap();
} }
return DrawMapWithObject(); return DrawMapWithObject();
} }
private bool CanMove(Direction direction, (float Left, float Right, float Top, float Bottom) position) private bool CanMove(Direction direction, (float Left, float Right, float Top, float Bottom) position)
{ {
if (direction == Direction.Left) for (int i = (int)(position.Top / _size_y); i <= (int)(position.Bottom / _size_y); ++i)
{ {
for (int i = (int)((position.Left - _drawningObject.Step) / _size_y); i <= (int)(position.Left / _size_y); ++i) for (int j = (int)(position.Left / _size_x); j <= (int)(position.Right / _size_x); ++j)
{ {
for (int j = (int)(position.Top / _size_x); j <= (int)(position.Bottom / _size_x); ++j) if (i >= 0 && j >= 0 && i < _map.GetLength(0) && j < _map.GetLength(1) && _map[i, j] == _barrier) return false;
{
if (i >= 0 && _map[i,j] == _barrier)
{
return false;
}
}
}
}
else if (direction == Direction.Right)
{
for (int i = (int)(position.Right / _size_y); i <= (int)((position.Right + _drawningObject.Step) / _size_y); ++i)
{
for (int j = (int)(position.Top / _size_x); j <= (int)(position.Bottom / _size_x); ++j)
{
if (i <= _map.GetLength(0) && _map[i, j] == _barrier)
{
return false;
}
}
}
}
else if (direction == Direction.Up)
{
for (int i = (int)(position.Left / _size_y); i <= (int)(position.Right / _size_y); ++i)
{
for (int j = (int)((position.Top - _drawningObject.Step) / _size_x); j <= (int)(position.Top / _size_x); ++j)
{
if (j >= 0 && _map[i, j] == _barrier)
{
return false;
}
}
}
}
else if (direction == Direction.Down)
{
for (int i = (int)(position.Left / _size_y); i <= (int)(position.Right / _size_y); ++i)
{
for (int j = (int)(position.Bottom / _size_x); j <= (int)((position.Bottom + _drawningObject.Step) / _size_x); ++j)
{
if (j <= _map.GetLength(1) && _map[i, j] == _barrier)
{
return false;
}
}
} }
} }
return true; return true;
@ -91,10 +46,27 @@ namespace AirBomber
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
if (CanMove(direction, _drawningObject.GetCurrentPosition())) (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); _drawningObject.MoveObject(direction);
} }
return DrawMapWithObject(); return DrawMapWithObject();
} }
@ -107,8 +79,17 @@ namespace AirBomber
} }
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height); _drawningObject.SetObject(x, y, _width, _height);
//TODO (float Left, float Right, float Top, float Bottom) position = _drawningObject.GetCurrentPosition();
for (int i = (int)(position.Top / _size_y); i <= (int)(position.Bottom / _size_y); ++i)
{
for (int j = (int)(position.Left / _size_x); j <= (int)(position.Right / _size_x); ++j)
{
_map[i, j] = _freeRoad;
}
}
return true; return true;
} }

View File

@ -80,6 +80,8 @@ namespace AirBomber
return; return;
} }
Pen pen = new Pen(Color.Black);
SolidBrush brush = new SolidBrush(AirBomber?.BodyColor ?? Color.Cyan); SolidBrush brush = new SolidBrush(AirBomber?.BodyColor ?? Color.Cyan);
g.FillPolygon(brush, new[] { g.FillPolygon(brush, new[] {
@ -108,7 +110,7 @@ namespace AirBomber
new Point((int)(_startPosX), (int)(_startPosY + _airBomberHeight / 2)), new Point((int)(_startPosX), (int)(_startPosY + _airBomberHeight / 2)),
new Point((int)(_startPosX + 20), (int)(_startPosY + _airBomberHeight / 2 - 5)), new Point((int)(_startPosX + 20), (int)(_startPosY + _airBomberHeight / 2 - 5)),
new Point((int)(_startPosX + 20), (int)(_startPosY + _airBomberHeight / 2 + 5)), new Point((int)(_startPosX + 20), (int)(_startPosY + _airBomberHeight / 2 + 5)),
}); });
} }
public void ChangeBorders(int width, int height) public void ChangeBorders(int width, int height)

View File

@ -158,8 +158,10 @@
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] { this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта"}); "Простая карта",
this.comboBoxSelectorMap.Location = new System.Drawing.Point(21, 12); "Вторая карта",
"Третья карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(20, 12);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);
this.comboBoxSelectorMap.TabIndex = 8; this.comboBoxSelectorMap.TabIndex = 8;

View File

@ -17,6 +17,7 @@ namespace AirBomber
public FormMap() public FormMap()
{ {
InitializeComponent(); InitializeComponent();
comboBoxSelectorMap.SelectedIndex = 0;
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
} }
@ -75,6 +76,12 @@ namespace AirBomber
case "Простая карта": case "Простая карта":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Вторая карта":
_abstractMap = new SecondMap();
break;
case "Третья карта":
_abstractMap = new ThirdMap();
break;
} }
} }
} }

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class SecondMap : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.Yellow);
Brush roadColor = new SolidBrush(Color.Blue);
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 < 35)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -34,7 +34,7 @@ namespace AirBomber
_map[i, j] = _freeRoad; _map[i, j] = _freeRoad;
} }
} }
while (counter < 50) while (counter < 250)
{ {
int x = _random.Next(0, 100); int x = _random.Next(0, 100);
int y = _random.Next(0, 100); int y = _random.Next(0, 100);

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class ThirdMap : AbstractMap
{
Brush barrierColor = new SolidBrush(Color.Red);
Brush roadColor = new SolidBrush(Color.DarkGreen);
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 < 40)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}