начало

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

View File

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