diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index 78795d4..ba1ce3f 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -13,7 +13,7 @@ namespace WarmlyShip { public partial class FormMapWithSetShip : Form { - private MapWithSetShipGeneric _mapCarsCollectionGeneric; + private MapWithSetShipGeneric _mapShipsCollectionGeneric; public FormMapWithSetShip() { @@ -31,17 +31,17 @@ namespace WarmlyShip } if (map != null) { - _mapCarsCollectionGeneric = new MapWithSetShipGeneric(pictureBox.Width, pictureBox.Height, map); + _mapShipsCollectionGeneric = new MapWithSetShipGeneric(pictureBox.Width, pictureBox.Height, map); } else { - _mapCarsCollectionGeneric = null; + _mapShipsCollectionGeneric = null; } } private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapShipsCollectionGeneric == null) { return; } @@ -49,10 +49,10 @@ namespace WarmlyShip if (form.ShowDialog() == DialogResult.OK) { DrawningObjectShip ship = new(form.SelectedShip); - if (_mapCarsCollectionGeneric + ship) + if (_mapShipsCollectionGeneric + ship) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); } else { @@ -72,10 +72,10 @@ namespace WarmlyShip return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapCarsCollectionGeneric - pos) + if (_mapShipsCollectionGeneric - pos) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); } else { @@ -85,25 +85,25 @@ namespace WarmlyShip private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapShipsCollectionGeneric == null) { return; } - pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapShipsCollectionGeneric == null) { return; } - pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapCarsCollectionGeneric == null) + if (_mapShipsCollectionGeneric == null) { return; } @@ -124,7 +124,7 @@ namespace WarmlyShip dir = Direction.Right; break; } - pictureBox.Image = _mapCarsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapShipsCollectionGeneric.MoveObject(dir); } } } diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 1fba030..04dd7af 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -99,8 +99,8 @@ namespace WarmlyShip Pen pen = new(Color.Black, 3); 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 * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index e334b1d..86ad35e 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -11,37 +11,52 @@ namespace WarmlyShip { private readonly T[] _places; public int Count => _places.Length; + public SetShipGeneric(int count) { _places = new T[count]; } + public bool Insert(T ship) { - // TODO вставка в начало набора + _places[0] = ship; 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) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции + if (position < 0 || position > _places.Length) return false; + if (_places[position] != null && CanInsert(position)) + { + for (int i = _places.Length - 1; i > position; --i) + { + if (_places[i] == null) + { + _places[i] = _places[i - 1]; + _places[i - 1] = null; + } + } + } _places[position] = ship; return true; } public bool Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null + _places[position] = null; return true; } public T Get(int position) { - // TODO проверка позиции + if (position < 0 || position > _places.Length) return null; return _places[position]; }