Этап 1. Смена массива на список

This commit is contained in:
just1valery 2022-10-09 12:52:05 +04:00
parent 0ce75bbe16
commit 903669666a
2 changed files with 45 additions and 18 deletions

View File

@ -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);
}

View File

@ -10,20 +10,23 @@ namespace WarmlyShip
where T : class
{
/// <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>
/// Добавление объекта в набор
@ -32,6 +35,7 @@ namespace WarmlyShip
/// <returns></returns>
public bool Insert(T ship)
{
//проверка на макс каунт
return Insert(ship, 0);
}
private bool isCorrectPosition(int position)
@ -70,7 +74,7 @@ namespace WarmlyShip
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
{
if (!isCorrectPosition(position))
return false;
_places[position-1] = null;
@ -81,9 +85,36 @@ namespace WarmlyShip
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
return isCorrectPosition(position) ? _places[position] : null;
get
{
// TODO проверка позиции
return _places[position];
}
set
{
// TODO проверка позиции
// TODO вставка в список по позиции
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetShips()
{
foreach (var ship in _places)
{
if (ship != null)
{
yield return ship;
}
else
{
yield break;
}
}
}
}
}