diff --git a/Battleship/Battleship/FormShipCollection.Designer.cs b/Battleship/Battleship/FormShipCollection.Designer.cs
index 8da44c5..f51f865 100644
--- a/Battleship/Battleship/FormShipCollection.Designer.cs
+++ b/Battleship/Battleship/FormShipCollection.Designer.cs
@@ -29,12 +29,18 @@
private void InitializeComponent()
{
this.groupBoxBattleShip = new System.Windows.Forms.GroupBox();
+ this.groupBox1 = new System.Windows.Forms.GroupBox();
+ this.textBoxStorageName = new System.Windows.Forms.TextBox();
+ this.listBoxStorages = new System.Windows.Forms.ListBox();
+ this.buttonAddObject = new System.Windows.Forms.Button();
+ this.buttonDelObject = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.buttonRefreshCollection = new System.Windows.Forms.Button();
this.buttonRemoveShip = new System.Windows.Forms.Button();
this.buttonAddShip = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.groupBoxBattleShip.SuspendLayout();
+ this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
@@ -42,6 +48,7 @@
//
this.groupBoxBattleShip.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
+ this.groupBoxBattleShip.Controls.Add(this.groupBox1);
this.groupBoxBattleShip.Controls.Add(this.maskedTextBoxNumber);
this.groupBoxBattleShip.Controls.Add(this.buttonRefreshCollection);
this.groupBoxBattleShip.Controls.Add(this.buttonRemoveShip);
@@ -53,16 +60,65 @@
this.groupBoxBattleShip.TabStop = false;
this.groupBoxBattleShip.Text = "Инструменты";
//
+ // groupBox1
+ //
+ this.groupBox1.Controls.Add(this.textBoxStorageName);
+ this.groupBox1.Controls.Add(this.listBoxStorages);
+ this.groupBox1.Controls.Add(this.buttonAddObject);
+ this.groupBox1.Controls.Add(this.buttonDelObject);
+ this.groupBox1.Location = new System.Drawing.Point(6, 22);
+ this.groupBox1.Name = "groupBox1";
+ this.groupBox1.Size = new System.Drawing.Size(171, 253);
+ this.groupBox1.TabIndex = 1;
+ this.groupBox1.TabStop = false;
+ this.groupBox1.Text = "Набор";
+ //
+ // textBoxStorageName
+ //
+ this.textBoxStorageName.Location = new System.Drawing.Point(17, 40);
+ this.textBoxStorageName.Name = "textBoxStorageName";
+ this.textBoxStorageName.Size = new System.Drawing.Size(147, 23);
+ this.textBoxStorageName.TabIndex = 4;
+ //
+ // listBoxStorages
+ //
+ this.listBoxStorages.FormattingEnabled = true;
+ this.listBoxStorages.ItemHeight = 15;
+ this.listBoxStorages.Location = new System.Drawing.Point(17, 124);
+ this.listBoxStorages.Name = "listBoxStorages";
+ this.listBoxStorages.Size = new System.Drawing.Size(147, 94);
+ this.listBoxStorages.TabIndex = 3;
+ //
+ // buttonAddObject
+ //
+ this.buttonAddObject.Location = new System.Drawing.Point(18, 81);
+ this.buttonAddObject.Name = "buttonAddObject";
+ this.buttonAddObject.Size = new System.Drawing.Size(147, 23);
+ this.buttonAddObject.TabIndex = 2;
+ this.buttonAddObject.Text = "Добавить набор";
+ this.buttonAddObject.UseVisualStyleBackColor = true;
+ this.buttonAddObject.Click += new System.EventHandler(this.buttonAddObject_Click);
+ //
+ // buttonDelObject
+ //
+ this.buttonDelObject.Location = new System.Drawing.Point(17, 224);
+ this.buttonDelObject.Name = "buttonDelObject";
+ this.buttonDelObject.Size = new System.Drawing.Size(147, 23);
+ this.buttonDelObject.TabIndex = 1;
+ this.buttonDelObject.Text = "Удалить набор";
+ this.buttonDelObject.UseVisualStyleBackColor = true;
+ this.buttonDelObject.Click += new System.EventHandler(this.buttonDelObject_Click);
+ //
// maskedTextBoxNumber
//
- this.maskedTextBoxNumber.Location = new System.Drawing.Point(22, 152);
+ this.maskedTextBoxNumber.Location = new System.Drawing.Point(23, 330);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(147, 23);
this.maskedTextBoxNumber.TabIndex = 3;
//
// buttonRefreshCollection
//
- this.buttonRefreshCollection.Location = new System.Drawing.Point(22, 265);
+ this.buttonRefreshCollection.Location = new System.Drawing.Point(23, 411);
this.buttonRefreshCollection.Name = "buttonRefreshCollection";
this.buttonRefreshCollection.Size = new System.Drawing.Size(147, 33);
this.buttonRefreshCollection.TabIndex = 2;
@@ -72,7 +128,7 @@
//
// buttonRemoveShip
//
- this.buttonRemoveShip.Location = new System.Drawing.Point(22, 217);
+ this.buttonRemoveShip.Location = new System.Drawing.Point(23, 359);
this.buttonRemoveShip.Name = "buttonRemoveShip";
this.buttonRemoveShip.Size = new System.Drawing.Size(147, 33);
this.buttonRemoveShip.TabIndex = 1;
@@ -82,7 +138,7 @@
//
// buttonAddShip
//
- this.buttonAddShip.Location = new System.Drawing.Point(22, 22);
+ this.buttonAddShip.Location = new System.Drawing.Point(23, 281);
this.buttonAddShip.Name = "buttonAddShip";
this.buttonAddShip.Size = new System.Drawing.Size(147, 33);
this.buttonAddShip.TabIndex = 0;
@@ -112,6 +168,8 @@
this.Text = "FormShipCollection";
this.groupBoxBattleShip.ResumeLayout(false);
this.groupBoxBattleShip.PerformLayout();
+ this.groupBox1.ResumeLayout(false);
+ this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
@@ -125,5 +183,10 @@
private Button buttonAddShip;
private PictureBox pictureBoxCollection;
private MaskedTextBox maskedTextBoxNumber;
+ private GroupBox groupBox1;
+ private TextBox textBoxStorageName;
+ private ListBox listBoxStorages;
+ private Button buttonAddObject;
+ private Button buttonDelObject;
}
}
\ No newline at end of file
diff --git a/Battleship/Battleship/FormShipCollection.cs b/Battleship/Battleship/FormShipCollection.cs
index 27c8eb4..404ff0e 100644
--- a/Battleship/Battleship/FormShipCollection.cs
+++ b/Battleship/Battleship/FormShipCollection.cs
@@ -18,14 +18,76 @@ namespace Battleship
///
/// Набор объектов
///
- private readonly ShipGenericCollection _ships;
+ private readonly ShipsGenericStorage _storage;
///
/// Конструктор
///
public FormShipCollection()
{
InitializeComponent();
- _ships = new ShipGenericCollection(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();
+ }
}
///
/// Добавление объекта в набор
@@ -34,13 +96,23 @@ namespace Battleship
///
private void buttonAddShip_Click(object sender, EventArgs e)
{
+ if (listBoxStorages.SelectedIndex == -1)
+ {
+ return;
+ }
+ var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
+ string.Empty];
+ if (obj == null)
+ {
+ return;
+ }
FormBattleship form = new();
if (form.ShowDialog() == DialogResult.OK)
{
- if (_ships + form.SelectedShip != -1)
+ if (obj + form.SelectedShip)
{
MessageBox.Show("Объект добавлен");
- pictureBoxCollection.Image = _ships.ShowShips();
+ pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@@ -51,16 +123,24 @@ namespace Battleship
private void buttonRemoveShip_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("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
- if (_ships - pos != null)
+ if (obj - pos != null)
{
MessageBox.Show("Объект удален");
- pictureBoxCollection.Image = _ships.ShowShips();
+ pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@@ -70,8 +150,18 @@ namespace Battleship
private void buttonRefreshCollection_Click(object sender, EventArgs e)
{
- pictureBoxCollection.Image = _ships.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/Battleship/Battleship/SetGeneric.cs b/Battleship/Battleship/SetGeneric.cs
index 4211842..fae0f1d 100644
--- a/Battleship/Battleship/SetGeneric.cs
+++ b/Battleship/Battleship/SetGeneric.cs
@@ -6,91 +6,45 @@ using System.Threading.Tasks;
namespace Battleship.Generics
{
- ///
- /// Параметризованный набор объектов
- ///
- ///
+
internal class SetGeneric
where T : class
{
- ///
- /// Массив объектов, которые храним
- ///
private readonly List _places;
- ///
- /// Количество объектов в массиве
- ///
public int Count => _places.Count;
-
private readonly int _maxCount;
- ///
- /// Конструктор
- ///
- ///
public SetGeneric(int count)
{
_maxCount = count;
_places = new List(count);
}
- ///
- /// Добавление объекта в набор
- ///
- /// Добавляемый автомобиль
- ///
- public int Insert(T ship)
+
+ public bool Insert(T ship)
{
- // TODO вставка в начало набора
- if (_places[Count - 1] != null)
- return -1;
- return Insert(ship,0);
- }
- ///
- /// Добавление объекта в набор на конкретную позицию
- /// ///
- /// Добавляемый автомобиль
- /// Позиция
- ///
- public int Insert(T ship, int position)
- {
- // TODO проверка позиции
- // TODO проверка, что элемент массива по этой позиции пустой, если нет, то
- // проверка, что после вставляемого элемента в массиве есть пустой элемент
- // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
- // TODO вставка по позиции_places[position] = car;
- if (!(position >= 0 && position < Count))
- return -1;
- if (_places[position] != null)
+ if(_places.Count == _maxCount)
{
- int ind = position;
- while (ind < Count && _places[ind] != null)
- ind++;
- if (ind == Count)
- return -1;
- for (int i = ind - 1; i >= position; i--)
- _places[i + 1] = _places[i];
+ return false;
}
- _places[position] = ship;
- return position;
+ Insert(ship, 0);
+ return true;
+ }
+
+ public bool Insert(T ship, int position)
+ {
+ if(!(position >= 0 && position <= Count && _places.Count < _maxCount))
+ {
+ return false;
+ }
+ _places.Insert(position, ship);
+ return true;
}
- ///
- /// Удаление объекта из набора с конкретной позиции
- ///
- ///
- ///
public bool Remove(int position)
{
- // TODO проверка позиции
- // TODO удаление объекта из массива, присвоив элементу массива значение null
- if (!(position >= 0 && position < Count) || _places[position] == null)
+ if (!(position >= 0 && position < Count))
return false;
_places[position] = null;
return true;
}
- ///
- /// Получение объекта из набора по позиции
- ///
- ///
- ///
public T? this[int position]
{
get
diff --git a/Battleship/Battleship/ShipGenericCollection.cs b/Battleship/Battleship/ShipGenericCollection.cs
index 9fb13ff..ac2d497 100644
--- a/Battleship/Battleship/ShipGenericCollection.cs
+++ b/Battleship/Battleship/ShipGenericCollection.cs
@@ -25,12 +25,15 @@ namespace Battleship.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric(width * height);
}
- public static int operator +(ShipGenericCollection? collect, T? obj)
+ public static bool operator +(ShipGenericCollection? collect, T? obj)
{
if (obj != null && collect != null)
+ {
return collect._collection.Insert(obj);
- return -1;
+ }
+ return false;
}
+
public static T? operator -(ShipGenericCollection? collect, int pos)
{
T? obj = collect?._collection[pos];
diff --git a/Battleship/Battleship/ShipsGenericStorage.cs b/Battleship/Battleship/ShipsGenericStorage.cs
index ffe3377..6260ff3 100644
--- a/Battleship/Battleship/ShipsGenericStorage.cs
+++ b/Battleship/Battleship/ShipsGenericStorage.cs
@@ -10,28 +10,15 @@ namespace Battleship.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
- /// Добавление набора
- ///
- /// Название набора
+
public void AddSet(string name)
{
- // TODO Прописать логику для добавления
+ if (_shipStorages.ContainsKey(name))
+ return;
+ _shipStorages[name] = new ShipGenericCollection(_pictureWidth,_pictureHeight);
}
- ///
- /// Удаление набора
- ///
- /// Название набора
+
public void DelSet(string name)
{
- // TODO Прописать логику для удаления
+ if (!_shipStorages.ContainsKey(name))
+ return;
+ _shipStorages.Remove(name);
}
- ///
- /// Доступ к набору
- ///
- ///
- ///
+
public ShipGenericCollection?
this[string ind]
{
get
{
- // TODO Продумать логику получения набора
+ if(_shipStorages.ContainsKey(ind))
+ return _shipStorages[ind];
return null;
}
}