лаб - 4
This commit is contained in:
parent
57a167173d
commit
cfc4fe61b7
@ -90,7 +90,7 @@ namespace AircraftCarrier
|
||||
Shaking();
|
||||
for (int i = 0; i < _setAircraftCarriers.Count; i++)
|
||||
{
|
||||
var aircraftcarrier = _setAircraftCarriers.Get(i);
|
||||
var aircraftcarrier = _setAircraftCarriers[i];
|
||||
if (aircraftcarrier != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, aircraftcarrier);
|
||||
@ -119,11 +119,11 @@ namespace AircraftCarrier
|
||||
int j = _setAircraftCarriers.Count - 1;
|
||||
for (int i = 0; i < _setAircraftCarriers.Count; i++)
|
||||
{
|
||||
if (_setAircraftCarriers.Get(i) == null)
|
||||
if (_setAircraftCarriers[i] == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var aircraftcarrier = _setAircraftCarriers.Get(j);
|
||||
var aircraftcarrier = _setAircraftCarriers[j];
|
||||
if (aircraftcarrier != null)
|
||||
{
|
||||
_setAircraftCarriers.Insert(aircraftcarrier, i);
|
||||
@ -148,7 +148,7 @@ namespace AircraftCarrier
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{//линия рамзетки места
|
||||
{//линия разметки места
|
||||
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
|
||||
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
}
|
||||
@ -166,14 +166,13 @@ namespace AircraftCarrier
|
||||
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
|
||||
int rowNum = yNumOfPlaces - 1;
|
||||
int columnNum = 0;
|
||||
|
||||
for (int i = 0; i < _setAircraftCarriers.Count; i++)
|
||||
{
|
||||
if (_setAircraftCarriers.Get(i) != null)
|
||||
if (_setAircraftCarriers[i] != null)
|
||||
{
|
||||
(float Left, float Top, float Right, float Bottom) = _setAircraftCarriers.Get(i).GetCurrentPosition();
|
||||
_setAircraftCarriers.Get(i).SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight);
|
||||
_setAircraftCarriers.Get(i).DrawningObject(g);
|
||||
(float Left, float Top, float Right, float Bottom) = _setAircraftCarriers[i].GetCurrentPosition();
|
||||
_setAircraftCarriers[i].SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight);
|
||||
_setAircraftCarriers[i].DrawningObject(g);
|
||||
}
|
||||
if (columnNum == xNumOfPlaces - 1)
|
||||
{
|
||||
|
76
AircraftCarrier/AircraftCarrier/MapsCollection.cs
Normal file
76
AircraftCarrier/AircraftCarrier/MapsCollection.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AircraftCarrier
|
||||
{
|
||||
internal class MapsCollection
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с картами
|
||||
/// </summary>
|
||||
readonly Dictionary<string, MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier,
|
||||
AbstractMap>> _mapStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий карт
|
||||
/// </summary>
|
||||
public List<string> Keys => _mapStorages.Keys.ToList();
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
private readonly int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна отрисовки
|
||||
/// </summary>
|
||||
private readonly int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
/// <param name="pictureHeight"></param>
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new Dictionary<string,
|
||||
MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление карты
|
||||
/// </summary>
|
||||
/// <param name="name">Название карты</param>
|
||||
/// <param name="map">Карта</param>
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
// TODO Прописать логику для добавления
|
||||
if (Keys.Contains(name))
|
||||
return;
|
||||
_mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление карты
|
||||
/// </summary>
|
||||
/// <param name="name">Название карты</param>
|
||||
public void DelMap(string name)
|
||||
{
|
||||
// TODO Прописать логику для удаления
|
||||
_mapStorages.Remove(name);
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к парковке
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public MapWithSetAircraftCarriersGeneric<DrawningObjectAircraftCarrier, AbstractMap> this[string
|
||||
ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
_mapStorages.TryGetValue(ind, out var mapWithSetAircraftCarriersGeneric);
|
||||
return mapWithSetAircraftCarriersGeneric;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -9,18 +9,20 @@ namespace AircraftCarrier
|
||||
internal class SetAircraftCarriersGeneric<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly T[] _places;
|
||||
private readonly List<T> _places;
|
||||
/// <summary>
|
||||
/// Количество объектов в массиве
|
||||
/// Количество объектов в списке
|
||||
/// </summary>
|
||||
public int Count => _places.Length;
|
||||
public int Count => _places.Count;
|
||||
private readonly int _maxCount;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public SetAircraftCarriersGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -29,8 +31,13 @@ namespace AircraftCarrier
|
||||
/// <returns></returns>
|
||||
public int Insert(T AircraftCarrier)
|
||||
{
|
||||
// TODO вставка в начало набора
|
||||
return Insert(AircraftCarrier, 0);
|
||||
}
|
||||
private bool CorrectPos(int pos)
|
||||
{
|
||||
return 0 <= pos && pos < _maxCount;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// </summary>
|
||||
@ -39,37 +46,15 @@ namespace AircraftCarrier
|
||||
/// <returns></returns>
|
||||
public int Insert(T AircraftCarrier, int position)
|
||||
{
|
||||
// TODO проверка позиции
|
||||
if (position >= _places.Length || position < 0)
|
||||
// проверка позиции
|
||||
if (!CorrectPos(position))
|
||||
{
|
||||
return -1;
|
||||
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
|
||||
if (_places[position] == null)
|
||||
{
|
||||
_places[position] = AircraftCarrier;
|
||||
}
|
||||
// вставка по позиции
|
||||
_places.Insert(position, AircraftCarrier);
|
||||
return position;
|
||||
}
|
||||
// проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||
int findEmptyPos = -1;
|
||||
|
||||
for (int i = position + 1; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
findEmptyPos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (findEmptyPos < 0) return -1;
|
||||
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
|
||||
for (int i = findEmptyPos; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
// TODO вставка по позиции
|
||||
_places[position] = AircraftCarrier;
|
||||
return position;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
/// </summary>
|
||||
@ -77,11 +62,12 @@ namespace AircraftCarrier
|
||||
/// <returns></returns>
|
||||
public T Remove(int position)
|
||||
{
|
||||
// TODO проверка позиции
|
||||
if (position >= _places.Length || position < 0) return null;
|
||||
// TODO удаление объекта из массива, присовив элементу массива значение null
|
||||
// проверка позиции
|
||||
if (!CorrectPos(position))
|
||||
return null;
|
||||
// удаление объекта из массива, присовив элементу массива значение null
|
||||
T temp = _places[position];
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return temp;
|
||||
}
|
||||
/// <summary>
|
||||
@ -89,11 +75,31 @@ namespace AircraftCarrier
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
if (position >= _places.Length || position < 0)
|
||||
return null;
|
||||
return _places[position];
|
||||
get
|
||||
{
|
||||
return CorrectPos(position) && position < Count ? _places[position] : null;
|
||||
}
|
||||
set
|
||||
{
|
||||
Insert(value, position);
|
||||
}
|
||||
|
||||
}
|
||||
public IEnumerable<T> GetAircraftCarriers()
|
||||
{
|
||||
foreach (var aircraftcarrier in _places)
|
||||
{
|
||||
if (aircraftcarrier != null)
|
||||
{
|
||||
yield return aircraftcarrier;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user