diff --git a/Sailboat/FormMapWithSetBoats.Designer.cs b/Sailboat/FormMapWithSetBoats.Designer.cs index 9aaacf6..d05dcce 100644 --- a/Sailboat/FormMapWithSetBoats.Designer.cs +++ b/Sailboat/FormMapWithSetBoats.Designer.cs @@ -29,7 +29,13 @@ namespace Sailboat /// private void InitializeComponent() { - this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.btn_delete_map = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.btn_add_map = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); this.btn_left = new System.Windows.Forms.Button(); this.btn_up = new System.Windows.Forms.Button(); this.btn_down = new System.Windows.Forms.Button(); @@ -39,38 +45,100 @@ namespace Sailboat this.btn_remove_boat = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.btn_add_boat = new System.Windows.Forms.Button(); - this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); - this.groupBox1.SuspendLayout(); + this.groupBoxTools.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // - // groupBox1 + // groupBoxTools // - this.groupBox1.Controls.Add(this.btn_left); - this.groupBox1.Controls.Add(this.btn_up); - this.groupBox1.Controls.Add(this.btn_down); - this.groupBox1.Controls.Add(this.btn_right); - this.groupBox1.Controls.Add(this.btn_show_map); - this.groupBox1.Controls.Add(this.btn_show_storage); - this.groupBox1.Controls.Add(this.btn_remove_boat); - this.groupBox1.Controls.Add(this.maskedTextBoxPosition); - this.groupBox1.Controls.Add(this.btn_add_boat); - this.groupBox1.Controls.Add(this.comboBoxMapSelector); - this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(704, 0); - this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(250, 501); - this.groupBox1.TabIndex = 0; - this.groupBox1.TabStop = false; - this.groupBox1.Text = "Инструменты"; + this.groupBoxTools.Controls.Add(this.groupBoxMaps); + this.groupBoxTools.Controls.Add(this.btn_left); + this.groupBoxTools.Controls.Add(this.btn_up); + this.groupBoxTools.Controls.Add(this.btn_down); + this.groupBoxTools.Controls.Add(this.btn_right); + this.groupBoxTools.Controls.Add(this.btn_show_map); + this.groupBoxTools.Controls.Add(this.btn_show_storage); + this.groupBoxTools.Controls.Add(this.btn_remove_boat); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.btn_add_boat); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(707, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(250, 609); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.btn_delete_map); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.btn_add_map); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxMapSelector); + this.groupBoxMaps.Location = new System.Drawing.Point(3, 23); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(247, 276); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // btn_delete_map + // + this.btn_delete_map.Location = new System.Drawing.Point(3, 241); + this.btn_delete_map.Name = "btn_delete_map"; + this.btn_delete_map.Size = new System.Drawing.Size(232, 29); + this.btn_delete_map.TabIndex = 4; + this.btn_delete_map.Text = "Удалить карту"; + this.btn_delete_map.UseVisualStyleBackColor = true; + this.btn_delete_map.Click += new System.EventHandler(this.btn_delete_map_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(0, 125); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(235, 104); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); + // + // btn_add_map + // + this.btn_add_map.Location = new System.Drawing.Point(3, 90); + this.btn_add_map.Name = "btn_add_map"; + this.btn_add_map.Size = new System.Drawing.Size(232, 29); + this.btn_add_map.TabIndex = 2; + this.btn_add_map.Text = "Добавить карту"; + this.btn_add_map.UseVisualStyleBackColor = true; + this.btn_add_map.Click += new System.EventHandler(this.btn_add_map_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(3, 23); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(232, 27); + this.textBoxNewMapName.TabIndex = 0; + // + // comboBoxMapSelector + // + this.comboBoxMapSelector.FormattingEnabled = true; + this.comboBoxMapSelector.Items.AddRange(new object[] { + "Простая карта", + "Водная карта"}); + this.comboBoxMapSelector.Location = new System.Drawing.Point(3, 56); + this.comboBoxMapSelector.Name = "comboBoxMapSelector"; + this.comboBoxMapSelector.Size = new System.Drawing.Size(232, 28); + this.comboBoxMapSelector.TabIndex = 1; // // btn_left // this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left; this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_left.Location = new System.Drawing.Point(19, 444); + this.btn_left.Location = new System.Drawing.Point(19, 552); this.btn_left.Name = "btn_left"; this.btn_left.Size = new System.Drawing.Size(70, 40); this.btn_left.TabIndex = 11; @@ -82,7 +150,7 @@ namespace Sailboat this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up; this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_up.Location = new System.Drawing.Point(95, 398); + this.btn_up.Location = new System.Drawing.Point(95, 506); this.btn_up.Name = "btn_up"; this.btn_up.Size = new System.Drawing.Size(75, 40); this.btn_up.TabIndex = 10; @@ -94,7 +162,7 @@ namespace Sailboat this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down; this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_down.Location = new System.Drawing.Point(95, 444); + this.btn_down.Location = new System.Drawing.Point(95, 552); this.btn_down.Name = "btn_down"; this.btn_down.Size = new System.Drawing.Size(75, 40); this.btn_down.TabIndex = 9; @@ -106,7 +174,7 @@ namespace Sailboat this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right; this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_right.Location = new System.Drawing.Point(174, 444); + this.btn_right.Location = new System.Drawing.Point(174, 552); this.btn_right.Name = "btn_right"; this.btn_right.Size = new System.Drawing.Size(70, 40); this.btn_right.TabIndex = 8; @@ -115,7 +183,7 @@ namespace Sailboat // // btn_show_map // - this.btn_show_map.Location = new System.Drawing.Point(6, 339); + this.btn_show_map.Location = new System.Drawing.Point(6, 471); this.btn_show_map.Name = "btn_show_map"; this.btn_show_map.Size = new System.Drawing.Size(232, 29); this.btn_show_map.TabIndex = 6; @@ -125,7 +193,7 @@ namespace Sailboat // // btn_show_storage // - this.btn_show_storage.Location = new System.Drawing.Point(6, 284); + this.btn_show_storage.Location = new System.Drawing.Point(6, 436); this.btn_show_storage.Name = "btn_show_storage"; this.btn_show_storage.Size = new System.Drawing.Size(232, 29); this.btn_show_storage.TabIndex = 5; @@ -135,7 +203,7 @@ namespace Sailboat // // btn_remove_boat // - this.btn_remove_boat.Location = new System.Drawing.Point(6, 184); + this.btn_remove_boat.Location = new System.Drawing.Point(6, 391); this.btn_remove_boat.Name = "btn_remove_boat"; this.btn_remove_boat.Size = new System.Drawing.Size(232, 29); this.btn_remove_boat.TabIndex = 4; @@ -145,7 +213,7 @@ namespace Sailboat // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 151); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 358); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(232, 27); @@ -153,7 +221,7 @@ namespace Sailboat // // btn_add_boat // - this.btn_add_boat.Location = new System.Drawing.Point(6, 89); + this.btn_add_boat.Location = new System.Drawing.Point(6, 305); this.btn_add_boat.Name = "btn_add_boat"; this.btn_add_boat.Size = new System.Drawing.Size(232, 29); this.btn_add_boat.TabIndex = 2; @@ -161,24 +229,12 @@ namespace Sailboat this.btn_add_boat.UseVisualStyleBackColor = true; this.btn_add_boat.Click += new System.EventHandler(this.btn_add_boat_Click); // - // comboBoxMapSelector - // - this.comboBoxMapSelector.FormattingEnabled = true; - this.comboBoxMapSelector.Items.AddRange(new object[] { - "Простая карта", - "Водная карта"}); - this.comboBoxMapSelector.Location = new System.Drawing.Point(6, 26); - this.comboBoxMapSelector.Name = "comboBoxMapSelector"; - this.comboBoxMapSelector.Size = new System.Drawing.Size(232, 28); - this.comboBoxMapSelector.TabIndex = 1; - this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelector_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(704, 501); + this.pictureBox.Size = new System.Drawing.Size(707, 609); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // @@ -186,13 +242,15 @@ namespace Sailboat // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(954, 501); + this.ClientSize = new System.Drawing.Size(957, 609); this.Controls.Add(this.pictureBox); - this.Controls.Add(this.groupBox1); + this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetBoats"; this.Text = "FormMapWithSetBoats"; - this.groupBox1.ResumeLayout(false); - this.groupBox1.PerformLayout(); + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -200,7 +258,7 @@ namespace Sailboat #endregion - private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.GroupBox groupBoxTools; private System.Windows.Forms.Button btn_show_map; private System.Windows.Forms.Button btn_show_storage; private System.Windows.Forms.Button btn_remove_boat; @@ -212,5 +270,10 @@ namespace Sailboat private System.Windows.Forms.Button btn_up; private System.Windows.Forms.Button btn_down; private System.Windows.Forms.Button btn_right; + private System.Windows.Forms.GroupBox groupBoxMaps; + private System.Windows.Forms.TextBox textBoxNewMapName; + private System.Windows.Forms.Button btn_delete_map; + private System.Windows.Forms.ListBox listBoxMaps; + private System.Windows.Forms.Button btn_add_map; } } \ No newline at end of file diff --git a/Sailboat/FormMapWithSetBoats.cs b/Sailboat/FormMapWithSetBoats.cs index 184e184..6e16202 100644 --- a/Sailboat/FormMapWithSetBoats.cs +++ b/Sailboat/FormMapWithSetBoats.cs @@ -12,42 +12,45 @@ namespace Sailboat { public partial class FormMapWithSetBoats : Form { - private MapWithSetBoatsGeneric _mapBoatsCollectionGeneric; + private readonly Dictionary _mapsDict = new Dictionary() { + { "Простая карта", new SimpleMap() }, + { "Водная карта", new WaterMap() } + }; + + private readonly MapsCollection _mapsCollection; public FormMapWithSetBoats() { InitializeComponent(); - AbstractMap map = new SimpleMap(); - _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric( - pictureBox.Width, pictureBox.Height, map); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxMapSelector.Items.Clear(); + foreach (var item in _mapsDict) + { + comboBoxMapSelector.Items.Add(item.Key); + } } - private void comboBoxMapSelector_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxMapSelector.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 WaterMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - if (map != null) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMaps.SelectedIndex = 0; } - else + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapBoatsCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } private void btn_add_boat_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -62,10 +65,10 @@ namespace Sailboat DrawingObjectBoat boat = new(form.SelectedBoat); - if (_mapBoatsCollectionGeneric + boat != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -85,10 +88,10 @@ namespace Sailboat return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapBoatsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -98,25 +101,25 @@ namespace Sailboat private void btn_show_storage_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void btn_show_map_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void btn_move_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -138,7 +141,42 @@ namespace Sailboat dir = Direction.Right; break; } - pictureBox.Image = _mapBoatsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + + private void btn_add_map_Click(object sender, EventArgs e) + { + if (comboBoxMapSelector.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxMapSelector.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxMapSelector.Text]); + ReloadMaps(); + } + + private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void btn_delete_map_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/Sailboat/MapWithSetBoatsGeneric.cs b/Sailboat/MapWithSetBoatsGeneric.cs index e6fa5f0..9abe3dd 100644 --- a/Sailboat/MapWithSetBoatsGeneric.cs +++ b/Sailboat/MapWithSetBoatsGeneric.cs @@ -49,13 +49,9 @@ namespace Sailboat public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setBoats.Count; i++) + foreach (var bus in _setBoats.GetBoats()) { - var ship = _setBoats.Get(i); - if (ship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, ship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, bus); } return new(_pictureWidth, _pictureHeight); } @@ -74,11 +70,11 @@ namespace Sailboat int j = _setBoats.Count - 1; for (int i = 0; i < _setBoats.Count; i++) { - if (_setBoats.Get(i) == null) + if (_setBoats[i] == null) { for (; j > i; j--) { - var boat = _setBoats.Get(j); + var boat = _setBoats[j]; if (boat != null) { _setBoats.Insert(boat, i); @@ -116,13 +112,13 @@ namespace Sailboat int currentWidth = 0; int currentHeight = 0; - for (int i = 0; i < _setBoats.Count; i++) + foreach (var boat in _setBoats.GetBoats()) { - _setBoats.Get(i)?.SetObject(currentWidth * _placeSizeWidth, + boat?.SetObject(currentWidth * _placeSizeWidth, currentHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setBoats.Get(i)?.DrawingObject(g); + boat?.DrawingObject(g); if (currentWidth < width - 1) { diff --git a/Sailboat/MapsCollection.cs b/Sailboat/MapsCollection.cs new file mode 100644 index 0000000..84f5969 --- /dev/null +++ b/Sailboat/MapsCollection.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + 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 (Keys.Contains(name)) return; + _mapStorages.Add(name, new MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + + public MapWithSetBoatsGeneric this[string ind] + { + get + { + _mapStorages.TryGetValue(ind, out var result); + return result; + } + } + } +} diff --git a/Sailboat/SetBoatsGeneric.cs b/Sailboat/SetBoatsGeneric.cs index a13c350..08ba651 100644 --- a/Sailboat/SetBoatsGeneric.cs +++ b/Sailboat/SetBoatsGeneric.cs @@ -6,16 +6,19 @@ using System.Threading.Tasks; namespace Sailboat { - class SetBoatsGeneric where T : class + class SetBoatsGeneric 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; private int BusyPlaces = 0; public SetBoatsGeneric(int count) { - _places = new T[count]; ; + _maxCount = count; + _places = new List(); } public int Insert(T boat) @@ -25,45 +28,54 @@ namespace Sailboat public int Insert(T boat, int position) { - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) { return -1; } BusyPlaces++; - while (_places[position] != null) - { - for (int i = 0; i < BusyPlaces; i++) - { - if (_places[i] == null && _places[i - 1] != null) - { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = boat; + _places.Insert(position, boat); return position; } public T Remove(int position) { - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= _maxCount) { return null; } BusyPlaces--; T boat = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return boat; } - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= _places.Length) + get { - return null; + if (position < 0 || position >= _maxCount) return null; + return _places[position]; + } + set + { + Insert(value, position); + } + + } + + public IEnumerable GetBoats() + { + foreach (var boat in _places) + { + if (boat != null) + { + yield return boat; + } + else + { + yield break; + } } - return _places[position]; } } -} +} \ No newline at end of file