diff --git a/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs index a4477a6..f6617f0 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.buttonRemoveAirBomber = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddAirBomber = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonDown); this.groupBox1.Controls.Add(this.buttonLeft); @@ -55,21 +62,83 @@ this.groupBox1.Controls.Add(this.buttonRemoveAirBomber); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.buttonAddAirBomber); - this.groupBox1.Controls.Add(this.comboBoxSelectorMap); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(600, 0); + this.groupBox1.Location = new System.Drawing.Point(1034, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 450); + this.groupBox1.Size = new System.Drawing.Size(200, 764); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(22, 32); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(150, 326); + this.groupBoxMaps.TabIndex = 11; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 15; + this.listBoxMaps.Location = new System.Drawing.Point(0, 155); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(150, 94); + this.listBoxMaps.TabIndex = 5; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(0, 273); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(150, 34); + this.buttonDeleteMap.TabIndex = 4; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(0, 102); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(150, 34); + this.buttonAddMap.TabIndex = 3; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(0, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(150, 23); + this.textBoxNewMapName.TabIndex = 2; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Вторая карта", + "Третья карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(0, 60); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(150, 23); + this.comboBoxSelectorMap.TabIndex = 1; + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(116, 404); + this.buttonRight.Location = new System.Drawing.Point(116, 718); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -81,7 +150,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(80, 404); + this.buttonDown.Location = new System.Drawing.Point(80, 718); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -93,7 +162,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(44, 404); + this.buttonLeft.Location = new System.Drawing.Point(44, 718); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; @@ -105,7 +174,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(80, 368); + this.buttonUp.Location = new System.Drawing.Point(80, 682); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -114,7 +183,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(22, 319); + this.buttonShowOnMap.Location = new System.Drawing.Point(22, 602); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(150, 33); this.buttonShowOnMap.TabIndex = 5; @@ -124,7 +193,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(22, 258); + this.buttonShowStorage.Location = new System.Drawing.Point(22, 560); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(150, 36); this.buttonShowStorage.TabIndex = 4; @@ -134,7 +203,7 @@ // // buttonRemoveAirBomber // - this.buttonRemoveAirBomber.Location = new System.Drawing.Point(22, 195); + this.buttonRemoveAirBomber.Location = new System.Drawing.Point(22, 504); this.buttonRemoveAirBomber.Name = "buttonRemoveAirBomber"; this.buttonRemoveAirBomber.Size = new System.Drawing.Size(150, 36); this.buttonRemoveAirBomber.TabIndex = 3; @@ -144,7 +213,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 166); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 475); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(150, 23); @@ -152,7 +221,7 @@ // // buttonAddAirBomber // - this.buttonAddAirBomber.Location = new System.Drawing.Point(22, 94); + this.buttonAddAirBomber.Location = new System.Drawing.Point(22, 422); this.buttonAddAirBomber.Name = "buttonAddAirBomber"; this.buttonAddAirBomber.Size = new System.Drawing.Size(150, 35); this.buttonAddAirBomber.TabIndex = 1; @@ -160,25 +229,12 @@ this.buttonAddAirBomber.UseVisualStyleBackColor = true; this.buttonAddAirBomber.Click += new System.EventHandler(this.ButtonAddAirBomber_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Вторая карта", - "Третья карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(22, 44); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(150, 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(600, 450); + this.pictureBox.Size = new System.Drawing.Size(1034, 764); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -186,13 +242,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1234, 764); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetAirBomber"; - this.Text = "FormMapWithSetAirBomber"; + this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -203,7 +261,6 @@ private GroupBox groupBox1; private MaskedTextBox maskedTextBoxPosition; private Button buttonAddAirBomber; - private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; private Button buttonShowOnMap; private Button buttonShowStorage; @@ -212,5 +269,11 @@ private Button buttonDown; private Button buttonLeft; private Button buttonUp; + private GroupBox groupBoxMaps; + private TextBox textBoxNewMapName; + private ComboBox comboBoxSelectorMap; + private ListBox listBoxMaps; + private Button buttonDeleteMap; + private Button buttonAddMap; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs index 5344495..d2b9515 100644 --- a/AirBomber/AirBomber/FormMapWithSetAirBomber.cs +++ b/AirBomber/AirBomber/FormMapWithSetAirBomber.cs @@ -14,14 +14,49 @@ namespace AirBomber { private MapWithSetAirBomberGeneric _mapAirBomberCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Вторая карта", new SecondMap() }, + { "Третья карта", new ThirdMap() } + }; + + private readonly MapsCollection _mapsCollection; + public FormMapWithSetAirBomber() { InitializeComponent(); - } + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } + } + + 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 ButtonAddAirBomber_Click(object sender, EventArgs e) { - if (_mapAirBomberCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -29,10 +64,10 @@ namespace AirBomber if (form.ShowDialog() == DialogResult.OK) { DrawningObjectBomber airBomber = new(form.SelectedAirBomber); - if (_mapAirBomberCollectionGeneric + airBomber > -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airBomber > -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAirBomberCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -43,6 +78,10 @@ namespace AirBomber private void ButtonRemoveAirBomber_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -52,10 +91,10 @@ namespace AirBomber return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapAirBomberCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapAirBomberCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -65,50 +104,25 @@ namespace AirBomber private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapAirBomberCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirBomberCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapAirBomberCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirBomberCollectionGeneric.ShowOnMap(); - } - - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Вторая карта": - map = new SecondMap(); - break; - case "Третья карта": - map = new ThirdMap(); - break; - } - if (map != null) - { - _mapAirBomberCollectionGeneric = new MapWithSetAirBomberGeneric(pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapAirBomberCollectionGeneric = null; - } + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapAirBomberCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -129,7 +143,42 @@ namespace AirBomber dir = Direction.Right; break; } - pictureBox.Image = _mapAirBomberCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + + 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(); + } + } } } diff --git a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs index 6b7ecbf..70295c7 100644 --- a/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/MapWithSetAirBomberGeneric.cs @@ -49,20 +49,16 @@ namespace AirBomber public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setAirBomber.Count; i++) + foreach (var airBomber in _setAirBomber.GetAirBomber()) { - var airBomber = _setAirBomber.Get(i); - if (airBomber != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber); } return new(_pictureWidth, _pictureHeight); } public Bitmap MoveObject(Direction direction) { - if ( _map != null) + if (_map != null) { return _map.MoveObject(direction); } @@ -74,11 +70,11 @@ namespace AirBomber int j = _setAirBomber.Count - 1; for (int i = 0; i < _setAirBomber.Count; i++) { - if (_setAirBomber.Get(i) == null) + if (_setAirBomber[i] == null) { for (; j > i; j--) { - var airBomber = _setAirBomber.Get(j); + var airBomber = _setAirBomber[j]; if (airBomber != null) { _setAirBomber.Insert(airBomber, i); @@ -112,13 +108,15 @@ namespace AirBomber private void DrawAirBomber(Graphics g) { - for (int i = 0; i < _setAirBomber.Count; ++i) + int i = 0; + foreach (var airBomber in _setAirBomber.GetAirBomber()) { - if (_setAirBomber.Get(i) != null) + if (airBomber != null) { - _setAirBomber.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setAirBomber.Get(i).DrawningObject(g); + _setAirBomber[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight, _pictureWidth, _pictureHeight); + airBomber.DrawningObject(g); } + i++; } } } diff --git a/AirBomber/AirBomber/MapsCollection.cs b/AirBomber/AirBomber/MapsCollection.cs new file mode 100644 index 0000000..cb7ca87 --- /dev/null +++ b/AirBomber/AirBomber/MapsCollection.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorages; + public List Keys => _mapStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddMap(string name, AbstractMap map) + { + if (_mapStorages.ContainsKey(name)) + return; + _mapStorages.Add(name, new MapWithSetAirBomberGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(string name) + { + if (!_mapStorages.ContainsKey(name)) + return; + _mapStorages.Remove(name); + } + + public MapWithSetAirBomberGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind]; + return null; + } + } + } +} diff --git a/AirBomber/AirBomber/SetAirBomberGeneric.cs b/AirBomber/AirBomber/SetAirBomberGeneric.cs index cb41a83..db6bec4 100644 --- a/AirBomber/AirBomber/SetAirBomberGeneric.cs +++ b/AirBomber/AirBomber/SetAirBomberGeneric.cs @@ -9,13 +9,16 @@ namespace AirBomber internal class SetAirBomberGeneric where T : class { - private readonly T[] _places; + private readonly List _places; - public int Count => _places.Length; + public int Count => _places.Count; + + private readonly int _maxCount; public SetAirBomberGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T airBomber) @@ -25,48 +28,17 @@ namespace AirBomber public int Insert(T airBomber, int position) { - if (position < 0 || position >= Count) - { - return -1; - } - int emptyPos = -1; - if (_places[position] == null) - { - _places[position] = airBomber; - return position; - } - else - { - for (int i = position + 1; i < Count; ++i) - { - if (_places[i] == null) - { - emptyPos = i; - break; - } - } - if (emptyPos != -1) - { - for (int i = emptyPos; i > position; --i) - { - _places[i] = _places[i - 1]; - } - } - } - if (emptyPos != -1) - { - _places[position] = airBomber; - return position; - } - else + if (position < 0 || position > _maxCount) { return -1; } + _places.Insert(position, airBomber); + return position; } public T Remove(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position > _maxCount) { return null; } @@ -75,13 +47,35 @@ namespace AirBomber return elem; } - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) + get { - return null; + if (position < 0 || position > _maxCount) + { + return null; + } + return _places[position]; + } + set + { + Insert(value, position); + } + } + + public IEnumerable GetAirBomber() + { + foreach (var airBomber in _places) + { + if (airBomber != null) + { + yield return airBomber; + } + else + { + yield break; + } } - return _places[position]; } } }