Этап 1. Смена массива на список
This commit is contained in:
parent
e8e0bd9329
commit
ba6554f76a
@ -93,14 +93,10 @@ namespace AircraftCarrier
|
|||||||
public Bitmap ShowOnMap()
|
public Bitmap ShowOnMap()
|
||||||
{
|
{
|
||||||
Shaking();
|
Shaking();
|
||||||
for (int i = 0; i < _setWarships.Count; i++)
|
foreach(var warship in _setWarships.GetWarships())
|
||||||
{
|
|
||||||
var warship = _setWarships.Get(i);
|
|
||||||
if (warship != null)
|
|
||||||
{
|
{
|
||||||
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
|
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return new(_pictureWidth, _pictureHeight);
|
return new(_pictureWidth, _pictureHeight);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -124,11 +120,11 @@ namespace AircraftCarrier
|
|||||||
int j = _setWarships.Count - 1;
|
int j = _setWarships.Count - 1;
|
||||||
for (int i = 0; i < _setWarships.Count; i++)
|
for (int i = 0; i < _setWarships.Count; i++)
|
||||||
{
|
{
|
||||||
if (_setWarships.Get(i) == null)
|
if (_setWarships[i] == null)
|
||||||
{
|
{
|
||||||
for (; j > i; j--)
|
for (; j > i; j--)
|
||||||
{
|
{
|
||||||
var car = _setWarships.Get(j);
|
var car = _setWarships[j];
|
||||||
if (car != null)
|
if (car != null)
|
||||||
{
|
{
|
||||||
_setWarships.Insert(car, i);
|
_setWarships.Insert(car, i);
|
||||||
@ -166,13 +162,13 @@ namespace AircraftCarrier
|
|||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
private void DrawWarships(Graphics g)
|
private void DrawWarships(Graphics g)
|
||||||
{
|
{
|
||||||
int countInLine = _pictureWidth / _placeSizeWidth;
|
int width = _pictureWidth / _placeSizeWidth;
|
||||||
int maxLeft = (countInLine - 1) * _placeSizeWidth;
|
int height = _pictureHeight / _placeSizeHeight;
|
||||||
//for (int i = 0; i < _setWarships.Count; i++)
|
|
||||||
for (int i = 0; i < _setWarships.Count; i++)
|
for (int i = 0; i < _setWarships.Count; i++)
|
||||||
{
|
{
|
||||||
_setWarships.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
|
var warship = _setWarships[i];
|
||||||
_setWarships.Get(i)?.DrawningObject(g);
|
warship?.SetObject(i % _pictureWidth * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight + 4, _pictureWidth, _pictureHeight);
|
||||||
|
warship?.DrawningObject(g);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,20 +14,23 @@ namespace AircraftCarrier
|
|||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Массив объектов, которые храним
|
/// Список объектов, которые храним
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly T[] _places;
|
private readonly List<T> _places;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Количество объектов в массиве
|
/// Количество объектов в списке
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Count => _places.Length;
|
public int Count => _places.Count;
|
||||||
|
|
||||||
|
private readonly int _maxCount;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="count"></param>
|
/// <param name="count"></param>
|
||||||
public SetWarshipsGeneric(int count)
|
public SetWarshipsGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = new T[count];
|
_maxCount = count;
|
||||||
|
_places = new List<T>();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление объекта в набор
|
/// Добавление объекта в набор
|
||||||
@ -46,32 +49,8 @@ namespace AircraftCarrier
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Insert(T warship, int position)
|
public int Insert(T warship, int position)
|
||||||
{
|
{
|
||||||
int EmptyElement = -1;
|
if (position >= _maxCount || position < 0) return -1;
|
||||||
if (position >= Count || position < 0) return -1;
|
_places.Insert(position, warship);
|
||||||
|
|
||||||
if (_places[position] == null)
|
|
||||||
{
|
|
||||||
_places[position] = warship;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (_places[position] != null)
|
|
||||||
{
|
|
||||||
for (int i = position + 1; i < Count; i++)
|
|
||||||
if (_places[i] == null)
|
|
||||||
{
|
|
||||||
EmptyElement = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (EmptyElement == -1)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
for (int i = EmptyElement; i > position; i--)
|
|
||||||
_places[i] = _places[i - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
_places[position] = warship;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -81,22 +60,47 @@ namespace AircraftCarrier
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position >= Count || position < 0 || _places[position] == null)
|
if (position >= _maxCount || position < 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
T deleted = _places[position];
|
var result = _places[position];
|
||||||
_places[position] = null;
|
_places.RemoveAt(position);
|
||||||
return deleted;
|
return result;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получение объекта из набора по позиции
|
/// Получение объекта из набора по позиции
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="position"></param>
|
/// <param name="position"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T Get(int position)
|
public T this[int position]
|
||||||
{
|
{
|
||||||
if (position >= _places.Length) return null;
|
get
|
||||||
|
{
|
||||||
|
if (position >= _maxCount || position < 0) return null;
|
||||||
return _places[position];
|
return _places[position];
|
||||||
}
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
Insert(value, position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Проход по набору до первого пустого
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public IEnumerable<T> GetWarships()
|
||||||
|
{
|
||||||
|
foreach(var warship in _places)
|
||||||
|
{
|
||||||
|
if(warship != null)
|
||||||
|
{
|
||||||
|
yield return warship;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user