Этап 1. Переход с массива на список.

This commit is contained in:
Данила Мочалов 2022-10-10 17:33:07 +04:00
parent ca917f0511
commit 40da0fe01d
3 changed files with 43 additions and 51 deletions

View File

@ -3,7 +3,6 @@ namespace Locomotive
public partial class FormLocomotive : Form
{
private DrawningLocomotive _locomotive;
public DrawningLocomotive SelectedLocomotive { get; private set; }
public FormLocomotive()
@ -41,7 +40,6 @@ namespace Locomotive
SetData();
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
//ïîëó÷àåì èìÿ êíîïêè

View File

@ -55,14 +55,10 @@ namespace Locomotive
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 Locomotive
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);
@ -148,11 +144,11 @@ namespace Locomotive
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 + 10, curHeight * _placeSizeHeight + 15, _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

@ -9,14 +9,18 @@ namespace Locomotive
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)
@ -26,53 +30,47 @@ namespace Locomotive
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (position >= _places.Length|| position < 0) return -1;
if (position >= _maxCount|| position < 0) return -1;
_places.Insert(position, locomotive);
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;
return position;
}
/// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
if (position >= _places.Length || position < 0) return null;
if (position >= _maxCount || position < 0) return null;
T result = _places[position];
_places[position] = null;
_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;
}
}
}
}
}