смена массива на список

This commit is contained in:
AnnZhimol 2022-10-18 16:19:04 +04:00
parent f8520664cc
commit 187957395e
2 changed files with 41 additions and 19 deletions

View File

@ -49,13 +49,9 @@ namespace Warship
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setWarship.Count; i++) foreach(var warship in _setWarship.GetWarships())
{ {
var warship = _setWarship.Get(i); return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
@ -74,11 +70,11 @@ namespace Warship
int j = _setWarship.Count - 1; int j = _setWarship.Count - 1;
for (int i = 0; i < _setWarship.Count; i++) for (int i = 0; i < _setWarship.Count; i++)
{ {
if (_setWarship.Get(i) == null) if (_setWarship[i] == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var warship = _setWarship.Get(j); var warship = _setWarship[j];
if (warship != null) if (warship != null)
{ {
_setWarship.Insert(warship, i); _setWarship.Insert(warship, i);
@ -111,15 +107,16 @@ namespace Warship
private void DrawWarship(Graphics gr) private void DrawWarship(Graphics gr)
{ {
//todo
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setWarship.Count; i++) for (int i = 0; i < _setWarship.Count; i++)
{ {
if (_setWarship.Get(i) != null) if (_setWarship[i] != null)
{ {
_setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); _setWarship[i].SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setWarship.Get(i)?.DrawingObject(gr); _setWarship[i]?.DrawingObject(gr);
} }
} }
} }

View File

@ -9,22 +9,27 @@ namespace Warship
internal class SetWarshipsGeneric<T> internal class SetWarshipsGeneric<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 SetWarshipsGeneric(int count) public SetWarshipsGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
_places = new List<T>();
} }
public int Insert(T warship) public int Insert(T warship)
{ {
//todo
return Insert(warship,0); return Insert(warship,0);
} }
public int Insert(T warship, int position) public int Insert(T warship, int position)
{ {
//todo
int EmptyEl=-1; int EmptyEl=-1;
if (position>=Count || position < 0) if (position>=Count || position < 0)
@ -58,6 +63,7 @@ namespace Warship
public T Remove(int position) public T Remove(int position)
{ {
//todo
if (position >= Count || position < 0 || _places[position]==null) if (position >= Count || position < 0 || _places[position]==null)
return null; return null;
@ -66,12 +72,31 @@ namespace Warship
return deleted; return deleted;
} }
public T Get(int position) public T this[int position]
{ {
if (position >= Count || position < 0) get
return null; {
//todo
return _places[position]; return _places[position];
}
set
{
//todo
}
}
public IEnumerable<T> GetWarships()
{
foreach(var warship in _places)
{
if (warship != null)
{
yield return warship;
}
else
{
yield break;
}
}
} }
} }
} }