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)
{