Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laba 4 Sokolova #5

Open
wants to merge 1 commit into
base: laba3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions Dock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class Dock<T> where T : class, IShip
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private T[] _places;
private Dictionary<int, T> _places;
/// <summary>
/// Максимальное количество мест на парковке
/// </summary>
private int _maxCount;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
Expand All @@ -34,72 +38,72 @@ class Dock<T> where T : class, IShip
/// </summary>
private int _placeSizeHeight = 80;
/// <summary>
/// <summary>
/// Конструктор
/// </summary>
/// <param name="sizes">Количество мест в доке</param>
/// <param name="sizes">Количество мест в доке</param>
/// <param name="pictureWidth">Рамзер дока - ширина</param>
/// <param name="pictureHeight">Рамзер дока - высота</param>
public Dock(int sizes, int pictureWidth, int pictureHeight)
{
_places = new T[sizes];
_maxCount = sizes;
_places = new Dictionary<int, T>();
PictureWidth = pictureWidth;
PictureHeight = pictureHeight;
for (int i = 0; i < _places.Length; i++)
{
_places[i] = null;
}
}

/// <summary>
/// Перегрузка оператора сложения
/// Логика действия: в док добавляется корабль
/// </summary>
/// <param name="p">Док</param>
/// <param name="car">Добавляемый корабль</param>
/// <param name="ship">Добавляемый корабль</param>
/// <returns></returns>
public static int operator +(Dock<T> p, T car)
public static int operator +(Dock<T> p, T ship)
{
for (int i = 0; i < p._places.Length; i++)
if (p._places.Count == p._maxCount)
{
return -1;
}
for (int i = 0; i < p._maxCount; i++)
{
if (p.CheckFreePlace(i))
{
p._places[i] = car;
p._places.Add(i, ship);
p._places[i].SetPosition(5 + i / 5 * p._placeSizeWidth + 5,
i % 5 * p._placeSizeHeight + 53, p.PictureWidth,
(i % 5 * p._placeSizeHeight + 15) + 50, p.PictureWidth,
p.PictureHeight);
return i;
}
}
return -1;
}

/// <summary>
/// Перегрузка оператора вычитания
/// Логика действия: Из дока забираем корабль
/// Логика действия: из дока забираем корабль
/// </summary>
/// <param name="p">Док</param>
/// <param name="index">Индекс места, с которого пытаемся извлечь объект</param>
/// <returns></returns>
public static T operator -(Dock<T> p, int index)
{
if (index < 0 || index > p._places.Length)
{
return null;
}
if (!p.CheckFreePlace(index))
{
T ship = p._places[index];
p._places[index] = null;
return ship;
T car = p._places[index];
p._places.Remove(index);
return car;
}
return null;
}
/// <summary>
/// Метод проверки заполнености парковочного места (ячейки массива)
/// </summary>
/// <param name="index">Номер парковочного места (порядковый номер в массиве)</param>
/// <param name="index">Номер парковочного места (порядковый номер в массиве)</param>
/// <returns></returns>
private bool CheckFreePlace(int index)
{
return _places[index] == null;
return !_places.ContainsKey(index);
}
/// <summary>
/// Метод отрисовки дока
Expand All @@ -108,13 +112,11 @@ private bool CheckFreePlace(int index)
public void Draw(Graphics g)
{
DrawMarking(g);
for (int i = 0; i < _places.Length; i++)
var keys = _places.Keys.ToList();
for (int i = 0; i < keys.Count; i++)
{
if (!CheckFreePlace(i))
{//если место не пустое
_places[i].DrawShip(g);
}
}
_places[keys[i]].DrawShip(g);
}
}
/// <summary>
/// Метод отрисовки разметки парковочных мест
Expand All @@ -124,8 +126,8 @@ private void DrawMarking(Graphics g)
{
Pen pen = new Pen(Color.Black, 3);
//границы праковки
g.DrawRectangle(pen, 0, 0, (_places.Length / 5) * _placeSizeWidth + 27, 430);
for (int i = 0; i < _places.Length / 5; i++)
g.DrawRectangle(pen, 0, 0, (_maxCount / 5) * _placeSizeWidth, 480);
for (int i = 0; i < _maxCount / 5; i++)
{//отрисовываем, по 5 мест на линии
for (int j = 0; j < 6; ++j)
{//линия рамзетки места
Expand Down
35 changes: 30 additions & 5 deletions FormDock.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading