From 52405afadb1670563368526e01d6a837107789ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=A7=D0=B5=D1=80?= =?UTF-8?q?=D0=BD=D1=8B=D1=88=D0=BE=D0=B2?= Date: Fri, 18 Nov 2022 14:01:01 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=BD=D0=B0=20=D1=81=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=BE=D0=BA.=20=D0=9A=D0=BB=D0=B0=D1=81=D1=81=20Ma?= =?UTF-8?q?psCollection.=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Warship/FormMapWithSetWarship.Designer.cs | 270 +++++++++++------- Warship/Warship/FormMapWithSetWarship.cs | 126 +++++--- Warship/Warship/MapWithSetWarshipGeneric.cs | 26 +- Warship/Warship/MapsCollection.cs | 69 +++++ Warship/Warship/SetWarshipGeneric.cs | 119 +++----- 5 files changed, 370 insertions(+), 240 deletions(-) create mode 100644 Warship/Warship/MapsCollection.cs diff --git a/Warship/Warship/FormMapWithSetWarship.Designer.cs b/Warship/Warship/FormMapWithSetWarship.Designer.cs index 4036d04..19e8ea0 100644 --- a/Warship/Warship/FormMapWithSetWarship.Designer.cs +++ b/Warship/Warship/FormMapWithSetWarship.Designer.cs @@ -22,106 +22,173 @@ #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); - this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); - this.buttonRemoveTruck = new System.Windows.Forms.Button(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); + this.buttonRemoveWarship = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddWarship = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); - this.buttonShowOnMap = new System.Windows.Forms.Button(); - this.buttonAddTruck = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonDown = new System.Windows.Forms.Button(); 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.maskedTextBoxPosition); - this.groupBoxTools.Controls.Add(this.buttonRemoveTruck); + this.groupBoxTools.Controls.Add(this.groupBoxMaps); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowStorage); - this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonAddWarship); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonUp); - this.groupBoxTools.Controls.Add(this.buttonShowOnMap); - this.groupBoxTools.Controls.Add(this.buttonAddTruck); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(927, 0); - this.groupBoxTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.groupBoxTools.Location = new System.Drawing.Point(833, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBoxTools.Size = new System.Drawing.Size(233, 739); + this.groupBoxTools.Size = new System.Drawing.Size(217, 649); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // - // maskedTextBoxPosition + // groupBoxMaps // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(19, 221); - this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.maskedTextBoxPosition.Mask = "00"; - this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(199, 27); - this.maskedTextBoxPosition.TabIndex = 2; - this.maskedTextBoxPosition.ValidatingType = typeof(int); + this.groupBoxMaps.Controls.Add(this.buttonAddMap); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.textBoxNewMapName); + this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxMaps.Location = new System.Drawing.Point(6, 22); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(205, 257); + this.groupBoxMaps.TabIndex = 19; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; // - // buttonRemoveTruck + // buttonAddMap // - this.buttonRemoveTruck.Location = new System.Drawing.Point(19, 260); - this.buttonRemoveTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.buttonRemoveTruck.Name = "buttonRemoveWarship"; - this.buttonRemoveTruck.Size = new System.Drawing.Size(200, 47); - this.buttonRemoveTruck.TabIndex = 3; - this.buttonRemoveTruck.Text = "Удалить военный корбаль"; - this.buttonRemoveTruck.UseMnemonic = false; - this.buttonRemoveTruck.UseVisualStyleBackColor = true; - this.buttonRemoveTruck.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + this.buttonAddMap.Location = new System.Drawing.Point(14, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(180, 35); + this.buttonAddMap.TabIndex = 2; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(14, 206); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(180, 35); + this.buttonDeleteMap.TabIndex = 4; + 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(14, 121); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(180, 79); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(14, 22); + this.textBoxNewMapName.Multiline = true; + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(180, 23); + this.textBoxNewMapName.TabIndex = 0; + // + // 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(14, 51); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(180, 23); + this.comboBoxSelectorMap.TabIndex = 9; + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(20, 516); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(180, 35); + this.buttonShowOnMap.TabIndex = 18; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(19, 383); - this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonShowStorage.Location = new System.Drawing.Point(20, 475); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(200, 47); - this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Size = new System.Drawing.Size(180, 35); + this.buttonShowStorage.TabIndex = 17; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); // - // buttonDown + // buttonRemoveWarship // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаbott; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(104, 672); - this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(34, 40); - this.buttonDown.TabIndex = 10; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 434); + this.buttonRemoveWarship.Name = "buttonRemoveWarship"; + this.buttonRemoveWarship.Size = new System.Drawing.Size(180, 35); + this.buttonRemoveWarship.TabIndex = 16; + this.buttonRemoveWarship.Text = "Удалить военный корабль"; + this.buttonRemoveWarship.UseVisualStyleBackColor = true; + this.buttonRemoveWarship.Click += new System.EventHandler(this.ButtonRemoveWarship_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 405); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(180, 23); + this.maskedTextBoxPosition.TabIndex = 15; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddWarship + // + this.buttonAddWarship.Location = new System.Drawing.Point(20, 364); + this.buttonAddWarship.Name = "buttonAddWarship"; + this.buttonAddWarship.Size = new System.Drawing.Size(180, 35); + this.buttonAddWarship.TabIndex = 14; + this.buttonAddWarship.Text = "Добавить военный корабль"; + this.buttonAddWarship.UseVisualStyleBackColor = true; + this.buttonAddWarship.Click += new System.EventHandler(this.ButtonAddWarship_Click); // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаright; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(145, 672); - this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRight.Location = new System.Drawing.Point(126, 609); this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(34, 40); - this.buttonRight.TabIndex = 9; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 13; + this.buttonRight.Text = " "; this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); // @@ -130,11 +197,11 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелка; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(63, 672); - this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonLeft.Location = new System.Drawing.Point(54, 609); this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(34, 40); - this.buttonLeft.TabIndex = 8; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 12; + this.buttonLeft.Text = " "; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // @@ -143,73 +210,49 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаtop; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(104, 624); - this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonUp.Location = new System.Drawing.Point(90, 573); this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(34, 40); - this.buttonUp.TabIndex = 7; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 11; + this.buttonUp.Text = " "; this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); // - // buttonShowOnMap + // buttonDown // - this.buttonShowOnMap.Location = new System.Drawing.Point(19, 521); - this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(200, 47); - this.buttonShowOnMap.TabIndex = 5; - this.buttonShowOnMap.Text = "Посмотреть карту"; - this.buttonShowOnMap.UseVisualStyleBackColor = true; - this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); - // - // buttonAddTruck - // - this.buttonAddTruck.Location = new System.Drawing.Point(19, 141); - this.buttonAddTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.buttonAddTruck.Name = "buttonAddTruck"; - this.buttonAddTruck.Size = new System.Drawing.Size(200, 47); - this.buttonAddTruck.TabIndex = 1; - this.buttonAddTruck.Text = "Добавить военный корабль"; - this.buttonAddTruck.UseVisualStyleBackColor = true; - this.buttonAddTruck.Click += new System.EventHandler(this.ButtonAddWarship_Click); - // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Морская карта", - }); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(19, 43); - this.comboBoxSelectorMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(199, 28); - this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.стрелкаbott; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(90, 609); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 10; + this.buttonDown.Text = " "; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // // pictureBox // this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Location = new System.Drawing.Point(0, 0); - this.pictureBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBox.Name = "pictureBox"; - this.pictureBox.Size = new System.Drawing.Size(927, 739); + this.pictureBox.Size = new System.Drawing.Size(833, 649); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // - // FormMapWithSetTrucks + // FormMapWithSetWarship // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1160, 739); + this.ClientSize = new System.Drawing.Size(1050, 649); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); - this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Name = "FormMapWithSetTrucks"; + this.Name = "FormMapWithSetWarship"; 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); @@ -220,14 +263,19 @@ private GroupBox groupBoxTools; private PictureBox pictureBox; private ComboBox comboBoxSelectorMap; - private Button buttonShowOnMap; - private Button buttonAddTruck; - private Button buttonDown; private Button buttonRight; private Button buttonLeft; private Button buttonUp; + private Button buttonDown; + private Button buttonShowOnMap; private Button buttonShowStorage; - private Button buttonRemoveTruck; + private Button buttonRemoveWarship; private MaskedTextBox maskedTextBoxPosition; + private Button buttonAddWarship; + private GroupBox groupBoxMaps; + private Button buttonAddMap; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Warship/Warship/FormMapWithSetWarship.cs b/Warship/Warship/FormMapWithSetWarship.cs index fc11391..5c9013a 100644 --- a/Warship/Warship/FormMapWithSetWarship.cs +++ b/Warship/Warship/FormMapWithSetWarship.cs @@ -15,40 +15,96 @@ namespace AircraftCarrier public partial class FormMapWithSetWarship : Form { /// - /// Объект от класса карты с набором объектов + /// Словарь для выпадающего списка /// - private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Морская карта", new SeaMap() } + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; /// /// Конструктор /// public FormMapWithSetWarship() { 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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + /// + /// Удаление карты + /// + /// + /// + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Морская карта": - map = new SeaMap(); - break; + return; } - if (map != null) + if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric(pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapWarshipsCollectionGeneric = null; + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); } } /// @@ -58,7 +114,7 @@ namespace AircraftCarrier /// private void ButtonAddWarship_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -66,10 +122,10 @@ namespace AircraftCarrier if (form.ShowDialog() == DialogResult.OK) { DrawingObjectWarship warship = new(form.SelectedWarship); - if (_mapWarshipsCollectionGeneric + warship != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship >= 0) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -88,15 +144,16 @@ namespace AircraftCarrier { return; } - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapWarshipsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -110,11 +167,11 @@ namespace AircraftCarrier /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -123,11 +180,11 @@ namespace AircraftCarrier /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -136,30 +193,29 @@ namespace AircraftCarrier /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapWarshipsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } //получаем имя кнопки string name = ((Button)sender)?.Name ?? string.Empty; - Direction direction = Direction.None; + Direction dir = Direction.None; switch (name) { case "buttonUp": - direction = Direction.Up; + dir = Direction.Up; break; case "buttonDown": - direction = Direction.Down; + dir = Direction.Down; break; case "buttonLeft": - direction = Direction.Left; + dir = Direction.Left; break; case "buttonRight": - direction = Direction.Right; + dir = Direction.Right; break; } - pictureBox.Image = _mapWarshipsCollectionGeneric.MoveObject(direction); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } - } diff --git a/Warship/Warship/MapWithSetWarshipGeneric.cs b/Warship/Warship/MapWithSetWarshipGeneric.cs index 1afb6dc..8e221ed 100644 --- a/Warship/Warship/MapWithSetWarshipGeneric.cs +++ b/Warship/Warship/MapWithSetWarshipGeneric.cs @@ -30,7 +30,7 @@ namespace AircraftCarrier /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 137; + private readonly int _placeSizeHeight = 161; /// /// Набор объектов /// @@ -94,13 +94,9 @@ namespace AircraftCarrier { { Shaking(); - for (int i = 0; i < _setWarships.Count; i++) + foreach (var warship in _setWarships.GetWarships()) { - var warship = _setWarships.Get(i); - if (warship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, warship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); } return new(_pictureWidth, _pictureHeight); } @@ -126,11 +122,11 @@ namespace AircraftCarrier int j = _setWarships.Count - 1; for (int i = 0; i < _setWarships.Count; i++) { - if (_setWarships.Get(i) == null) + if (_setWarships[i] == null) { for (; j > i; j--) { - var warship = _setWarships.Get(j); + var warship = _setWarships[j]; if (warship != null) { _setWarships.Insert(warship, i); @@ -172,15 +168,15 @@ namespace AircraftCarrier { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; + int i = 0; - for (int i = 0; i < _setWarships.Count; i++) + foreach (var warship in _setWarships.GetWarships()) { - if (_setWarships.Get(i) != null) - { - _setWarships.Get(i).SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 25, _pictureWidth, _pictureHeight); - _setWarships.Get(i)?.DrawningObject(g); + warship.SetObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 25, _pictureWidth, _pictureHeight); + warship.DrawningObject(g); + i++; } } } } -} + diff --git a/Warship/Warship/MapsCollection.cs b/Warship/Warship/MapsCollection.cs new file mode 100644 index 0000000..e12e716 --- /dev/null +++ b/Warship/Warship/MapsCollection.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + 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) + { + _mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map)); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + /// + /// Доступ к докам + /// + /// + /// + public MapWithSetWarshipsGeneric this[string ind] + { + get + { + _mapStorages.TryGetValue(ind, out var MapWithSetWarshipsGeneric); + return MapWithSetWarshipsGeneric; + } + } + } +} diff --git a/Warship/Warship/SetWarshipGeneric.cs b/Warship/Warship/SetWarshipGeneric.cs index f80b510..df2462c 100644 --- a/Warship/Warship/SetWarshipGeneric.cs +++ b/Warship/Warship/SetWarshipGeneric.cs @@ -14,21 +14,23 @@ namespace AircraftCarrier 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 SetWarshipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -37,31 +39,7 @@ namespace AircraftCarrier /// public int Insert(T ship) { - bool CheckEmptySpace = false; - int indexOfEmptyPlace = 0; - for (int i = 0; i < Count; ++i) - { - if (_places[i] == null || _places[i] == default(T)) - { - CheckEmptySpace = true; - indexOfEmptyPlace = i; - break; - } - } - if (CheckEmptySpace) - { - for (int i = indexOfEmptyPlace; i > 0; --i) - { - _places[i] = _places[i - 1]; - } - _places[0] = ship; - return 0; - } - else - { - return -1; - - } + return Insert(ship, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -71,42 +49,8 @@ namespace AircraftCarrier /// public int Insert(T ship, int position) { - if (position < 0 || position >= Count) - { - return -1; - } - if (_places[position] == null || _places[position] == default(T)) - { - _places[position] = ship; - return position; - } - else - { - bool hasEmptySpace = false; - int indexOfEmptyPlace = 0; - for (int i = position + 1; i < Count; ++i) - { - if (_places[i] == null || _places[i] == default(T)) - { - hasEmptySpace = true; - indexOfEmptyPlace = i; - break; - } - } - if (hasEmptySpace) - { - for (int i = indexOfEmptyPlace; i > position; --i) - { - _places[i] = _places[i - 1]; - } - _places[position] = ship; - return position; - } - else - { - return -1; - } - } + if (position >= _maxCount || position < 0) return -1; _places.Insert(position, ship); + return 1; } /// /// Удаление объекта из набора с конкретной позиции @@ -115,32 +59,49 @@ namespace AircraftCarrier /// public T Remove(int position) { - if (position < 0 || position >= Count) - { + if (position >= _maxCount || position < 0) return null; + + var result = _places[position]; + _places.RemoveAt(position); + return result; + } + public T this[int position] + { + get + { + if (position >= _maxCount || position < 0) return null; + return _places[position]; + } + set + { + Insert(value, position); } - T memoryObj = _places[position]; - _places[position] = null; - return memoryObj; } /// - /// Получение объекта из набора по позиции + /// Проход по набору до первого пустого /// - /// /// - public T Get(int position) + public IEnumerable GetWarships() { - if (position < 0 || position >= Count) + foreach (var warship in _places) { - return null; + if (warship != null) + { + yield return warship; + } + else + { + yield break; + } } - return _places[position]; - } } } - + + +