161 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
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 _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
_height = height;
_drawingObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
if (_drawingObject != null)
{
if (true)
{
_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 null;
}
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()
{
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();
float nowX = Left;
float nowY = Right;
float lenX = Top - Left;
float lenY = Bottom - Right;
while (Check(nowX, nowY, nowX + lenX, nowY + lenY) != 2)
{
int result;
do
{
result = Check(nowX, nowY, nowX + lenX, nowY + lenY);
if (result == 0)
{
_drawingObject.SetObject((int)nowX, (int)nowY, _width, _height);
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()
{
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] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawingObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}