From 9c7884da80c81fce6ca04cd9783a201d609baa39 Mon Sep 17 00:00:00 2001 From: DeerElk Date: Tue, 13 Feb 2024 02:15:07 +0400 Subject: [PATCH] lab4 --- .../FormWarshipCollection.Designer.cs | 116 +++++++++++---- .../AircraftCarrier/FormWarshipCollection.cs | 132 +++++++++++++++--- AircraftCarrier/AircraftCarrier/SetGeneric.cs | 85 ++++++----- .../WarshipsGenericCollection.cs | 44 +++--- .../AircraftCarrier/WarshipsGenericStorage.cs | 84 +++++++++++ 5 files changed, 364 insertions(+), 97 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs index 5c53066..27d0e98 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.Designer.cs @@ -28,35 +28,92 @@ /// private void InitializeComponent() { - groupBoxCollection = new GroupBox(); + groupBoxCollectionTools = new GroupBox(); + groupBoxCollectionSets = new GroupBox(); + textBoxStorageName = new TextBox(); + buttonDelObject = new Button(); + buttonAddObject = new Button(); + listBoxStorages = new ListBox(); maskedTextBoxNumber = new MaskedTextBox(); buttonRefreshCollection = new Button(); buttonRemoveWarship = new Button(); buttonAddWarship = new Button(); pictureBoxCollection = new PictureBox(); - groupBoxCollection.SuspendLayout(); + groupBoxCollectionTools.SuspendLayout(); + groupBoxCollectionSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // - // groupBoxCollection + // groupBoxCollectionTools // - groupBoxCollection.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; - groupBoxCollection.Controls.Add(maskedTextBoxNumber); - groupBoxCollection.Controls.Add(buttonRefreshCollection); - groupBoxCollection.Controls.Add(buttonRemoveWarship); - groupBoxCollection.Controls.Add(buttonAddWarship); - groupBoxCollection.Location = new Point(893, 3); - groupBoxCollection.Margin = new Padding(4, 5, 4, 5); - groupBoxCollection.Name = "groupBoxCollection"; - groupBoxCollection.Padding = new Padding(4, 5, 4, 5); - groupBoxCollection.Size = new Size(247, 743); - groupBoxCollection.TabIndex = 0; - groupBoxCollection.TabStop = false; - groupBoxCollection.Text = "Tools"; + groupBoxCollectionTools.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right; + groupBoxCollectionTools.Controls.Add(groupBoxCollectionSets); + groupBoxCollectionTools.Controls.Add(maskedTextBoxNumber); + groupBoxCollectionTools.Controls.Add(buttonRefreshCollection); + groupBoxCollectionTools.Controls.Add(buttonRemoveWarship); + groupBoxCollectionTools.Controls.Add(buttonAddWarship); + groupBoxCollectionTools.Location = new Point(893, 3); + groupBoxCollectionTools.Margin = new Padding(4, 5, 4, 5); + groupBoxCollectionTools.Name = "groupBoxCollectionTools"; + groupBoxCollectionTools.Padding = new Padding(4, 5, 4, 5); + groupBoxCollectionTools.Size = new Size(247, 743); + groupBoxCollectionTools.TabIndex = 0; + groupBoxCollectionTools.TabStop = false; + groupBoxCollectionTools.Text = "Tools"; + // + // groupBoxCollectionSets + // + groupBoxCollectionSets.Controls.Add(textBoxStorageName); + groupBoxCollectionSets.Controls.Add(buttonDelObject); + groupBoxCollectionSets.Controls.Add(buttonAddObject); + groupBoxCollectionSets.Controls.Add(listBoxStorages); + groupBoxCollectionSets.Location = new Point(9, 32); + groupBoxCollectionSets.Name = "groupBoxCollectionSets"; + groupBoxCollectionSets.Size = new Size(230, 323); + groupBoxCollectionSets.TabIndex = 5; + groupBoxCollectionSets.TabStop = false; + groupBoxCollectionSets.Text = "Sets"; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(6, 30); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(218, 31); + textBoxStorageName.TabIndex = 4; + // + // buttonDelObject + // + buttonDelObject.Location = new Point(6, 276); + buttonDelObject.Name = "buttonDelObject"; + buttonDelObject.Size = new Size(218, 41); + buttonDelObject.TabIndex = 3; + buttonDelObject.Text = "Delete Set"; + buttonDelObject.UseVisualStyleBackColor = true; + buttonDelObject.Click += ButtonDelObject_Click; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(6, 67); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(218, 43); + buttonAddObject.TabIndex = 2; + buttonAddObject.Text = "Add set"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += ButtonAddObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 25; + listBoxStorages.Location = new Point(6, 116); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(218, 154); + listBoxStorages.TabIndex = 1; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(56, 115); + maskedTextBoxNumber.Location = new Point(56, 468); maskedTextBoxNumber.Margin = new Padding(4, 5, 4, 5); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(141, 31); @@ -65,10 +122,10 @@ // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(9, 300); + buttonRefreshCollection.Location = new Point(9, 688); buttonRefreshCollection.Margin = new Padding(4, 5, 4, 5); buttonRefreshCollection.Name = "buttonRefreshCollection"; - buttonRefreshCollection.Size = new Size(224, 45); + buttonRefreshCollection.Size = new Size(230, 45); buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.Text = "Refresh collection"; buttonRefreshCollection.UseVisualStyleBackColor = true; @@ -76,10 +133,10 @@ // // buttonRemoveWarship // - buttonRemoveWarship.Location = new Point(9, 187); + buttonRemoveWarship.Location = new Point(9, 509); buttonRemoveWarship.Margin = new Padding(4, 5, 4, 5); buttonRemoveWarship.Name = "buttonRemoveWarship"; - buttonRemoveWarship.Size = new Size(224, 45); + buttonRemoveWarship.Size = new Size(230, 45); buttonRemoveWarship.TabIndex = 2; buttonRemoveWarship.Text = "Remove warship"; buttonRemoveWarship.UseVisualStyleBackColor = true; @@ -87,7 +144,7 @@ // // buttonAddWarship // - buttonAddWarship.Location = new Point(9, 37); + buttonAddWarship.Location = new Point(9, 413); buttonAddWarship.Margin = new Padding(4, 5, 4, 5); buttonAddWarship.Name = "buttonAddWarship"; buttonAddWarship.Size = new Size(230, 45); @@ -111,23 +168,30 @@ AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1143, 750); Controls.Add(pictureBoxCollection); - Controls.Add(groupBoxCollection); + Controls.Add(groupBoxCollectionTools); Margin = new Padding(4, 5, 4, 5); Name = "FormWarshipCollection"; Text = "Warship Collection"; - groupBoxCollection.ResumeLayout(false); - groupBoxCollection.PerformLayout(); + groupBoxCollectionTools.ResumeLayout(false); + groupBoxCollectionTools.PerformLayout(); + groupBoxCollectionSets.ResumeLayout(false); + groupBoxCollectionSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } #endregion - private GroupBox groupBoxCollection; + private GroupBox groupBoxCollectionTools; private Button buttonRefreshCollection; private Button buttonRemoveWarship; private Button buttonAddWarship; private PictureBox pictureBoxCollection; private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBoxCollectionSets; + private ListBox listBoxStorages; + private Button buttonDelObject; + private Button buttonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs index c3922d4..7b4201e 100644 --- a/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs +++ b/AircraftCarrier/AircraftCarrier/FormWarshipCollection.cs @@ -11,36 +11,115 @@ namespace ProjectAircraftCarrier /// /// Набор объектов /// - private readonly WarshipsGenericCollection _warships; + private readonly WarshipsGenericStorage _storage; /// /// Конструктор /// public FormWarshipCollection() { InitializeComponent(); - _warships = new WarshipsGenericCollection(pictureBoxCollection.Width, + _storage = new WarshipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(_storage.Keys[i]); + } + if (listBoxStorages.Items.Count > 0 && (index == -1 || index + >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + else if (listBoxStorages.Items.Count > 0 && index > -1 && + index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + } + } + /// + /// Добавление набора в коллекцию + /// + /// + /// + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Not all data is filled in", "Error", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? + string.Empty]?.ShowWarships(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Delete an object" + + $"{ listBoxStorages.SelectedItem}?", + "Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) + == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } + } + /// /// Добавление объекта в набор /// /// /// private void ButtonAddWarship_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormAircraftCarrier form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_warships + form.SelectedWarship != -1) + if (obj + form.SelectedWarship) { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _warships.ShowWarships(); + MessageBox.Show("Object added"); + pictureBoxCollection.Image = obj.ShowWarships(); } else { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show("Failed to add an object"); } } } @@ -51,20 +130,31 @@ namespace ProjectAircraftCarrier /// private void ButtonRemoveWarship_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Delete an object?", "Deletion", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == + DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_warships - pos) + if (obj - pos != null) { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _warships.ShowWarships(); + MessageBox.Show("Object deleted"); + pictureBoxCollection.Image = obj.ShowWarships(); } else { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show("Failed to delete an object"); } } /// @@ -74,7 +164,17 @@ namespace ProjectAircraftCarrier /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _warships.ShowWarships(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowWarships(); } } -} \ No newline at end of file +} diff --git a/AircraftCarrier/AircraftCarrier/SetGeneric.cs b/AircraftCarrier/AircraftCarrier/SetGeneric.cs index ff8423d..4bb19ed 100644 --- a/AircraftCarrier/AircraftCarrier/SetGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetGeneric.cs @@ -1,4 +1,6 @@ -namespace ProjectAircraftCarrier.Generics +using System.Numerics; + +namespace ProjectAircraftCarrier.Generics { /// /// Параметризованный набор объектов @@ -10,29 +12,35 @@ /// /// Массив объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// /// Добавляемый автомобиль /// - public int Insert(T warship) + public bool Insert(T warship) { - if (_places[Count - 1] != null) - return -1; - return Insert(warship, 0); + if (_places.Count == _maxCount) + return false; + Insert(warship, 0); + return true; } /// /// Добавление объекта в набор на конкретную позицию @@ -40,27 +48,13 @@ /// Добавляемый автомобиль /// Позиция /// - public int Insert(T warship, int position) + public bool Insert(T warship, int position) { - int nullIndex = -1, i; - if (position < 0 || position >= Count) - return -1; - for (i = position; i < Count; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - if (nullIndex < 0) - return -1; - for (i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = warship; - return position; + if (!(position >= 0 && position <= Count && _places.Count < + _maxCount)) + return false; + _places.Insert(position, warship); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -69,8 +63,9 @@ /// public bool Remove(int position) { - if (position < 0 || position >= Count) return false; - _places[position] = null; + if (!(position >= 0 && position < Count)) + return false; + _places.RemoveAt(position); return true; } /// @@ -78,11 +73,33 @@ /// /// /// - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= Count) - return null; - return _places[position]; + get + { + if (!(position >= 0 && position <= Count)) + return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position <= Count)) + return; + _places.Insert(position, value); + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetWarships(int? maxWarships = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxWarships.HasValue && i == maxWarships.Value) + yield break; + } } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs index 775822b..42a736e 100644 --- a/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs +++ b/AircraftCarrier/AircraftCarrier/WarshipsGenericCollection.cs @@ -51,12 +51,11 @@ namespace ProjectAircraftCarrier.Generics /// /// /// - public static int operator +(WarshipsGenericCollection collect, T? obj) + public static bool operator +(WarshipsGenericCollection? + collect, T? obj) { - if (obj == null) - { - return -1; - } + if (obj == null || collect == null) + return false; return collect._collection.Insert(obj); } /// @@ -65,14 +64,15 @@ namespace ProjectAircraftCarrier.Generics /// /// /// - public static bool operator -(WarshipsGenericCollection collect, int pos) + public static T? operator -(WarshipsGenericCollection + collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - return false; + return obj; } /// /// Получение объекта IMoveableObject @@ -81,7 +81,7 @@ namespace ProjectAircraftCarrier.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -104,15 +104,14 @@ namespace ProjectAircraftCarrier.Generics Pen pen = new(Color.Black, 3); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + - 1; ++j) + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { g.DrawLine(pen, i * _placeSizeWidth, j * - _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * - _placeSizeHeight); + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth + / 2, j * _placeSizeHeight); } - g.DrawLine(pen, i * _placeSizeWidth, 0, i * - _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, + _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } /// @@ -121,16 +120,19 @@ namespace ProjectAircraftCarrier.Generics /// private void DrawObjects(Graphics g) { - int inRow = _pictureWidth / _placeSizeWidth; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var warship in _collection.GetWarships()) { - DrawingWarship warship = _collection.Get(i); if (warship != null) { - warship.SetPosition(i % inRow * _placeSizeWidth + 3, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5); + int inRow = _pictureWidth / _placeSizeWidth; + warship.SetPosition(i % inRow * _placeSizeWidth, + _pictureHeight - _pictureHeight % _placeSizeHeight + - (i / inRow + 1) * _placeSizeHeight); warship.DrawTransport(g); } + i++; } } } -} +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs b/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs new file mode 100644 index 0000000..d5c9e6d --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/WarshipsGenericStorage.cs @@ -0,0 +1,84 @@ +using ProjectAircraftCarrier.DrawingObjects; +using ProjectAircraftCarrier.MovementStrategy; +namespace ProjectAircraftCarrier.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class WarshipsGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _warshipStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _warshipStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public WarshipsGenericStorage(int pictureWidth, int pictureHeight) + { + _warshipStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (!_warshipStorages.ContainsKey(name)) + { + _warshipStorages.Add(name, new WarshipsGenericCollection + (_pictureWidth, + _pictureHeight)); + } + else + { + MessageBox.Show("This set already exists"); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_warshipStorages.ContainsKey(name)) + return; + _warshipStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public WarshipsGenericCollection + ?this[string ind] + { + get + { + if (_warshipStorages.ContainsKey(ind)) + return _warshipStorages[ind]; + return null; + } + } + } +} +