From e8e0bd932937391180282d6ae85babf63ca4c971 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 4 Oct 2022 23:32:55 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D1=81=D0=BB=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftCarrier/AbstractMap.cs | 21 +++++--- .../AircraftCarrier/FormMapWithSetWarships.cs | 10 ++-- .../AircraftCarrier/FormWarship.cs | 2 +- .../MapWithSetWarshipsGeneric.cs | 22 ++++---- .../AircraftCarrier/SetWarshipsGeneric.cs | 53 +++++++++++-------- 5 files changed, 61 insertions(+), 47 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/AbstractMap.cs b/AircraftCarrier/AircraftCarrier/AbstractMap.cs index 667881f..2193c03 100644 --- a/AircraftCarrier/AircraftCarrier/AbstractMap.cs +++ b/AircraftCarrier/AircraftCarrier/AbstractMap.cs @@ -32,17 +32,22 @@ namespace AircraftCarrier } public Bitmap MoveObject(Direction direction) { - if (true) + if (_drawingObject != null) { - _drawingObject.MoveObject(direction); - } - (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition(); + if (true) + { + _drawingObject.MoveObject(direction); + } + (float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition(); - if (Check(Left, Right, Top, Bottom) != 0) - { - _drawingObject.MoveObject(GetOpositDirection(direction)); + if (Check(Left, Right, Top, Bottom) != 0) + { + _drawingObject.MoveObject(GetOpositDirection(direction)); + } + return DrawMapWithObject(); } - return DrawMapWithObject(); + return null; + } private Direction GetOpositDirection(Direction dir) diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs index 96c5f9d..ffd81ae 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -61,7 +61,7 @@ namespace AircraftCarrier if (form.ShowDialog() == DialogResult.OK) { DrawingObjectWarship warship = new(form.SelectedWarship); - if (_mapWarshipsCollectionGeneric + warship) + if (_mapWarshipsCollectionGeneric + warship >= 0) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); @@ -88,7 +88,7 @@ namespace AircraftCarrier return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapWarshipsCollectionGeneric - pos) + if (_mapWarshipsCollectionGeneric - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); @@ -118,7 +118,11 @@ namespace AircraftCarrier /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - //// + if(_mapWarshipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap(); } /// /// Перемещение diff --git a/AircraftCarrier/AircraftCarrier/FormWarship.cs b/AircraftCarrier/AircraftCarrier/FormWarship.cs index c6d7f9c..28646d5 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarship.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarship.cs @@ -46,7 +46,7 @@ namespace AircraftCarrier { color = dialog.Color; } - _warship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + _warship = new DrawingWarship(rnd.Next(100, 300), rnd.Next(1000, 2000), color); SetData(); Draw(); } diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs index 4caabe2..ed82a7b 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetWarshipsGeneric.cs @@ -26,11 +26,11 @@ namespace AircraftCarrier /// /// Размер занимаемого объектом места (ширина) /// - private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeWidth = 120; /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 90; + private readonly int _placeSizeHeight = 50; /// /// Набор объектов /// @@ -60,7 +60,7 @@ namespace AircraftCarrier /// /// /// - public static bool operator +(MapWithSetWarshipsGeneric map, T warship) + public static int operator +(MapWithSetWarshipsGeneric map, T warship) { return map._setWarships.Insert(warship); } @@ -70,7 +70,7 @@ namespace AircraftCarrier /// /// /// - public static bool operator -(MapWithSetWarshipsGeneric map, int position) + public static T operator -(MapWithSetWarshipsGeneric map, int position) { return map._setWarships.Remove(position); } @@ -149,7 +149,8 @@ namespace AircraftCarrier /// private void DrawBackground(Graphics g) { - Pen pen = new(Color.Black, 3); + Pen pen = new(Color.Black, 2); + g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) @@ -165,15 +166,12 @@ namespace AircraftCarrier /// private void DrawWarships(Graphics g) { + int countInLine = _pictureWidth / _placeSizeWidth; + int maxLeft = (countInLine - 1) * _placeSizeWidth; + //for (int i = 0; i < _setWarships.Count; i++) for (int i = 0; i < _setWarships.Count; i++) { - for (int k = 0; k < _pictureWidth / _placeSizeWidth; k++) - { - for (int l = 0; l < _pictureHeight / _placeSizeHeight + 1; ++l) - { - _setWarships.Get(i)?.SetObject(k * _placeSizeWidth, l * _placeSizeHeight, _pictureWidth, _pictureHeight); - } - } + _setWarships.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); _setWarships.Get(i)?.DrawningObject(g); } } diff --git a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs index 3c3e27f..e2a38df 100644 --- a/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetWarshipsGeneric.cs @@ -34,15 +34,9 @@ namespace AircraftCarrier /// /// Добавляемый военный корабль /// - public bool Insert(T warship) + public int Insert(T warship) { - if(Count >= 5 ) return false; - for(int i = Count - 1; i >= 0; i--) - { - _places[i + 1] = _places[i]; - } - _places[0] = warship; - return true; + return Insert(warship, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,36 +44,49 @@ namespace AircraftCarrier /// Добавляемый военный корабль /// Позиция /// - public bool Insert(T warship, int position) + public int Insert(T warship, int position) { - if(position >= _places.Length) return false; - if (_places[position] != null) + int EmptyElement = -1; + if (position >= Count || position < 0) return -1; + + if (_places[position] == null) { - for(int i = position + 1; i < _places.Length; i++) - { + _places[position] = warship; + return 1; + } + + else if (_places[position] != null) + { + for (int i = position + 1; i < Count; i++) if (_places[i] == null) { - for(int j = i - 1; j >= position; j--) - { - _places[j + 1] = _places[j]; - } + EmptyElement = i; break; - } - } + } + + if (EmptyElement == -1) + return -1; + + for (int i = EmptyElement; i > position; i--) + _places[i] = _places[i - 1]; } + _places[position] = warship; - return true; + return 1; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) + public T Remove(int position) { - if(position >= _places.Length) return false; + if (position >= Count || position < 0 || _places[position] == null) + return null; + + T deleted = _places[position]; _places[position] = null; - return true; + return deleted; } /// /// Получение объекта из набора по позиции