From 458092c11cfe74f4091d037d2027f0cda926fb4a Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Thu, 7 Mar 2024 16:30:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 16 ++++++----- .../ICollectionGenericObjects.cs | 6 ++-- .../MassiveGenericObjects.cs | 28 +++++++++---------- .../StormtrooperSharingService.cs | 16 ++++------- .../ProjectStormtrooper/FormStormtrooper.cs | 1 + .../FormStormtrooperCollection.cs | 6 ++-- 6 files changed, 36 insertions(+), 37 deletions(-) diff --git a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs index 722123b..3062187 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/AbstractCompany.cs @@ -25,12 +25,16 @@ public abstract class AbstractCompany /// Высота окна /// protected readonly int _pictureHeight; - + protected static int amountOfObjects = 0; /// /// Коллекция бомбардировщиков /// protected ICollectionGenericObjects? _collection = null; + public static int getAmountOfObjects() { + return amountOfObjects; + } + /// /// Вычисление максимального количества элементов, который можно разместить в окне /// @@ -56,9 +60,9 @@ public abstract class AbstractCompany /// Компания /// Добавляемый объект /// - public static bool operator +(AbstractCompany company, DrawningBaseStormtrooper stormtrooper) + public static int operator +(AbstractCompany company, DrawningBaseStormtrooper stormtrooper) { - return company._collection?.Insert(stormtrooper) ?? false; + return company._collection.Insert(stormtrooper); } /// @@ -67,9 +71,9 @@ public abstract class AbstractCompany /// Компания /// Номер удаляемого объекта /// - public static bool operator -(AbstractCompany company, int position) + public static DrawningBaseStormtrooper operator -(AbstractCompany company, int position) { - return company._collection?.Remove(position) ?? false; + return company._collection.Remove(position); } /// @@ -91,14 +95,12 @@ public abstract class AbstractCompany Bitmap bitmap = new(_pictureWidth, _pictureHeight); Graphics graphics = Graphics.FromImage(bitmap); DrawBackgound(graphics); - SetObjectsPosition(); for (int i = 0; i < (_collection?.Count ?? 0); ++i) { DrawningBaseStormtrooper? obj = _collection?.Get(i); obj?.DrawTransport(graphics); } - return bitmap; } diff --git a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs index a5e5039..372b2ce 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -22,7 +22,7 @@ public interface ICollectionGenericObjects /// /// Добавляемый объект /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj); + int Insert(T obj); /// /// Добавление объекта в коллекцию на конкретную позицию @@ -30,14 +30,14 @@ public interface ICollectionGenericObjects /// Добавляемый объект /// Позиция /// true - вставка прошла удачно, false - вставка не удалась - bool Insert(T obj, int position); + int Insert(T obj, int position); /// /// Удаление объекта из коллекции с конкретной позиции /// /// Позиция /// true - удаление прошло удачно, false - удаление не удалось - bool Remove(int position); + T Remove(int position); /// /// Получение объекта по позиции diff --git a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs index b21e76a..85dc44d 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/MassiveGenericObjects.cs @@ -1,4 +1,4 @@ - +using ProjectStormtrooper.Drawnings; namespace ProjectStormtrooper.CollectionGenericObjects; /// @@ -32,7 +32,6 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } } - /// /// Конструктор /// @@ -48,7 +47,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects return _collection[position]; } - public bool Insert(T obj) + public int Insert(T obj) { // TODO вставка в свободное место набора int index = 0; @@ -56,25 +55,25 @@ public class MassiveGenericObjects : ICollectionGenericObjects { if (_collection[index] == null) { _collection[index] = obj; - return true; + return index; } index++; } - return false; + return -1; } - public bool Insert(T obj, int position) + public int Insert(T obj, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, если нет, то // ищется свободное место после этой позиции и идет вставка туда // если нет после, ищем до // TODO вставка - if (position >= _collection.Length || position < 0) return false; + if (position >= _collection.Length || position < 0) return -1; if (_collection[position] == null) { _collection[position] = obj; - return true; + return position; } int index = position + 1; while (index < _collection.Length) @@ -82,7 +81,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[index] = obj; - return true; + return index; } index++; } @@ -92,19 +91,20 @@ public class MassiveGenericObjects : ICollectionGenericObjects if (_collection[index] == null) { _collection[index] = obj; - return true; + return index; } index--; } - return false; + return -1; } - public bool Remove(int position) + public T? Remove(int position) { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null - if (position >= _collection.Length || position < 0) return false; + if (position >= _collection.Length || position < 0) return null; + T temp = _collection[position]; _collection[position] = null; - return true; + return temp; } } \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/StormtrooperSharingService.cs b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/StormtrooperSharingService.cs index fb320c9..e8143f5 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/StormtrooperSharingService.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/CollectionGenericObjects/StormtrooperSharingService.cs @@ -14,8 +14,6 @@ public class StormtrooperSharingService : AbstractCompany public StormtrooperSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) { } - - protected override void DrawBackgound(Graphics g) { int width = _pictureWidth / _placeSizeWidth; @@ -35,23 +33,20 @@ public class StormtrooperSharingService : AbstractCompany { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - - int curWidth = 0; + int curWidth = width-1; int curHeight = 0; - for (int i = 0; i < (_collection?.Count ?? 0); i++) { if (_collection.Get(i) != null) { _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); - _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 20, curHeight * _placeSizeHeight + 4); + _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 15, curHeight * _placeSizeHeight + 3); } - - if (curWidth < width-1) - curWidth++; + if (curWidth >0) + curWidth--; else { - curWidth = 0; + curWidth = width -1; curHeight++; } if (curHeight > height) @@ -59,6 +54,5 @@ public class StormtrooperSharingService : AbstractCompany return; } } - } } diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs index 5ad37d2..6c7306a 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs @@ -86,6 +86,7 @@ namespace ProjectStormtrooper Draw(); } } + private void ButtonStrategyStep_Click(object sender, EventArgs e) { diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooperCollection.cs b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooperCollection.cs index 805ef99..5be0f14 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooperCollection.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooperCollection.cs @@ -65,7 +65,8 @@ public partial class FormStormtrooperCollection : Form default: return; } - if (_company + drawningBaseStormtrooper) + int tempSize = StormtrooperSharingService.getAmountOfObjects(); + if (_company + drawningBaseStormtrooper != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); @@ -119,7 +120,8 @@ public partial class FormStormtrooperCollection : Form } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_company - pos) + int tempSize = StormtrooperSharingService.getAmountOfObjects(); + if (_company-pos!=null) { MessageBox.Show("Объект удалён"); pictureBox.Image = _company.Show();