From 274596e219f66e998172a4403f02ce971a58326a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Fri, 4 Nov 2022 19:40:58 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82.=20=D0=9B=D0=A04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MotorBoat/FormMapWithSetBoats.Designer.cs | 79 ++++++++-- MotorBoat/MotorBoat/FormMapWithSetBoats.cs | 144 ++++++++++++++++-- MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs | 30 ++-- MotorBoat/MotorBoat/MapsCollection.cs | 45 ++++++ MotorBoat/MotorBoat/SetBoatsGeneric.cs | 83 +++++----- 5 files changed, 296 insertions(+), 85 deletions(-) create mode 100644 MotorBoat/MotorBoat/MapsCollection.cs diff --git a/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs index 0bdda33..1d9d8ba 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.Designer.cs @@ -29,6 +29,10 @@ private void InitializeComponent() { this.groupBoxTools = 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.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonRemoveBoat = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); @@ -46,6 +50,10 @@ // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.ButtonDeleteMap); + this.groupBoxTools.Controls.Add(this.ListBoxMaps); + this.groupBoxTools.Controls.Add(this.ButtonAddMap); + this.groupBoxTools.Controls.Add(this.textBoxNewMapName); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonRemoveBoat); this.groupBoxTools.Controls.Add(this.buttonShowStorage); @@ -57,16 +65,53 @@ this.groupBoxTools.Controls.Add(this.buttonAddBoat); this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(738, 0); + this.groupBoxTools.Location = new System.Drawing.Point(809, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(251, 561); + this.groupBoxTools.Size = new System.Drawing.Size(251, 645); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; - this.groupBoxTools.Text = "Инструменты"; + this.groupBoxTools.Text = "v"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(38, 209); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(175, 33); + this.ButtonDeleteMap.TabIndex = 34; + 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(38, 124); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(175, 79); + this.ListBoxMaps.TabIndex = 33; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(38, 89); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(175, 29); + this.ButtonAddMap.TabIndex = 32; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(38, 31); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(175, 23); + this.textBoxNewMapName.TabIndex = 31; // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 174); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 328); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -75,7 +120,7 @@ // // buttonRemoveBoat // - this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 203); + this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 357); this.buttonRemoveBoat.Name = "buttonRemoveBoat"; this.buttonRemoveBoat.Size = new System.Drawing.Size(175, 35); this.buttonRemoveBoat.TabIndex = 14; @@ -85,7 +130,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(38, 298); + this.buttonShowStorage.Location = new System.Drawing.Point(38, 412); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); this.buttonShowStorage.TabIndex = 15; @@ -98,7 +143,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::MotorBoat.Properties.Resources.d; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(112, 469); + this.buttonDown.Location = new System.Drawing.Point(112, 553); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 20; @@ -110,7 +155,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::MotorBoat.Properties.Resources.up; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(148, 469); + this.buttonRight.Location = new System.Drawing.Point(148, 553); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 19; @@ -122,7 +167,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::MotorBoat.Properties.Resources.left; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(76, 469); + this.buttonLeft.Location = new System.Drawing.Point(76, 553); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 18; @@ -134,7 +179,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::MotorBoat.Properties.Resources.r; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(112, 433); + this.buttonUp.Location = new System.Drawing.Point(112, 517); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 17; @@ -143,7 +188,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(38, 377); + this.buttonShowOnMap.Location = new System.Drawing.Point(38, 453); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); this.buttonShowOnMap.TabIndex = 16; @@ -153,7 +198,7 @@ // // buttonAddBoat // - this.buttonAddBoat.Location = new System.Drawing.Point(38, 115); + this.buttonAddBoat.Location = new System.Drawing.Point(38, 287); this.buttonAddBoat.Name = "buttonAddBoat"; this.buttonAddBoat.Size = new System.Drawing.Size(175, 35); this.buttonAddBoat.TabIndex = 12; @@ -169,7 +214,7 @@ "Простая карта", "Розовая карта", "Морская карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(38, 44); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(38, 60); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); this.comboBoxSelectorMap.TabIndex = 11; @@ -180,7 +225,7 @@ 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(738, 561); + this.pictureBox.Size = new System.Drawing.Size(809, 645); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // @@ -188,7 +233,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(989, 561); + this.ClientSize = new System.Drawing.Size(1060, 645); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetBoats"; @@ -214,5 +259,9 @@ private Button buttonAddBoat; private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs index f9155e4..5b05c00 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs @@ -12,12 +12,54 @@ namespace MotorBoat { public partial class FormMapWithSetBoats : Form { - private MapWithSetBoatsGeneric _mapBoatsCollectionGeneric; + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetBoatsGeneric _mapShipCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Розовая карта", new PinkMap() }, + {"Морская карта",new SeaMap() } + }; + private readonly MapsCollection _mapsCollection; + /// + /// Конструктор + /// public FormMapWithSetBoats() { 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() + { + int index = ListBoxMaps.SelectedIndex; + + ListBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) + { + string? list = _mapsCollection.Keys[i]; + ListBoxMaps.Items.Add(list); + } + + 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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, + EventArgs e) { AbstractMap map = null; switch (comboBoxSelectorMap.Text) @@ -34,17 +76,28 @@ namespace MotorBoat } if (map != null) { - _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric( + _mapShipCollectionGeneric = new MapWithSetBoatsGeneric( pictureBox.Width, pictureBox.Height, map); } else { - _mapBoatsCollectionGeneric = null; + _mapShipCollectionGeneric = null; } } + /// + /// Выбор карты + /// + /// + /// + + /// + /// Добавление объекта + /// + /// + /// private void ButtonAddBoat_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -52,19 +105,31 @@ namespace MotorBoat if (form.ShowDialog() == DialogResult.OK) { DrawningObjectBoat 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 { MessageBox.Show("Не удалось добавить объект"); } } + } + /// + /// Удаление объекта + /// + /// + /// private void ButtonRemoveBoat_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -74,38 +139,54 @@ namespace MotorBoat 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 { MessageBox.Show("Не удалось удалить объект"); } } + /// + /// Вывод набора + /// + /// + /// private void ButtonShowStorage_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 ButtonShowOnMap_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 ButtonMove_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } + //получаем имя кнопки string name = ((Button)sender)?.Name ?? string.Empty; Direction dir = Direction.None; switch (name) @@ -123,7 +204,40 @@ namespace MotorBoat dir = Direction.Right; break; } - pictureBox.Image = _mapBoatsCollectionGeneric.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/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs b/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs index 08e38f4..27da92e 100644 --- a/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs +++ b/MotorBoat/MotorBoat/MapWithSetBoatsGeneric.cs @@ -50,13 +50,9 @@ public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setBoats.Count; i++) + foreach (var boat in _setBoats.GetBoat()) { - var boat = _setBoats.Get(i); - if (boat != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, boat); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, boat); } return new(_pictureWidth, _pictureHeight); } @@ -75,14 +71,14 @@ 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 car = _setBoats.Get(j); - if (car != null) + var boat = _setBoats[j]; + if (boat != null) { - _setBoats.Insert(car, i); + _setBoats.Insert(boat, i); _setBoats.Remove(j); break; } @@ -116,14 +112,14 @@ bool down = false; bool rightdown = false; - for (int i = 0; i < _setBoats.Count; i++) + foreach (var boat in _setBoats.GetBoat()) { if (right) { - _setBoats.Get(i)?.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); + boat.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); g.RotateTransform(270, 0); g.TranslateTransform(-190, 0); - _setBoats.Get(i)?.DrawningObject(g); + boat.DrawningObject(g); g.TranslateTransform(190, 0); g.RotateTransform(-270, 0); @@ -147,18 +143,18 @@ if (rightdown) { rightdown = false; - _setBoats.Get(i)?.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); + boat.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); g.TranslateTransform(_placeSizeWidth + 90, 0); - _setBoats.Get(i)?.DrawningObject(g); + boat.DrawningObject(g); curHeight += _placeSizeHeight; } else { rightdown = true; - _setBoats.Get(i)?.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); + boat.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); g.TranslateTransform(-_placeSizeWidth - 90, 0); - _setBoats.Get(i)?.DrawningObject(g); + boat.DrawningObject(g); } if (curHeight >= _pictureHeight - 30) { down = false; return; } diff --git a/MotorBoat/MotorBoat/MapsCollection.cs b/MotorBoat/MotorBoat/MapsCollection.cs new file mode 100644 index 0000000..e48268e --- /dev/null +++ b/MotorBoat/MotorBoat/MapsCollection.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorBoat +{ + 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 MapWithSetBoatsGeneric(_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); + } + public MapWithSetBoatsGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +} diff --git a/MotorBoat/MotorBoat/SetBoatsGeneric.cs b/MotorBoat/MotorBoat/SetBoatsGeneric.cs index 0c788f4..3fcde51 100644 --- a/MotorBoat/MotorBoat/SetBoatsGeneric.cs +++ b/MotorBoat/MotorBoat/SetBoatsGeneric.cs @@ -9,63 +9,70 @@ namespace MotorBoat internal class SetBoatsGeneric 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 SetBoatsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T boat) { + if (_places.Count > _maxCount) + { + return -1; + } return Insert(boat, 0); } public int Insert(T boat, int position) { - // TODO проверка позиции - if (position >= _places.Length || position < 0) - return -1; - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] == null) - { - _places[position] = boat; - return position; - } - // проверка, что после вставляемого элемента в массиве есть пустой элемент - int EmptyPos = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - EmptyPos = i; - break; - } - } - if (EmptyPos < 0) return -1; - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - for (int i = EmptyPos; i > position; i--) - { - _places[i] = _places[i - 1]; - } - // вставка по позиции - _places[position] = boat; + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, boat); return position; } public T Remove(int position) { // TODO проверка позиции - if (position >= _places.Length || position < 0) return null; + if (position >= _maxCount || position < 0) return null; // TODO удаление объекта из массива, присовив элементу массива значение null T temp = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return temp; } - public T Get(int position) + public T this[int position] { - // TODO проверка позиции - if (position >= _places.Length || position < 0) - return null; - return _places[position]; + get + { + if (position >= _places.Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position >= _places.Count || position < 0) + { + return; + } + Insert(value, position); + + } + } + public IEnumerable GetBoat() + { + foreach (var boat in _places) + { + if (boat != null) + { + yield return boat; + } + else + { + yield break; + } + } } } } \ No newline at end of file -- 2.25.1 From 3a2b3a5bb92d4c0b50114a5b88a650f73f8dd409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Fri, 4 Nov 2022 19:50:01 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A7=D0=B5=D1=82=D0=B2=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotorBoat/MotorBoat/FormMapWithSetBoats.cs | 37 ---------------------- MotorBoat/MotorBoat/MapsCollection.cs | 1 - 2 files changed, 38 deletions(-) diff --git a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs index 5b05c00..00ef65f 100644 --- a/MotorBoat/MotorBoat/FormMapWithSetBoats.cs +++ b/MotorBoat/MotorBoat/FormMapWithSetBoats.cs @@ -12,9 +12,6 @@ namespace MotorBoat { public partial class FormMapWithSetBoats : Form { - /// - /// Объект от класса карты с набором объектов - /// private MapWithSetBoatsGeneric _mapShipCollectionGeneric; private readonly Dictionary _mapDict = new() { @@ -23,9 +20,6 @@ namespace MotorBoat {"Морская карта",new SeaMap() } }; private readonly MapsCollection _mapsCollection; - /// - /// Конструктор - /// public FormMapWithSetBoats() { InitializeComponent(); @@ -84,17 +78,6 @@ namespace MotorBoat _mapShipCollectionGeneric = null; } } - /// - /// Выбор карты - /// - /// - /// - - /// - /// Добавление объекта - /// - /// - /// private void ButtonAddBoat_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) @@ -119,11 +102,6 @@ namespace MotorBoat } } - /// - /// Удаление объекта - /// - /// - /// private void ButtonRemoveBoat_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) @@ -149,11 +127,6 @@ namespace MotorBoat MessageBox.Show("Не удалось удалить объект"); } } - /// - /// Вывод набора - /// - /// - /// private void ButtonShowStorage_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) @@ -162,11 +135,6 @@ namespace MotorBoat } pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } - /// - /// Вывод карты - /// - /// - /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) @@ -175,11 +143,6 @@ namespace MotorBoat } pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } - /// - /// Перемещение - /// - /// - /// private void ButtonMove_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) diff --git a/MotorBoat/MotorBoat/MapsCollection.cs b/MotorBoat/MotorBoat/MapsCollection.cs index e48268e..06b1dd9 100644 --- a/MotorBoat/MotorBoat/MapsCollection.cs +++ b/MotorBoat/MotorBoat/MapsCollection.cs @@ -8,7 +8,6 @@ namespace MotorBoat { internal class MapsCollection { - readonly Dictionary> _mapStorages; public List Keys => _mapStorages.Keys.ToList(); private readonly int _pictureWidth; -- 2.25.1