Четвертая лабораторная работа. Этап 1. Смена массива на список.
This commit is contained in:
parent
6344340950
commit
bf9dbaa9dc
@ -44,7 +44,7 @@ namespace ContainerShip
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setShips = new SetShipsGeneric<T>(width * height);
|
||||
_setShips = new SetShipsGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
@ -68,13 +68,9 @@ namespace ContainerShip
|
||||
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);
|
||||
}
|
||||
@ -99,11 +95,11 @@ namespace ContainerShip
|
||||
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);
|
||||
@ -141,12 +137,14 @@ namespace ContainerShip
|
||||
/// <param name="g"></param>
|
||||
private void DrawShips(Graphics g)
|
||||
{
|
||||
for (int i = 0; i < _setShips.Count; i++)
|
||||
int i = 0;
|
||||
int countOfShipsInLine = _pictureWidth / _placeSizeWidth;
|
||||
int countOfShipsInColumn = _pictureHeight / _placeSizeHeight;
|
||||
foreach (var ship in _setShips.GetShips())
|
||||
{
|
||||
int countOfShipsInLine = _pictureWidth / _placeSizeWidth;
|
||||
int countOfShipsInColumn = _pictureHeight / _placeSizeHeight;
|
||||
_setShips.Get(i)?.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||||
_setShips.Get(i)?.DrawingObject(g);
|
||||
ship.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||||
ship.DrawingObject(g);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -10,20 +10,22 @@ namespace ContainerShip
|
||||
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;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
private readonly int _maxCount;
|
||||
public SetShipsGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -41,41 +43,10 @@ namespace ContainerShip
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T ship, int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.Length)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (_places[position] == null)
|
||||
{
|
||||
|
||||
_places[position] = ship;
|
||||
}
|
||||
else
|
||||
{
|
||||
int freePlace = -1;
|
||||
for (int i = position; i < _places.Length; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
freePlace = i;
|
||||
}
|
||||
}
|
||||
if (freePlace == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = freePlace; i > position; i--)
|
||||
{
|
||||
T buf = _places[i];
|
||||
_places[i] = _places[i - 1];
|
||||
_places[i - 1] = buf;
|
||||
}
|
||||
_places[position] = ship;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (position < 0 || position >= _maxCount) return -1;
|
||||
if (_places.Count + 1 >= _maxCount) return -1;
|
||||
_places.Add(ship);
|
||||
return position;
|
||||
}
|
||||
/// <summary>
|
||||
@ -85,7 +56,7 @@ namespace ContainerShip
|
||||
/// <returns></returns>
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.Length)
|
||||
if (position < 0 || position >= _places.Count)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@ -98,13 +69,41 @@ namespace ContainerShip
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
if (position < 0 || position >= _places.Length)
|
||||
get
|
||||
{
|
||||
return null;
|
||||
// TODO проверка позиции
|
||||
if (position < 0 || position > _maxCount) return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position >= 0 || position < _maxCount)
|
||||
{
|
||||
Insert(value, position);
|
||||
}
|
||||
// TODO проверка позиции
|
||||
// TODO вставка в список по позиции
|
||||
}
|
||||
}
|
||||
/// <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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user