From 47b268ae21918d8beb7b2ee29374d3707e0b5ebb Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Mon, 28 Nov 2022 11:26:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftCarrier/FormMapWithSetWarships.cs | 5 ++-- .../MapWithSetWarshipsGeneric.cs | 29 +++++++++++++++---- .../AircraftCarrier/SetWarshipsGeneric.cs | 18 +++++++----- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs index b06332b..b2a3453 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -177,10 +177,11 @@ namespace AircraftCarrier int pos = Convert.ToInt32(maskedTextBoxPosition.Text); try { - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + var deletedWarship = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos; + if (deletedWarship != null) { MessageBox.Show("Объект удален"); - _logger.LogInformation("Из текущей карты удален объект {@ship}", (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos)); + _logger.LogInformation("Из текущей карты удален объект {@ship}", deletedWarship); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs index e2eb3cd..0fbb68b 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -149,10 +149,10 @@ namespace AircraftCarrier { for (; j > i; j--) { - var car = _setWarships[j]; - if (car != null) + var warship = _setWarships[j]; + if (warship != null) { - _setWarships.Insert(car, i); + _setWarships.Insert(warship, i); _setWarships.Remove(j); break; } @@ -189,14 +189,33 @@ namespace AircraftCarrier /// private void DrawWarships(Graphics g) { - int width = _pictureWidth / _placeSizeWidth; + int countInLine = _pictureWidth / _placeSizeWidth; + int countInColumn = _pictureHeight / _placeSizeHeight; + + int maxLeft = (countInLine - 1) * _placeSizeWidth; + int maxDown = (countInColumn - 1) * _placeSizeHeight; + for (int i = 0; i < _setWarships.Count; i++) + { + var warship = _setWarships[i]; + warship?.SetObject(i % countInLine * _placeSizeWidth, maxDown - i / countInLine * _placeSizeHeight + 7, _pictureWidth, _pictureHeight); + + + //warship?.SetObject(i % countInLine * _placeSizeWidth - 3, i / countInLine * _placeSizeHeight, _pictureWidth, _pictureHeight); + + //warship?.SetObject(i % countInLine * _placeSizeWidth - 3, maxDown - i * countInColumn * _placeSizeHeight, _pictureWidth, _pictureHeight); + //warship?.SetObject(i % _pictureWidth * _placeSizeWidth, (countInColumn - 1 - i / countInLine) * _placeSizeHeight + 5, _pictureWidth, _pictureHeight); + warship?.DrawningObject(g); + } + + + /*int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _setWarships.Count; i++) { var warship = _setWarships[i]; warship?.SetObject(i % _pictureWidth * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight + 4, _pictureWidth, _pictureHeight); warship?.DrawningObject(g); - } + }*/ } } } diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs index 538aad5..b896fc5 100644 --- a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs @@ -52,10 +52,16 @@ namespace AircraftCarrier if (Count == _maxCount) throw new StorageOverflowException(_maxCount); - if (position >= _maxCount || position < 0) return -1; + if (!isCorrectPosition(position)) return -1; _places.Insert(position, warship); return 1; } + + private bool isCorrectPosition(int position) + { + return 0 <= position && position < _maxCount; + } + /// /// Удаление объекта из набора с конкретной позиции /// @@ -63,11 +69,10 @@ namespace AircraftCarrier /// public T Remove(int position) { - if (position >= _maxCount || position < 0) + if (!isCorrectPosition(position)) return null; - - var result = _places[position]; - if(result == null) + var result = this[position]; + if (result == null) throw new WarshipNotFoundException(position); _places.RemoveAt(position); return result; @@ -81,8 +86,7 @@ namespace AircraftCarrier { get { - if (position >= _maxCount || position < 0) return null; - return _places[position]; + return isCorrectPosition(position) && position < Count ? _places[position] : null; } set {