Borschevskaya A.A. Lab Work 4 #4

Merged
eegov merged 7 commits from lab4 into lab3 2022-10-28 09:18:07 +04:00
5 changed files with 316 additions and 113 deletions

View File

@ -2,7 +2,7 @@
namespace ArmoredCar
{
partial class FormMapWithSetArmoredCars
partial class FormMapWithSetArmoredCars
{
/// <summary>
/// Required designer variable.
@ -31,6 +31,12 @@ namespace ArmoredCar
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonAddMap = new System.Windows.Forms.Button();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonRemoveCar = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
@ -40,14 +46,15 @@ namespace ArmoredCar
this.buttonUp = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonAddCar = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRemoveCar);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
@ -57,18 +64,79 @@ namespace ArmoredCar
this.groupBoxTools.Controls.Add(this.buttonUp);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonAddCar);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(811, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(204, 554);
this.groupBoxTools.Size = new System.Drawing.Size(204, 632);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(6, 22);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(192, 248);
this.groupBoxMaps.TabIndex = 0;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(11, 80);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(175, 35);
this.buttonAddMap.TabIndex = 2;
this.buttonAddMap.Text = "Добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(11, 206);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(175, 35);
this.buttonDeleteMap.TabIndex = 4;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(11, 121);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(175, 79);
this.listBoxMaps.TabIndex = 3;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(11, 22);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(175, 23);
this.textBoxNewMapName.TabIndex = 0;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(11, 51);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23);
this.comboBoxSelectorMap.TabIndex = 1;
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 166);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 355);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -77,7 +145,7 @@ namespace ArmoredCar
//
// buttonRemoveCar
//
this.buttonRemoveCar.Location = new System.Drawing.Point(17, 195);
this.buttonRemoveCar.Location = new System.Drawing.Point(17, 384);
this.buttonRemoveCar.Name = "buttonRemoveCar";
this.buttonRemoveCar.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveCar.TabIndex = 3;
@ -87,7 +155,7 @@ namespace ArmoredCar
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(17, 287);
this.buttonShowStorage.Location = new System.Drawing.Point(17, 437);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 4;
@ -100,7 +168,7 @@ namespace ArmoredCar
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ArmoredCar.Properties.Resources.Down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(91, 504);
this.buttonDown.Location = new System.Drawing.Point(91, 582);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 10;
@ -112,7 +180,7 @@ namespace ArmoredCar
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ArmoredCar.Properties.Resources.Right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(127, 504);
this.buttonRight.Location = new System.Drawing.Point(127, 582);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 9;
@ -124,7 +192,7 @@ namespace ArmoredCar
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ArmoredCar.Properties.Resources.Left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(55, 504);
this.buttonLeft.Location = new System.Drawing.Point(55, 582);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8;
@ -136,7 +204,7 @@ namespace ArmoredCar
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ArmoredCar.Properties.Resources.Up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(91, 468);
this.buttonUp.Location = new System.Drawing.Point(91, 546);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7;
@ -145,7 +213,7 @@ namespace ArmoredCar
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 391);
this.buttonShowOnMap.Location = new System.Drawing.Point(17, 487);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 5;
@ -155,7 +223,7 @@ namespace ArmoredCar
//
// buttonAddCar
//
this.buttonAddCar.Location = new System.Drawing.Point(17, 106);
this.buttonAddCar.Location = new System.Drawing.Point(17, 314);
this.buttonAddCar.Name = "buttonAddCar";
this.buttonAddCar.Size = new System.Drawing.Size(175, 35);
this.buttonAddCar.TabIndex = 1;
@ -163,25 +231,12 @@ namespace ArmoredCar
this.buttonAddCar.UseVisualStyleBackColor = true;
this.buttonAddCar.Click += new System.EventHandler(this.ButtonAddarmoredCar_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Карта 1"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(17, 32);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23);
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(811, 554);
this.pictureBox.Size = new System.Drawing.Size(811, 632);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -189,13 +244,15 @@ namespace ArmoredCar
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1015, 554);
this.ClientSize = new System.Drawing.Size(1015, 632);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetArmoredCars";
this.Text = "Карта с набором объектов";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
@ -215,5 +272,10 @@ namespace ArmoredCar
private Button buttonShowStorage;
private Button buttonRemoveCar;
private MaskedTextBox maskedTextBoxPosition;
private GroupBox groupBoxMaps;
private Button buttonDeleteMap;
private ListBox listBoxMaps;
private TextBox textBoxNewMapName;
private Button buttonAddMap;
}
}

View File

@ -1,4 +1,4 @@
using ArmoredCar;
using System.Collections.Generic;
using System.Windows.Forms;
using System;
@ -7,41 +7,99 @@ namespace ArmoredCar
public partial class FormMapWithSetArmoredCars : Form
{
/// <summary>
/// Объект от класса карты с набором объектов
/// Словарь для выпадающего списка
/// </summary>
private MapWithSetArmoredCarsGeneric<DrawningObjectArmCar, AbstractMap> _mapArmoredCarsCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() },
{ "Карта 1", new MyMapLabirinth() },
{ "Карта 2", new MyMapWooden() }
};
/// <summary>
/// Объект от коллекции карт
/// </summary>
private readonly MapsCollection _mapsCollection;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetArmoredCars()
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
/// <summary>
/// Заполнение listBoxMaps
/// </summary>
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
{
listBoxMaps.SelectedIndex = 0;
}
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count)
{
listBoxMaps.SelectedIndex = index;
}
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
if (listBoxMaps.SelectedIndex == -1)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Карта 1":
map = new MyMapLabirinth();
break;
return;
}
if (map != null)
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapArmoredCarsCollectionGeneric = new MapWithSetArmoredCarsGeneric<DrawningObjectArmCar, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapArmoredCarsCollectionGeneric = null;
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
}
}
/// <summary>
@ -51,7 +109,7 @@ namespace ArmoredCar
/// <param name="e"></param>
private void ButtonAddarmoredCar_Click(object sender, EventArgs e)
{
if (_mapArmoredCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -59,10 +117,10 @@ namespace ArmoredCar
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectArmCar armoredCar = new(form.SelectedCar);
if (_mapArmoredCarsCollectionGeneric + armoredCar > -1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + armoredCar > -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapArmoredCarsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -77,6 +135,10 @@ namespace ArmoredCar
/// <param name="e"></param>
private void ButtonRemoveArmoredCar_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
@ -86,10 +148,10 @@ namespace ArmoredCar
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapArmoredCarsCollectionGeneric - pos != null)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapArmoredCarsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -103,11 +165,11 @@ namespace ArmoredCar
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapArmoredCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapArmoredCarsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Вывод карты
@ -116,11 +178,11 @@ namespace ArmoredCar
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapArmoredCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapArmoredCarsCollectionGeneric.ShowOnMap();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
/// <summary>
/// Перемещение
@ -129,11 +191,10 @@ namespace ArmoredCar
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapArmoredCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
@ -152,7 +213,7 @@ namespace ArmoredCar
dir = Direction.Right;
break;
}
pictureBox.Image = _mapArmoredCarsCollectionGeneric.MoveObject(dir);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
}
}

View File

@ -90,13 +90,9 @@ namespace ArmoredCar
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setCars.Count; i++)
foreach (var armoredCar in _setCars.GetCars())
{
var car = _setCars.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar);
}
return new(_pictureWidth, _pictureHeight);
}
@ -121,14 +117,14 @@ namespace ArmoredCar
int j = _setCars.Count - 1;
for (int i = 0; i < _setCars.Count; i++)
{
if (_setCars.Get(i) == null)
if (_setCars[i] == null)
{
for (; j > i; j--)
{
var car = _setCars.Get(j);
if (car != null)
var armoredCar = _setCars[i];
if (armoredCar != null)
{
_setCars.Insert(car, i);
_setCars.Insert(armoredCar, i);
_setCars.Remove(j);
break;
}
@ -177,14 +173,13 @@ namespace ArmoredCar
private void DrawArmoredCars(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _setCars.Count; i++)
{
if (_setCars.Get(i) != null) {
_setCars.Get(i)?.SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setCars.Get(i)?.DrawningObject(g);
}
}
int k = 0;
foreach (var armoredCar in _setCars.GetCars())
{
armoredCar.SetObject(k % width * _placeSizeWidth + 5, k / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
armoredCar.DrawningObject(g);
k++;
}
}
}
}

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArmoredCar
{
internal class MapsCollection
{
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetArmoredCarsGeneric<DrawningObjectArmCar, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.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 MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetArmoredCarsGeneric<DrawningObjectArmCar, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
if (!Keys.Contains(name))
_mapStorages.Add(name, new MapWithSetArmoredCarsGeneric<DrawningObjectArmCar, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
if (Keys.Contains(name))
_mapStorages.Remove(name);
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetArmoredCarsGeneric<DrawningObjectArmCar, AbstractMap> this[string
ind]
{
get
{
if (Keys.Contains(ind))
return _mapStorages[ind];
return null;
}
}
}
}

View File

@ -10,20 +10,22 @@ namespace ArmoredCar
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;
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetArmoredCarsGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
/// <summary>
/// Добавление объекта в набор
@ -31,8 +33,10 @@ namespace ArmoredCar
/// <param name="armoredCar">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T armoredCar)
{
return Insert(armoredCar, 0);
{
if (Count < _maxCount)
return Insert(armoredCar, 0);
return -1;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -42,31 +46,10 @@ namespace ArmoredCar
/// <returns></returns>
public int Insert(T armoredCar, int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position >= _maxCount)
return -1;
if (!(_places[position] == null))
{
int index_empty = -1;
// поиск первого пустого элемента
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
index_empty = i;
}
}
if (index_empty == -1)
return -1;
else
{
for (int i = index_empty; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
}
_places[position] = armoredCar;
_places.Insert(position, armoredCar);
return position;
}
/// <summary>
@ -76,10 +59,10 @@ namespace ArmoredCar
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= Count)
if (position < 0 || position >= Count)
return null;
T armoredCar = _places[position];
_places[position] = null;
_places.RemoveAt(position);
return armoredCar;
}
/// <summary>
@ -87,11 +70,41 @@ namespace ArmoredCar
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= Count)
return null;
return _places[position];
get
{
if (position < 0 || position >= Count)
return null;
return _places[position];
}
set
{
if (position < 0 || position >= _maxCount)
return;
_places.Add(value);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetCars()
{
foreach (var car in _places)
{
if (car != null)
{
yield return car;
}
else
{
yield break;
}
}
}
}
}