SemesterFirstLabFourthBomberBase
This commit is contained in:
parent
4b4f3eed79
commit
4beddff8a2
@ -29,31 +29,87 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.groupBox1 = new System.Windows.Forms.GroupBox();
|
||||
this.groupBoxSet = new System.Windows.Forms.GroupBox();
|
||||
this.ButtonDelObject = new System.Windows.Forms.Button();
|
||||
this.ListBoxStorages = new System.Windows.Forms.ListBox();
|
||||
this.ButtonAddObject = new System.Windows.Forms.Button();
|
||||
this.textBoxStorageName = new System.Windows.Forms.TextBox();
|
||||
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
|
||||
this.ButtonRemovePlane = new System.Windows.Forms.Button();
|
||||
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
|
||||
this.ButtonAddPlane = new System.Windows.Forms.Button();
|
||||
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
|
||||
this.groupBox1.SuspendLayout();
|
||||
this.groupBoxSet.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBox1
|
||||
//
|
||||
this.groupBox1.Controls.Add(this.groupBoxSet);
|
||||
this.groupBox1.Controls.Add(this.ButtonRefreshCollection);
|
||||
this.groupBox1.Controls.Add(this.ButtonRemovePlane);
|
||||
this.groupBox1.Controls.Add(this.maskedTextBoxNumber);
|
||||
this.groupBox1.Controls.Add(this.ButtonAddPlane);
|
||||
this.groupBox1.Location = new System.Drawing.Point(586, 2);
|
||||
this.groupBox1.Name = "groupBox1";
|
||||
this.groupBox1.Size = new System.Drawing.Size(216, 453);
|
||||
this.groupBox1.Size = new System.Drawing.Size(216, 563);
|
||||
this.groupBox1.TabIndex = 0;
|
||||
this.groupBox1.TabStop = false;
|
||||
this.groupBox1.Text = "Инструменты";
|
||||
//
|
||||
// groupBoxSet
|
||||
//
|
||||
this.groupBoxSet.Controls.Add(this.ButtonDelObject);
|
||||
this.groupBoxSet.Controls.Add(this.ListBoxStorages);
|
||||
this.groupBoxSet.Controls.Add(this.ButtonAddObject);
|
||||
this.groupBoxSet.Controls.Add(this.textBoxStorageName);
|
||||
this.groupBoxSet.Location = new System.Drawing.Point(21, 25);
|
||||
this.groupBoxSet.Name = "groupBoxSet";
|
||||
this.groupBoxSet.Size = new System.Drawing.Size(180, 274);
|
||||
this.groupBoxSet.TabIndex = 4;
|
||||
this.groupBoxSet.TabStop = false;
|
||||
this.groupBoxSet.Text = "Наборы";
|
||||
//
|
||||
// ButtonDelObject
|
||||
//
|
||||
this.ButtonDelObject.Location = new System.Drawing.Point(7, 226);
|
||||
this.ButtonDelObject.Name = "ButtonDelObject";
|
||||
this.ButtonDelObject.Size = new System.Drawing.Size(161, 37);
|
||||
this.ButtonDelObject.TabIndex = 3;
|
||||
this.ButtonDelObject.Text = "Удалить набор";
|
||||
this.ButtonDelObject.UseVisualStyleBackColor = true;
|
||||
this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
|
||||
//
|
||||
// ListBoxStorages
|
||||
//
|
||||
this.ListBoxStorages.FormattingEnabled = true;
|
||||
this.ListBoxStorages.Location = new System.Drawing.Point(9, 112);
|
||||
this.ListBoxStorages.Name = "ListBoxStorages";
|
||||
this.ListBoxStorages.Size = new System.Drawing.Size(162, 95);
|
||||
this.ListBoxStorages.TabIndex = 2;
|
||||
this.ListBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxStorages_SelectedIndexChanged);
|
||||
//
|
||||
// ButtonAddObject
|
||||
//
|
||||
this.ButtonAddObject.Location = new System.Drawing.Point(8, 61);
|
||||
this.ButtonAddObject.Name = "ButtonAddObject";
|
||||
this.ButtonAddObject.Size = new System.Drawing.Size(164, 34);
|
||||
this.ButtonAddObject.TabIndex = 1;
|
||||
this.ButtonAddObject.Text = "Добавить набор";
|
||||
this.ButtonAddObject.UseVisualStyleBackColor = true;
|
||||
this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
this.textBoxStorageName.Location = new System.Drawing.Point(7, 21);
|
||||
this.textBoxStorageName.Name = "textBoxStorageName";
|
||||
this.textBoxStorageName.Size = new System.Drawing.Size(166, 20);
|
||||
this.textBoxStorageName.TabIndex = 0;
|
||||
//
|
||||
// ButtonRefreshCollection
|
||||
//
|
||||
this.ButtonRefreshCollection.Location = new System.Drawing.Point(17, 237);
|
||||
this.ButtonRefreshCollection.Location = new System.Drawing.Point(18, 509);
|
||||
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
|
||||
this.ButtonRefreshCollection.Size = new System.Drawing.Size(178, 41);
|
||||
this.ButtonRefreshCollection.TabIndex = 3;
|
||||
@ -63,24 +119,24 @@
|
||||
//
|
||||
// ButtonRemovePlane
|
||||
//
|
||||
this.ButtonRemovePlane.Location = new System.Drawing.Point(17, 136);
|
||||
this.ButtonRemovePlane.Location = new System.Drawing.Point(18, 430);
|
||||
this.ButtonRemovePlane.Name = "ButtonRemovePlane";
|
||||
this.ButtonRemovePlane.Size = new System.Drawing.Size(179, 41);
|
||||
this.ButtonRemovePlane.TabIndex = 2;
|
||||
this.ButtonRemovePlane.Text = "Удалить автомобиль";
|
||||
this.ButtonRemovePlane.Text = "Удалить самолет";
|
||||
this.ButtonRemovePlane.UseVisualStyleBackColor = true;
|
||||
this.ButtonRemovePlane.Click += new System.EventHandler(this.ButtonRemovePlane_Click);
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
this.maskedTextBoxNumber.Location = new System.Drawing.Point(17, 99);
|
||||
this.maskedTextBoxNumber.Location = new System.Drawing.Point(18, 388);
|
||||
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
this.maskedTextBoxNumber.Size = new System.Drawing.Size(180, 20);
|
||||
this.maskedTextBoxNumber.TabIndex = 1;
|
||||
//
|
||||
// ButtonAddPlane
|
||||
//
|
||||
this.ButtonAddPlane.Location = new System.Drawing.Point(17, 36);
|
||||
this.ButtonAddPlane.Location = new System.Drawing.Point(18, 327);
|
||||
this.ButtonAddPlane.Name = "ButtonAddPlane";
|
||||
this.ButtonAddPlane.Size = new System.Drawing.Size(185, 40);
|
||||
this.ButtonAddPlane.TabIndex = 0;
|
||||
@ -92,7 +148,7 @@
|
||||
//
|
||||
this.pictureBoxCollection.Location = new System.Drawing.Point(-2, 2);
|
||||
this.pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
this.pictureBoxCollection.Size = new System.Drawing.Size(600, 452);
|
||||
this.pictureBoxCollection.Size = new System.Drawing.Size(600, 563);
|
||||
this.pictureBoxCollection.TabIndex = 1;
|
||||
this.pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
@ -100,13 +156,15 @@
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.ClientSize = new System.Drawing.Size(800, 564);
|
||||
this.Controls.Add(this.pictureBoxCollection);
|
||||
this.Controls.Add(this.groupBox1);
|
||||
this.Name = "FormPlaneCollection";
|
||||
this.Text = "Набор самолетов";
|
||||
this.groupBox1.ResumeLayout(false);
|
||||
this.groupBox1.PerformLayout();
|
||||
this.groupBoxSet.ResumeLayout(false);
|
||||
this.groupBoxSet.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
@ -120,5 +178,10 @@
|
||||
private System.Windows.Forms.TextBox maskedTextBoxNumber;
|
||||
private System.Windows.Forms.Button ButtonAddPlane;
|
||||
private System.Windows.Forms.PictureBox pictureBoxCollection;
|
||||
private System.Windows.Forms.GroupBox groupBoxSet;
|
||||
private System.Windows.Forms.Button ButtonDelObject;
|
||||
private System.Windows.Forms.ListBox ListBoxStorages;
|
||||
private System.Windows.Forms.Button ButtonAddObject;
|
||||
private System.Windows.Forms.TextBox textBoxStorageName;
|
||||
}
|
||||
}
|
@ -14,23 +14,87 @@ using ProjectBomber.DrawningObjects;
|
||||
namespace ProjectBomber
|
||||
{
|
||||
/// <summary>
|
||||
/// Форма для работы с набором объектов класса DrawningCar
|
||||
/// Форма для работы с набором объектов класса DrawningBomber
|
||||
/// </summary>
|
||||
public partial class FormPlaneCollection : Form
|
||||
{
|
||||
/// <summary>
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private readonly PlanesGenericCollection<DrawningBomber,
|
||||
DrawningObjectBomber> _planes;
|
||||
private readonly PlanesGenericStorage _storage;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
public FormPlaneCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_planes = new PlanesGenericCollection<DrawningBomber,
|
||||
DrawningObjectBomber>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_storage = new PlanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
}
|
||||
/// <summary>
|
||||
/// Заполнение listBoxObjects
|
||||
/// </summary>
|
||||
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 ListBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image =
|
||||
_storage[ListBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
|
||||
}
|
||||
/// <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>
|
||||
/// Добавление объекта в набор
|
||||
@ -39,13 +103,23 @@ namespace ProjectBomber
|
||||
/// <param name="e"></param>
|
||||
private void ButtonAddPlane_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (ListBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[ListBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormBomber form = new FormBomber();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_planes + form.SelectedBomber != 1)
|
||||
if (obj + form.SelectedBomber)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _planes.ShowPlanes();
|
||||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -60,16 +134,25 @@ namespace ProjectBomber
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRemovePlane_Click(object sender, EventArgs e)
|
||||
{
|
||||
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 (_planes - pos != null)
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = _planes.ShowPlanes();
|
||||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -83,7 +166,17 @@ namespace ProjectBomber
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _planes.ShowPlanes();
|
||||
if (ListBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[ListBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowPlanes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,14 +57,15 @@ namespace ProjectBomber.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static int? operator +(PlanesGenericCollection<T, U> collect, T
|
||||
public static bool operator +(PlanesGenericCollection<T, U> collect, T
|
||||
obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
return collect?._collection.Insert(obj);
|
||||
collect?._collection.Insert(obj);
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
@ -72,15 +73,14 @@ namespace ProjectBomber.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public static bool operator -(PlanesGenericCollection<T, U> collect, int
|
||||
pos)
|
||||
public static T operator -(PlanesGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T obj = collect._collection.Get(pos);
|
||||
if (obj == null)
|
||||
T obj = collect._collection[pos];
|
||||
if (obj != null)
|
||||
{
|
||||
return false;
|
||||
collect._collection.Remove(pos);
|
||||
}
|
||||
return collect._collection.Remove(pos);
|
||||
return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объекта IMoveableObject
|
||||
@ -89,7 +89,7 @@ namespace ProjectBomber.Generics
|
||||
/// <returns></returns>
|
||||
public U GetU(int pos)
|
||||
{
|
||||
return (U)_collection.Get(pos)?.GetMoveableObject;
|
||||
return (U)_collection[pos]?.GetMoveableObject;
|
||||
}
|
||||
/// <summary>
|
||||
/// Вывод всего набора объектов
|
||||
@ -126,7 +126,6 @@ namespace ProjectBomber.Generics
|
||||
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, numRows * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Метод прорисовки объектов
|
||||
/// </summary>
|
||||
@ -134,22 +133,34 @@ namespace ProjectBomber.Generics
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
int numColumns = _pictureWidth / _placeSizeWidth;
|
||||
int numRows = _pictureHeight / _placeSizeHeight;
|
||||
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
int column = numColumns - 1;
|
||||
int row = 0;
|
||||
foreach (var plane in _collection.GetPlanes())
|
||||
{
|
||||
DrawningBomber plane = _collection.Get(i);
|
||||
// Установка позиции бомбардировщика
|
||||
int xPosition = column * _placeSizeWidth;
|
||||
int yPosition = row * _placeSizeHeight;
|
||||
if (plane != null)
|
||||
{
|
||||
// Вычисляем позицию объекта
|
||||
int row = i / numColumns; // Номер строки
|
||||
int column = numColumns - 1 - (i % numColumns); // Номер столбца, инвертируем для движения справа налево
|
||||
int x = column * _placeSizeWidth;
|
||||
int y = row * _placeSizeHeight;
|
||||
|
||||
plane.SetPosition(x, y);
|
||||
// Перемещение по ячейкам влево, вниз
|
||||
column--;
|
||||
if (column < 0)
|
||||
{
|
||||
column = numColumns - 1;
|
||||
row++;
|
||||
}
|
||||
plane.SetPosition(xPosition, yPosition);
|
||||
plane.DrawTransport(g);
|
||||
}
|
||||
else
|
||||
{
|
||||
column--;
|
||||
if (column < 0)
|
||||
{
|
||||
column = numColumns - 1;
|
||||
row++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
97
ProjectBomber/ProjectBomber/PlanesGenericStorage.cs
Normal file
97
ProjectBomber/ProjectBomber/PlanesGenericStorage.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectBomber.DrawningObjects;
|
||||
using ProjectBomber.MovementStrategy;
|
||||
|
||||
namespace ProjectBomber.Generics
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для хранения коллекции
|
||||
/// </summary>
|
||||
internal class PlanesGenericStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, PlanesGenericCollection<DrawningBomber,
|
||||
DrawningObjectBomber>> _planeStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _planeStorages.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 PlanesGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_planeStorages = new Dictionary<string,
|
||||
PlanesGenericCollection<DrawningBomber, DrawningObjectBomber>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
// Создаем новый набор и добавляем его в словарь
|
||||
if (!_planeStorages.ContainsKey(name))
|
||||
{
|
||||
_planeStorages[name] = new PlanesGenericCollection<DrawningBomber, DrawningObjectBomber>(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Набор с таким именем уже существует");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void DelSet(string name)
|
||||
{
|
||||
// Удаляем набор из словаря по имени
|
||||
if (_planeStorages.ContainsKey(name))
|
||||
{
|
||||
_planeStorages.Remove(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Набор с таким именем не найден.");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public PlanesGenericCollection<DrawningBomber, DrawningObjectBomber> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_planeStorages.ContainsKey(ind))
|
||||
{
|
||||
return _planeStorages[ind];
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new KeyNotFoundException($"Набор с именем '{ind}' не найден.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -85,6 +85,7 @@
|
||||
<Compile Include="MoveToCenter.cs" />
|
||||
<Compile Include="ObjectParameters.cs" />
|
||||
<Compile Include="PlanesGenericCollection.cs" />
|
||||
<Compile Include="PlanesGenericStorage.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SetGeneric.cs" />
|
||||
|
@ -14,84 +14,83 @@ namespace ProjectBomber.Generics
|
||||
where T : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Массив объектов, которые храним
|
||||
/// Список объектов, которые храним
|
||||
/// </summary>
|
||||
private readonly T[] _places;
|
||||
private readonly List<T> _places;
|
||||
/// <summary>
|
||||
/// Количество объектов в массиве
|
||||
/// Количество объектов в списке
|
||||
/// </summary>
|
||||
public int Count => _places.Length;
|
||||
public int Count => _places.Count;
|
||||
/// <summary>
|
||||
/// Максимальное количество объектов в списке
|
||||
/// </summary>
|
||||
private readonly int _maxCount;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>(count);
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
/// </summary>
|
||||
/// <param name="plane">Добавляемый самолет</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T plane)
|
||||
public bool Insert(T plane)
|
||||
{
|
||||
int index = -1;
|
||||
for (int i = 0; i < _places.Length; i++)
|
||||
if (_places.Count == 0)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
_places.Add(plane);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_places.Count < _maxCount)
|
||||
{
|
||||
index = i; break;
|
||||
_places.Add(plane);
|
||||
for (int i = 0; i < _places.Count; i++)
|
||||
{
|
||||
T temp = _places[i];
|
||||
_places[i] = _places[_places.Count - 1];
|
||||
_places[_places.Count - 1] = temp;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (index < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
for (int i = index; i > 0; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[0] = plane;
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// </summary>
|
||||
/// <param name="plane">Добавляемый автомобиль</param>
|
||||
/// <param name="plane">Добавляемый самолет</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T plane, int position)
|
||||
public bool Insert(T plane, int position)
|
||||
{
|
||||
///Проверка позиции
|
||||
if (position < 0 || position >= _places.Length)
|
||||
return -1;
|
||||
/// Проверка, что элемент массива по этой позиции пустой, если нет, то проверка,
|
||||
/// что после вставляемого элемента в массиве есть пустой элемент
|
||||
if (_places[position] != null)
|
||||
if (position < 0 || position >= _maxCount)
|
||||
{
|
||||
/// Проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||
int firstEmptyPosition = -1;
|
||||
for (int i = position + 1; i < _places.Length; i++)
|
||||
return false;
|
||||
}
|
||||
if (position < _places.Count && _places[position] == null)
|
||||
{
|
||||
_places[position] = plane;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ищем первую пустую позицию и вставляем туда.
|
||||
for (int i = 0; i < _maxCount; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
firstEmptyPosition = i;
|
||||
_places[i] = plane;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// Нет пустой позиции после вставляемой
|
||||
if (firstEmptyPosition == -1)
|
||||
return -1;
|
||||
/// Сдвиг всех объектов, находящихся справа от позиции, до первого пустого элемента
|
||||
for (int i = firstEmptyPosition; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
}
|
||||
/// Вставка по позиции
|
||||
_places[position] = plane;
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
@ -100,10 +99,8 @@ namespace ProjectBomber.Generics
|
||||
/// <returns></returns>
|
||||
public bool Remove(int position)
|
||||
{
|
||||
/// Проверка позиции
|
||||
if (position < 0 || position >= _places.Length)
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return false;
|
||||
/// Удаление объекта из массива, присвоив элементу массива значение null
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
@ -112,12 +109,35 @@ namespace ProjectBomber.Generics
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
public T this[int position]
|
||||
{
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= _places.Length)
|
||||
return null;
|
||||
return _places[position];
|
||||
get
|
||||
{
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position < 0 || position >= _maxCount || _places.Count >= _maxCount)
|
||||
return; // Неправильная позиция или нет места для вставки
|
||||
_places[position] = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Проход по списку
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<T> GetPlanes(int? maxPlanes = null)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; ++i)
|
||||
{
|
||||
yield return _places[i];
|
||||
if (maxPlanes.HasValue && i == maxPlanes.Value)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user