ISEBbd-22_Novopoltsev_A.A._.../Warship/Warship/AbstractMap.cs

153 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Warship
{
internal abstract class AbstractMap
{
private IDrawingObject _drawingObject = null;
protected int[,] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected readonly Random _random = new();
protected readonly int _freeWaterArea = 0;
protected readonly int _land = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
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)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
(float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition();
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;
}
else
{
Left += _size_x;
}
} while (res != 2);
Left = x;
Right += _size_y;
}
return false;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _freeWaterArea)
{
DrawWaterPart(gr, i, j);
}
else if (_map[i, j] == _land)
{
DrawLandPart(gr, i, j);
}
}
}
_drawingObject.DrawingObject(gr);
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 DrawWaterPart(Graphics gr, int i, int j);
protected abstract void DrawLandPart(Graphics gr, int i, int j);
}
}