Этап 1. Смена массива на список

This commit is contained in:
Sem730 2022-10-23 22:09:08 +03:00
parent e4d9e2aed1
commit 7bb13c445e
3 changed files with 46 additions and 85 deletions

View File

@ -7,12 +7,10 @@ namespace ProjectLocomotive
/// Âűáđŕííűé îáúĺęň
/// </summary>
public DrawningLocomotive SelectedLocomotive { get; private set; }
public FormLocomotive()
{
InitializeComponent();
}
/// <summary>
/// Ěĺňîä ďđîđčńîâęč ýëĺęňđîëîęîěîňčâŕ
/// </summary>
@ -39,11 +37,9 @@ namespace ProjectLocomotive
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
//_elloc = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
@ -114,18 +110,10 @@ namespace ProjectLocomotive
_elloc = new DrawningElectroLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000),
color,
dopColor,
//Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
//Color.FromArgb(rnd.Next(1, 100), rnd.Next(1, 100), rnd.Next(1, 100)),
Convert.ToBoolean(rnd.Next(0, 1)), Convert.ToBoolean(rnd.Next(0, 1)));
SetData();
Draw();
}
private void pictureBoxLocomotive_Click(object sender, EventArgs e)
{
}
private void buttonSelectLocomotive_Click(object sender, EventArgs e)
{
SelectedLocomotive = _elloc;

View File

@ -55,14 +55,10 @@ namespace ProjectLocomotive
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setLocomotives.Count; i++)
{
var locomotive = _setLocomotives.Get(i);
if (locomotive != null)
foreach (var locomotive in _setLocomotives.GetLocomotives())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
}
}
return new(_pictureWidth, _pictureHeight);
}
/// Перемещение объекта по крате
@ -80,11 +76,11 @@ namespace ProjectLocomotive
int j = _setLocomotives.Count - 1;
for (int i = 0; i < _setLocomotives.Count; i++)
{
if (_setLocomotives.Get(i) == null)
if (_setLocomotives[i] == null)
{
for (; j > i; j--)
{
var locomotive = _setLocomotives.Get(j);
var locomotive = _setLocomotives[j];
if (locomotive != null)
{
_setLocomotives.Insert(locomotive, i);
@ -102,42 +98,26 @@ namespace ProjectLocomotive
/// Метод отрисовки фона
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);
//}
Pen pen;
for (int j = _placeSizeHeight; j < _pictureHeight; j += _placeSizeHeight)
{
//нижняя линия рельс
pen = new(Color.Black, 5);
g.DrawLine(pen, 0, j, _pictureWidth, j);
for (int i = 0; i < _pictureWidth; i += 20)
{
g.DrawLine(pen, i, j, i, j + 10);
}
g.DrawLine(pen, 0, j + 10, _pictureWidth, j + 10);
//верхняя линия рельс
pen = new(Color.DarkGray, 4);
g.DrawLine(pen, 0, j - 20, _pictureWidth, j - 20);
for (int i = 0; i < _pictureWidth; i += 20)
{
g.DrawLine(pen, i, j - 20, i, j - 10);
}
g.DrawLine(pen, 0, j - 10, _pictureWidth, j - 10);
//фонари
for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth)
{
@ -157,12 +137,10 @@ namespace ProjectLocomotive
int curWidth = 0;
int curHeight = 0;
for (int i = 0; i < _setLocomotives.Count; i++)
foreach (var locomotive in _setLocomotives.GetLocomotives())
{
// установка позиции
//_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 470, _pictureWidth, _pictureHeight);
_setLocomotives.Get(i)?.DrawningObject(g);
locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
locomotive?.DrawningObject(g);
if (curWidth < width) curWidth++;
else
{

View File

@ -13,14 +13,17 @@ namespace ProjectLocomotive
internal class SetLocomotivesGeneric<T>
where T : class
{
private readonly T[] _places;
/// Список хранимых объектов
private readonly List<T> _places;
/// Количество объектов в массиве
public int Count => _places.Length;
/// Количество объектов в списке
public int Count => _places.Count;
private readonly int _maxCount;
/// Конструктор
public SetLocomotivesGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// Добавление объекта в набор
public int Insert(T locomotive)
@ -30,54 +33,46 @@ namespace ProjectLocomotive
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (position >= _places.Length || position < 0) return -1;
if (_places[position] == null)
{
_places[position] = locomotive;
return position;
}
int emptyEl = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
emptyEl = i;
break;
}
}
if (emptyEl == -1)
{
return -1;
}
for (int i = emptyEl; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
if (position >= _maxCount || position < 0) return -1;
_places.Insert(position, locomotive);
return position;
}
/// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
if (position >= _places.Length || position < 0) return null;
_places[position] = null;
if (position >= _maxCount || position < 0) return null;
T result = _places[position];
_places.RemoveAt(position);
return result;
}
/// Получение объекта из набора по позиции
public T Get(int position)
// Индексатор
public T this[int position]
{
if (position >= _places.Length || position < 0)
get
{
return null;
}
if (position >= _maxCount || position < 0) return null;
return _places[position];
}
set
{
if (position >= _maxCount || position < 0) return;
Insert(value, position);
}
}
/// Проход по набору до первого пустого
public IEnumerable<T> GetLocomotives()
{
foreach (var locomotive in _places)
{
if (locomotive != null)
{
yield return locomotive;
}
else
{
yield break;
}
}
}
}
}