Полностью готовая вторая лабораторная

This commit is contained in:
Hells Hound 2022-09-21 10:52:37 +04:00
parent 711b7d213b
commit 9a8bafa283
4 changed files with 82 additions and 68 deletions

View File

@ -32,13 +32,38 @@ namespace AircraftCarrier
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемом направлении
if (true) if (true)
{ {
_drawingObject.MoveObject(direction); _drawingObject.MoveObject(direction);
} }
(float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition();
if (Check(Left, Right, Top, Bottom) != 0)
{
_drawingObject.MoveObject(GetOpositDirection(direction));
}
return DrawMapWithObject(); return DrawMapWithObject();
} }
private Direction GetOpositDirection(Direction dir)
{
switch (dir)
{
case Direction.None:
return Direction.None;
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;
}
private bool SetObjectOnMap() private bool SetObjectOnMap()
{ {
if (_drawingObject == null || _map == null) if (_drawingObject == null || _map == null)
@ -48,68 +73,56 @@ namespace AircraftCarrier
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height); _drawingObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition();
float nowX = Left;
float nowY = Right;
float lenX = Top - Left;
float lenY = Bottom - Right;
while (Check(nowX, nowY, nowX + lenX, nowY + lenY) != 2)
/*(float left, float right, float top, float bottom) = _drawingObject.GetCurrentPosition();
float step = _drawingObject?.Step ?? 0;
int searchX = 0,
searchY = 0,
startX = 0,
startY = 0;
switch (direction)
{ {
case Direction.UP: int result;
searchX = (int)right; do
searchY = (int)top;
startX = (int)left;
startY = (int)(top - step);
break;
case Direction.DOWN:
searchX = (int)right;
searchY = (int)(bottom + step);
startX = (int)left;
startY = (int)bottom;
break;
case Direction.LEFT:
searchX = (int)left;
searchY = (int)bottom;
startX = (int)(left - step);
startY = (int)top;
break;
case Direction.RIGHT:
searchX = (int)(right + step);
searchY = (int)bottom;
startX = (int)right;
startY = (int)top;
break;
}
searchX = (int)(searchX / _size_x) + 1;
searchY = (int)(searchY / _size_y) + 1;
startX = (int)(startX / _size_x);
startY = (int)(startY / _size_y);
if (searchX < 0 || searchX > _width || searchY < 0 || searchY > _height) return false;
for (int i = startY; i < searchY; i++)
{ {
for (int j = startX; j < searchX; j++) result = Check(nowX, nowY, nowX + lenX, nowY + lenY);
if (result == 0)
{ {
if (_map[j, i] == _barrier) return false; _drawingObject.SetObject((int)nowX, (int)nowY, _width, _height);
}
}
*/
return true; return true;
} }
else
{
nowX += _size_x;
}
} while (result != 2);
nowX = x;
nowY += _size_y;
}
return false;
}
private int Check(float Left, float Right, float Top, float Bottom)
{
int startX = (int)(Left / _size_x);
int startY = (int)(Right / _size_y);
int endX = (int)(Top / _size_x);
int endY = (int)(Bottom / _size_y);
if (startX < 0 || startY < 0 || endX >= _map.GetLength(0) || endY >= _map.GetLength(0))
{
return 2;
}
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
if (_map[i, j] == _barrier)
{
return 1;
}
}
}
return 0;
}
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()
{ {
Bitmap bmp = new(_width, _height); Bitmap bmp = new(_width, _height);

View File

@ -95,7 +95,7 @@ namespace AircraftCarrier
case "Простая карта": case "Простая карта":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Преграды линии": case "Преграды-линии":
_abstractMap = new LineMap(); _abstractMap = new LineMap();
break; break;
} }

View File

@ -12,18 +12,18 @@ namespace AircraftCarrier
/// <summary> /// <summary>
/// Цвет участка закрытого /// Цвет участка закрытого
/// </summary> /// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Blue); private readonly Brush barrierColor = new SolidBrush(Color.Brown);
/// <summary> /// <summary>
/// Цвет участка открытого /// Цвет участка открытого
/// </summary> /// </summary>
private readonly Brush roadColor = new SolidBrush(Color.Gray); private readonly Brush waterColor = new SolidBrush(Color.Blue);
protected override void DrawBarrierPart(Graphics g, int i, int j) 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, i * _size_x, j * _size_y, _size_x, _size_y);
} }
protected override void DrawRoadPart(Graphics g, int i, int j) 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(waterColor, i * _size_x, j * _size_y, _size_x, _size_y);
} }
protected override void GenerateMap() protected override void GenerateMap()
{ {
@ -39,19 +39,20 @@ namespace AircraftCarrier
} }
} }
bool flag = true; bool flag = true;
while (counter < 10) while (counter < 20)
{ {
int lineX = _random.Next(0, 100); int lineX = _random.Next(11, 89);
int lineY = _random.Next(0, 100); int lineY = _random.Next(11, 89);
if (flag) if (flag)
{ {
for (int i = 0; i <= 10; i++) for (int i = lineY; i <= lineY + 10; i++)
_map[lineX, i] = _barrier; _map[lineX, i] = _barrier;
flag = false; flag = false;
} }
else else
{ {
for (int i = 0; i <= 10; i++) for (int i = lineX; i <= lineX + 10; i++)
_map[i, lineY] = _barrier; _map[i, lineY] = _barrier;
flag = true; flag = true;
} }

View File

@ -14,7 +14,7 @@ namespace AircraftCarrier
/// <summary> /// <summary>
/// Цвет участка закрытого /// Цвет участка закрытого
/// </summary> /// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Black); private readonly Brush barrierColor = new SolidBrush(Color.Brown);
/// <summary> /// <summary>
/// Цвет участка открытого /// Цвет участка открытого
/// </summary> /// </summary>