From 656f12f9a9dae423c4ce6841a18702a8dbade8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Tue, 10 Oct 2023 14:57:21 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=203?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectMonorail/DrawingMonorail.cs | 10 ++ .../ProjectMonorail/FormMonorail.Designer.cs | 14 ++ .../ProjectMonorail/FormMonorail.cs | 62 +++++-- .../FormMonorailCollection.Designer.cs | 149 +++++++++++++++++ .../ProjectMonorail/FormMonorailCollection.cs | 83 ++++++++++ .../FormMonorailCollection.resx | 120 ++++++++++++++ .../MonorailsGenericCollection.cs | 155 ++++++++++++++++++ ProjectMonorail/ProjectMonorail/Program.cs | 2 +- ProjectMonorail/ProjectMonorail/SetGeneric.cs | 113 +++++++++++++ 9 files changed, 694 insertions(+), 14 deletions(-) create mode 100644 ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs create mode 100644 ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs create mode 100644 ProjectMonorail/ProjectMonorail/FormMonorailCollection.resx create mode 100644 ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs create mode 100644 ProjectMonorail/ProjectMonorail/SetGeneric.cs diff --git a/ProjectMonorail/ProjectMonorail/DrawingMonorail.cs b/ProjectMonorail/ProjectMonorail/DrawingMonorail.cs index d5a2e5f..91d934b 100644 --- a/ProjectMonorail/ProjectMonorail/DrawingMonorail.cs +++ b/ProjectMonorail/ProjectMonorail/DrawingMonorail.cs @@ -1,4 +1,5 @@ using ProjectMonorail.Entities; +using ProjectMonorail.MovementStrategy; namespace ProjectMonorail.DrawingObjects { @@ -62,6 +63,11 @@ namespace ProjectMonorail.DrawingObjects /// public int GetHeight => _monorailHeight; + /// + /// Получение объекта IMoveableObject из объекта DrawingMonorail + /// + public IMoveableObject GetMoveableObject => new DrawingObjectMonorail(this); + /// /// Конструктор /// @@ -188,6 +194,10 @@ namespace ProjectMonorail.DrawingObjects Brush brWhite = new SolidBrush(Color.White); Brush brGray = new SolidBrush(Color.Gray); + //надстройка + g.FillRectangle(mainBrush, _startPosX + 55, _startPosY, 25, 15); + g.DrawRectangle(mainPen, _startPosX + 55, _startPosY, 25, 15); + //корпус локомотива Point[] locoPoints = { new Point(_startPosX + 29, _startPosY + 15), new Point(_startPosX + 112, _startPosY + 15), new Point(_startPosX + 112, _startPosY + 46), new Point(_startPosX + 25, _startPosY + 46), new Point(_startPosX + 25, _startPosY + 31) }; diff --git a/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs b/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs index 081b234..c2f4786 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorail.Designer.cs @@ -37,6 +37,7 @@ comboBoxStrategy = new ComboBox(); buttonCreateMonorail = new Button(); buttonStep = new Button(); + buttonSelectMonorail = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).BeginInit(); SuspendLayout(); // @@ -142,11 +143,23 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += buttonStep_Click; // + // buttonSelectMonorail + // + buttonSelectMonorail.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonSelectMonorail.Location = new Point(324, 403); + buttonSelectMonorail.Name = "buttonSelectMonorail"; + buttonSelectMonorail.Size = new Size(140, 39); + buttonSelectMonorail.TabIndex = 9; + buttonSelectMonorail.Text = "Select"; + buttonSelectMonorail.UseVisualStyleBackColor = true; + buttonSelectMonorail.Click += buttonSelectMonorail_Click; + // // FormMonorail // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonSelectMonorail); Controls.Add(buttonStep); Controls.Add(buttonCreateMonorail); Controls.Add(comboBoxStrategy); @@ -175,5 +188,6 @@ private ComboBox comboBoxStrategy; private Button buttonCreateMonorail; private Button buttonStep; + private Button buttonSelectMonorail; } } \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail/FormMonorail.cs b/ProjectMonorail/ProjectMonorail/FormMonorail.cs index 2e84ddb..1806841 100644 --- a/ProjectMonorail/ProjectMonorail/FormMonorail.cs +++ b/ProjectMonorail/ProjectMonorail/FormMonorail.cs @@ -16,7 +16,12 @@ namespace ProjectMonorail /// /// Стратегия перемещения /// - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + + /// + /// Выбранный монорельс + /// + public DrawingMonorail? SelectedMonorail { get; private set; } /// /// Инициализация формы @@ -24,6 +29,8 @@ namespace ProjectMonorail public FormMonorail() { InitializeComponent(); + _strategy = null; + SelectedMonorail = null; } /// @@ -49,8 +56,21 @@ namespace ProjectMonorail private void buttonCreateExtendedMonorail_Click(object sender, EventArgs e) { Random random = new(); - _drawingMonorail = new DrawingExtendedMonorail(random.Next(200, 400), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), - random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + + Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog additionalDialog = new(); + if (additionalDialog.ShowDialog() == DialogResult.OK) + { + additionalColor = additionalDialog.Color; + } + + _drawingMonorail = new DrawingExtendedMonorail(random.Next(200, 400), random.Next(1000, 3000), color, additionalColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxMonorail.Width, pictureBoxMonorail.Height); _drawingMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -64,8 +84,13 @@ namespace ProjectMonorail private void buttonCreateMonorail_Click(object sender, EventArgs e) { Random random = new(); - _drawingMonorail = new DrawingMonorail(random.Next(200, 400), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), pictureBoxMonorail.Width, pictureBoxMonorail.Height); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _drawingMonorail = new DrawingMonorail(random.Next(200, 400), random.Next(1000, 3000), color, pictureBoxMonorail.Width, pictureBoxMonorail.Height); _drawingMonorail.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -113,31 +138,42 @@ namespace ProjectMonorail } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new DrawingObjectMonorail(_drawingMonorail), pictureBoxMonorail.Width, pictureBoxMonorail.Height); - comboBoxStrategy.Enabled = false; + _strategy.SetData(new DrawingObjectMonorail(_drawingMonorail), pictureBoxMonorail.Width, pictureBoxMonorail.Height); } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } + + /// + /// Выбор монорельса + /// + /// + /// + private void buttonSelectMonorail_Click(object sender, EventArgs e) + { + SelectedMonorail = _drawingMonorail; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs new file mode 100644 index 0000000..e2b0474 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.Designer.cs @@ -0,0 +1,149 @@ +namespace ProjectMonorail +{ + partial class FormMonorailCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + panelTools = new Panel(); + buttonRefreshCollection = new Button(); + labelName = new Label(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRemoveMonorail = new Button(); + buttonAddMonorail = new Button(); + pictureBoxCollection = new PictureBox(); + panelTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // panelTools + // + panelTools.Anchor = AnchorStyles.Top | AnchorStyles.Right; + panelTools.BorderStyle = BorderStyle.FixedSingle; + panelTools.Controls.Add(buttonRefreshCollection); + panelTools.Controls.Add(labelName); + panelTools.Controls.Add(maskedTextBoxNumber); + panelTools.Controls.Add(buttonRemoveMonorail); + panelTools.Controls.Add(buttonAddMonorail); + panelTools.Location = new Point(788, 10); + panelTools.Name = "panelTools"; + panelTools.Size = new Size(186, 430); + panelTools.TabIndex = 0; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRefreshCollection.BackColor = SystemColors.Window; + buttonRefreshCollection.FlatAppearance.BorderColor = Color.Black; + buttonRefreshCollection.FlatStyle = FlatStyle.Flat; + buttonRefreshCollection.Location = new Point(18, 281); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(155, 34); + buttonRefreshCollection.TabIndex = 3; + buttonRefreshCollection.Text = "Refresh collection"; + buttonRefreshCollection.UseVisualStyleBackColor = false; + buttonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // labelName + // + labelName.AutoSize = true; + labelName.Font = new Font("Segoe UI Semibold", 12F, FontStyle.Bold, GraphicsUnit.Point); + labelName.Location = new Point(17, -2); + labelName.Name = "labelName"; + labelName.Size = new Size(48, 21); + labelName.TabIndex = 0; + labelName.Text = "Tools"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.BorderStyle = BorderStyle.FixedSingle; + maskedTextBoxNumber.Location = new Point(17, 144); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(155, 23); + maskedTextBoxNumber.TabIndex = 2; + // + // buttonRemoveMonorail + // + buttonRemoveMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRemoveMonorail.BackColor = SystemColors.Window; + buttonRemoveMonorail.FlatAppearance.BorderColor = Color.Black; + buttonRemoveMonorail.FlatStyle = FlatStyle.Flat; + buttonRemoveMonorail.Location = new Point(18, 173); + buttonRemoveMonorail.Name = "buttonRemoveMonorail"; + buttonRemoveMonorail.Size = new Size(155, 34); + buttonRemoveMonorail.TabIndex = 1; + buttonRemoveMonorail.Text = "Remove monorail"; + buttonRemoveMonorail.UseVisualStyleBackColor = false; + buttonRemoveMonorail.Click += buttonRemoveMonorail_Click; + // + // buttonAddMonorail + // + buttonAddMonorail.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonAddMonorail.BackColor = SystemColors.Window; + buttonAddMonorail.FlatAppearance.BorderColor = Color.Black; + buttonAddMonorail.FlatStyle = FlatStyle.Flat; + buttonAddMonorail.Location = new Point(18, 33); + buttonAddMonorail.Name = "buttonAddMonorail"; + buttonAddMonorail.Size = new Size(155, 34); + buttonAddMonorail.TabIndex = 0; + buttonAddMonorail.Text = "Add monorail"; + buttonAddMonorail.UseVisualStyleBackColor = false; + buttonAddMonorail.Click += buttonAddMonorail_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(5, 10); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(777, 430); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormMonorailCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(974, 439); + Controls.Add(pictureBoxCollection); + Controls.Add(panelTools); + Name = "FormMonorailCollection"; + Text = "Monorail collection"; + panelTools.ResumeLayout(false); + panelTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panelTools; + private Label labelName; + private Button buttonAddMonorail; + private Button buttonRemoveMonorail; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonRefreshCollection; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs new file mode 100644 index 0000000..e395aa5 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.cs @@ -0,0 +1,83 @@ +using ProjectMonorail.Generics; +using ProjectMonorail.MovementStrategy; +using ProjectMonorail.DrawingObjects; + +namespace ProjectMonorail +{ + /// + /// Форма для работы с набором объектов класса DrawingMonorail + /// + public partial class FormMonorailCollection : Form + { + /// + /// Набор объектов + /// + private readonly MonorailsGenericCollection _monorails; + + /// + /// Конструктор + /// + public FormMonorailCollection() + { + InitializeComponent(); + _monorails = new MonorailsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddMonorail_Click(object sender, EventArgs e) + { + FormMonorail form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_monorails + form.SelectedMonorail != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _monorails.ShowMonorails(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + /// + /// Удаление объекта из набора + /// + /// + /// + private void buttonRemoveMonorail_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + + if (_monorails - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _monorails.ShowMonorails(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + /// + /// Обновление рисунка по набору + /// + /// + /// + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _monorails.ShowMonorails(); + } + } +} diff --git a/ProjectMonorail/ProjectMonorail/FormMonorailCollection.resx b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/FormMonorailCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs b/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs new file mode 100644 index 0000000..791d935 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/MonorailsGenericCollection.cs @@ -0,0 +1,155 @@ +using ProjectMonorail.DrawingObjects; +using ProjectMonorail.MovementStrategy; + +namespace ProjectMonorail.Generics +{ + /// + /// Параметризованный класс для набора объектов + /// + /// + /// + internal class MonorailsGenericCollection + where T : DrawingMonorail + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 193; + + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 102; + + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + + /// + /// Конструктор + /// + /// + /// + public MonorailsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(MonorailsGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MonorailsGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowMonorails() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { //линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, + _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + T? obj; + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + int diff = 1, currWidth = 0; + for (int i = 0; i < _collection.Count; i++) + { + currWidth++; + if (currWidth > width) + { + diff++; + currWidth = 1; + } + obj = _collection.Get(i); + if (obj != null) + { + obj.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40, + (height - diff) * _placeSizeHeight + _placeSizeHeight / 15); + obj.DrawTransport(g); + } + } + } + } +} diff --git a/ProjectMonorail/ProjectMonorail/Program.cs b/ProjectMonorail/ProjectMonorail/Program.cs index 3f68a06..beba8fb 100644 --- a/ProjectMonorail/ProjectMonorail/Program.cs +++ b/ProjectMonorail/ProjectMonorail/Program.cs @@ -11,7 +11,7 @@ namespace ProjectMonorail // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMonorail()); + Application.Run(new FormMonorailCollection()); } } } \ No newline at end of file diff --git a/ProjectMonorail/ProjectMonorail/SetGeneric.cs b/ProjectMonorail/ProjectMonorail/SetGeneric.cs new file mode 100644 index 0000000..91fb691 --- /dev/null +++ b/ProjectMonorail/ProjectMonorail/SetGeneric.cs @@ -0,0 +1,113 @@ +namespace ProjectMonorail.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + + /// + /// Добавление объекта в набор + /// + /// Добавляемый монорельс + /// + public int Insert(T monorail) + { + int nullIndex = -1, i; + for (i = 0; i < Count; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + if (nullIndex < 0) + return -1; + + for (i = nullIndex; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = monorail; + return 0; + } + + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый монорельс + /// Позиция + /// + public int Insert(T monorail, int position) + { + int nullIndex = -1, i; + + if (position < 0 || position >= Count) + return -1; + + 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] = monorail; + return position; + } + + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= Count) + return false; + _places[position] = null; + return true; + } + + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= Count) + return null; + return _places[position]; + } + } +}