Логика доведена до рабочего состояния

This commit is contained in:
Oleg 2022-10-20 20:24:25 +04:00
parent 9d9893d6ff
commit fcf8c4df4e
8 changed files with 259 additions and 182 deletions

@ -47,8 +47,13 @@ namespace Trolleybus
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreate_Click(object sender, EventArgs e)
{ {
Random rnd = new Random(); Random random = new();
_trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); Color myColor = new Color();
ColorDialog MyDialog = new ColorDialog();
if (MyDialog.ShowDialog() == DialogResult.OK)
myColor = MyDialog.Color;
_trolleybus = new DrawingTrolleybus(random.Next(30, 50), random.Next(1000, 2000),
myColor);
SetData(); SetData();
Draw(); Draw();
} }
@ -82,11 +87,20 @@ namespace Trolleybus
private void buttonCreateModif_Click(object sender, EventArgs e) private void buttonCreateModif_Click(object sender, EventArgs e)
{ {
Random rnd = new Random(); Random random = new();
_trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color firstColor = new Color();
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color secondColor = new Color();
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), ColorDialog MyDialog = new ColorDialog();
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); if (MyDialog.ShowDialog() == DialogResult.OK)
firstColor = MyDialog.Color;
MyDialog = new ColorDialog();
if (MyDialog.ShowDialog() == DialogResult.OK)
secondColor = MyDialog.Color;
_trolleybus = new DrawningSmallTrolleybus(random.Next(100, 300), random.Next(1000, 2000),
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)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
SetData(); SetData();
Draw(); Draw();
} }

@ -29,17 +29,17 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonAddTrolleybus = new System.Windows.Forms.Button();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonRemoveTrolleybus = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveTrolleybus = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddTrolleybus = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
@ -64,85 +64,16 @@
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты"; this.groupBox1.Text = "Инструменты";
// //
// comboBoxSelectorMap // buttonLeft
// //
this.comboBoxSelectorMap.FormattingEnabled = true; this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30;
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 42); this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.buttonLeft.Location = new System.Drawing.Point(59, 402);
this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 21); this.buttonLeft.Name = "buttonLeft";
this.comboBoxSelectorMap.TabIndex = 0; this.buttonLeft.Size = new System.Drawing.Size(32, 31);
// this.buttonLeft.TabIndex = 9;
// buttonAddTrolleybus this.buttonLeft.UseVisualStyleBackColor = true;
// this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
this.buttonAddTrolleybus.Location = new System.Drawing.Point(7, 85);
this.buttonAddTrolleybus.Name = "buttonAddTrolleybus";
this.buttonAddTrolleybus.Size = new System.Drawing.Size(181, 23);
this.buttonAddTrolleybus.TabIndex = 1;
this.buttonAddTrolleybus.Text = "Добавить троллейбус";
this.buttonAddTrolleybus.UseVisualStyleBackColor = true;
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 128);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(181, 20);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonRemoveTrolleybus
//
this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(6, 170);
this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus";
this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(182, 23);
this.buttonRemoveTrolleybus.TabIndex = 3;
this.buttonRemoveTrolleybus.Text = "Удалить троллейбус";
this.buttonRemoveTrolleybus.UseVisualStyleBackColor = true;
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(7, 216);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(181, 23);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(7, 256);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(181, 23);
this.buttonShowOnMap.TabIndex = 5;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
//
// buttonUp
//
this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(97, 365);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(32, 31);
this.buttonUp.TabIndex = 6;
this.buttonUp.UseVisualStyleBackColor = true;
//
// buttonDown
//
this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(97, 402);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(32, 31);
this.buttonDown.TabIndex = 7;
this.buttonDown.UseVisualStyleBackColor = true;
// //
// buttonRight // buttonRight
// //
@ -153,16 +84,97 @@
this.buttonRight.Size = new System.Drawing.Size(32, 31); this.buttonRight.Size = new System.Drawing.Size(32, 31);
this.buttonRight.TabIndex = 8; this.buttonRight.TabIndex = 8;
this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// buttonLeft // buttonDown
// //
this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30; this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(59, 402); this.buttonDown.Location = new System.Drawing.Point(97, 402);
this.buttonLeft.Name = "buttonLeft"; this.buttonDown.Name = "buttonDown";
this.buttonLeft.Size = new System.Drawing.Size(32, 31); this.buttonDown.Size = new System.Drawing.Size(32, 31);
this.buttonLeft.TabIndex = 9; this.buttonDown.TabIndex = 7;
this.buttonLeft.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(97, 365);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(32, 31);
this.buttonUp.TabIndex = 6;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(7, 256);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(181, 23);
this.buttonShowOnMap.TabIndex = 5;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(7, 216);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(181, 23);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonRemoveTrolleybus
//
this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(6, 170);
this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus";
this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(182, 23);
this.buttonRemoveTrolleybus.TabIndex = 3;
this.buttonRemoveTrolleybus.Text = "Удалить троллейбус";
this.buttonRemoveTrolleybus.UseVisualStyleBackColor = true;
this.buttonRemoveTrolleybus.Click += new System.EventHandler(this.ButtonRemoveTrolleybus_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 128);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(181, 20);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonAddTrolleybus
//
this.buttonAddTrolleybus.Location = new System.Drawing.Point(7, 85);
this.buttonAddTrolleybus.Name = "buttonAddTrolleybus";
this.buttonAddTrolleybus.Size = new System.Drawing.Size(181, 23);
this.buttonAddTrolleybus.TabIndex = 1;
this.buttonAddTrolleybus.Text = "Добавить троллейбус";
this.buttonAddTrolleybus.UseVisualStyleBackColor = true;
this.buttonAddTrolleybus.Click += new System.EventHandler(this.ButtonAddTrolleybus_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Сложная карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 42);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 21);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
// //
// FormMapWithSetTrolleybus // FormMapWithSetTrolleybus
// //

@ -12,11 +12,11 @@ namespace Trolleybus
{ {
public partial class FormMapWithSetTrolleybus : Form public partial class FormMapWithSetTrolleybus : Form
{ {
private MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap> _mapCarsCollectionGeneric;
public FormMapWithSetTrolleybus() public FormMapWithSetTrolleybus()
{ {
InitializeComponent(); InitializeComponent();
} }
private MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap> _mapTrolleybusCollectionGeneric;
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{ {
AbstractMap map = null; AbstractMap map = null;
@ -25,36 +25,34 @@ namespace Trolleybus
case "Простая карта": case "Простая карта":
map = new SimpleMap(); map = new SimpleMap();
break; break;
case "Сложная карта":
map = new HardMap();
break;
} }
if (map != null) if (map != null)
{ {
_mapCarsCollectionGeneric = new MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap>( _mapTrolleybusCollectionGeneric = new MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap>(
pictureBox.Width, pictureBox.Height, map); pictureBox.Width, pictureBox.Height, map);
} }
else else
{ {
_mapCarsCollectionGeneric = null; _mapTrolleybusCollectionGeneric = null;
} }
} }
/// <summary> private void ButtonAddTrolleybus_Click(object sender, EventArgs e)
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddCar_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapTrolleybusCollectionGeneric == null)
{ {
return; return;
} }
Form1 form = new(); Form1 form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawningObjectTrolleybus car = new(form.SelectedTrolleybus); DrawningObjectTrolleybus trolleybus = new(form.SelectedTrolleybus);
if (_mapCarsCollectionGeneric + car) if (_mapTrolleybusCollectionGeneric + trolleybus !=-1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet();
} }
else else
{ {
@ -67,7 +65,7 @@ namespace Trolleybus
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveCar_Click(object sender, EventArgs e) private void ButtonRemoveTrolleybus_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{ {
@ -78,10 +76,10 @@ namespace Trolleybus
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapCarsCollectionGeneric - pos) if (_mapTrolleybusCollectionGeneric - pos !=null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet();
} }
else else
{ {
@ -95,11 +93,11 @@ namespace Trolleybus
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e) private void ButtonShowStorage_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapTrolleybusCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet(); pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet();
} }
/// <summary> /// <summary>
/// Вывод карты /// Вывод карты
@ -108,11 +106,11 @@ namespace Trolleybus
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e) private void ButtonShowOnMap_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapTrolleybusCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap(); pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowOnMap();
} }
/// <summary> /// <summary>
/// Перемещение /// Перемещение
@ -121,7 +119,7 @@ namespace Trolleybus
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_mapCarsCollectionGeneric == null) if (_mapTrolleybusCollectionGeneric == null)
{ {
return; return;
} }
@ -143,7 +141,7 @@ namespace Trolleybus
dir = Direction.Right; dir = Direction.Right;
break; break;
} }
pictureBox.Image = _mapCarsCollectionGeneric.MoveObject(dir); pictureBox.Image = _mapTrolleybusCollectionGeneric.MoveObject(dir);
} }
} }
} }

@ -0,0 +1,30 @@
using System.Threading;
using Trolleybus;
namespace Trolleybus
{
internal class HardMap : SimpleMap
{
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
_map[i, j] = _barrier;
}
}
}
}
}

@ -54,11 +54,11 @@ namespace Trolleybus
/// Перегрузка оператора сложения /// Перегрузка оператора сложения
/// </summary> /// </summary>
/// <param name="map"></param> /// <param name="map"></param>
/// <param name="car"></param> /// <param name="trolleybus"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator +(MapWithSetTrolleybusGeneric<T, U> map, T car) public static int operator +(MapWithSetTrolleybusGeneric<T, U> map, T trolleybus)
{ {
return map._setTrolleybus.Insert(car); return map._setTrolleybus.Insert(trolleybus);
} }
/// <summary> /// <summary>
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
@ -66,7 +66,7 @@ namespace Trolleybus
/// <param name="map"></param> /// <param name="map"></param>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public static bool operator -(MapWithSetTrolleybusGeneric<T, U> map, int position) public static T operator -(MapWithSetTrolleybusGeneric<T, U> map, int position)
{ {
return map._setTrolleybus.Remove(position); return map._setTrolleybus.Remove(position);
} }
@ -79,7 +79,7 @@ namespace Trolleybus
Bitmap bmp = new(_pictureWidth, _pictureHeight); Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr); DrawBackground(gr);
DrawCars(gr); DrawTrolleybus(gr);
return bmp; return bmp;
} }
/// <summary> /// <summary>
@ -159,12 +159,29 @@ namespace Trolleybus
/// Метод прорисовки объектов /// Метод прорисовки объектов
/// </summary> /// </summary>
/// <param name="g"></param> /// <param name="g"></param>
private void DrawCars(Graphics g) private void DrawTrolleybus(Graphics g)
{ {
for (int i = 0; i < _setTrolleybus.Count; i++)
{ {
// TODO установка позиции int xForLocomotive = 2;
_setTrolleybus.Get(i)?.DrawningObject(g); int yForLocomotive = 10;
int countInRow = 0;
for (int i = 0; i < _setTrolleybus.Count; i++)
{
if (countInRow >= _pictureWidth / (_placeSizeWidth + 30))
{
xForLocomotive = 2;
yForLocomotive += _placeSizeHeight;
countInRow = 0;
}
if (_setTrolleybus.Get(i) != null)
{
T locomotive = _setTrolleybus.Get(i);
locomotive.SetObject(xForLocomotive, yForLocomotive, _pictureWidth, _pictureHeight);
locomotive.DrawningObject(g);
}
xForLocomotive += _placeSizeWidth + 30;
countInRow++;
}
} }
} }
} }

@ -16,7 +16,7 @@ namespace Trolleybus
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMap()); Application.Run(new FormMapWithSetTrolleybus());
} }
} }
} }

@ -6,71 +6,76 @@ using System.Threading.Tasks;
namespace Trolleybus namespace Trolleybus
{ {
// Параметризованный набор объектов
internal class SetTrolleybusGeneric<T> internal class SetTrolleybusGeneric<T>
where T : class where T : class
{ {
/// <summary> // Массив объектов, которые храним
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places; private readonly T[] _places;
/// <summary> // Количество объектов в массиве
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length; public int Count => _places.Length;
/// <summary> // Конструктор
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetTrolleybusGeneric(int count) public SetTrolleybusGeneric(int count)
{ {
_places = new T[count]; _places = new T[count];
} }
/// <summary> // Добавление объекта в набор
/// Добавление объекта в набор public int Insert(T trolleybus)
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public bool Insert(T trolleybus)
{ {
// TODO вставка в начало набора // вставка в начало набора
return true; return Insert(trolleybus, 0);
} }
/// <summary> // Добавление объекта в набор на конкретную позицию
/// Добавление объекта в набор на конкретную позицию public int Insert(T trolleybus, int position)
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T trolleybus, int position)
{ {
// TODO проверка позиции // проверка позиции
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то if (position >= _places.Length || position < 0)
// проверка, что после вставляемого элемента в массиве есть пустой элемент return -1;
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента //проверка, что элемент массива по этой позиции пустой, если нет, то
// TODO вставка по позиции if (_places[position] == null)
{
_places[position] = trolleybus;
return position;
}
//проверка, что после вставляемого элемента в массиве есть пустой элемент
int findEmptyPos = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
findEmptyPos = i;
break;
}
}
if (findEmptyPos < 0) return -1;
//сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
for (int i = findEmptyPos; i > position; i--)
{
_places[i] = _places[i - 1];
}
// вставка по позиции
_places[position] = trolleybus; _places[position] = trolleybus;
return true; return position;
} }
/// <summary> // Удаление объекта из набора с конкретной позиции
/// Удаление объекта из набора с конкретной позиции public T Remove(int position)
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{ {
// TODO проверка позиции // проверка позиции
// TODO удаление объекта из массива, присовив элементу массива значение null if (position >= _places.Length || position < 0) return null;
return true; // удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places[position] = null;
return temp;
} }
/// <summary> // Получение объекта из набора по позиции
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position) public T Get(int position)
{ {
// TODO проверка позиции // проверка позиции
if (position >= _places.Length || position < 0)
return null;
return _places[position]; return _places[position];
} }
} }
} }

@ -73,6 +73,7 @@
<Compile Include="FormMapWithSetTrolleybus.Designer.cs"> <Compile Include="FormMapWithSetTrolleybus.Designer.cs">
<DependentUpon>FormMapWithSetTrolleybus.cs</DependentUpon> <DependentUpon>FormMapWithSetTrolleybus.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="HardMap.cs" />
<Compile Include="IDrawingObject.cs" /> <Compile Include="IDrawingObject.cs" />
<Compile Include="MapWithSetTrolleybusGeneric.cs" /> <Compile Include="MapWithSetTrolleybusGeneric.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />