diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs
index 212d2f0..bf56fd9 100644
--- a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs
+++ b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs
@@ -88,13 +88,9 @@ namespace WarmlyShip
public Bitmap ShowOnMap()
{
Shaking();
- for (int i = 0; i < _setShips.Count; i++)
+ foreach (var ship in _setShips.GetShips())
{
- var ship = _setShips.Get(i);
- if (ship != null)
- {
- return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
- }
+ return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
return new(_pictureWidth, _pictureHeight);
}
@@ -119,11 +115,11 @@ namespace WarmlyShip
int j = _setShips.Count - 1;
for (int i = 0; i < _setShips.Count; i++)
{
- if (_setShips.Get(i) == null)
+ if (_setShips[i] == null)
{
for (; j > i; j--)
{
- var ship = _setShips.Get(j);
+ var ship = _setShips[j];
if (ship != null)
{
_setShips.Insert(ship, i);
@@ -170,9 +166,9 @@ namespace WarmlyShip
{
int countInLine = _pictureWidth / _placeSizeWidth;
int maxLeft = (countInLine - 1) * _placeSizeWidth;
- for (int i = 0; i < _setShips.Count; i++)
+ foreach (var ship in _setShips.GetShips())
{
- var ship = _setShips.Get(i);
+ var ship = _setShips[i];
ship?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
ship?.DrawningObject(g);
}
diff --git a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs
index 8fa50b1..62df44e 100644
--- a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs
+++ b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs
@@ -10,20 +10,23 @@ namespace WarmlyShip
where T : class
{
///
- /// Массив объектов, которые храним
+ /// Список объектов, которые храним
///
- private readonly T[] _places;
+ private readonly List _places;
///
- /// Количество объектов в массиве
+ /// Количество объектов в списке
///
- public int Count => _places.Length;
+ public int Count => _places.Count;
+
+ private readonly int _maxCount;
///
/// Конструктор
///
///
public SetShipsGeneric(int count)
{
- _places = new T[count];
+ _maxCount = count;
+ _places = new List();
}
///
/// Добавление объекта в набор
@@ -32,6 +35,7 @@ namespace WarmlyShip
///
public bool Insert(T ship)
{
+ //проверка на макс каунт
return Insert(ship, 0);
}
private bool isCorrectPosition(int position)
@@ -70,7 +74,7 @@ namespace WarmlyShip
///
///
public bool Remove(int position)
- {
+ {
if (!isCorrectPosition(position))
return false;
_places[position-1] = null;
@@ -81,9 +85,36 @@ namespace WarmlyShip
///
///
///
- public T Get(int position)
+ public T this[int position]
{
- return isCorrectPosition(position) ? _places[position] : null;
+ get
+ {
+ // TODO проверка позиции
+ return _places[position];
+ }
+ set
+ {
+ // TODO проверка позиции
+ // TODO вставка в список по позиции
+ }
+ }
+ ///
+ /// Проход по набору до первого пустого
+ ///
+ ///
+ public IEnumerable GetShips()
+ {
+ foreach (var ship in _places)
+ {
+ if (ship != null)
+ {
+ yield return ship;
+ }
+ else
+ {
+ yield break;
+ }
+ }
}
}
}