Kadyrov A.F. LabWork04 #4

Closed
NAP wants to merge 3 commits from LabWork04 into LabWork03
2 changed files with 56 additions and 53 deletions
Showing only changes of commit 9811cd100e - Show all commits

View File

@ -52,13 +52,9 @@ namespace Battleship
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBattleship.Count; i++)
foreach (var battleship in _setBattleship.GetBattleship())
{
var battleship = _setBattleship.Get(i);
if (battleship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
}
return new(_pictureWidth, _pictureHeight);
}
@ -77,11 +73,11 @@ namespace Battleship
int j = _setBattleship.Count - 1;
for (int i = 0; i < _setBattleship.Count; i++)
{
if (_setBattleship.Get(i) == null)
if (_setBattleship[i] == null)
{
for (; j > i; j--)
{
var battleship = _setBattleship.Get(j);
var battleship = _setBattleship[j];
if (battleship != null)
{
_setBattleship.Insert(battleship, i);
@ -115,14 +111,13 @@ namespace Battleship
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int i = 0;
for (int i = 0; i < _setBattleship.Count; i++)
foreach (var battleship in _setBattleship.GetBattleship())
{
if (_setBattleship.Get(i) != null)
{
_setBattleship.Get(i).SetObject(i % width * _placeSizeWidth + 15, (height - 1 - i / width) * _placeSizeHeight + 8, _pictureWidth, _pictureHeight);
_setBattleship.Get(i)?.DrawningObject(gr);
}
battleship.SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
battleship.DrawningObject(gr);
i++;
}
}
}

View File

@ -13,64 +13,72 @@ namespace Battleship
internal class SetBattleshipGeneric<T>
where T : class
{
private readonly T[] _places;
private readonly List<T> _places;
public int Count => _places.Length;
public int Count => _places.Count;
private int BattleshipPlaces = 0;
private readonly int _maxCount;
public SetBattleshipGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T battleship)
{
return Insert(battleship, 0);
if (_places.Count + 1 >= _maxCount)
return -1;
_places.Insert(0, battleship);
return 0;
}
public int Insert(T battleship, int position)
{
if (position >= _places.Length)
{
if (position >= _maxCount || position < 0)
return -1;
}
if (_places[position] != null)
{
int indexNull = -1;
for (int i = position; i < _places.Length; i++)
{
if (_places[i] == null)
{
indexNull = i;
break;
}
}
if (indexNull == -1) return -1;
for (int i = indexNull; i > position; i--)
{
T tmp = _places[i];
_places[i] = _places[i - 1];
_places[i - 1] = tmp;
}
}
_places[position] = battleship;
if (_places.Count + 1 >= _maxCount)
return -1;
_places.Insert(position, battleship);
return position;
}
public T Remove(int position)
{
if (position >= _places.Length)
{
return null;
}
T removedObject = _places[position];
_places[position] = null;
return removedObject;
if (position < 0 || position >= _maxCount) return null;
T savedBattleship = _places[position];
_places.RemoveAt(position);
return savedBattleship;
}
public T Get(int position)
{
if (position >= Count || position < 0)
return null;
return _places[position];
public T this[int position]
{
get
{
if (position < 0 || position >= _maxCount) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetBattleship()
{
foreach (var battleship in _places)
{
if (battleship != null)
{
yield return battleship;
}
else
{
yield break;
}
}
}
}
}