Karamushko M.K lab4_base #6

Closed
maxKarme wants to merge 2 commits from lab4_base into lab3_base
5 changed files with 326 additions and 107 deletions
Showing only changes of commit dfdc51e414 - Show all commits

View File

@ -29,6 +29,12 @@
private void InitializeComponent()
{
this.tools = new System.Windows.Forms.GroupBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.RightButton = new System.Windows.Forms.Button();
this.LeftButton = new System.Windows.Forms.Button();
this.UpButton = new System.Windows.Forms.Button();
@ -38,14 +44,15 @@
this.ButtonShowOnMap = new System.Windows.Forms.Button();
this.ButtonShowStorage = new System.Windows.Forms.Button();
this.buttonAdd = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.tools.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// tools
//
this.tools.Controls.Add(this.groupBox1);
this.tools.Controls.Add(this.RightButton);
this.tools.Controls.Add(this.LeftButton);
this.tools.Controls.Add(this.UpButton);
@ -55,21 +62,83 @@
this.tools.Controls.Add(this.ButtonShowOnMap);
this.tools.Controls.Add(this.ButtonShowStorage);
this.tools.Controls.Add(this.buttonAdd);
this.tools.Controls.Add(this.comboBoxSelectorMap);
this.tools.Dock = System.Windows.Forms.DockStyle.Right;
this.tools.Location = new System.Drawing.Point(839, 0);
this.tools.Location = new System.Drawing.Point(940, 0);
this.tools.Name = "tools";
this.tools.Size = new System.Drawing.Size(250, 597);
this.tools.Size = new System.Drawing.Size(250, 725);
this.tools.TabIndex = 0;
this.tools.TabStop = false;
this.tools.Text = "Инструменты";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonDeleteMap);
this.groupBox1.Controls.Add(this.listBoxMaps);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Controls.Add(this.buttonAddMap);
this.groupBox1.Controls.Add(this.textBoxNewMapName);
this.groupBox1.Location = new System.Drawing.Point(12, 33);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(226, 286);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "карты";
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(12, 243);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(196, 29);
this.buttonDeleteMap.TabIndex = 13;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 20;
this.listBoxMaps.Location = new System.Drawing.Point(12, 133);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(196, 104);
this.listBoxMaps.TabIndex = 12;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// 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(12, 59);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(196, 28);
this.comboBoxSelectorMap.TabIndex = 0;
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(12, 93);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(196, 29);
this.buttonAddMap.TabIndex = 11;
this.buttonAddMap.Text = "Добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(12, 26);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(196, 27);
this.textBoxNewMapName.TabIndex = 0;
//
// RightButton
//
this.RightButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.RightButton.BackgroundImage = global::AirFighter.Properties.Resources.right;
this.RightButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.RightButton.Location = new System.Drawing.Point(148, 555);
this.RightButton.Location = new System.Drawing.Point(148, 683);
this.RightButton.Name = "RightButton";
this.RightButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.RightButton.Size = new System.Drawing.Size(30, 30);
@ -82,7 +151,7 @@
this.LeftButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.LeftButton.BackgroundImage = global::AirFighter.Properties.Resources.left;
this.LeftButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.LeftButton.Location = new System.Drawing.Point(76, 555);
this.LeftButton.Location = new System.Drawing.Point(76, 683);
this.LeftButton.Name = "LeftButton";
this.LeftButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.LeftButton.Size = new System.Drawing.Size(30, 30);
@ -95,7 +164,7 @@
this.UpButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.UpButton.BackgroundImage = global::AirFighter.Properties.Resources.up;
this.UpButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.UpButton.Location = new System.Drawing.Point(112, 520);
this.UpButton.Location = new System.Drawing.Point(112, 648);
this.UpButton.Name = "UpButton";
this.UpButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.UpButton.Size = new System.Drawing.Size(30, 30);
@ -108,7 +177,7 @@
this.DownButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.DownButton.BackgroundImage = global::AirFighter.Properties.Resources.down;
this.DownButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.DownButton.Location = new System.Drawing.Point(112, 555);
this.DownButton.Location = new System.Drawing.Point(112, 683);
this.DownButton.Name = "DownButton";
this.DownButton.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.DownButton.Size = new System.Drawing.Size(30, 30);
@ -118,17 +187,17 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(3, 145);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 392);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(235, 27);
this.maskedTextBoxPosition.Size = new System.Drawing.Size(196, 27);
this.maskedTextBoxPosition.TabIndex = 5;
//
// buttonRemove
//
this.buttonRemove.Location = new System.Drawing.Point(3, 178);
this.buttonRemove.Location = new System.Drawing.Point(24, 425);
this.buttonRemove.Name = "buttonRemove";
this.buttonRemove.Size = new System.Drawing.Size(235, 29);
this.buttonRemove.Size = new System.Drawing.Size(196, 29);
this.buttonRemove.TabIndex = 4;
this.buttonRemove.Text = "Удалить";
this.buttonRemove.UseVisualStyleBackColor = true;
@ -136,9 +205,9 @@
//
// ButtonShowOnMap
//
this.ButtonShowOnMap.Location = new System.Drawing.Point(3, 313);
this.ButtonShowOnMap.Location = new System.Drawing.Point(24, 520);
this.ButtonShowOnMap.Name = "ButtonShowOnMap";
this.ButtonShowOnMap.Size = new System.Drawing.Size(235, 29);
this.ButtonShowOnMap.Size = new System.Drawing.Size(196, 29);
this.ButtonShowOnMap.TabIndex = 3;
this.ButtonShowOnMap.Text = "Посмотреть карту";
this.ButtonShowOnMap.UseVisualStyleBackColor = true;
@ -146,9 +215,9 @@
//
// ButtonShowStorage
//
this.ButtonShowStorage.Location = new System.Drawing.Point(3, 244);
this.ButtonShowStorage.Location = new System.Drawing.Point(24, 472);
this.ButtonShowStorage.Name = "ButtonShowStorage";
this.ButtonShowStorage.Size = new System.Drawing.Size(235, 29);
this.ButtonShowStorage.Size = new System.Drawing.Size(196, 29);
this.ButtonShowStorage.TabIndex = 2;
this.ButtonShowStorage.Text = "Посмотреть хранилище";
this.ButtonShowStorage.UseVisualStyleBackColor = true;
@ -156,33 +225,20 @@
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(3, 91);
this.buttonAdd.Location = new System.Drawing.Point(24, 357);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(235, 29);
this.buttonAdd.Size = new System.Drawing.Size(196, 29);
this.buttonAdd.TabIndex = 1;
this.buttonAdd.Text = "Добавить";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCar_Click);
//
// 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(3, 23);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(235, 28);
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(839, 597);
this.pictureBox.Size = new System.Drawing.Size(940, 725);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -190,13 +246,15 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1089, 597);
this.ClientSize = new System.Drawing.Size(1190, 725);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.tools);
this.Name = "FormMapWithSetCars";
this.Text = "FormMapWithSetCars";
this.tools.ResumeLayout(false);
this.tools.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
@ -216,5 +274,10 @@
private Button LeftButton;
private Button UpButton;
private Button DownButton;
private GroupBox groupBox1;
private Button buttonDeleteMap;
private ListBox listBoxMaps;
private Button buttonAddMap;
private TextBox textBoxNewMapName;
}
}

View File

@ -12,50 +12,103 @@ namespace AirFighter
{
public partial class FormMapWithSetCars : Form
{
private MapWithSetCarsGeneric<DrawingObjectAirFighter, AbstractMap> _mapCarsCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() },
{ "Моя карта", new MyMap() }
};
/// <summary>
/// Объект от коллекции карт
/// </summary>
private readonly MapsCollection _mapsCollection;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetCars()
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width,
pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Моя карта":
map = new MyMap();
break;
}
if (map != null)
{
_mapCarsCollectionGeneric = new
MapWithSetCarsGeneric<DrawingObjectAirFighter, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapCarsCollectionGeneric = null;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
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;
}
}
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();
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ??
string.Empty);
ReloadMaps();
}
}
private void ButtonAddCar_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -63,10 +116,11 @@ namespace AirFighter
if (form.ShowDialog() == DialogResult.OK)
{
DrawingObjectAirFighter car = new(form.SelectedAirFighter);
if (_mapCarsCollectionGeneric + car != -1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + car != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet();
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -81,6 +135,10 @@ namespace AirFighter
/// <param name="e"></param>
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
@ -91,15 +149,17 @@ namespace AirFighter
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapCarsCollectionGeneric - pos != null)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet();
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
@ -108,11 +168,12 @@ namespace AirFighter
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapCarsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Вывод карты
@ -121,11 +182,11 @@ namespace AirFighter
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapCarsCollectionGeneric.ShowOnMap();
pictureBox.Image =_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
/// <summary>
/// Перемещение
@ -134,7 +195,7 @@ namespace AirFighter
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapCarsCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -156,7 +217,8 @@ namespace AirFighter
dir = Direction.Right;
break;
}
pictureBox.Image = _mapCarsCollectionGeneric.MoveObject(dir);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
}
}

View File

@ -89,13 +89,10 @@ namespace AirFighter
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setCars.Count; i++)
foreach(var car in _setCars.GetCars())
{
var car = _setCars.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return new(_pictureWidth, _pictureHeight);
}
@ -120,11 +117,11 @@ namespace AirFighter
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);
var car = _setCars[j];
if (car != null)
{
_setCars.Insert(car, i);
@ -181,13 +178,17 @@ namespace AirFighter
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setCars.Count; i++)
int i = 0;
foreach(var car in _setCars.GetCars())
{
int x = i % width;
int y = i / width;
_setCars.Get(i)?.SetObject(x * _placeSizeWidth, y * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setCars.Get(i)?.DrawningObject(g);
car.SetObject((width - x - 1) * _placeSizeWidth, (height - y - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
car.DrawningObject(g);
i++;
}
}
}

View File

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetCarsGeneric<DrawingObjectAirFighter, 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,
MapWithSetCarsGeneric<DrawingObjectAirFighter, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
// TODO Прописать логику для добавления
bool check = _mapStorages.TryGetValue(name, out var value);
Review

У словаря есть метод проверки, есть ли запись с таким ключом

У словаря есть метод проверки, есть ли запись с таким ключом
if (check) return;
_mapStorages[name] = new(_pictureWidth, _pictureHeight, map);
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
// TODO Прописать логику для удаления
_mapStorages.Remove(name);
}
/// <summary>
/// Доступ к парковке
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetCarsGeneric<DrawingObjectAirFighter, AbstractMap> this[string ind]
{
get
{
// TODO Продумать логику получения объекта
bool check = _mapStorages.TryGetValue(ind, out var mapWithSet);
return mapWithSet;
}
}
}
}

View File

@ -10,33 +10,29 @@ namespace AirFighter
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 SetCarsGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T car)
{
// TODO вставка в начало набора
for(int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
_places[i] = car;
return i;
}
}
return -1;
if (_places.Count == _maxCount) return -1;
_places.Insert(0, car);
return 0;
}
public int Insert(T car, int position)
{
@ -45,15 +41,8 @@ namespace AirFighter
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
int index = position;
while (_places[index] != null && index < _places.Length) index++;
if (index == _places.Length) return -1;
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
_places[position] = car;
if (_places.Count == _maxCount) return -1;
_places.Insert(position, car);
return position;
}
/// <summary>
@ -66,7 +55,7 @@ namespace AirFighter
// TODO проверка позиции
// TODO удаление объекта из массива, присовив элементу массива значение null
T res = _places[position];
_places[position] = null;
_places.Remove(res);
return res;
}
/// <summary>
@ -74,11 +63,38 @@ namespace AirFighter
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
public T this[int position]
{
// TODO проверка позиции
return _places[position];
get
{
// TODO проверка позиции
if(position < 0) return null;
if(position >= _places.Count) return null;
return _places[position];
}
set
{
// TODO проверка позиции
if (position < 0) return;
if (position >= _places.Count) return;
// TODO вставка в список по позиции
Insert(value, position);
}
}
public IEnumerable<T> GetCars()
{
foreach (var car in _places)
{
if (car != null)
{
yield return car;
}
else
{
yield break;
}
}
}
}
}