Хранение в списке.

This commit is contained in:
the 2022-10-11 10:59:59 +04:00
parent 45aa67a6bc
commit 32edffdb33
2 changed files with 50 additions and 51 deletions

View File

@ -92,7 +92,7 @@ namespace Ship
Shaking();
for (int i = 0; i < _setShips.Count; i++)
{
var car = _setShips.Get(i);
var car = _setShips[i];
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
@ -121,11 +121,11 @@ namespace Ship
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 car = _setShips.Get(j);
var car = _setShips[j];
if (car != null)
{
_setShips.Insert(car, i);
@ -174,14 +174,12 @@ namespace Ship
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setShips.Count; i++)
int i = 0;
foreach (var ship in _setShips.GetShips())
{
var ship = _setShips.Get(i);
if (ship != null)
{
ship.SetObject((width - i % width - 1) * _placeSizeWidth + 10, (i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
ship.DrawningObject(g);
}
ship.SetObject((width - i % width - 1) * _placeSizeWidth + 10, (i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
ship.DrawningObject(g);
i++;
}
}
}

View File

@ -12,18 +12,21 @@ namespace Ship
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places;
private readonly List<T> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetShipsGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -42,38 +45,9 @@ namespace Ship
/// <returns></returns>
public int Insert(T ship, int position)
{
if (position < 0 || position >= Count)
{
return -1;
}
if (position > _maxCount || Count == _maxCount || position < 0) return -1;
if (_places[position] == null)
{
_places[position] = ship;
return position;
}
int firstNull = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
firstNull = i;
break;
}
}
if (firstNull == -1)
{
return -1;
}
for (int i = firstNull; i > position; i--)
{
(_places[i], _places[i - 1]) = (_places[i - 1], _places[i]);
}
_places[position] = ship;
_places.Insert(position, ship);
return position;
}
/// <summary>
@ -83,14 +57,14 @@ namespace Ship
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= Count || _places[position] == null)
if (position < 0 || position >= Count)
{
return null;
}
var result = _places[position];
_places[position] = null;
_places.RemoveAt(position);
return result;
}
/// <summary>
@ -98,14 +72,41 @@ namespace Ship
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= Count)
get
{
return null;
if (position >= Count || position < 0)
{
return null;
}
return _places[position];
}
set
{
if (position < Count && position >= 0)
{
Insert(value, position);
}
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetShips()
{
foreach (var ship in _places)
{
if (ship != null)
{
yield return ship;
}
else
{
yield break;
}
}
return _places[position];
}
}
}