Этап 1. Смена массива на список.
This commit is contained in:
parent
1429feb1a0
commit
9811cd100e
@ -52,14 +52,10 @@ namespace Battleship
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setBattleship.Count; i++)
|
||||
{
|
||||
var battleship = _setBattleship.Get(i);
|
||||
if (battleship != null)
|
||||
foreach (var battleship in _setBattleship.GetBattleship())
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
if (position < 0 || position >= _maxCount) return null;
|
||||
T savedBattleship = _places[position];
|
||||
_places.RemoveAt(position);
|
||||
return savedBattleship;
|
||||
}
|
||||
T removedObject = _places[position];
|
||||
_places[position] = null;
|
||||
return removedObject;
|
||||
}
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= Count || position < 0)
|
||||
return null;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user