diff --git a/WarmlyShip/FormShipCollection.Designer.cs b/WarmlyShip/FormShipCollection.Designer.cs
index 37938ce..0a63cd1 100644
--- a/WarmlyShip/FormShipCollection.Designer.cs
+++ b/WarmlyShip/FormShipCollection.Designer.cs
@@ -29,49 +29,115 @@
private void InitializeComponent()
{
pictureBoxCollection = new PictureBox();
- toolsLabel = new Panel();
- label1 = new Label();
+ toolsPanel = new Panel();
+ LabelSets = new Label();
+ panelSets = new Panel();
+ ListBoxObjects = new ListBox();
+ textBoxStorageName = new TextBox();
+ ButtonDelObject = new Button();
+ ButtonAddObject = new Button();
+ LabelTools = new Label();
ButtonRefreshCollection = new Button();
ButtonDeleteShip = new Button();
maskedTextBoxNumber = new TextBox();
ButtonAddShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
- toolsLabel.SuspendLayout();
+ toolsPanel.SuspendLayout();
+ panelSets.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
- pictureBoxCollection.Location = new Point(0, 0);
+ pictureBoxCollection.Location = new Point(-1, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
- pictureBoxCollection.Size = new Size(655, 395);
+ pictureBoxCollection.Size = new Size(655, 507);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
- // toolsLabel
+ // toolsPanel
//
- toolsLabel.Controls.Add(label1);
- toolsLabel.Controls.Add(ButtonRefreshCollection);
- toolsLabel.Controls.Add(ButtonDeleteShip);
- toolsLabel.Controls.Add(maskedTextBoxNumber);
- toolsLabel.Controls.Add(ButtonAddShip);
- toolsLabel.Location = new Point(660, 12);
- toolsLabel.Name = "toolsLabel";
- toolsLabel.Size = new Size(212, 383);
- toolsLabel.TabIndex = 0;
+ toolsPanel.Controls.Add(LabelSets);
+ toolsPanel.Controls.Add(panelSets);
+ toolsPanel.Controls.Add(LabelTools);
+ toolsPanel.Controls.Add(ButtonRefreshCollection);
+ toolsPanel.Controls.Add(ButtonDeleteShip);
+ toolsPanel.Controls.Add(maskedTextBoxNumber);
+ toolsPanel.Controls.Add(ButtonAddShip);
+ toolsPanel.Location = new Point(660, 12);
+ toolsPanel.Name = "toolsLabel";
+ toolsPanel.Size = new Size(230, 495);
+ toolsPanel.TabIndex = 0;
//
- // label1
+ // LabelSets
//
- label1.AutoSize = true;
- label1.Location = new Point(58, -3);
- label1.Name = "label1";
- label1.Size = new Size(83, 15);
- label1.TabIndex = 1;
- label1.Text = "Инструменты";
+ LabelSets.AutoSize = true;
+ LabelSets.Location = new Point(89, 21);
+ LabelSets.Name = "LabelSets";
+ LabelSets.Size = new Size(52, 15);
+ LabelSets.TabIndex = 0;
+ LabelSets.Text = "Наборы";
+ //
+ // panelSets
+ //
+ panelSets.Controls.Add(ListBoxObjects);
+ panelSets.Controls.Add(textBoxStorageName);
+ panelSets.Controls.Add(ButtonDelObject);
+ panelSets.Controls.Add(ButtonAddObject);
+ panelSets.Location = new Point(17, 30);
+ panelSets.Name = "panelSets";
+ panelSets.Size = new Size(200, 225);
+ panelSets.TabIndex = 3;
+ //
+ // ListBoxObjects
+ //
+ ListBoxObjects.FormattingEnabled = true;
+ ListBoxObjects.ItemHeight = 15;
+ ListBoxObjects.Location = new Point(28, 76);
+ ListBoxObjects.Name = "ListBoxObjects";
+ ListBoxObjects.Size = new Size(142, 94);
+ ListBoxObjects.TabIndex = 3;
+ ListBoxObjects.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
+ //
+ // textBoxStorageName
+ //
+ textBoxStorageName.Location = new Point(28, 9);
+ textBoxStorageName.Name = "textBoxStorageName";
+ textBoxStorageName.Size = new Size(142, 23);
+ textBoxStorageName.TabIndex = 2;
+ //
+ // ButtonDelObject
+ //
+ ButtonDelObject.Location = new Point(28, 180);
+ ButtonDelObject.Name = "ButtonDelObject";
+ ButtonDelObject.Size = new Size(142, 33);
+ ButtonDelObject.TabIndex = 1;
+ ButtonDelObject.Text = "Удалить набор";
+ ButtonDelObject.UseVisualStyleBackColor = true;
+ ButtonDelObject.Click += ButtonDelObject_Click;
+ //
+ // ButtonAddObject
+ //
+ ButtonAddObject.Location = new Point(28, 38);
+ ButtonAddObject.Name = "ButtonAddObject";
+ ButtonAddObject.Size = new Size(142, 32);
+ ButtonAddObject.TabIndex = 0;
+ ButtonAddObject.Text = "Добавить набор";
+ ButtonAddObject.UseVisualStyleBackColor = true;
+ ButtonAddObject.Click += ButtonAddObject_Click;
+ //
+ // LabelTools
+ //
+ LabelTools.AutoSize = true;
+ LabelTools.Location = new Point(85, -3);
+ LabelTools.Name = "LabelTools";
+ LabelTools.Size = new Size(83, 15);
+ LabelTools.TabIndex = 1;
+ LabelTools.Text = "Инструменты";
//
// ButtonRefreshCollection
//
- ButtonRefreshCollection.Location = new Point(35, 187);
+ ButtonRefreshCollection.Location = new Point(45, 432);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(142, 37);
ButtonRefreshCollection.TabIndex = 2;
@@ -81,7 +147,7 @@
//
// ButtonDeleteShip
//
- ButtonDeleteShip.Location = new Point(35, 128);
+ ButtonDeleteShip.Location = new Point(45, 369);
ButtonDeleteShip.Name = "ButtonDeleteShip";
ButtonDeleteShip.Size = new Size(142, 37);
ButtonDeleteShip.TabIndex = 2;
@@ -91,14 +157,14 @@
//
// maskedTextBoxNumber
//
- maskedTextBoxNumber.Location = new Point(58, 83);
+ maskedTextBoxNumber.Location = new Point(68, 340);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 23);
maskedTextBoxNumber.TabIndex = 2;
//
// ButtonAddShip
//
- ButtonAddShip.Location = new Point(35, 31);
+ ButtonAddShip.Location = new Point(45, 274);
ButtonAddShip.Name = "ButtonAddShip";
ButtonAddShip.Size = new Size(142, 36);
ButtonAddShip.TabIndex = 2;
@@ -110,14 +176,16 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
- ClientSize = new Size(884, 395);
- Controls.Add(toolsLabel);
+ ClientSize = new Size(889, 508);
+ Controls.Add(toolsPanel);
Controls.Add(pictureBoxCollection);
Name = "FormShipCollection";
Text = "Набор кораблей";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
- toolsLabel.ResumeLayout(false);
- toolsLabel.PerformLayout();
+ toolsPanel.ResumeLayout(false);
+ toolsPanel.PerformLayout();
+ panelSets.ResumeLayout(false);
+ panelSets.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
@@ -125,11 +193,17 @@
#endregion
private PictureBox pictureBoxCollection;
- private Panel toolsLabel;
+ private Panel toolsPanel;
private Button ButtonAddShip;
- private Label label1;
+ private Label LabelTools;
private TextBox maskedTextBoxNumber;
private Button ButtonDeleteShip;
private Button ButtonRefreshCollection;
+ private Label LabelSets;
+ private Panel panelSets;
+ private ListBox ListBoxObjects;
+ private TextBox textBoxStorageName;
+ private Button ButtonDelObject;
+ private Button ButtonAddObject;
}
}
\ No newline at end of file
diff --git a/WarmlyShip/FormShipCollection.cs b/WarmlyShip/FormShipCollection.cs
index d6fc78c..fb03af0 100644
--- a/WarmlyShip/FormShipCollection.cs
+++ b/WarmlyShip/FormShipCollection.cs
@@ -21,7 +21,7 @@ namespace WarmlyShip
///
/// Набор объектов
///
- private readonly ShipsGenericCollection _theShips;
+ private readonly ShipsGenericStorage _storage;
///
/// Конструктор
@@ -29,8 +29,77 @@ namespace WarmlyShip
public FormShipCollection()
{
InitializeComponent();
- _theShips = new ShipsGenericCollection
- (pictureBoxCollection.Width, pictureBoxCollection.Height);
+ _storage = new ShipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
+ }
+
+ ///
+ /// Заполнение ListBoxObjects
+ ///
+ private void ReloadObjects()
+ {
+ int index = ListBoxObjects.SelectedIndex;
+
+ ListBoxObjects.Items.Clear();
+ for (int i = 0; i < _storage.Keys.Count; i++)
+ {
+ ListBoxObjects.Items.Add(_storage.Keys[i]);
+ }
+
+ if (ListBoxObjects.Items.Count > 0 && (index == -1 || index >= ListBoxObjects.Items.Count))
+ {
+ ListBoxObjects.SelectedIndex = 0;
+ }
+
+ else if (ListBoxObjects.Items.Count > 0 && index > -1 && index < ListBoxObjects.Items.Count)
+ {
+ ListBoxObjects.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[ListBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowShips();
+ }
+
+ ///
+ /// Удаление набора
+ ///
+ ///
+ ///
+ private void ButtonDelObject_Click(object sender, EventArgs e)
+ {
+ if (ListBoxObjects.SelectedIndex == -1)
+ {
+ return;
+ }
+ if (MessageBox.Show($"Удалить объект {ListBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
+ MessageBoxIcon.Question) == DialogResult.Yes)
+ {
+ _storage.DelSet(ListBoxObjects.SelectedItem.ToString()
+ ?? string.Empty);
+ ReloadObjects();
+ }
}
///
@@ -40,13 +109,22 @@ namespace WarmlyShip
///
private void ButtonAddShip_Click(object sender, EventArgs e)
{
+ if (ListBoxObjects.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[ListBoxObjects.SelectedItem.ToString() ?? string.Empty];
+ if (obj == null)
+ {
+ return;
+ }
FormWarmlyShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
- if (_theShips + form.SelectedShip != -1)
+ if (obj + form.SelectedShip)
{
MessageBox.Show("Объект добавлен");
- pictureBoxCollection.Image = _theShips.ShowShips();
+ pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@@ -62,17 +140,26 @@ namespace WarmlyShip
///
private void ButtonDeleteShip_Click(object sender, EventArgs e)
{
- if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
+ if (ListBoxObjects.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[ListBoxObjects.SelectedItem.ToString() ??
+ string.Empty];
+ if (obj == null)
+ {
+ return;
+ }
+ if (MessageBox.Show("Удалить объект?", "Удаление",
+ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
-
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
-
- if (_theShips - pos != true)
+ if (obj - pos != null)
{
MessageBox.Show("Объект удален");
- pictureBoxCollection.Image = _theShips.ShowShips();
+ pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@@ -87,7 +174,17 @@ namespace WarmlyShip
///
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
- pictureBoxCollection.Image = _theShips.ShowShips();
+ if (ListBoxObjects.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[ListBoxObjects.SelectedItem.ToString() ??
+ string.Empty];
+ if (obj == null)
+ {
+ return;
+ }
+ pictureBoxCollection.Image = obj.ShowShips();
}
}
}
diff --git a/WarmlyShip/Generics/SetGeneric.cs b/WarmlyShip/Generics/SetGeneric.cs
index 2a1b235..b368718 100644
--- a/WarmlyShip/Generics/SetGeneric.cs
+++ b/WarmlyShip/Generics/SetGeneric.cs
@@ -14,22 +14,28 @@ namespace WarmlyShip.Generics
where T : class
{
///
- /// Массив объектов, которые храним
+ /// Список объектов, которые храним
///
- private readonly T?[] _places;
+ private readonly List _places;
///
- /// Количество объектов в массиве
+ /// Количество объектов в списке
///
- public int Count => _places.Length;
+ public int Count => _places.Count;
///
+ /// Максимальное количество объектов в cписке
+ ///
+ private readonly int _maxCount;
+
+ ////
/// Конструктор
///
///
public SetGeneric(int count)
{
- _places = new T?[count];
+ _maxCount = count;
+ _places = new List(count);
}
///
@@ -37,9 +43,15 @@ namespace WarmlyShip.Generics
///
/// Добавляемый корабль
///
- public int Insert(T ship)
+ public bool Insert(T ship)
{
- return Insert(ship, 0);
+ if (_places.Count == _maxCount)
+ {
+ return false;
+ }
+
+ Insert(ship, 0);
+ return true;
}
///
@@ -48,36 +60,15 @@ namespace WarmlyShip.Generics
/// Добавляемый корабль
/// Позиция
///
- public int Insert(T ship, int position)
+ public bool Insert(T ship, int position)
{
- int nullIndex = -1, i;
-
- if (position < 0 || position >= Count)
+ if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
- return -1;
+ return false;
}
- 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] = ship;
- return position;
+ _places.Insert(position, ship);
+ return true;
}
///
@@ -92,7 +83,7 @@ namespace WarmlyShip.Generics
return false;
}
- _places[position] = null;
+ _places.RemoveAt(position);
return true;
}
@@ -101,14 +92,42 @@ namespace WarmlyShip.Generics
///
///
///
- public T? Get(int position)
+ public T? this[int position]
{
- if (position < 0 || position >= Count)
+ get
{
- return null;
+ if (position < 0 || position >= _maxCount)
+ {
+ return null;
+ }
+ return _places[position];
}
+ set
+ {
+ if (!(position >= 0 && position < Count && _places.Count < _maxCount))
+ {
+ return;
+ }
- return _places[position];
+ _places.Insert(position, value);
+ return;
+ }
+ }
+
+ ///
+ /// Проход по списку
+ ///
+ ///
+ public IEnumerable GetShips(int? maxShips = null)
+ {
+ for (int i = 0; i < _places.Count; ++i)
+ {
+ yield return _places[i];
+ if (maxShips.HasValue && i == maxShips.Value)
+ {
+ yield break;
+ }
+ }
}
}
}
diff --git a/WarmlyShip/Generics/ShipsGenericCollection.cs b/WarmlyShip/Generics/ShipsGenericCollection.cs
index 6eb2675..67edce7 100644
--- a/WarmlyShip/Generics/ShipsGenericCollection.cs
+++ b/WarmlyShip/Generics/ShipsGenericCollection.cs
@@ -63,13 +63,13 @@ namespace WarmlyShip.Generics
///
///
///
- public static int operator +(ShipsGenericCollection collect, T? obj)
+ public static bool operator +(ShipsGenericCollection collect, T? obj)
{
if (obj == null)
{
- return -1;
+ return false;
}
- return collect._collection.Insert(obj);
+ return collect?._collection.Insert(obj) ?? false;
}
///
@@ -78,15 +78,14 @@ namespace WarmlyShip.Generics
///
///
///
- public static bool operator -(ShipsGenericCollection collect, int
- pos)
+ public static T? operator -(ShipsGenericCollection collect, int pos)
{
- T? obj = collect._collection.Get(pos);
+ T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
- return false;
+ return obj;
}
///
@@ -96,7 +95,7 @@ namespace WarmlyShip.Generics
///
public U? GetU(int pos)
{
- return (U?)_collection.Get(pos)?.GetMoveableObject;
+ return (U?)_collection[pos]?.GetMoveableObject;
}
///
@@ -137,16 +136,16 @@ namespace WarmlyShip.Generics
///
private void DrawObjects(Graphics g)
{
- for (int i = 0; i < _collection.Count; i++)
+ int i = 0;
+ foreach (var ship in _collection.GetShips())
{
- DrawningShip ship = _collection.Get(i);
-
if (ship != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
ship.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight);
ship.DrawTransport(g);
}
+ i++;
}
}
}
diff --git a/WarmlyShip/Generics/ShipsGenericStorage.cs b/WarmlyShip/Generics/ShipsGenericStorage.cs
new file mode 100644
index 0000000..82845a9
--- /dev/null
+++ b/WarmlyShip/Generics/ShipsGenericStorage.cs
@@ -0,0 +1,89 @@
+using WarmlyShip.DrawningObjects;
+using WarmlyShip.MovementStrategy;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace WarmlyShip.Generics
+{
+ ///
+ /// Класс для хранения коллекции
+ ///
+ 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)
+ {
+ _shipStorages.Add(name, new ShipsGenericCollection(_pictureWidth, _pictureHeight));
+ }
+
+ ///
+ /// Удаление набора
+ ///
+ /// Название набора
+ public void DelSet(string name)
+ {
+ if (!_shipStorages.ContainsKey(name))
+ {
+ return;
+ }
+
+ _shipStorages.Remove(name);
+ }
+
+ ///
+ /// Доступ к набору
+ ///
+ ///
+ ///
+ public ShipsGenericCollection? this[string ind]
+ {
+ get
+ {
+ if (_shipStorages.ContainsKey(ind))
+ {
+ return _shipStorages[ind];
+ }
+ return null;
+ }
+ }
+ }
+}