From d97e259186499eefb8bb602390bb47ac86b46e35 Mon Sep 17 00:00:00 2001 From: shoot Date: Sat, 16 Dec 2023 11:14:14 +0400 Subject: [PATCH] PIbd_NikiforovaMV_lab4 --- ContainerShip/DrawingContainerShip.cs | 2 +- ContainerShip/DrawningShip.cs | 2 +- ContainerShip/FormShipCollection.Designer.cs | 130 +++++++++++++------ ContainerShip/FormShipCollection.cs | 98 +++++++++++--- ContainerShip/FormShipCollection.resx | 2 +- ContainerShip/FormShips.cs | 1 - ContainerShip/IMovableObject.cs | 2 - ContainerShip/ObjectParameters.cs | 1 - ContainerShip/SetGeneric.cs | 88 ++++++------- ContainerShip/ShipsGenericCollection.cs | 44 +++---- ContainerShip/ShipsGenericStorage.cs | 57 ++++++++ 11 files changed, 294 insertions(+), 133 deletions(-) create mode 100644 ContainerShip/ShipsGenericStorage.cs diff --git a/ContainerShip/DrawingContainerShip.cs b/ContainerShip/DrawingContainerShip.cs index 68b3098..14e924a 100644 --- a/ContainerShip/DrawingContainerShip.cs +++ b/ContainerShip/DrawingContainerShip.cs @@ -30,7 +30,7 @@ namespace ContainerShip.DrawningObjects SolidBrush(containerShip.AdditionalColor); if (containerShip.Load) { - g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20); + g.FillRectangle(additionalBrush, _startPosX + 100, _startPosY + 32, 30, 20); } base.DrawTransport(g); if (containerShip.Crane) diff --git a/ContainerShip/DrawningShip.cs b/ContainerShip/DrawningShip.cs index 806c072..28bacd0 100644 --- a/ContainerShip/DrawningShip.cs +++ b/ContainerShip/DrawningShip.cs @@ -16,7 +16,7 @@ namespace ContainerShip.DrawningObjects private int _pictureHeight; protected int _startPosX; protected int _startPosY; - protected readonly int _shipWidth = 150; + protected readonly int _shipWidth = 150; protected readonly int _shipHeight = 65; public int GetPosX => _startPosX; public int GetPosY => _startPosY; diff --git a/ContainerShip/FormShipCollection.Designer.cs b/ContainerShip/FormShipCollection.Designer.cs index c95156d..f3532aa 100644 --- a/ContainerShip/FormShipCollection.Designer.cs +++ b/ContainerShip/FormShipCollection.Designer.cs @@ -30,19 +30,23 @@ { buttonAdd = new Button(); buttonDelete = new Button(); - buttonUpdate = new Button(); - label1 = new Label(); + labelTools = new Label(); maskedTextBoxNumber = new TextBox(); pictureBoxCollection = new PictureBox(); + labelSet = new Label(); + textBoxStorageName = new TextBox(); + listBoxStorages = new ListBox(); + buttonAddStorage = new Button(); + buttonDeleteStorage = new Button(); + buttonUpdate = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // buttonAdd // - buttonAdd.Location = new Point(736, 80); - buttonAdd.Margin = new Padding(3, 4, 3, 4); + buttonAdd.Location = new Point(647, 305); buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(165, 32); + buttonAdd.Size = new Size(144, 24); buttonAdd.TabIndex = 0; buttonAdd.Text = "Добавить корабль"; buttonAdd.UseVisualStyleBackColor = true; @@ -50,64 +54,111 @@ // // buttonDelete // - buttonDelete.Location = new Point(738, 217); - buttonDelete.Margin = new Padding(3, 4, 3, 4); + buttonDelete.Location = new Point(645, 362); buttonDelete.Name = "buttonDelete"; - buttonDelete.Size = new Size(163, 32); + buttonDelete.Size = new Size(143, 24); buttonDelete.TabIndex = 1; buttonDelete.Text = "Удалить корабль"; buttonDelete.UseVisualStyleBackColor = true; buttonDelete.Click += buttonDelete_Click; // - // buttonUpdate + // labelTools // - buttonUpdate.Location = new Point(738, 292); - buttonUpdate.Margin = new Padding(3, 4, 3, 4); - buttonUpdate.Name = "buttonUpdate"; - buttonUpdate.Size = new Size(163, 32); - buttonUpdate.TabIndex = 2; - buttonUpdate.Text = "Обновить коллекцию"; - buttonUpdate.UseVisualStyleBackColor = true; - buttonUpdate.Click += buttonUpdate_Click; - // - // label1 - // - label1.AutoSize = true; - label1.Location = new Point(774, 12); - label1.Name = "label1"; - label1.Size = new Size(103, 20); - label1.TabIndex = 3; - label1.Text = "Инструменты"; + labelTools.AutoSize = true; + labelTools.Location = new Point(677, 9); + labelTools.Name = "labelTools"; + labelTools.Size = new Size(83, 15); + labelTools.TabIndex = 3; + labelTools.Text = "Инструменты"; // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(738, 149); - maskedTextBoxNumber.Margin = new Padding(3, 4, 3, 4); + maskedTextBoxNumber.Location = new Point(645, 335); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(162, 27); + maskedTextBoxNumber.Size = new Size(142, 23); maskedTextBoxNumber.TabIndex = 4; // // pictureBoxCollection // pictureBoxCollection.Location = new Point(0, 0); - pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(731, 531); + pictureBoxCollection.Size = new Size(640, 386); pictureBoxCollection.TabIndex = 5; pictureBoxCollection.TabStop = false; // + // labelSet + // + labelSet.AutoSize = true; + labelSet.Location = new Point(654, 31); + labelSet.Name = "labelSet"; + labelSet.Size = new Size(43, 15); + labelSet.TabIndex = 6; + labelSet.Text = "Набор"; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(647, 49); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(142, 23); + textBoxStorageName.TabIndex = 7; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(647, 134); + listBoxStorages.Margin = new Padding(3, 2, 3, 2); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(145, 94); + listBoxStorages.TabIndex = 8; + listBoxStorages.SelectedIndexChanged += buttonUpdate_Click; + // + // buttonAddStorage + // + buttonAddStorage.Location = new Point(647, 75); + buttonAddStorage.Name = "buttonAddStorage"; + buttonAddStorage.Size = new Size(144, 24); + buttonAddStorage.TabIndex = 9; + buttonAddStorage.Text = "Добавить набор"; + buttonAddStorage.UseVisualStyleBackColor = true; + buttonAddStorage.Click += ButtonAddObject_Click; + // + // buttonDeleteStorage + // + buttonDeleteStorage.Location = new Point(647, 242); + buttonDeleteStorage.Name = "buttonDeleteStorage"; + buttonDeleteStorage.Size = new Size(144, 24); + buttonDeleteStorage.TabIndex = 11; + buttonDeleteStorage.Text = "Удалить набор"; + buttonDeleteStorage.UseVisualStyleBackColor = true; + buttonDeleteStorage.Click += ButtonDelObject_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(647, 105); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(143, 24); + buttonUpdate.TabIndex = 2; + buttonUpdate.Text = "Обновить набор"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += buttonUpdate_Click; + // // FormShipCollection // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(914, 529); + ClientSize = new Size(800, 393); + Controls.Add(buttonDeleteStorage); + Controls.Add(buttonAddStorage); + Controls.Add(listBoxStorages); + Controls.Add(textBoxStorageName); + Controls.Add(labelSet); Controls.Add(pictureBoxCollection); Controls.Add(maskedTextBoxNumber); - Controls.Add(label1); + Controls.Add(labelTools); Controls.Add(buttonUpdate); Controls.Add(buttonDelete); Controls.Add(buttonAdd); - Margin = new Padding(3, 4, 3, 4); Name = "FormShipCollection"; Text = "Набор кораблей"; ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); @@ -119,9 +170,14 @@ private Button buttonAdd; private Button buttonDelete; - private Button buttonUpdate; - private Label label1; + private Label labelTools; private TextBox maskedTextBoxNumber; private PictureBox pictureBoxCollection; + private Label labelSet; + private TextBox textBoxStorageName; + private ListBox listBoxStorages; + private Button buttonAddStorage; + private Button buttonDeleteStorage; + private Button buttonUpdate; } } \ No newline at end of file diff --git a/ContainerShip/FormShipCollection.cs b/ContainerShip/FormShipCollection.cs index 85ebbba..5e49624 100644 --- a/ContainerShip/FormShipCollection.cs +++ b/ContainerShip/FormShipCollection.cs @@ -15,58 +15,126 @@ namespace ContainerShip { public partial class FormShipCollection : Form { - private readonly ShipsGenericCollection _ship; + private readonly ShipsGenericStorage _storage; public FormShipCollection() { InitializeComponent(); - _ship = new ShipsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new ShipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + 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("Не все данные заполнены", "Ошибка", + 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]?.ShowShips(); + } + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } } private void buttonAdd_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormShips form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_ship + form.SelectedShip != -1) + if (obj + form.SelectedShip != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _ship.ShowShips(); + pictureBoxCollection.Image = obj.ShowShips(); } else { MessageBox.Show("Не удалось добавить объект"); } } + } private void buttonDelete_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) { return; } - int pos = 0; - try + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) { - pos = Convert.ToInt32(maskedTextBoxNumber.Text); - } - catch - { - MessageBox.Show("Ошибка ввода данных"); return; } - if (_ship - pos) + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _ship.ShowShips(); + pictureBoxCollection.Image = obj.ShowShips(); } else { MessageBox.Show("Не удалось удалить объект"); } + } private void buttonUpdate_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _ship.ShowShips(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowShips(); } + } } diff --git a/ContainerShip/FormShipCollection.resx b/ContainerShip/FormShipCollection.resx index a395bff..af32865 100644 --- a/ContainerShip/FormShipCollection.resx +++ b/ContainerShip/FormShipCollection.resx @@ -18,7 +18,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, ... System.Resources.ResXResourceWriter, System.Windows.Forms, ... this is my long stringthis is a comment - Blue + Blue [base64 mime encoded serialized .NET Framework object] diff --git a/ContainerShip/FormShips.cs b/ContainerShip/FormShips.cs index 7f52e1d..2a10433 100644 --- a/ContainerShip/FormShips.cs +++ b/ContainerShip/FormShips.cs @@ -132,7 +132,6 @@ namespace ContainerShip SelectedShip = _drawningShip; DialogResult = DialogResult.OK; } - } } diff --git a/ContainerShip/IMovableObject.cs b/ContainerShip/IMovableObject.cs index 10f55ef..992ebe0 100644 --- a/ContainerShip/IMovableObject.cs +++ b/ContainerShip/IMovableObject.cs @@ -9,9 +9,7 @@ namespace ContainerShip.MovementStrategy { public interface IMoveableObject { - ObjectParameters? GetObjectPosition { get; } - int GetStep { get; } bool CheckCanMove(DirectionType direction); void MoveObject(DirectionType direction); diff --git a/ContainerShip/ObjectParameters.cs b/ContainerShip/ObjectParameters.cs index 4058be0..f3036e4 100644 --- a/ContainerShip/ObjectParameters.cs +++ b/ContainerShip/ObjectParameters.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; namespace ContainerShip.MovementStrategy { - public class ObjectParameters { private readonly int _x; diff --git a/ContainerShip/SetGeneric.cs b/ContainerShip/SetGeneric.cs index 2b2aaa6..3414007 100644 --- a/ContainerShip/SetGeneric.cs +++ b/ContainerShip/SetGeneric.cs @@ -9,77 +9,71 @@ namespace ContainerShip.Generic internal class SetGeneric where T : class { - private readonly T?[] _places; - public int Count => _places.Length - 1; + private readonly List _places; + 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 ship) { - int pos = Count - 1; - if (_places[Count] != null) - { - - for (int i = pos; i >= 0; --i) - { - if (_places[i] == null) - { - pos = i; - break; - } - } - for (int i = pos + 1; i <= Count; ++i) - { - _places[i - 1] = _places[i]; - } - } - _places[Count] = ship; - return pos; + _places.Insert(0, ship); + return 0; } - public bool Insert(T ship, int position) { - if (position < 0 || position > Count) + // TODO проверка позиции + if (position < 0 || position >= Count || Count >= _maxCount) { return false; } - if (_places[Count] != null) - { - int pos = Count; - for (int i = Count; i > 0; --i) - { - if (_places[i] == null) - { - pos = i; - break; - } - } - for (int i = Count; i >= pos; --i) - { - _places[i - 1] = _places[i]; - } - } - _places[Count] = ship; + // TODO вставка по позиции + _places.Insert(position, ship); return true; } public bool Remove(int position) { - if (position < 0 || position > Count) + if (position < 0 || position >= Count) { return false; } - _places[position] = null; + _places.RemoveAt(position); + return true; } - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position > Count) + get { - return null; + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + set + { + if (position < 0 || position > Count || Count >= _maxCount) + { + return; + } + _places.Insert(position, value); } - return _places[position]; } + public IEnumerable GetShip(int? maxShip = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxShip.HasValue && i == maxShip.Value) + { + yield break; + } + } + } + } } diff --git a/ContainerShip/ShipsGenericCollection.cs b/ContainerShip/ShipsGenericCollection.cs index 08815ba..ee4650e 100644 --- a/ContainerShip/ShipsGenericCollection.cs +++ b/ContainerShip/ShipsGenericCollection.cs @@ -36,7 +36,7 @@ namespace ContainerShip.Generic } public static bool operator -(ShipsGenericCollection collect, int pos) { - if (collect._collection.Get(pos) == null) + if (collect._collection[pos] == null) { return false; } @@ -44,7 +44,7 @@ namespace ContainerShip.Generic } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowShips() { @@ -59,8 +59,9 @@ namespace ContainerShip.Generic 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); @@ -71,31 +72,20 @@ namespace ContainerShip.Generic } private void DrawObjects(Graphics g) { - int x; - int y; - int index = -1; - for (int i = 0; i <= _collection.Count; ++i) - { - DrawningShip _ship = _collection.Get(i); - x = 0; - y = _pictureHeight / _placeSizeHeight - 1; - if (_ship != null) - { - index = _collection.Count - i; - while ((index - _pictureWidth / _placeSizeWidth) >= 0) - { - y--; - index -= _pictureWidth / _placeSizeWidth; - } - if (index > 0) - { - x += index; - } - x = _pictureWidth / _placeSizeWidth - 1 - x; - _ship.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); + int x = _pictureWidth / _placeSizeWidth - 1; + int y = _pictureHeight / _placeSizeHeight - 1; + for (int i = 0; i < _collection.Count; i++) + { + DrawningShip _ship = _collection[i]; + if (x < 0) + { + x = _pictureWidth / _placeSizeWidth - 1; + y--; + } + _ship.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); _ship.DrawTransport(g); - } + x--; } } } diff --git a/ContainerShip/ShipsGenericStorage.cs b/ContainerShip/ShipsGenericStorage.cs new file mode 100644 index 0000000..12b28a2 --- /dev/null +++ b/ContainerShip/ShipsGenericStorage.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ContainerShip.MovementStrategy; +using ContainerShip.DrawningObjects; + +namespace ContainerShip.Generic +{ + internal class ShipsGenericStorage + { + readonly Dictionary> _shipStorages; + public List Keys => _shipStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public ShipsGenericStorage(int pictureWidth, int pictureHeight) + { + _shipStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + foreach (string nameStorage in Keys) + { + if (nameStorage == name) + { + MessageBox.Show("Набор с заданным именем уже существует", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + _shipStorages.Add(name, new ShipsGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (_shipStorages.ContainsKey(name)) + { + _shipStorages.Remove(name); + } + + } + public ShipsGenericCollection? this[string ind] + { + get + { + if (_shipStorages.ContainsKey(ind)) + { + return _shipStorages[ind]; + } + return null; + } + } + + } +}