Generic Classes

This commit is contained in:
shadowik 2022-10-03 21:24:08 +04:00
parent 5ee4ef719c
commit d5d9bed5dc
2 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class MapWithSetBusesGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetBusesGeneric<T> _setBuses;
private readonly U _map;
public MapWithSetBusesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBuses = new SetBusesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static bool operator +(MapWithSetBusesGeneric<T, U> map, T bus)
{
return map._setBuses.Insert(bus);
}
public static bool operator -(MapWithSetBusesGeneric<T, U> map, int position)
{
return map._setBuses.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawBuses(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBuses.Count; i++)
{
var bus = _setBuses.Get(i);
if (bus != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
}
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 = _setBuses.Count - 1;
for (int i = 0; i < _setBuses.Count; i++)
{
if (_setBuses.Get(i) == null)
{
for (; j > i; j--)
{
var bus = _setBuses.Get(j);
if (bus != null)
{
_setBuses.Insert(bus, i);
_setBuses.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 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 DrawBuses(Graphics g)
{
for (int i = 0; i < _setBuses.Count; i++)
{
// TODO установка позиции
_setBuses.Get(i)?.DrawingObject(g);
}
}
}
}

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class SetBusesGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
public SetBusesGeneric(int count)
{
_places = new T[count];
}
public bool Insert(T car)
{
// TODO вставка в начало набора
return true;
}
public bool Insert(T bus, int position)
{
// TODO проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
_places[position] = bus;
return true;
}
public bool Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из массива, присовив элементу массива значение null
return true;
}
public T Get(int position)
{
// TODO проверка позиции
return _places[position];
}
}
}