Этап 1. Смена массива на список.
This commit is contained in:
parent
50d81bcc08
commit
a23192133f
@ -44,13 +44,9 @@ namespace Liner
|
||||
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);
|
||||
}
|
||||
@ -67,11 +63,11 @@ namespace Liner
|
||||
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);
|
||||
@ -107,11 +103,11 @@ namespace Liner
|
||||
int heightEl = _pictureHeight / _placeSizeHeight;
|
||||
int curWidth = 1;
|
||||
int curHeight = 0;
|
||||
for (int i = 0; i < _setShips.Count; i++)
|
||||
foreach (var ship in _setShips.GetShips())
|
||||
{
|
||||
_setShips.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - (_pictureWidth / 7),
|
||||
ship?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 10,
|
||||
curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||
_setShips.Get(i)?.DrawningObject(g);
|
||||
ship?.DrawningObject(g);
|
||||
|
||||
if (curWidth < widthEl)
|
||||
curWidth++;
|
||||
|
@ -9,64 +9,71 @@ namespace Liner
|
||||
internal class SetShipsGeneric<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly T[] _places;
|
||||
public int Count => _places.Length;
|
||||
private readonly List<T> _places;
|
||||
public int Count => _places.Count;
|
||||
private readonly int _maxCount;
|
||||
public SetShipsGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
public int Insert(T ship)
|
||||
{
|
||||
if (_places.Count > _maxCount)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return Insert(ship, 0);
|
||||
}
|
||||
public int Insert(T ship, int position)
|
||||
{
|
||||
if (position >= _places.Length || position < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (_places[position] == null)
|
||||
{
|
||||
_places[position] = ship;
|
||||
return position;
|
||||
}
|
||||
int emptyIndex = -1;
|
||||
for (int i = position + 1; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
emptyIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (emptyIndex < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
for (int i = emptyIndex; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = ship;
|
||||
if (position >= _maxCount || position < 0) return -1;
|
||||
_places.Insert(position, ship);
|
||||
return position;
|
||||
}
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= _places.Length || position < 0)
|
||||
if (position >= _maxCount || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
T ship = _places[position];
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return ship;
|
||||
}
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
if (position >= _places.Length || position < 0)
|
||||
get
|
||||
{
|
||||
return null;
|
||||
if (position >= _places.Count || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position >= _places.Count || position < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Insert(value, position);
|
||||
|
||||
}
|
||||
}
|
||||
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