Правки

This commit is contained in:
nikbel2004@outlook.com 2023-10-29 00:39:44 +04:00
parent 1dd901762a
commit a974a4341e
4 changed files with 22 additions and 7 deletions

View File

@ -55,7 +55,6 @@ namespace Tank
Direction.Right => _startPosX + _Width + Tank.Step < _pictureWidth, Direction.Right => _startPosX + _Width + Tank.Step < _pictureWidth,
Direction.Down => _startPosY + _Height + Tank.Step < _pictureHeight, Direction.Down => _startPosY + _Height + Tank.Step < _pictureHeight,
_ => false _ => false
}; };
} }

View File

@ -9,22 +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 tank) public bool Insert(T tank)
{ {
return Insert(tank, 0); return Insert(tank, 0);
} }
// Добавление на конкретную позицию
public bool Insert(T tank, int position) public bool Insert(T tank, int position)
{ {
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
@ -36,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)
@ -46,6 +52,7 @@ namespace Tank
return true; return true;
} }
// Получение объекта из набора по позиции
public T? this[int position] public T? this[int position]
{ {
get get
@ -62,12 +69,13 @@ namespace Tank
} }
} }
public IEnumerable<T?> GetTanks(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

@ -99,12 +99,11 @@ namespace Tank.Generics
{ {
if (tank != null) if (tank != null)
{ {
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++;
} }
} }
} }
} }

View File

@ -11,28 +11,37 @@ namespace Tank
{ {
internal class TanksGenericStorage internal class TanksGenericStorage
{ {
// Словарь
readonly Dictionary<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>> _tankStorages; readonly Dictionary<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>> _tankStorages;
// Возвращение списка названий наборов
public List<string> Keys => _tankStorages.Keys.ToList(); public List<string> Keys => _tankStorages.Keys.ToList();
private readonly int _pictureWidth; private readonly int _pictureWidth;
private readonly int _pictureHeight; private readonly int _pictureHeight;
// Конструктор
public TanksGenericStorage(int pictureWidth, int pictureHeight) public TanksGenericStorage(int pictureWidth, int pictureHeight)
{ {
_tankStorages = new Dictionary<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>>(); _tankStorages = new Dictionary<string, TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>>();
_pictureWidth = pictureWidth; _pictureWidth = pictureWidth;
_pictureHeight = pictureHeight; _pictureHeight = pictureHeight;
} }
// Добавление набора
public void AddSet(string name) public void AddSet(string name)
{ {
if (_tankStorages.ContainsKey(name)) return; if (_tankStorages.ContainsKey(name)) return;
_tankStorages[name] = new TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>(_pictureWidth, _pictureHeight); _tankStorages[name] = new TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>(_pictureWidth, _pictureHeight);
} }
// Удаление набора
public void DelSet(string name) public void DelSet(string name)
{ {
if (!_tankStorages.ContainsKey(name)) return; if (!_tankStorages.ContainsKey(name)) return;
_tankStorages.Remove(name); _tankStorages.Remove(name);
} }
// Доступ к набору
public TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>? public TanksGenericCollection<DrawArmoVehicle, DrawingObjectTank>?
this[string ind] this[string ind]
{ {
@ -43,4 +52,4 @@ namespace Tank
} }
} }
} }
} }