начало

This commit is contained in:
Макс Бондаренко 2022-10-29 21:11:34 +04:00
parent d29a47f44e
commit 62bae752ae
2 changed files with 46 additions and 18 deletions

View File

@ -51,7 +51,7 @@ namespace WarmlyShip
Shaking();
for (int i = 0; i < _setShips.Count; i++)
{
var ship = _setShips.Get(i);
var ship = _setShips[i];
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
@ -74,11 +74,11 @@ namespace WarmlyShip
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 car = _setShips.Get(j);
var car = _setShips[j];
if (car != null)
{
_setShips.Insert(car, i);
@ -113,12 +113,12 @@ namespace WarmlyShip
{
for (int i = 0; i < _setShips.Count; ++i)
{
if (_setShips.Get(i) != null )
if (_setShips[i] != null )
{
int temp = 0;
if (_setShips.Get(i).GetCurrentPosition().Bottom - _setShips.Get(i).GetCurrentPosition().Top < 75) temp = (int)(_setShips.Get(i).GetCurrentPosition().Bottom - _setShips.Get(i).GetCurrentPosition().Top);
_setShips.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight);
_setShips.Get(i).DrawningObject(g);
if (_setShips[i].GetCurrentPosition().Bottom - _setShips[i].GetCurrentPosition().Top < 75) temp = (int)(_setShips[i].GetCurrentPosition().Bottom - _setShips[i].GetCurrentPosition().Top);
_setShips[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight);
_setShips[i].DrawningObject(g);
}
}
}

View File

@ -9,17 +9,20 @@ namespace WarmlyShip
internal class SetShipGeneric<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 SetShipGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
private bool CanInsert(int position)
{
for (int i = position; i < _places.Length; ++i)
for (int i = position; i < Count; ++i)
if (_places[i] == null) return true;
return false;
}
@ -28,7 +31,7 @@ namespace WarmlyShip
{
if (CanInsert(0))
{
for (int i = _places.Length - 1; i > 0; --i)
for (int i = Count - 1; i > 0; --i)
{
if (_places[i] == null)
{
@ -44,10 +47,10 @@ namespace WarmlyShip
public int Insert(T ship, int position)
{
if (position < 0 || position > _places.Length) return 0;
if (_places[position] != null && CanInsert(position))
if (position < 0 || position > Count || Count == _maxCount) return 0;
/*if (_places[position] != null && CanInsert(position))
{
for (int i = _places.Length - 1; i > position; --i)
for (int i = _places.Count - 1; i > position; --i)
{
if (_places[i] == null)
{
@ -57,6 +60,8 @@ namespace WarmlyShip
}
}
_places[position] = ship;
return 1;*/
_places.Insert(position, ship);
return 1;
}
@ -66,11 +71,34 @@ namespace WarmlyShip
return 1;
}
public T Get(int position)
public T this[int position]
{
if (position < 0 || position > _places.Length) return null;
get
{
if (position < 0 || position > Count) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetShips()
{
foreach (var ship in _places)
{
if (ship != null)
{
yield return ship;
}
else
{
yield break;
}
}
}
}
}