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

View File

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

View File

@ -13,14 +13,17 @@ namespace ProjectLocomotive
internal class SetLocomotivesGeneric<T> internal class SetLocomotivesGeneric<T>
where T : class 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) public SetLocomotivesGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
_places = new List<T>();
} }
/// Добавление объекта в набор /// Добавление объекта в набор
public int Insert(T locomotive) public int Insert(T locomotive)
@ -30,54 +33,46 @@ namespace ProjectLocomotive
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position) 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; return position;
} }
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
public T Remove(int position) public T Remove(int position)
{ {
if (position >= _places.Length || position < 0) return null; if (position >= _maxCount || position < 0) return null;
_places[position] = null;
T result = _places[position]; T result = _places[position];
_places.RemoveAt(position);
return result; 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;
}
} }
return _places[position];
} }
} }
} }