265 lines
9.7 KiB
C#
265 lines
9.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AircraftCarrier
|
|
{
|
|
internal abstract class AbstractMap : IEquatable<AbstractMap>
|
|
{
|
|
private IDrawningObject _drawningObject = 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 bool Equals(AbstractMap? other)
|
|
{
|
|
if (other == null)
|
|
{
|
|
return false;
|
|
}
|
|
var otherMap = other as AbstractMap;
|
|
if (otherMap == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (_width != otherMap._width)
|
|
{
|
|
return false;
|
|
}
|
|
if (_height != otherMap._height)
|
|
{
|
|
return false;
|
|
}
|
|
if (_size_x != otherMap._size_x)
|
|
{
|
|
return false;
|
|
}
|
|
if (_size_y != otherMap._size_y)
|
|
{
|
|
return false;
|
|
}
|
|
for (int i = 0; i < _map.GetLength(0); i++)
|
|
{
|
|
for (int j = 0; j < _map.GetLength(1); j++)
|
|
{
|
|
if (_map[i, j] != otherMap._map[i, j])
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
public Bitmap CreateMap(int width, int height, IDrawningObject
|
|
drawningObject)
|
|
{
|
|
_width = width;
|
|
_height = height;
|
|
_drawningObject = drawningObject;
|
|
GenerateMap();
|
|
while (!SetObjectOnMap())
|
|
{
|
|
GenerateMap();
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
public Bitmap MoveObject(Direction direction)
|
|
{
|
|
bool roadIsClear = true;
|
|
(float Left, float Top, float Right, float Bottom) = _drawningObject.GetCurrentPosition();
|
|
int xNumOfCells;
|
|
int yNumOfCells;
|
|
int xObjOffset;
|
|
int yObjOffset;
|
|
switch (direction)
|
|
{
|
|
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++)
|
|
{
|
|
if (!roadIsClear)
|
|
{
|
|
break;
|
|
}
|
|
for (int j = 0; j < xNumOfCells; j++)
|
|
{
|
|
if (yObjOffset - i < 0 || xObjOffset + j >= _map.GetLength(1))
|
|
{
|
|
break;
|
|
}
|
|
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 (roadIsClear)
|
|
{
|
|
_drawningObject.MoveObject(direction);
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
private bool SetObjectOnMap()
|
|
{
|
|
if (_drawningObject == null || _map == null)
|
|
{
|
|
return false;
|
|
}
|
|
int x = _random.Next(0, 10);
|
|
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))
|
|
{
|
|
while (x < _width - (Right - Left))
|
|
{
|
|
if (AreaIsFree(xNumOfCells, yNumOfCells, xObjOffset, yObjOffset))
|
|
{
|
|
_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;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
private Bitmap DrawMapWithObject()
|
|
{
|
|
Bitmap bmp = new(_width, _height);
|
|
if (_drawningObject == 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);
|
|
}
|
|
}
|
|
}
|
|
_drawningObject.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);
|
|
}
|
|
} |