This commit is contained in:
Макс Бондаренко 2022-10-23 17:43:37 +04:00
parent a23ecbf81c
commit 8f20040737
3 changed files with 40 additions and 25 deletions

View File

@ -13,7 +13,7 @@ namespace WarmlyShip
{ {
public partial class FormMapWithSetShip : Form public partial class FormMapWithSetShip : Form
{ {
private MapWithSetShipGeneric<DrawningObjectShip, AbstractMap> _mapCarsCollectionGeneric; private MapWithSetShipGeneric<DrawningObjectShip, AbstractMap> _mapShipsCollectionGeneric;
public FormMapWithSetShip() public FormMapWithSetShip()
{ {
@ -31,17 +31,17 @@ namespace WarmlyShip
} }
if (map != null) if (map != null)
{ {
_mapCarsCollectionGeneric = new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(pictureBox.Width, pictureBox.Height, map); _mapShipsCollectionGeneric = new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
} }
else else
{ {
_mapCarsCollectionGeneric = null; _mapShipsCollectionGeneric = null;
} }
} }
private void ButtonAddShip_Click(object sender, EventArgs e) private void ButtonAddShip_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapShipsCollectionGeneric == null)
{ {
return; return;
} }
@ -49,10 +49,10 @@ namespace WarmlyShip
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawningObjectShip ship = new(form.SelectedShip); DrawningObjectShip ship = new(form.SelectedShip);
if (_mapCarsCollectionGeneric + ship) if (_mapShipsCollectionGeneric + ship)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
} }
else else
{ {
@ -72,10 +72,10 @@ namespace WarmlyShip
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapCarsCollectionGeneric - pos) if (_mapShipsCollectionGeneric - pos)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
} }
else else
{ {
@ -85,25 +85,25 @@ namespace WarmlyShip
private void ButtonShowStorage_Click(object sender, EventArgs e) private void ButtonShowStorage_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapShipsCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); pictureBox.Image = _mapShipsCollectionGeneric.ShowSet();
} }
private void ButtonShowOnMap_Click(object sender, EventArgs e) private void ButtonShowOnMap_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapShipsCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap(); pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapShipsCollectionGeneric == null)
{ {
return; return;
} }
@ -124,7 +124,7 @@ namespace WarmlyShip
dir = Direction.Right; dir = Direction.Right;
break; break;
} }
pictureBox.Image = _mapCarsCollectionGeneric.MoveObject(dir); pictureBox.Image = _mapShipsCollectionGeneric.MoveObject(dir);
} }
} }
} }

View File

@ -99,8 +99,8 @@ namespace WarmlyShip
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) //линия рамзетки места
{//линия рамзетки места {
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
} }

View File

@ -11,37 +11,52 @@ namespace WarmlyShip
{ {
private readonly T[] _places; private readonly T[] _places;
public int Count => _places.Length; public int Count => _places.Length;
public SetShipGeneric(int count) public SetShipGeneric(int count)
{ {
_places = new T[count]; _places = new T[count];
} }
public bool Insert(T ship) public bool Insert(T ship)
{ {
// TODO вставка в начало набора _places[0] = ship;
return true; return true;
} }
private bool CanInsert(int position)
{
for (int i = position; i < _places.Length; ++i)
if (_places[i] != null) return true;
return false;
}
public bool Insert(T ship, int position) public bool Insert(T ship, int position)
{ {
// TODO проверка позиции if (position < 0 || position > _places.Length) return false;
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то if (_places[position] != null && CanInsert(position))
// проверка, что после вставляемого элемента в массиве есть пустой элемент {
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента for (int i = _places.Length - 1; i > position; --i)
// TODO вставка по позиции {
if (_places[i] == null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = ship; _places[position] = ship;
return true; return true;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
// TODO проверка позиции _places[position] = null;
// TODO удаление объекта из массива, присовив элементу массива значение null
return true; return true;
} }
public T Get(int position) public T Get(int position)
{ {
// TODO проверка позиции if (position < 0 || position > _places.Length) return null;
return _places[position]; return _places[position];
} }