From 903669666a1fc09a4af4fe50e24ac454f1982eec Mon Sep 17 00:00:00 2001 From: just1valery Date: Sun, 9 Oct 2022 12:52:05 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyShip/MapWithSetShipsGeneric.cs | 16 +++---- WarmlyShip/WarmlyShip/SetShipsGeneric.cs | 47 +++++++++++++++---- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs index 212d2f0..bf56fd9 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs @@ -88,13 +88,9 @@ namespace WarmlyShip 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); } @@ -119,11 +115,11 @@ namespace WarmlyShip 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); @@ -170,9 +166,9 @@ namespace WarmlyShip { int countInLine = _pictureWidth / _placeSizeWidth; int maxLeft = (countInLine - 1) * _placeSizeWidth; - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - var ship = _setShips.Get(i); + var ship = _setShips[i]; ship?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); ship?.DrawningObject(g); } diff --git a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs index 8fa50b1..62df44e 100644 --- a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs @@ -10,20 +10,23 @@ namespace WarmlyShip 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 SetShipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -32,6 +35,7 @@ namespace WarmlyShip /// public bool Insert(T ship) { + //проверка на макс каунт return Insert(ship, 0); } private bool isCorrectPosition(int position) @@ -70,7 +74,7 @@ namespace WarmlyShip /// /// public bool Remove(int position) - { + { if (!isCorrectPosition(position)) return false; _places[position-1] = null; @@ -81,9 +85,36 @@ namespace WarmlyShip /// /// /// - public T Get(int position) + public T this[int position] { - return isCorrectPosition(position) ? _places[position] : null; + get + { + // TODO проверка позиции + return _places[position]; + } + set + { + // TODO проверка позиции + // TODO вставка в список по позиции + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } + } } } } -- 2.25.1 From d34467b4a52ffb01da1bce3b0d172bf26c3b17e8 Mon Sep 17 00:00:00 2001 From: just1valery Date: Sun, 9 Oct 2022 12:56:33 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=202.=20=D0=9A?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=20MapsCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/MapsCollection.cs | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 WarmlyShip/WarmlyShip/MapsCollection.cs diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs new file mode 100644 index 0000000..58deb99 --- /dev/null +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip +{ + 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) + { + // TODO Прописать логику для добавления + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // TODO Прописать логику для удаления + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetShipsGeneric this[string ind] + { + get + { + // TODO Продумать логику получения объекта + return null; + } + } + } +} -- 2.25.1 From 73d5bf2ccf6a9cf524d3fe5e8c419140cea354f9 Mon Sep 17 00:00:00 2001 From: just1valery Date: Sun, 9 Oct 2022 13:13:06 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetShips.Designer.cs | 120 ++++++++++++---- WarmlyShip/WarmlyShip/FormMapWithSetShips.cs | 134 +++++++++++++----- 2 files changed, 191 insertions(+), 63 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs index d2b41f7..7fa69ee 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.groupBoxMaps = 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.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = 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.groupBoxTools.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxMaps); this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonUp); @@ -55,21 +62,83 @@ this.groupBoxTools.Controls.Add(this.buttonRemoveShip); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonAddShip); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(550, 0); + this.groupBoxTools.Location = new System.Drawing.Point(616, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(250, 450); + this.groupBoxTools.Size = new System.Drawing.Size(250, 594); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // groupBoxMaps + // + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + 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(6, 26); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(241, 283); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(0, 235); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(238, 29); + this.buttonDeleteMap.TabIndex = 3; + 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(6, 125); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(229, 104); + this.listBoxMaps.TabIndex = 2; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(0, 90); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(238, 29); + this.buttonAddMap.TabIndex = 1; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(3, 23); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(235, 27); + this.textBoxNewMapName.TabIndex = 0; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Океан"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(3, 56); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(235, 28); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // // buttonLeft // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(73, 400); + this.buttonLeft.Location = new System.Drawing.Point(73, 544); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 10; @@ -82,7 +151,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(145, 399); + this.buttonRight.Location = new System.Drawing.Point(145, 543); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 9; @@ -95,7 +164,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(109, 363); + this.buttonUp.Location = new System.Drawing.Point(109, 507); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 8; @@ -108,7 +177,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.arrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(109, 399); + this.buttonDown.Location = new System.Drawing.Point(109, 543); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 7; @@ -118,7 +187,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 316); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 478); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(238, 29); this.buttonShowOnMap.TabIndex = 5; @@ -128,7 +197,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 250); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 443); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(238, 29); this.buttonShowStorage.TabIndex = 4; @@ -138,7 +207,7 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(6, 180); + this.buttonRemoveShip.Location = new System.Drawing.Point(6, 399); this.buttonRemoveShip.Name = "buttonRemoveShip"; this.buttonRemoveShip.Size = new System.Drawing.Size(238, 29); this.buttonRemoveShip.TabIndex = 3; @@ -148,7 +217,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 147); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 366); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(238, 27); @@ -156,7 +225,7 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(6, 86); + this.buttonAddShip.Location = new System.Drawing.Point(6, 331); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(238, 29); this.buttonAddShip.TabIndex = 1; @@ -164,24 +233,12 @@ this.buttonAddShip.UseVisualStyleBackColor = true; this.buttonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Океан"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 26); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(238, 28); - 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(550, 450); + this.pictureBox.Size = new System.Drawing.Size(616, 594); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -189,13 +246,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(866, 594); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetShips"; this.Text = "Карта с набором объектов"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -210,10 +269,15 @@ private Button buttonRemoveShip; private MaskedTextBox maskedTextBoxPosition; private Button buttonAddShip; - private ComboBox comboBoxSelectorMap; private Button buttonLeft; private Button buttonRight; private Button buttonUp; private Button buttonDown; + private GroupBox groupBoxMaps; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; + private ComboBox comboBoxSelectorMap; } } \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs index c8c6313..f2326b3 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs @@ -13,38 +13,98 @@ namespace WarmlyShip public partial class FormMapWithSetShips : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка /// - private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() } + }; /// - /// Конструктор + /// Объект от коллекции карт /// + private readonly MapsCollection _mapsCollection; public FormMapWithSetShips() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + comboBoxSelectorMap.Items.Add(elem.Key); + } } + /// + /// Заполнение listBoxMaps + /// + 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 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(); + } + } private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Океан": - map = new OceanMap(); - break; - } - if (map != null) - { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapShipsCollectionGeneric = null; - } + } /// /// Добавление объекта @@ -53,7 +113,7 @@ namespace WarmlyShip /// private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -61,14 +121,14 @@ namespace WarmlyShip if (form.ShowDialog() == DialogResult.OK) { DrawningObjectShip ship = new(form.SelectedShip); - if (form.SelectedShip == null || !(_mapShipsCollectionGeneric + ship)) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship) { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + MessageBox.Show("Не удалось добавить объект"); } } } @@ -79,6 +139,10 @@ namespace WarmlyShip /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -88,10 +152,10 @@ namespace WarmlyShip return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapShipsCollectionGeneric - pos) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -105,11 +169,11 @@ namespace WarmlyShip /// 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(); } /// /// Вывод карты @@ -118,11 +182,11 @@ namespace WarmlyShip /// 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(); } /// /// Перемещение @@ -131,7 +195,7 @@ namespace WarmlyShip /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -161,7 +225,7 @@ namespace WarmlyShip } break; } - pictureBox.Image = _mapShipsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } } -- 2.25.1 From 54e29c6e9adb1c2b7190ac8b36cba2c7675d3ac2 Mon Sep 17 00:00:00 2001 From: just1valery Date: Sun, 9 Oct 2022 14:00:23 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20"ToDo"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetShips.Designer.cs | 11 +++-- WarmlyShip/WarmlyShip/FormMapWithSetShips.cs | 22 +++------- .../WarmlyShip/MapWithSetShipsGeneric.cs | 6 +-- WarmlyShip/WarmlyShip/MapsCollection.cs | 8 ++-- WarmlyShip/WarmlyShip/SetShipsGeneric.cs | 44 +++++++------------ 5 files changed, 34 insertions(+), 57 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs index 7fa69ee..3e5154a 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs @@ -109,7 +109,7 @@ this.buttonAddMap.Location = new System.Drawing.Point(0, 90); this.buttonAddMap.Name = "buttonAddMap"; this.buttonAddMap.Size = new System.Drawing.Size(238, 29); - this.buttonAddMap.TabIndex = 1; + this.buttonAddMap.TabIndex = 2; this.buttonAddMap.Text = "Добавить карту"; this.buttonAddMap.UseVisualStyleBackColor = true; this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); @@ -123,15 +123,14 @@ // // 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(3, 56); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(235, 28); - this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + this.comboBoxSelectorMap.Size = new System.Drawing.Size(232, 28); + this.comboBoxSelectorMap.TabIndex = 1; // // buttonLeft // diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs index f2326b3..3fac642 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs @@ -17,7 +17,8 @@ namespace WarmlyShip /// private readonly Dictionary _mapsDict = new() { - { "Простая карта", new SimpleMap() } + { "Простая карта", new SimpleMap() }, + { "Океан", new OceanMap() } }; /// /// Объект от коллекции карт @@ -101,10 +102,6 @@ namespace WarmlyShip _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); ReloadMaps(); } - } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - } /// /// Добавление объекта @@ -121,7 +118,7 @@ namespace WarmlyShip if (form.ShowDialog() == DialogResult.OK) { DrawningObjectShip ship = new(form.SelectedShip); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); @@ -139,20 +136,13 @@ namespace WarmlyShip /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { - if (listBoxMaps.SelectedIndex == -1) - { - return; - } - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) - { - return; - } - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxMaps.SelectedIndex == -1 || string.IsNullOrEmpty(maskedTextBoxPosition.Text) || + MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs index bf56fd9..43ed7f8 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs @@ -55,7 +55,7 @@ namespace WarmlyShip /// /// /// - public static bool operator +(MapWithSetShipsGeneric map, T ship) + public static int operator +(MapWithSetShipsGeneric map, T ship) { return map._setShips.Insert(ship); } @@ -65,7 +65,7 @@ namespace WarmlyShip /// /// /// - public static bool operator -(MapWithSetShipsGeneric map, int position) + public static T operator -(MapWithSetShipsGeneric map, int position) { return map._setShips.Remove(position); } @@ -166,7 +166,7 @@ namespace WarmlyShip { int countInLine = _pictureWidth / _placeSizeWidth; int maxLeft = (countInLine - 1) * _placeSizeWidth; - foreach (var ship in _setShips.GetShips()) + for (int i = 0; i < _setShips.Count; i++) { var ship = _setShips[i]; ship?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 58deb99..d26ae01 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -42,7 +42,7 @@ namespace WarmlyShip /// Карта public void AddMap(string name, AbstractMap map) { - // TODO Прописать логику для добавления + _mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -50,7 +50,7 @@ namespace WarmlyShip /// Название карты public void DelMap(string name) { - // TODO Прописать логику для удаления + _mapStorages.Remove(name); } /// /// Доступ к парковке @@ -61,8 +61,8 @@ namespace WarmlyShip { get { - // TODO Продумать логику получения объекта - return null; + _mapStorages.TryGetValue(ind, out var mapWithSetShipsGeneric); + return mapWithSetShipsGeneric; } } } diff --git a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs index 62df44e..f42f0da 100644 --- a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs @@ -33,14 +33,14 @@ namespace WarmlyShip /// /// Добавляемый корабль /// - public bool Insert(T ship) + public int Insert(T ship) { - //проверка на макс каунт return Insert(ship, 0); } + private bool isCorrectPosition(int position) { - return 0 <= position && position < Count; + return 0 <= position && position < _maxCount; } /// /// Добавление объекта в набор на конкретную позицию @@ -48,37 +48,27 @@ namespace WarmlyShip /// Добавляемый корабль /// Позиция /// - public bool Insert(T ship, int position) + public int Insert(T airplane, int position) { - int positionNullElement = position; - while (Get(positionNullElement) != null) + if (!isCorrectPosition(position)) { - positionNullElement++; + return -1; } - // Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false - if (!isCorrectPosition(positionNullElement)) - { - return false; - } - while (positionNullElement != position) // Смещение вправо - { - _places[positionNullElement] = _places[positionNullElement - 1]; - positionNullElement--; - } - _places[position] = ship; - return true; + _places.Insert(position, airplane); + return position; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) - { + public T Remove(int position) + { if (!isCorrectPosition(position)) - return false; - _places[position-1] = null; - return true; + return null; + var result = _places[position]; + _places.RemoveAt(position); + return result; } /// /// Получение объекта из набора по позиции @@ -89,13 +79,11 @@ namespace WarmlyShip { get { - // TODO проверка позиции - return _places[position]; + return isCorrectPosition(position) && position < Count ? _places[position] : null; } set { - // TODO проверка позиции - // TODO вставка в список по позиции + Insert(value, position); } } /// -- 2.25.1 From 795aff0752a0ea52f69a9fb22d0767163c3f13b5 Mon Sep 17 00:00:00 2001 From: just1valery Date: Sat, 15 Oct 2022 12:50:12 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D0=B0=D1=80=D0=B8=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/MapsCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index d26ae01..742b90f 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -53,7 +53,7 @@ namespace WarmlyShip _mapStorages.Remove(name); } /// - /// Доступ к парковке + /// Доступ к хранилищу /// /// /// -- 2.25.1