From bf9dbaa9dcf459dd791a23339abeddb04362ad49 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:27:32 +0400 Subject: [PATCH 1/3] =?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?= =?UTF-8?q?.=20=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=BD?= =?UTF-8?q?=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 --- .../ContainerShip/MapWithSetShipsGeneric.cs | 26 +++--- .../ContainerShip/SetShipsGeneric.cs | 89 +++++++++---------- 2 files changed, 56 insertions(+), 59 deletions(-) diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs index dfda657..56c9a2d 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -44,7 +44,7 @@ namespace ContainerShip { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; - _setShips = new SetShipsGeneric(width * height); + _setShips = new SetShipsGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; @@ -68,13 +68,9 @@ namespace ContainerShip 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); } @@ -99,11 +95,11 @@ namespace ContainerShip 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); @@ -141,12 +137,14 @@ namespace ContainerShip /// private void DrawShips(Graphics g) { - for (int i = 0; i < _setShips.Count; i++) + int i = 0; + int countOfShipsInLine = _pictureWidth / _placeSizeWidth; + int countOfShipsInColumn = _pictureHeight / _placeSizeHeight; + foreach (var ship in _setShips.GetShips()) { - int countOfShipsInLine = _pictureWidth / _placeSizeWidth; - int countOfShipsInColumn = _pictureHeight / _placeSizeHeight; - _setShips.Get(i)?.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setShips.Get(i)?.DrawingObject(g); + ship.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); + ship.DrawingObject(g); + i++; } } /// diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index 883942d..04cbea5 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -10,20 +10,22 @@ namespace ContainerShip 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(); } /// /// Добавление объекта в набор @@ -41,41 +43,10 @@ namespace ContainerShip /// Позиция /// public int Insert(T ship, int position) - { - if (position < 0 || position >= _places.Length) - { - return -1; - } - if (_places[position] == null) - { - - _places[position] = ship; - } - else - { - int freePlace = -1; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - freePlace = i; - } - } - if (freePlace == -1) - { - return -1; - } - else - { - for (int i = freePlace; i > position; i--) - { - T buf = _places[i]; - _places[i] = _places[i - 1]; - _places[i - 1] = buf; - } - _places[position] = ship; - } - } + { + if (position < 0 || position >= _maxCount) return -1; + if (_places.Count + 1 >= _maxCount) return -1; + _places.Add(ship); return position; } /// @@ -85,7 +56,7 @@ namespace ContainerShip /// public T Remove(int position) { - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= _places.Count) { return null; } @@ -98,13 +69,41 @@ namespace ContainerShip /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= _places.Length) + get { - return null; + // TODO проверка позиции + if (position < 0 || position > _maxCount) return null; + return _places[position]; + } + set + { + if (position >= 0 || position < _maxCount) + { + Insert(value, position); + } + // TODO проверка позиции + // TODO вставка в список по позиции + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } } } From 6c787200aa09200f5ca0720d6c481757710ebfc9 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 1 Nov 2022 12:29:58 +0400 Subject: [PATCH 2/3] =?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 --- ContainerShip/ContainerShip/MapsCollection.cs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 ContainerShip/ContainerShip/MapsCollection.cs diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs new file mode 100644 index 0000000..d1c9b62 --- /dev/null +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + 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 Прописать логику для добавления + if (_mapStorages.ContainsKey(name)) return; + else + { + _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); + } + + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // TODO Прописать логику для удаления + if (_mapStorages.ContainsKey(name)) + { + _mapStorages.Remove(name); + } + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetShipsGeneric this[string ind] + { + get + { + //TODO логика получения объекта + if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind]; + return null; + } + } + } +} From 20291ba6a898e5dff36a0ba56e0c2b486f88c6f4 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Tue, 1 Nov 2022 13:45:02 +0400 Subject: [PATCH 3/3] =?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=20+=20=D1=84=D0=B8=D0=BA=D1=81=20T=20R?= =?UTF-8?q?emove.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetShips.Designer.cs | 113 ++++++++++++---- .../ContainerShip/FormMapWithSetShips.cs | 127 +++++++++++++----- ContainerShip/ContainerShip/MapsCollection.cs | 2 +- .../ContainerShip/SetShipsGeneric.cs | 4 +- 4 files changed, 185 insertions(+), 61 deletions(-) diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs index 4e94da7..bce3b84 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -29,8 +29,13 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.ButtonAddShip = new System.Windows.Forms.Button(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.ButtonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.ButtonAddMap = new System.Windows.Forms.Button(); this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.ButtonAddShip = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); @@ -41,13 +46,14 @@ this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.ButtonAddShip); - this.groupBox1.Controls.Add(this.ComboBoxSelectorMap); this.groupBox1.Controls.Add(this.buttonUp); this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonDown); @@ -59,20 +65,61 @@ this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Location = new System.Drawing.Point(553, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(300, 508); + this.groupBox1.Size = new System.Drawing.Size(300, 687); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // - // ButtonAddShip + // groupBoxMaps // - this.ButtonAddShip.Location = new System.Drawing.Point(6, 74); - this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(288, 34); - this.ButtonAddShip.TabIndex = 11; - this.ButtonAddShip.Text = "Добавить корабль"; - this.ButtonAddShip.UseVisualStyleBackColor = true; - this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); + this.groupBoxMaps.Controls.Add(this.ButtonDeleteMap); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.ButtonAddMap); + this.groupBoxMaps.Controls.Add(this.ComboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(10, 32); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(284, 327); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 277); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(272, 44); + this.ButtonDeleteMap.TabIndex = 15; + this.ButtonDeleteMap.Text = "Удалить карту"; + this.ButtonDeleteMap.UseVisualStyleBackColor = true; + this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 25; + this.listBoxMaps.Location = new System.Drawing.Point(6, 167); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(272, 104); + this.listBoxMaps.TabIndex = 14; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(6, 39); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(272, 31); + this.textBoxNewMapName.TabIndex = 13; + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(6, 115); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(272, 44); + this.ButtonAddMap.TabIndex = 12; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); // // ComboBoxSelectorMap // @@ -80,18 +127,27 @@ this.ComboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Модифицированная карта"}); - this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 35); + this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 76); this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; - this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33); + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(272, 33); this.ComboBoxSelectorMap.TabIndex = 10; - this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // ButtonAddShip + // + this.ButtonAddShip.Location = new System.Drawing.Point(6, 365); + this.ButtonAddShip.Name = "ButtonAddShip"; + this.ButtonAddShip.Size = new System.Drawing.Size(288, 34); + this.ButtonAddShip.TabIndex = 11; + this.ButtonAddShip.Text = "Добавить корабль"; + this.ButtonAddShip.UseVisualStyleBackColor = true; + this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); // // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(131, 390); + this.buttonUp.Location = new System.Drawing.Point(131, 569); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.TabIndex = 9; @@ -103,7 +159,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(187, 446); + this.buttonRight.Location = new System.Drawing.Point(187, 625); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(50, 50); this.buttonRight.TabIndex = 8; @@ -115,7 +171,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.DownArrow; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(131, 446); + this.buttonDown.Location = new System.Drawing.Point(131, 625); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.TabIndex = 7; @@ -127,7 +183,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(75, 446); + this.buttonLeft.Location = new System.Drawing.Point(75, 625); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.TabIndex = 6; @@ -136,9 +192,9 @@ // // button3 // - this.button3.Location = new System.Drawing.Point(8, 292); + this.button3.Location = new System.Drawing.Point(6, 529); this.button3.Name = "button3"; - this.button3.Size = new System.Drawing.Size(286, 34); + this.button3.Size = new System.Drawing.Size(288, 34); this.button3.TabIndex = 3; this.button3.Text = "Посмотреть карту"; this.button3.UseVisualStyleBackColor = true; @@ -146,7 +202,7 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(6, 243); + this.ButtonShowStorage.Location = new System.Drawing.Point(6, 489); this.ButtonShowStorage.Name = "ButtonShowStorage"; this.ButtonShowStorage.Size = new System.Drawing.Size(288, 34); this.ButtonShowStorage.TabIndex = 2; @@ -156,7 +212,7 @@ // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 162); + this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 442); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; this.ButtonRemoveShip.Size = new System.Drawing.Size(288, 41); this.ButtonRemoveShip.TabIndex = 1; @@ -166,7 +222,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 125); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 405); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(288, 31); @@ -177,7 +233,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(553, 508); + this.pictureBox.Size = new System.Drawing.Size(553, 687); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -185,13 +241,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(853, 508); + this.ClientSize = new System.Drawing.Size(853, 687); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetShips"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -211,5 +269,10 @@ private Button buttonRight; private Button ButtonAddShip; private ComboBox ComboBoxSelectorMap; + private ListBox listBoxMaps; + private TextBox textBoxNewMapName; + private Button ButtonAddMap; + private GroupBox groupBoxMaps; + private Button ButtonDeleteMap; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs index faa900b..e04780f 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShips.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -13,46 +13,60 @@ namespace ContainerShip { public partial class FormMapWithSetShips : Form { + /// + /// Словарь для выпадающего списка + /// + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() } + }; /// /// Объект от класса карты с набором объектов /// - private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + 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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - AbstractMap map = null; - switch (ComboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Модифицированная карта": - map = new ModifyMap(); - break; - } - if (map != null) - { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric(pictureBox.Width, pictureBox.Height, map); - } - else -{ - _mapShipsCollectionGeneric = null; -} - } /// /// private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -60,10 +74,10 @@ namespace ContainerShip 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 { @@ -78,6 +92,10 @@ namespace ContainerShip /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -87,10 +105,10 @@ namespace ContainerShip 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 { @@ -104,11 +122,11 @@ namespace ContainerShip /// 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(); } /// /// Вывод карты @@ -117,11 +135,11 @@ namespace ContainerShip /// 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(); } /// /// Перемещение @@ -130,7 +148,7 @@ namespace ContainerShip /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -152,7 +170,50 @@ namespace ContainerShip 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 (!_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(); + } } } } diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs index d1c9b62..aa31364 100644 --- a/ContainerShip/ContainerShip/MapsCollection.cs +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -43,7 +43,7 @@ namespace ContainerShip public void AddMap(string name, AbstractMap map) { // TODO Прописать логику для добавления - if (_mapStorages.ContainsKey(name)) return; + if (_mapStorages.ContainsKey(name)) return; //уникальное имя else { _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs index 04cbea5..cb778d4 100644 --- a/ContainerShip/ContainerShip/SetShipsGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -56,12 +56,12 @@ namespace ContainerShip /// public T Remove(int position) { - if (position < 0 || position >= _places.Count) + if (position < 0 || position >= _maxCount) { return null; } T ship = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return ship; } ///