Этап 3. Форма и замена bool.

This commit is contained in:
Nap 2022-10-30 15:40:58 +04:00
parent bed8d7d922
commit a678395c8b
3 changed files with 29 additions and 35 deletions

View File

@ -69,7 +69,7 @@ namespace Battleship
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawningObjectBattleship battleship = new(form.SelectedBattleship); DrawningObjectBattleship battleship = new(form.SelectedBattleship);
if (_mapBattleshipCollectionGeneric + battleship) if (_mapBattleshipCollectionGeneric + battleship != null )
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet();
@ -96,7 +96,7 @@ namespace Battleship
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapBattleshipCollectionGeneric - pos) if (_mapBattleshipCollectionGeneric - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet();

View File

@ -31,13 +31,11 @@ namespace Battleship
_pictureHeight = picHeight; _pictureHeight = picHeight;
_map = map; _map = map;
} }
public static int operator +(MapWithSetBattleshipGeneric<T, U> map, T battleship)
public static bool operator +(MapWithSetBattleshipGeneric<T, U> map, T warship)
{ {
return map._setBattleship.Insert(warship); return map._setBattleship.Insert(battleship);
} }
public static T operator -(MapWithSetBattleshipGeneric<T, U> map, int position)
public static bool operator -(MapWithSetBattleshipGeneric<T, U> map, int position)
{ {
return map._setBattleship.Remove(position); return map._setBattleship.Remove(position);
} }

View File

@ -22,53 +22,49 @@ namespace Battleship
_places = new T[count]; _places = new T[count];
} }
public bool Insert(T battleship) public int Insert(T battleship)
{ {
return Insert(battleship, 0); return Insert(battleship, 0);
} }
public bool Insert(T battleship, int position) public int Insert(T battleship, int position)
{ {
int EmptyEl = -1; if (position >= _places.Length)
if (position >= Count || position < 0)
return false;
if (_places[position] == null)
{ {
_places[position] = battleship; return -1;
return true;
} }
if (_places[position] != null)
else if (_places[position] != null) {
int indexNull = -1;
for (int i = position; i < _places.Length; i++)
{ {
for (int i = position + 1; i < Count; i++)
if (_places[i] == null) if (_places[i] == null)
{ {
EmptyEl = i; indexNull = i;
break; break;
} }
if (EmptyEl == -1)
return false;
for (int i = EmptyEl; i > position; i--)
_places[i] = _places[i - 1];
} }
if (indexNull == -1) return -1;
_places[position] = battleship; for (int i = indexNull; i > position; i--)
return true;
}
public bool Remove(int position)
{ {
if (position >= Count || position < 0 || _places[position] == null) T tmp = _places[i];
return false; _places[i] = _places[i - 1];
_places[i - 1] = tmp;
_places[position] = null; }
return true; }
_places[position] = battleship;
return position;
}
public T Remove(int position)
{
if (position >= _places.Length)
{
return null;
}
T removedObject = _places[position];
_places[position] = null;
return removedObject;
} }
public T Get(int position) public T Get(int position)
{ {
if (position >= Count || position < 0) if (position >= Count || position < 0)