diff --git a/GasolineTanker/GasolineTanker/MapWithSetTrucksGeneric.cs b/GasolineTanker/GasolineTanker/MapWithSetTrucksGeneric.cs new file mode 100644 index 0000000..1ff386d --- /dev/null +++ b/GasolineTanker/GasolineTanker/MapWithSetTrucksGeneric.cs @@ -0,0 +1,128 @@ +namespace GasolineTanker +{ + internal class MapWithSetTrucksGeneric + 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 _setTrucks; + private readonly U _map; + + public MapWithSetTrucksGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setTrucks = new SetTrucksGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static bool operator +(MapWithSetTrucksGeneric map, T truck) + { + if (map._setTrucks.Insert(truck) >= 0) + return true; + else return false; + } + public static bool operator -(MapWithSetTrucksGeneric 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; + } + } + } + } +} + diff --git a/GasolineTanker/GasolineTanker/SetTrucksGeneric.cs b/GasolineTanker/GasolineTanker/SetTrucksGeneric.cs new file mode 100644 index 0000000..7c77f6d --- /dev/null +++ b/GasolineTanker/GasolineTanker/SetTrucksGeneric.cs @@ -0,0 +1,80 @@ +namespace GasolineTanker +{ + internal class SetTrucksGeneric + 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; + } + } +}