diff --git a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs index 89100c3..151ca24 100644 --- a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs +++ b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.Designer.cs @@ -29,29 +29,35 @@ private void InitializeComponent() { this.toolsGroupBox = new System.Windows.Forms.GroupBox(); + this.mapsGroupBox = new System.Windows.Forms.GroupBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonRemoveArtillery = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddArtillery = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.pictureBoxArtilleries = new System.Windows.Forms.PictureBox(); this.toolsGroupBox.SuspendLayout(); + this.mapsGroupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxArtilleries)).BeginInit(); this.SuspendLayout(); // // toolsGroupBox // + this.toolsGroupBox.Controls.Add(this.mapsGroupBox); this.toolsGroupBox.Controls.Add(this.buttonShowOnMap); this.toolsGroupBox.Controls.Add(this.buttonShowStorage); this.toolsGroupBox.Controls.Add(this.buttonRemoveArtillery); this.toolsGroupBox.Controls.Add(this.maskedTextBoxPosition); this.toolsGroupBox.Controls.Add(this.buttonAddArtillery); - this.toolsGroupBox.Controls.Add(this.comboBoxSelectorMap); this.toolsGroupBox.Controls.Add(this.buttonRight); this.toolsGroupBox.Controls.Add(this.buttonDown); this.toolsGroupBox.Controls.Add(this.buttonUp); @@ -59,14 +65,77 @@ this.toolsGroupBox.Dock = System.Windows.Forms.DockStyle.Right; this.toolsGroupBox.Location = new System.Drawing.Point(600, 0); this.toolsGroupBox.Name = "toolsGroupBox"; - this.toolsGroupBox.Size = new System.Drawing.Size(200, 450); + this.toolsGroupBox.Size = new System.Drawing.Size(200, 596); this.toolsGroupBox.TabIndex = 0; this.toolsGroupBox.TabStop = false; this.toolsGroupBox.Text = "Инструменты"; // + // mapsGroupBox + // + this.mapsGroupBox.Controls.Add(this.buttonDeleteMap); + this.mapsGroupBox.Controls.Add(this.listBoxMaps); + this.mapsGroupBox.Controls.Add(this.buttonAddMap); + this.mapsGroupBox.Controls.Add(this.textBoxNewMapName); + this.mapsGroupBox.Controls.Add(this.comboBoxSelectorMap); + this.mapsGroupBox.Location = new System.Drawing.Point(6, 22); + this.mapsGroupBox.Name = "mapsGroupBox"; + this.mapsGroupBox.Size = new System.Drawing.Size(188, 258); + this.mapsGroupBox.TabIndex = 25; + this.mapsGroupBox.TabStop = false; + this.mapsGroupBox.Text = "Карты"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(12, 218); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(170, 32); + this.buttonDeleteMap.TabIndex = 26; + 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(12, 118); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(170, 94); + this.listBoxMaps.TabIndex = 27; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(12, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(170, 32); + this.buttonAddMap.TabIndex = 26; + 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, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(170, 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(12, 51); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(170, 23); + this.comboBoxSelectorMap.TabIndex = 19; + // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(13, 315); + this.buttonShowOnMap.Location = new System.Drawing.Point(13, 470); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 32); this.buttonShowOnMap.TabIndex = 24; @@ -76,7 +145,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(13, 249); + this.buttonShowStorage.Location = new System.Drawing.Point(13, 423); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 32); this.buttonShowStorage.TabIndex = 23; @@ -86,7 +155,7 @@ // // buttonRemoveArtillery // - this.buttonRemoveArtillery.Location = new System.Drawing.Point(13, 177); + this.buttonRemoveArtillery.Location = new System.Drawing.Point(13, 376); this.buttonRemoveArtillery.Name = "buttonRemoveArtillery"; this.buttonRemoveArtillery.Size = new System.Drawing.Size(175, 32); this.buttonRemoveArtillery.TabIndex = 22; @@ -96,7 +165,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 148); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 347); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -104,7 +173,7 @@ // // buttonAddArtillery // - this.buttonAddArtillery.Location = new System.Drawing.Point(13, 83); + this.buttonAddArtillery.Location = new System.Drawing.Point(13, 309); this.buttonAddArtillery.Name = "buttonAddArtillery"; this.buttonAddArtillery.Size = new System.Drawing.Size(175, 32); this.buttonAddArtillery.TabIndex = 20; @@ -112,25 +181,12 @@ this.buttonAddArtillery.UseVisualStyleBackColor = true; this.buttonAddArtillery.Click += new System.EventHandler(this.buttonAddArtillery_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(13, 22); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); - this.comboBoxSelectorMap.TabIndex = 19; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged); - // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(124, 399); + this.buttonRight.Location = new System.Drawing.Point(124, 545); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 18; @@ -142,7 +198,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(88, 399); + this.buttonDown.Location = new System.Drawing.Point(88, 545); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 17; @@ -154,7 +210,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(88, 366); + this.buttonUp.Location = new System.Drawing.Point(88, 512); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 16; @@ -166,7 +222,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::SelfPropelledArtilleryUnit.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(54, 399); + this.buttonLeft.Location = new System.Drawing.Point(54, 545); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 15; @@ -178,7 +234,7 @@ this.pictureBoxArtilleries.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxArtilleries.Location = new System.Drawing.Point(0, 0); this.pictureBoxArtilleries.Name = "pictureBoxArtilleries"; - this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 450); + this.pictureBoxArtilleries.Size = new System.Drawing.Size(600, 596); this.pictureBoxArtilleries.TabIndex = 1; this.pictureBoxArtilleries.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(800, 596); this.Controls.Add(this.pictureBoxArtilleries); this.Controls.Add(this.toolsGroupBox); this.Name = "FormMapWithSetArtilleries"; this.Text = "Artillery"; this.toolsGroupBox.ResumeLayout(false); this.toolsGroupBox.PerformLayout(); + this.mapsGroupBox.ResumeLayout(false); + this.mapsGroupBox.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxArtilleries)).EndInit(); this.ResumeLayout(false); @@ -212,5 +270,10 @@ private Button buttonRemoveArtillery; private MaskedTextBox maskedTextBoxPosition; private Button buttonAddArtillery; + private GroupBox mapsGroupBox; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs index 47f71e5..a2696c8 100644 --- a/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs +++ b/SelfPropelledArtilleryUnit/FormMapWithSetArtilleries.cs @@ -13,49 +13,93 @@ namespace Artilleries { public partial class FormMapWithSetArtilleries : Form { - private MapWithSetArtilleriesGeneric _mapArtilleriesCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Лесная карта", new ForestMap() } + }; + + private readonly MapsCollection _mapsCollection; public FormMapWithSetArtilleries() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBoxArtilleries.Width, pictureBoxArtilleries.Height); + comboBoxSelectorMap.Items.Clear(); + foreach(var element in _mapsDict) + { + comboBoxSelectorMap.Items.Add(element.Key); + } } - private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + + listBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Лесная карта": - map = new ForestMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - _mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric(pictureBoxArtilleries.Width, pictureBoxArtilleries.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapArtilleriesCollectionGeneric = null; + 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) + { + pictureBoxArtilleries.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 buttonAddArtillery_Click(object sender, EventArgs e) { - if (_mapArtilleriesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } FormArtillery form = new(); if (form.ShowDialog() == DialogResult.OK) { - DrawingObjectArtillery car = new(form.SelectedArtillery); - if (_mapArtilleriesCollectionGeneric + car != -1) + DrawingObjectArtillery artillery = new(form.SelectedArtillery); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + artillery != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.ShowSet(); + pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -66,6 +110,10 @@ namespace Artilleries private void buttonRemoveArtillery_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -75,10 +123,10 @@ namespace Artilleries return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapArtilleriesCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.ShowSet(); + pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -88,25 +136,25 @@ namespace Artilleries private void buttonShowStorage_Click(object sender, EventArgs e) { - if (_mapArtilleriesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.ShowSet(); + pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void buttonShowOnMap_Click(object sender, EventArgs e) { - if (_mapArtilleriesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.ShowOnMap(); + pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void buttonMove_Click(object sender, EventArgs e) { - if (_mapArtilleriesCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -128,7 +176,7 @@ namespace Artilleries dir = Direction.Right; break; } - pictureBoxArtilleries.Image = _mapArtilleriesCollectionGeneric.MoveObject(dir); + pictureBoxArtilleries.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } } diff --git a/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs index 4c03bfe..29bd440 100644 --- a/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/MapWithSetArtilleriesGeneric.cs @@ -49,13 +49,9 @@ namespace Artilleries public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setArtilleries.Count; i++) + foreach (var artillery in _setArtilleries.GetArtilleries()) { - var car = _setArtilleries.Get(i); - if (car != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, artillery); } return new(_pictureWidth, _pictureHeight); } @@ -74,11 +70,11 @@ namespace Artilleries int j = _setArtilleries.Count - 1; for (int i = 0; i < _setArtilleries.Count; i++) { - if (_setArtilleries.Get(i) == null) + if (_setArtilleries[i] == null) { for (; j > i; j--) { - var car = _setArtilleries.Get(j); + var car = _setArtilleries[j]; if (car != null) { _setArtilleries.Insert(car, i); @@ -120,14 +116,12 @@ namespace Artilleries int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _setArtilleries.Count; i++) + int index = 0; + foreach (var artillery in _setArtilleries.GetArtilleries()) { - var artillery = _setArtilleries.Get(i); - if (artillery != null) - { - artillery.SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - artillery.DrawingObject(g); - } + artillery.SetObject(index % width * _placeSizeWidth + 10, (height - 1 - index / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + artillery.DrawingObject(g); + index++; } } } diff --git a/SelfPropelledArtilleryUnit/MapsCollection.cs b/SelfPropelledArtilleryUnit/MapsCollection.cs new file mode 100644 index 0000000..cdd47ac --- /dev/null +++ b/SelfPropelledArtilleryUnit/MapsCollection.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Artilleries +{ + internal class MapsCollection + { + readonly Dictionary> _mapsStorage; + public List Keys => _mapsStorage.Keys.ToList(); + + private readonly int _pictureWidth; + private readonly int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapsStorage = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddMap(string name, AbstractMap map) + { + if (!_mapsStorage.ContainsKey(name)) + { + _mapsStorage.Add(name, new MapWithSetArtilleriesGeneric(_pictureWidth, _pictureHeight, map)); + } + } + + public void DelMap(string name) + { + if (_mapsStorage.ContainsKey(name)) + { + _mapsStorage.Remove(name); + } + } + + public MapWithSetArtilleriesGeneric this[string index] + { + get + { + return _mapsStorage.ContainsKey(index) ? _mapsStorage[index] : null; + } + } + } +} diff --git a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs index 35c69f5..1f60477 100644 --- a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs @@ -9,12 +9,14 @@ namespace Artilleries internal class SetArtilleriesGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; public SetArtilleriesGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T artillery) @@ -24,39 +26,13 @@ namespace Artilleries public int Insert(T artillery, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position > Count || Count == _maxCount) { return -1; } - if (_places[position] == null) - { - _places[position] = artillery; - return position; - } + _places.Insert(position, artillery); - int firstNull = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - firstNull = i; - break; - } - } - - if (firstNull == -1) - { - return -1; - } - - for (int i = firstNull; i > position; i--) - { - _places[i] = _places[i - 1]; - } - - _places[position] = artillery; return position; } @@ -69,18 +45,43 @@ namespace Artilleries var result = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return result; } - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) + get { - return null; - } + if (position < 0 || position >= Count) + { + return null; + } - return _places[position]; + return _places[position]; + } + set + { + if (position >= 0 && position < Count) + { + Insert(value, position); + } + } + } + + public IEnumerable GetArtilleries() + { + foreach (var artillery in _places) + { + if (artillery != null) + { + yield return artillery; + } + else + { + yield break; + } + } } } }