файлики

This commit is contained in:
Казначеева Елизавета 2023-11-04 23:16:55 +04:00
parent 596b0127e7
commit 6eb869fd5c
5 changed files with 205 additions and 113 deletions

View File

@ -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;
}
}

View File

@ -18,14 +18,76 @@ namespace Battleship
/// <summary>
/// Набор объектов
/// </summary>
private readonly ShipGenericCollection<DrawningShip,DrawningObjectShip> _ships;
private readonly ShipsGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormShipCollection()
{
InitializeComponent();
_ships = new ShipGenericCollection<DrawningShip, DrawningObjectShip>(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;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowShips();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
/// <summary>
/// Добавление объекта в набор
@ -34,13 +96,23 @@ namespace Battleship
/// <param name="e"></param>
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();
}
}
}

View File

@ -6,91 +6,45 @@ using System.Threading.Tasks;
namespace Battleship.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Count;
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T ship)
public bool Insert(T ship)
{
// TODO вставка в начало набора
if (_places[Count - 1] != null)
return -1;
return Insert(ship,0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// /// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get

View File

@ -25,12 +25,15 @@ namespace Battleship.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(ShipGenericCollection<T, U>? collect, T? obj)
public static bool operator +(ShipGenericCollection<T, U>? collect, T? obj)
{
if (obj != null && collect != null)
{
return collect._collection.Insert(obj);
return -1;
}
return false;
}
public static T? operator -(ShipGenericCollection<T, U>? collect, int pos)
{
T? obj = collect?._collection[pos];

View File

@ -10,28 +10,15 @@ namespace Battleship.Generics
{
internal class ShipsGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, ShipGenericCollection<DrawningShip,
DrawningObjectShip>> _shipStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _shipStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// /// <param name="pictureHeight"></param>
public ShipsGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new Dictionary<string,
@ -39,33 +26,28 @@ namespace Battleship.Generics
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
// TODO Прописать логику для добавления
if (_shipStorages.ContainsKey(name))
return;
_shipStorages[name] = new ShipGenericCollection<DrawningShip, DrawningObjectShip>(_pictureWidth,_pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
// TODO Прописать логику для удаления
if (!_shipStorages.ContainsKey(name))
return;
_shipStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public ShipGenericCollection<DrawningShip, DrawningObjectShip>?
this[string ind]
{
get
{
// TODO Продумать логику получения набора
if(_shipStorages.ContainsKey(ind))
return _shipStorages[ind];
return null;
}
}