From a678395c8b08eb3eb1c700a423bcc27ac61a488a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B9=D0=B4=D0=B0=D1=80?= Date: Sun, 30 Oct 2022 15:40:58 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=20=D0=B8=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20bool.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/FormMapWithSetBattleship.cs | 4 +- .../Battleship/MapWithSetBattleshipGeneric.cs | 8 ++- Battleship/Battleship/SetBattleshipGeneric.cs | 52 +++++++++---------- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Battleship/Battleship/FormMapWithSetBattleship.cs b/Battleship/Battleship/FormMapWithSetBattleship.cs index 1e90bfe..d569a75 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.cs +++ b/Battleship/Battleship/FormMapWithSetBattleship.cs @@ -69,7 +69,7 @@ namespace Battleship if (form.ShowDialog() == DialogResult.OK) { DrawningObjectBattleship battleship = new(form.SelectedBattleship); - if (_mapBattleshipCollectionGeneric + battleship) + if (_mapBattleshipCollectionGeneric + battleship != null ) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); @@ -96,7 +96,7 @@ namespace Battleship return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapBattleshipCollectionGeneric - pos) + if (_mapBattleshipCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs index f4d2163..a458549 100644 --- a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -31,13 +31,11 @@ namespace Battleship _pictureHeight = picHeight; _map = map; } - - public static bool operator +(MapWithSetBattleshipGeneric map, T warship) + public static int operator +(MapWithSetBattleshipGeneric map, T battleship) { - return map._setBattleship.Insert(warship); + return map._setBattleship.Insert(battleship); } - - public static bool operator -(MapWithSetBattleshipGeneric map, int position) + public static T operator -(MapWithSetBattleshipGeneric map, int position) { return map._setBattleship.Remove(position); } diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs index 9733c69..815ba49 100644 --- a/Battleship/Battleship/SetBattleshipGeneric.cs +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -22,53 +22,49 @@ namespace Battleship _places = new T[count]; } - public bool Insert(T battleship) + public int Insert(T battleship) { return Insert(battleship, 0); } - public bool Insert(T battleship, int position) + public int Insert(T battleship, int position) { - int EmptyEl = -1; - - if (position >= Count || position < 0) - return false; - - if (_places[position] == null) + if (position >= _places.Length) { - _places[position] = battleship; - return true; + return -1; } - - else if (_places[position] != null) + if (_places[position] != null) { - for (int i = position + 1; i < Count; i++) + int indexNull = -1; + for (int i = position; i < _places.Length; i++) + { if (_places[i] == null) { - EmptyEl = i; + indexNull = i; break; } - - if (EmptyEl == -1) - return false; - - for (int i = EmptyEl; i > position; i--) + } + if (indexNull == -1) return -1; + for (int i = indexNull; i > position; i--) + { + T tmp = _places[i]; _places[i] = _places[i - 1]; + _places[i - 1] = tmp; + } } - _places[position] = battleship; - return true; + return position; } - - public bool Remove(int position) + public T Remove(int position) { - if (position >= Count || position < 0 || _places[position] == null) - return false; - + if (position >= _places.Length) + { + return null; + } + T removedObject = _places[position]; _places[position] = null; - return true; + return removedObject; } - public T Get(int position) { if (position >= Count || position < 0)