Готовая лабораторная работа 5

This commit is contained in:
nikbel2004@outlook.com 2023-11-13 02:33:10 +04:00
parent 7668db2e11
commit 1560754552
4 changed files with 15 additions and 14 deletions

View File

@ -173,7 +173,6 @@
Margin = new Padding(3, 4, 3, 4); Margin = new Padding(3, 4, 3, 4);
Name = "FormTank"; Name = "FormTank";
Text = "FormTank"; Text = "FormTank";
Load += FormTank_Load;
((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }

View File

@ -128,10 +128,5 @@ namespace Tank
SelectedTank = _Tank; SelectedTank = _Tank;
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
} }
private void FormTank_Load(object sender, EventArgs e)
{
}
} }
} }

View File

@ -9,21 +9,27 @@ namespace Tank
{ {
internal class SetGeneric<T> where T : class internal class SetGeneric<T> where T : class
{ {
// Список объектов, которые храним и их количество
private readonly List<T?> _places; private readonly List<T?> _places;
public int Count => _places.Count; public int Count => _places.Count;
// Максимальное количество объектов
private readonly int _maxCount; private readonly int _maxCount;
// Конструктор
public SetGeneric(int count) public SetGeneric(int count)
{ {
_maxCount = count; _maxCount = count;
_places = new List<T?>(_maxCount); _places = new List<T?>(_maxCount);
} }
// Добавление объекта в набор
public bool Insert(T car) public bool Insert(T car)
{ {
return Insert(car, 0); return Insert(car, 0);
} }
// Добавление на конкретную позицию
public bool Insert(T car, int position) public bool Insert(T car, int position)
{ {
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
@ -35,6 +41,7 @@ namespace Tank
return true; return true;
} }
// Удаление объекта из набора с конкретной позиции
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position > _maxCount) if (position < 0 || position > _maxCount)
@ -44,6 +51,8 @@ namespace Tank
_places.RemoveAt(position); _places.RemoveAt(position);
return true; return true;
} }
// Получение объекта из набора по позиции
public T? this[int position] public T? this[int position]
{ {
get get
@ -60,18 +69,17 @@ namespace Tank
} }
} }
// Проход по списку
public IEnumerable<T?> GetCars(int? maxCars = null) public IEnumerable<T?> GetTanks(int? maxTanks = null)
{ {
for (int i = 0; i < _places.Count; ++i) for (int i = 0; i < _places.Count; ++i)
{ {
yield return _places[i]; yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value) if (maxTanks.HasValue && i == maxTanks.Value)
{ {
yield break; yield break;
} }
} }
} }
} }
} }

View File

@ -95,13 +95,13 @@ namespace Tank.Generics
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
int i = 0; int i = 0;
foreach(var tank in _collection.GetCars()) foreach(var tank in _collection.GetTanks())
{ {
if (tank != null) if (tank != null)
{ {
tank._pictureWidth = _pictureWidth; tank._pictureWidth = _pictureWidth;
tank._pictureHeight = _pictureHeight; tank._pictureHeight = _pictureHeight;
tank.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); tank.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
tank.DrawTransport(g); tank.DrawTransport(g);
} }
i++; i++;
@ -109,4 +109,3 @@ namespace Tank.Generics
} }
} }
} }