diff --git a/Liner/Liner/FormMapWithSetShips.Designer.cs b/Liner/Liner/FormMapWithSetShips.Designer.cs index b2e1d3e..064deb1 100644 --- a/Liner/Liner/FormMapWithSetShips.Designer.cs +++ b/Liner/Liner/FormMapWithSetShips.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBox = 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.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.buttonUp = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.ButtonRemoveShip = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.ButtonAddShip = new System.Windows.Forms.Button(); - this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox.SuspendLayout(); + this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox // + this.groupBox.Controls.Add(this.groupBox1); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonUp); @@ -55,21 +62,84 @@ this.groupBox.Controls.Add(this.ButtonRemoveShip); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.ButtonAddShip); - this.groupBox.Controls.Add(this.ComboBoxSelectorMap); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(627, 0); + this.groupBox.Location = new System.Drawing.Point(696, 0); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(250, 450); + this.groupBox.Size = new System.Drawing.Size(279, 668); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // groupBox1 + // + this.groupBox1.Controls.Add(this.ButtonDeleteMap); + this.groupBox1.Controls.Add(this.ListBoxMaps); + this.groupBox1.Controls.Add(this.ButtonAddMap); + this.groupBox1.Controls.Add(this.textBoxNewMapName); + this.groupBox1.Controls.Add(this.ComboBoxSelectorMap); + this.groupBox1.Location = new System.Drawing.Point(6, 26); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(250, 300); + this.groupBox1.TabIndex = 18; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(10, 255); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(222, 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(10, 136); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(222, 104); + this.ListBoxMaps.TabIndex = 12; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(10, 101); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(222, 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(10, 34); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(222, 27); + this.textBoxNewMapName.TabIndex = 10; + // + // 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(10, 67); + this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(222, 28); + this.ComboBoxSelectorMap.TabIndex = 9; + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Liner.Properties.Resources._3; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(204, 408); + this.buttonRight.Location = new System.Drawing.Point(233, 626); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 17; @@ -81,7 +151,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Liner.Properties.Resources._2; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(168, 408); + this.buttonDown.Location = new System.Drawing.Point(197, 626); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 16; @@ -93,7 +163,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Liner.Properties.Resources._4; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(168, 372); + this.buttonUp.Location = new System.Drawing.Point(197, 590); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 15; @@ -105,7 +175,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Liner.Properties.Resources._1; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(132, 408); + this.buttonLeft.Location = new System.Drawing.Point(161, 626); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 14; @@ -114,9 +184,9 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(28, 323); + this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 558); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; - this.ButtonShowOnMap.Size = new System.Drawing.Size(200, 29); + this.ButtonShowOnMap.Size = new System.Drawing.Size(222, 29); this.ButtonShowOnMap.TabIndex = 13; this.ButtonShowOnMap.Text = "Посмотреть карту"; this.ButtonShowOnMap.UseVisualStyleBackColor = true; @@ -124,9 +194,9 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(28, 258); + this.ButtonShowStorage.Location = new System.Drawing.Point(16, 505); this.ButtonShowStorage.Name = "ButtonShowStorage"; - this.ButtonShowStorage.Size = new System.Drawing.Size(200, 29); + this.ButtonShowStorage.Size = new System.Drawing.Size(222, 29); this.ButtonShowStorage.TabIndex = 12; this.ButtonShowStorage.Text = "Посмотреть хранилище"; this.ButtonShowStorage.UseVisualStyleBackColor = true; @@ -134,9 +204,9 @@ // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(28, 191); + this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 452); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; - this.ButtonRemoveShip.Size = new System.Drawing.Size(200, 29); + this.ButtonRemoveShip.Size = new System.Drawing.Size(222, 29); this.ButtonRemoveShip.TabIndex = 11; this.ButtonRemoveShip.Text = "Удалить корабль"; this.ButtonRemoveShip.UseVisualStyleBackColor = true; @@ -144,42 +214,28 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 158); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 419); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(200, 27); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(222, 27); this.maskedTextBoxPosition.TabIndex = 10; // // ButtonAddShip // - this.ButtonAddShip.Location = new System.Drawing.Point(28, 110); + this.ButtonAddShip.Location = new System.Drawing.Point(16, 368); this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(200, 29); + this.ButtonAddShip.Size = new System.Drawing.Size(222, 29); this.ButtonAddShip.TabIndex = 2; this.ButtonAddShip.Text = "Добавить корабль"; this.ButtonAddShip.UseVisualStyleBackColor = true; this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_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(28, 38); - this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; - this.ComboBoxSelectorMap.Size = new System.Drawing.Size(200, 28); - this.ComboBoxSelectorMap.TabIndex = 9; - 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(627, 450); + this.pictureBox.Size = new System.Drawing.Size(696, 668); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -187,13 +243,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(877, 450); + this.ClientSize = new System.Drawing.Size(975, 668); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetShips"; this.Text = "FormMapWithSetShips"; this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -213,5 +271,10 @@ private Button buttonDown; private Button buttonUp; private Button buttonLeft; + private GroupBox groupBox1; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Liner/Liner/FormMapWithSetShips.cs b/Liner/Liner/FormMapWithSetShips.cs index 500b974..340096c 100644 --- a/Liner/Liner/FormMapWithSetShips.cs +++ b/Liner/Liner/FormMapWithSetShips.cs @@ -13,39 +13,45 @@ namespace Liner { public partial class FormMapWithSetShips : Form { - private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Море и айсберги", new SeaMap() }, + {"Болото",new SwampMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetShips() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + ComboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + ComboBoxSelectorMap.Items.Add(elem.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 SeaMap(); - break; - case "Болото": - map = new SwampMap(); - break; + ListBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + + if (ListBoxMaps.Items.Count > 0 && (index == -1 || index >= ListBoxMaps.Items.Count)) { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric( - pictureBox.Width, pictureBox.Height, map); + ListBoxMaps.SelectedIndex = 0; } - else + else if (ListBoxMaps.Items.Count > 0 && index > -1 && index < ListBoxMaps.Items.Count) { - _mapShipsCollectionGeneric = null; + ListBoxMaps.SelectedIndex = index; } } private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -53,10 +59,10 @@ namespace Liner if (form.ShowDialog() == DialogResult.OK) { DrawingObjectShip ship = new(form.SelectedShip); - if (_mapShipsCollectionGeneric + ship != -1) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -66,6 +72,10 @@ namespace Liner } private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -75,10 +85,10 @@ namespace Liner return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapShipsCollectionGeneric - pos != null) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -87,23 +97,23 @@ namespace Liner } private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -124,7 +134,38 @@ namespace Liner dir = Direction.Right; break; } - pictureBox.Image = _mapShipsCollectionGeneric.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 (!_mapDict.ContainsKey(ComboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[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/Liner/Liner/MapWithSetShipsGeneric.cs b/Liner/Liner/MapWithSetShipsGeneric.cs index b8e16b9..5d9931f 100644 --- a/Liner/Liner/MapWithSetShipsGeneric.cs +++ b/Liner/Liner/MapWithSetShipsGeneric.cs @@ -44,13 +44,9 @@ namespace Liner public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - var ship = _setShips.Get(i); - if (ship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, ship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } return new(_pictureWidth, _pictureHeight); } @@ -67,11 +63,11 @@ namespace Liner int j = _setShips.Count - 1; for (int i = 0; i < _setShips.Count; i++) { - if (_setShips.Get(i) == null) + if (_setShips[i] == null) { for (; j > i; j--) { - var ship = _setShips.Get(j); + var ship = _setShips[j]; if (ship != null) { _setShips.Insert(ship, i); @@ -107,11 +103,11 @@ namespace Liner int heightEl = _pictureHeight / _placeSizeHeight; int curWidth = 1; int curHeight = 0; - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - _setShips.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - (_pictureWidth / 7), + ship?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 10, curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setShips.Get(i)?.DrawningObject(g); + ship?.DrawningObject(g); if (curWidth < widthEl) curWidth++; diff --git a/Liner/Liner/MapsCollection.cs b/Liner/Liner/MapsCollection.cs new file mode 100644 index 0000000..5dd14d7 --- /dev/null +++ b/Liner/Liner/MapsCollection.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner +{ + 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)) + { + _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); + } + public MapWithSetShipsGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +} diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 0f4b0f0..6e6c3b5 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -9,11 +9,13 @@ namespace Liner internal class SetShipsGeneric 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 SetShipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T ship) { @@ -21,52 +23,53 @@ namespace Liner } public int Insert(T ship, int position) { - if (position >= _places.Length || position < 0) - { - return -1; - } - if (_places[position] == null) - { - _places[position] = ship; - return position; - } - int emptyIndex = -1; - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - emptyIndex = i; - break; - } - } - if (emptyIndex < 0) - { - return -1; - } - for (int i = emptyIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = ship; + if (position < 0 || position > Count || _maxCount == Count) return -1; + _places.Insert(position, ship); return position; } public T Remove(int position) { - if (position >= _places.Length || position < 0) + if (position >= Count || position < 0) { return null; } T ship = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return ship; } - public T Get(int position) + public T this[int position] { - if (position >= _places.Length || position < 0) + get { - return null; + if (position >= Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position >= Count || position < 0) + { + return; + } + Insert(value, position); + + } + } + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } } } \ No newline at end of file