Хранение в списке.
This commit is contained in:
parent
45aa67a6bc
commit
32edffdb33
@ -92,7 +92,7 @@ namespace Ship
|
|||||||
Shaking();
|
Shaking();
|
||||||
for (int i = 0; i < _setShips.Count; i++)
|
for (int i = 0; i < _setShips.Count; i++)
|
||||||
{
|
{
|
||||||
var car = _setShips.Get(i);
|
var car = _setShips[i];
|
||||||
if (car != null)
|
if (car != null)
|
||||||
{
|
{
|
||||||
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
|
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
|
||||||
@ -121,11 +121,11 @@ namespace Ship
|
|||||||
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 car = _setShips.Get(j);
|
var car = _setShips[j];
|
||||||
if (car != null)
|
if (car != null)
|
||||||
{
|
{
|
||||||
_setShips.Insert(car, i);
|
_setShips.Insert(car, i);
|
||||||
@ -174,14 +174,12 @@ namespace Ship
|
|||||||
int width = _pictureWidth / _placeSizeWidth;
|
int width = _pictureWidth / _placeSizeWidth;
|
||||||
int height = _pictureHeight / _placeSizeHeight;
|
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);
|
ship.SetObject((width - i % width - 1) * _placeSizeWidth + 10, (i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||||
if (ship != null)
|
ship.DrawningObject(g);
|
||||||
{
|
i++;
|
||||||
ship.SetObject((width - i % width - 1) * _placeSizeWidth + 10, (i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
|
||||||
ship.DrawningObject(g);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,18 +12,21 @@ namespace Ship
|
|||||||
/// <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;
|
||||||
|
|
||||||
|
private readonly int _maxCount;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="count"></param>
|
/// <param name="count"></param>
|
||||||
public SetShipsGeneric(int count)
|
public SetShipsGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = new T[count];
|
_maxCount = count;
|
||||||
|
_places = new List<T>();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор
|
/// Добавление объекта в набор
|
||||||
@ -42,38 +45,9 @@ namespace Ship
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T ship, int position)
|
public int Insert(T ship, int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= Count)
|
if (position > _maxCount || Count == _maxCount || position < 0) return -1;
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_places[position] == null)
|
_places.Insert(position, ship);
|
||||||
{
|
|
||||||
_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;
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -83,14 +57,14 @@ namespace Ship
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position < 0 || position >= Count || _places[position] == null)
|
if (position < 0 || position >= Count)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = _places[position];
|
var result = _places[position];
|
||||||
|
|
||||||
_places[position] = null;
|
_places.RemoveAt(position);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -98,14 +72,41 @@ namespace Ship
|
|||||||
/// </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 >= 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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user