Komlev_S.I. Lab3 #5

Closed
SemkaKMLV wants to merge 2 commits from Lab3 into Lab2
2 changed files with 208 additions and 0 deletions
Showing only changes of commit c8c28f4a68 - Show all commits

View File

@ -0,0 +1,128 @@
namespace GasolineTanker
{
internal class MapWithSetTrucksGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 110;
private readonly int _placeSizeHeight = 90;
private readonly SetTrucksGeneric<T> _setTrucks;
private readonly U _map;
public MapWithSetTrucksGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setTrucks = new SetTrucksGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetTrucksGeneric<T, U> map, T truck)
{
if (map._setTrucks.Insert(truck) >= 0)
return true;
else return false;
}
public static bool operator -(MapWithSetTrucksGeneric<T, U> map, int position)
{
if (map._setTrucks.Remove(position) != null)
return true;
else return false;
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawTrucks(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setTrucks.Count; i++)
{
var Truck = _setTrucks.Get(i);
if (Truck != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, Truck);
}
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
private void Shaking()
{
int j = _setTrucks.Count - 1;
for (int i = 0; i < _setTrucks.Count; i++)
{
if (_setTrucks.Get(i) == null)
{
for (; j > i; j--)
{
var Truck = _setTrucks.Get(j);
if (Truck != null)
{
_setTrucks.Insert(Truck, i);
_setTrucks.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Brush asphaltColor = new SolidBrush(Color.Gray);
g.FillRectangle(asphaltColor, 0, 0, _pictureWidth, _pictureHeight);
Pen pen = new(Color.Yellow, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawTrucks(Graphics g)
{
int xPosition = 10;
int yPosition = (_pictureHeight / _placeSizeHeight - 1) * _placeSizeHeight + 10;
for (int i = 0; i < _setTrucks.Count; i++)
{
_setTrucks.Get(i)?.SetObject(xPosition, yPosition, _pictureWidth, _pictureHeight);
_setTrucks.Get(i)?.DrawningObject(g);
xPosition += _placeSizeWidth;
if (xPosition + _placeSizeWidth > _pictureWidth)
{
yPosition -= _placeSizeHeight;
xPosition = 10;
}
}
}
}
}

View File

@ -0,0 +1,80 @@
namespace GasolineTanker
{
internal class SetTrucksGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetTrucksGeneric(int count)
{
_places = new T[count];
}
public int Insert(T truck)
{
bool freeSpace = false;
int firstFreeElement = -1;
for (int i = Count - 1; i >= 0; i--)
{
if (_places[i] == null)
{
freeSpace = true;
firstFreeElement = i;
}
}
if (!freeSpace)
return -1;
for (int i = firstFreeElement - 1; i >= 0; i--)
{
_places[i + 1] = _places[i];
}
_places[0] = truck;
return 0;
}
public int Insert(T truck, int position)
{
if (_places[position] != null)
{
bool freeSpace = false;
int firstFreeElement = -1;
for (int i = Count - 1; i < position; i--)
{
if (_places[i] == null)
{
freeSpace = true;
firstFreeElement = i;
}
}
if (!freeSpace)
return -1;
for (int i = firstFreeElement - 1; i > position; i--)
{
_places[i + 1] = _places[i];
}
}
_places[position] = truck;
return position;
}
public T Remove(int position)
{
if (_places[position] != null)
{
T result = _places[position];
_places[position] = null;
return result;
}
else
return null;
}
public T Get(int position)
{
if (_places[position] != null)
return _places[position];
else return null;
}
}
}