Прописана логика generic классов

This commit is contained in:
Данила Мочалов 2022-10-01 22:26:45 +04:00
parent c3c543ae46
commit a2333c47e3
2 changed files with 215 additions and 0 deletions

View File

@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class MapWithSetLocomotivesGeneric <T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
/// Ширина окна отрисовки
private readonly int _pictureWidth;
/// Высота окна отрисовки
private readonly int _pictureHeight;
/// Размер занимаемого объектом места (ширина)
private readonly int _placeSizeWidth = 210;
/// Размер занимаемого объектом места (высота)
private readonly int _placeSizeHeight = 90;
/// Набор объектов
private readonly SetLocomotivesGeneric<T> _setLocomotives;
/// Карта
private readonly U _map;
/// Конструктор
public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setLocomotives = new SetLocomotivesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// Перегрузка оператора сложения
public static bool operator +(MapWithSetLocomotivesGeneric<T, U> map, T locomotive)
{
return map._setLocomotives.Insert(locomotive);
}
/// Перегрузка оператора вычитания
public static bool operator -(MapWithSetLocomotivesGeneric<T, U> map, int position)
{
return map._setLocomotives.Remove(position);
}
/// Вывод всего набора объектов
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawLocomotives(gr);
return bmp;
}
/// Просмотр объекта на карте
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setLocomotives.Count; i++)
{
var locomotive = _setLocomotives.Get(i);
if (locomotive != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
}
}
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 = _setLocomotives.Count - 1;
for (int i = 0; i < _setLocomotives.Count; i++)
{
if (_setLocomotives.Get(i) == null)
{
for (; j > i; j--)
{
var locomotive = _setLocomotives.Get(j);
if (locomotive != null)
{
_setLocomotives.Insert(locomotive, i);
_setLocomotives.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// Метод отрисовки фона
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 DrawLocomotives(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int curWidth = 0;
int curHeight = 0;
for (int i = 0; i < _setLocomotives.Count; i++)
{
// установка позиции
_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setLocomotives.Get(i)?.DrawningObject(g);
if (curWidth < width) curWidth++;
else
{
curWidth = 0;
curHeight++;
}
}
}
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class SetLocomotivesGeneric <T>
where T : class
{
private readonly T[] _places;
/// Количество объектов в массиве
public int Count => _places.Length;
/// Конструктор
public SetLocomotivesGeneric(int count)
{
_places = new T[count];
}
/// Добавление объекта в набор
public bool Insert(T locomotive)
{
return Insert(locomotive, 0);
}
/// Добавление объекта в набор на конкретную позицию
public bool Insert(T locomotive, int position)
{
if (position >= _places.Length|| position < 0) return false;
if (_places[position] == null)
{
_places[position] = locomotive;
return true;
}
int emptyEl = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null) emptyEl = i;
break;
}
if (emptyEl == -1)
{
return false;
}
for (int i = emptyEl; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
return true;
}
/// Удаление объекта из набора с конкретной позиции
public bool Remove(int position)
{
if (position >= _places.Length || position < 0) return false;
_places[position] = null;
return true;
}
/// Получение объекта из набора по позиции
public T Get(int position)
{
if (position >= _places.Length || position < 0)
{
return null;
}
return _places[position];
}
}
}