PIbd-14_Gilmetdinov_I.A._Battleship_Simple_LabWork04 #4

Closed
sevnz wants to merge 1 commits from Lab_4 into Lab_3
5 changed files with 395 additions and 29 deletions

View File

@ -0,0 +1,22 @@
namespace ProjectBattleship.CollectionGenericObjects;
/// <summary>
/// Тип коллекции
/// </summary>
public enum CollectionType
{
/// <summary>
/// Неопределено
/// </summary>
None = 0,
/// <summary>
/// Массив
/// </summary>
Massive = 1,
/// <summary>
/// Список
/// </summary>
List = 2
}

View File

@ -0,0 +1,60 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectBattleship.CollectionGenericObjects;
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
/// <summary>
/// Список объектов, которые храним
/// </summary>
private readonly List<T?> _collection;
/// <summary>
/// Максимально допустимое число объектов в списке
/// </summary>
private int _maxCount;
public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
/// <summary>
/// Конструктор
/// </summary>
public ListGenericObjects()
{
_collection = new();
}
public T? Get(int position)
{
if (position < 0 || position >= _collection.Count)
return null;
return _collection[position];
}
public int Insert(T obj)
{
if (_collection.Count + 1 <= _maxCount)
{
_collection.Add(obj);
return _collection.Count - 1;
}
return -1;
}
public bool Insert(T obj, int position)
{
if (_collection.Count + 1 > _maxCount || position < 0 || position >= _collection.Count)
return false;
_collection.Insert(position, obj);
return true;
}
public T? Remove(int position)
{
if (position < 0 || position >= _collection.Count)
return null;
T? temp = _collection[position];
_collection.RemoveAt(position);
return temp;
}
}

View File

@ -0,0 +1,71 @@
using ProjectBattleship.CollectionGenericObjects;
namespace ProjectSportCar.CollectionGenericObjects;
/// <summary>
/// Класс-хранилище коллекций
/// </summary>
/// <typeparam name="T"></typeparam>
public class StorageCollection<T>
where T : class
{
/// <summary>
/// Словарь (хранилище) с коллекциями
/// </summary>
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
/// <summary>
/// Возвращение списка названий коллекций
/// </summary>
public List<string> Keys => _storages.Keys.ToList();
/// <summary>
/// Конструктор
/// </summary>
public StorageCollection()
{
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
}
/// <summary>
/// Добавление коллекции в хранилище
/// </summary>
/// <param name="name">Название коллекции</param>
/// <param name="collectionType">тип коллекции</param>
public void AddCollection(string name, CollectionType collectionType)
{
if (string.IsNullOrEmpty(name) || _storages.ContainsKey(name))
return;
switch (collectionType)
{
case CollectionType.List:
_storages.Add(name, new ListGenericObjects<T>());
break;
case CollectionType.Massive:
_storages.Add(name, new MassiveGenericObjects<T>());
break;
default:
break;
}
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
public void DelCollection(string name)
{
if (_storages.ContainsKey(name))
_storages.Remove(name);
}
/// <summary>
/// Доступ к коллекции
/// </summary>
/// <param name="name">Название коллекции</param>
/// <returns></returns>
public ICollectionGenericObjects<T>? this[string name]
{
get
{
if (_storages.TryGetValue(name, out ICollectionGenericObjects<T>? value))
return value;
return null;
}
}
}

View File

@ -29,6 +29,15 @@
private void InitializeComponent()
{
Tools = new GroupBox();
buttonCreateCompany = new Button();
panelStorage = new Panel();
buttonCollectionDel = new Button();
listBoxCollection = new ListBox();
buttonCollectionAdd = new Button();
radioButtonList = new RadioButton();
radioButtonMassive = new RadioButton();
textBoxCollectionName = new TextBox();
labelCollectionName = new Label();
buttonRefresh = new Button();
buttonGoToTest = new Button();
buttonDelShip = new Button();
@ -37,18 +46,18 @@
buttonAddShip = new Button();
comboBoxSelectorCompany = new ComboBox();
pictureBox = new PictureBox();
panelCompanyTools = new Panel();
Tools.SuspendLayout();
panelStorage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
panelCompanyTools.SuspendLayout();
SuspendLayout();
//
// Tools
//
Tools.Controls.Add(buttonRefresh);
Tools.Controls.Add(buttonGoToTest);
Tools.Controls.Add(buttonDelShip);
Tools.Controls.Add(maskedTextBox);
Tools.Controls.Add(buttonAddBettleship);
Tools.Controls.Add(buttonAddShip);
Tools.Controls.Add(panelCompanyTools);
Tools.Controls.Add(buttonCreateCompany);
Tools.Controls.Add(panelStorage);
Tools.Controls.Add(comboBoxSelectorCompany);
Tools.Dock = DockStyle.Right;
Tools.Location = new Point(1605, 0);
@ -58,12 +67,104 @@
Tools.TabStop = false;
Tools.Text = "Инструменты";
//
// buttonCreateCompany
//
buttonCreateCompany.Location = new Point(9, 612);
buttonCreateCompany.Name = "buttonCreateCompany";
buttonCreateCompany.Size = new Size(473, 68);
buttonCreateCompany.TabIndex = 7;
buttonCreateCompany.Text = "Создать компанию";
buttonCreateCompany.UseVisualStyleBackColor = true;
buttonCreateCompany.Click += ButtonCreateCompany_Click;
//
// panelStorage
//
panelStorage.Controls.Add(buttonCollectionDel);
panelStorage.Controls.Add(listBoxCollection);
panelStorage.Controls.Add(buttonCollectionAdd);
panelStorage.Controls.Add(radioButtonList);
panelStorage.Controls.Add(radioButtonMassive);
panelStorage.Controls.Add(textBoxCollectionName);
panelStorage.Controls.Add(labelCollectionName);
panelStorage.Dock = DockStyle.Top;
panelStorage.Location = new Point(3, 35);
panelStorage.Name = "panelStorage";
panelStorage.Size = new Size(482, 512);
panelStorage.TabIndex = 7;
//
// buttonCollectionDel
//
buttonCollectionDel.Location = new Point(6, 443);
buttonCollectionDel.Name = "buttonCollectionDel";
buttonCollectionDel.Size = new Size(473, 46);
buttonCollectionDel.TabIndex = 6;
buttonCollectionDel.Text = "Удалить коллекцию";
buttonCollectionDel.UseVisualStyleBackColor = true;
buttonCollectionDel.Click += ButtonCollectionDel_Click;
//
// listBoxCollection
//
listBoxCollection.FormattingEnabled = true;
listBoxCollection.ItemHeight = 32;
listBoxCollection.Location = new Point(9, 209);
listBoxCollection.Name = "listBoxCollection";
listBoxCollection.Size = new Size(473, 228);
listBoxCollection.TabIndex = 5;
//
// buttonCollectionAdd
//
buttonCollectionAdd.Location = new Point(6, 157);
buttonCollectionAdd.Name = "buttonCollectionAdd";
buttonCollectionAdd.Size = new Size(473, 46);
buttonCollectionAdd.TabIndex = 4;
buttonCollectionAdd.Text = "Добавить коллекцию";
buttonCollectionAdd.UseVisualStyleBackColor = true;
buttonCollectionAdd.Click += ButtonCollectionAdd_Click;
//
// radioButtonList
//
radioButtonList.AutoSize = true;
radioButtonList.Location = new Point(306, 102);
radioButtonList.Name = "radioButtonList";
radioButtonList.Size = new Size(125, 36);
radioButtonList.TabIndex = 3;
radioButtonList.TabStop = true;
radioButtonList.Text = "Список";
radioButtonList.UseVisualStyleBackColor = true;
//
// radioButtonMassive
//
radioButtonMassive.AutoSize = true;
radioButtonMassive.Location = new Point(43, 102);
radioButtonMassive.Name = "radioButtonMassive";
radioButtonMassive.Size = new Size(128, 36);
radioButtonMassive.TabIndex = 2;
radioButtonMassive.TabStop = true;
radioButtonMassive.Text = "Массив";
radioButtonMassive.UseVisualStyleBackColor = true;
//
// textBoxCollectionName
//
textBoxCollectionName.Location = new Point(6, 48);
textBoxCollectionName.Name = "textBoxCollectionName";
textBoxCollectionName.Size = new Size(476, 39);
textBoxCollectionName.TabIndex = 1;
//
// labelCollectionName
//
labelCollectionName.AutoSize = true;
labelCollectionName.Location = new Point(128, 13);
labelCollectionName.Name = "labelCollectionName";
labelCollectionName.Size = new Size(251, 32);
labelCollectionName.TabIndex = 0;
labelCollectionName.Text = "Название коллекции:";
//
// buttonRefresh
//
buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonRefresh.Location = new Point(14, 1134);
buttonRefresh.Location = new Point(16, 432);
buttonRefresh.Name = "buttonRefresh";
buttonRefresh.Size = new Size(468, 90);
buttonRefresh.Size = new Size(466, 90);
buttonRefresh.TabIndex = 6;
buttonRefresh.Text = "Обновить";
buttonRefresh.UseVisualStyleBackColor = true;
@ -72,9 +173,9 @@
// buttonGoToTest
//
buttonGoToTest.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonGoToTest.Location = new Point(6, 768);
buttonGoToTest.Location = new Point(16, 336);
buttonGoToTest.Name = "buttonGoToTest";
buttonGoToTest.Size = new Size(476, 90);
buttonGoToTest.Size = new Size(466, 90);
buttonGoToTest.TabIndex = 5;
buttonGoToTest.Text = "Передать на тест";
buttonGoToTest.UseVisualStyleBackColor = true;
@ -83,9 +184,9 @@
// buttonDelShip
//
buttonDelShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonDelShip.Location = new Point(6, 573);
buttonDelShip.Location = new Point(12, 240);
buttonDelShip.Name = "buttonDelShip";
buttonDelShip.Size = new Size(476, 90);
buttonDelShip.Size = new Size(473, 90);
buttonDelShip.TabIndex = 4;
buttonDelShip.Text = "Удалить корабль";
buttonDelShip.UseVisualStyleBackColor = true;
@ -94,19 +195,19 @@
// maskedTextBox
//
maskedTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
maskedTextBox.Location = new Point(6, 520);
maskedTextBox.Location = new Point(12, 195);
maskedTextBox.Mask = "00";
maskedTextBox.Name = "maskedTextBox";
maskedTextBox.Size = new Size(476, 39);
maskedTextBox.Size = new Size(470, 39);
maskedTextBox.TabIndex = 3;
maskedTextBox.ValidatingType = typeof(int);
//
// buttonAddBettleship
//
buttonAddBettleship.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddBettleship.Location = new Point(6, 283);
buttonAddBettleship.Location = new Point(9, 99);
buttonAddBettleship.Name = "buttonAddBettleship";
buttonAddBettleship.Size = new Size(476, 90);
buttonAddBettleship.Size = new Size(473, 90);
buttonAddBettleship.TabIndex = 2;
buttonAddBettleship.Text = "Добавить боевой корабль";
buttonAddBettleship.UseVisualStyleBackColor = true;
@ -115,13 +216,13 @@
// buttonAddShip
//
buttonAddShip.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonAddShip.Location = new Point(6, 172);
buttonAddShip.Location = new Point(9, 3);
buttonAddShip.Name = "buttonAddShip";
buttonAddShip.Size = new Size(476, 90);
buttonAddShip.Size = new Size(473, 90);
buttonAddShip.TabIndex = 1;
buttonAddShip.Text = "Добавить корабль";
buttonAddShip.UseVisualStyleBackColor = true;
buttonAddShip.Click += buttonAddShip_Click_1;
buttonAddShip.Click += ButtonAddShip_Click_1;
//
// comboBoxSelectorCompany
//
@ -129,7 +230,7 @@
comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxSelectorCompany.FormattingEnabled = true;
comboBoxSelectorCompany.Items.AddRange(new object[] { "Доки" });
comboBoxSelectorCompany.Location = new Point(6, 38);
comboBoxSelectorCompany.Location = new Point(9, 566);
comboBoxSelectorCompany.Name = "comboBoxSelectorCompany";
comboBoxSelectorCompany.Size = new Size(476, 40);
comboBoxSelectorCompany.TabIndex = 0;
@ -144,6 +245,20 @@
pictureBox.TabIndex = 1;
pictureBox.TabStop = false;
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddShip);
panelCompanyTools.Controls.Add(buttonAddBettleship);
panelCompanyTools.Controls.Add(buttonRefresh);
panelCompanyTools.Controls.Add(maskedTextBox);
panelCompanyTools.Controls.Add(buttonGoToTest);
panelCompanyTools.Controls.Add(buttonDelShip);
panelCompanyTools.Enabled = false;
panelCompanyTools.Location = new Point(0, 686);
panelCompanyTools.Name = "panelCompanyTools";
panelCompanyTools.Size = new Size(488, 569);
panelCompanyTools.TabIndex = 8;
//
// FormShipCollection
//
AutoScaleDimensions = new SizeF(13F, 32F);
@ -154,8 +269,11 @@
Name = "FormShipCollection";
Text = "Коллекция кораблей";
Tools.ResumeLayout(false);
Tools.PerformLayout();
panelStorage.ResumeLayout(false);
panelStorage.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBox).EndInit();
panelCompanyTools.ResumeLayout(false);
panelCompanyTools.PerformLayout();
ResumeLayout(false);
}
@ -170,5 +288,15 @@
private PictureBox pictureBox;
private Button buttonRefresh;
private Button buttonGoToTest;
private Panel panelStorage;
private Label labelCollectionName;
private RadioButton radioButtonList;
private RadioButton radioButtonMassive;
private TextBox textBoxCollectionName;
private Button buttonCollectionDel;
private ListBox listBoxCollection;
private Button buttonCollectionAdd;
private Button buttonCreateCompany;
private Panel panelCompanyTools;
}
}

View File

@ -1,6 +1,7 @@
using ProjectBattleship;
using ProjectBattleship.CollectionGenericObjects;
using ProjectBattleship.Drawnings;
using ProjectSportCar.CollectionGenericObjects;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -15,6 +16,7 @@ namespace Battleship;
public partial class FormShipCollection : Form
{
private readonly StorageCollection<DrawingShip> _storageCollection;
/// <summary>
/// Компания
/// </summary>
@ -26,6 +28,7 @@ public partial class FormShipCollection : Form
public FormShipCollection()
{
InitializeComponent();
_storageCollection = new();
}
/// <summary>
/// Выбор компании
@ -34,13 +37,7 @@ public partial class FormShipCollection : Form
/// <param name="e"></param>
private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorCompany.Text)
{
case "Доки":
_company = new ShipDocks(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects<DrawingShip>());
break;
}
panelCompanyTools.Enabled = false;
}
/// <summary>
/// Создание объекта класса-перемещения
@ -163,5 +160,93 @@ public partial class FormShipCollection : Form
}
private void ButtonAddBattleship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingBattleship));
private void buttonAddShip_Click_1(object sender, EventArgs e) => CreateObject(nameof(DrawingShip));
private void ButtonAddShip_Click_1(object sender, EventArgs e) => CreateObject(nameof(DrawingShip));
/// <summary>
/// Добавление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCollectionAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonMassive.Checked)
{
collectionType = CollectionType.Massive;
}
else if (radioButtonList.Checked)
{
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
RerfreshListBoxItems();
}
/// <summary>
/// Удаление коллекции
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCollectionDel_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
return;
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
RerfreshListBoxItems();
}
/// <summary>
/// Обновление списка в listBoxCollection
/// </summary>
private void RerfreshListBoxItems()
{
listBoxCollection.Items.Clear();
for (int i = 0; i < _storageCollection.Keys?.Count; ++i)
{
string? colName = _storageCollection.Keys?[i];
if (!string.IsNullOrEmpty(colName))
{
listBoxCollection.Items.Add(colName);
}
}
}
/// <summary>
/// Создание компании
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateCompany_Click(object sender, EventArgs e)
{
if (listBoxCollection.SelectedIndex < 0 || listBoxCollection.SelectedItem == null)
{
MessageBox.Show("Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawingShip>? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty];
if (collection == null)
{
MessageBox.Show("Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.Text)
{
case "Доки":
_company = new ShipDocks(pictureBox.Width, pictureBox.Height, collection);
break;
}
panelCompanyTools.Enabled = true;
RerfreshListBoxItems();
}
}