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