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

This commit is contained in:
Hells Hound 2022-10-15 18:59:41 +04:00
parent e8e0bd9329
commit ba6554f76a
2 changed files with 51 additions and 51 deletions

View File

@ -93,13 +93,9 @@ namespace AircraftCarrier
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setWarships.Count; i++)
foreach(var warship in _setWarships.GetWarships())
{
var warship = _setWarships.Get(i);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
return new(_pictureWidth, _pictureHeight);
}
@ -124,11 +120,11 @@ namespace AircraftCarrier
int j = _setWarships.Count - 1;
for (int i = 0; i < _setWarships.Count; i++)
{
if (_setWarships.Get(i) == null)
if (_setWarships[i] == null)
{
for (; j > i; j--)
{
var car = _setWarships.Get(j);
var car = _setWarships[j];
if (car != null)
{
_setWarships.Insert(car, i);
@ -166,13 +162,13 @@ namespace AircraftCarrier
/// <param name="g"></param>
private void DrawWarships(Graphics g)
{
int countInLine = _pictureWidth / _placeSizeWidth;
int maxLeft = (countInLine - 1) * _placeSizeWidth;
//for (int i = 0; i < _setWarships.Count; i++)
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setWarships.Count; i++)
{
_setWarships.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
_setWarships.Get(i)?.DrawningObject(g);
var warship = _setWarships[i];
warship?.SetObject(i % _pictureWidth * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight + 4, _pictureWidth, _pictureHeight);
warship?.DrawningObject(g);
}
}
}

View File

@ -14,20 +14,23 @@ namespace AircraftCarrier
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 SetWarshipsGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -46,32 +49,8 @@ namespace AircraftCarrier
/// <returns></returns>
public int Insert(T warship, int position)
{
int EmptyElement = -1;
if (position >= Count || position < 0) return -1;
if (_places[position] == null)
{
_places[position] = warship;
return 1;
}
else if (_places[position] != null)
{
for (int i = position + 1; i < Count; i++)
if (_places[i] == null)
{
EmptyElement = i;
break;
}
if (EmptyElement == -1)
return -1;
for (int i = EmptyElement; i > position; i--)
_places[i] = _places[i - 1];
}
_places[position] = warship;
if (position >= _maxCount || position < 0) return -1;
_places.Insert(position, warship);
return 1;
}
/// <summary>
@ -81,22 +60,47 @@ namespace AircraftCarrier
/// <returns></returns>
public T Remove(int position)
{
if (position >= Count || position < 0 || _places[position] == null)
if (position >= _maxCount || position < 0)
return null;
T deleted = _places[position];
_places[position] = null;
return deleted;
var result = _places[position];
_places.RemoveAt(position);
return result;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position >= _places.Length) return null;
return _places[position];
get
{
if (position >= _maxCount || position < 0) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetWarships()
{
foreach(var warship in _places)
{
if(warship != null)
{
yield return warship;
}
else
{
yield break;
}
}
}
}
}