Финальный вариант лабораторной работы

This commit is contained in:
AnnZhimol 2022-09-21 10:36:12 +04:00
parent 9b08e32244
commit ab43211760
5 changed files with 121 additions and 7 deletions

View File

@ -33,6 +33,12 @@ namespace Warship
public Bitmap MoveObject(Direction direction) 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) if (true)
{ {
@ -50,11 +56,30 @@ namespace Warship
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);
int i=(_width - x) / (int)_size_x;
int j = (_height - y) / (int)_size_y; (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition();
for
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; return true;
} }
else
{
Left += _size_x;
}
} while (res != 2);
Left = x;
Right += _size_y;
}
return false;
}
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()
{ {
@ -81,8 +106,44 @@ namespace Warship
_drawingObject.DrawingObject(gr); _drawingObject.DrawingObject(gr);
return bmp; 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 GenerateMap();
protected abstract void DrawWaterPart(Graphics gr, int i, int j); protected abstract void DrawWaterPart(Graphics gr, int i, int j);

View File

@ -12,6 +12,7 @@ namespace Warship
{ {
Warship = new EntityAdvancedWarship(speed, weight, bodyColor, dopColor, Helipad, Antenna, Missile); Warship = new EntityAdvancedWarship(speed, weight, bodyColor, dopColor, Helipad, Antenna, Missile);
} }
public override void DrawTransport(Graphics g) public override void DrawTransport(Graphics g)
{ {
if (Warship is not EntityAdvancedWarship advancedWarship) if (Warship is not EntityAdvancedWarship advancedWarship)

View File

@ -161,8 +161,9 @@
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(12, 12); "Вторая карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(667, 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

@ -69,6 +69,9 @@ namespace Warship
case "Простая карта": case "Простая карта":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Вторая карта":
_abstractMap = new SecondMap();
break;
} }
} }
} }

View File

@ -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++;
}
}
}
}
}