diff --git a/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs b/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs index 3d0a147..7adce49 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs +++ b/Battleship/Battleship/FormMapWithSetBattleship.Designer.cs @@ -29,46 +29,120 @@ 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.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonAddBattleship = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonRemoveBattleship = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); - this.buttonAddBattleship = 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.buttonDown); this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonAddBattleship); this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowStorage); this.groupBoxTools.Controls.Add(this.buttonRemoveBattleship); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); - this.groupBoxTools.Controls.Add(this.buttonAddBattleship); - this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBoxTools.Location = new System.Drawing.Point(632, 0); + this.groupBoxTools.Location = new System.Drawing.Point(744, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 493); + this.groupBoxTools.Size = new System.Drawing.Size(200, 611); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // groupBoxMaps + // + this.groupBoxMaps.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + 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(12, 22); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(188, 257); + this.groupBoxMaps.TabIndex = 10; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDeleteMap.Location = new System.Drawing.Point(12, 216); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(170, 35); + this.buttonDeleteMap.TabIndex = 14; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 15; + this.listBoxMaps.Location = new System.Drawing.Point(12, 116); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(170, 94); + this.listBoxMaps.TabIndex = 13; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonAddMap.Location = new System.Drawing.Point(12, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(170, 30); + this.buttonAddMap.TabIndex = 12; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.textBoxNewMapName.Location = new System.Drawing.Point(12, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(170, 23); + this.textBoxNewMapName.TabIndex = 11; + // + // 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(11, 51); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); + this.comboBoxSelectorMap.TabIndex = 1; + // // buttonDown // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Battleship.Properties.Resources.BatteleshipDown; - this.buttonDown.Location = new System.Drawing.Point(77, 449); + this.buttonDown.Location = new System.Drawing.Point(77, 567); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 9; @@ -79,18 +153,29 @@ // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Battleship.Properties.Resources.BattleshipLeft; - this.buttonLeft.Location = new System.Drawing.Point(41, 449); + this.buttonLeft.Location = new System.Drawing.Point(41, 567); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 8; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // + // buttonAddBattleship + // + this.buttonAddBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonAddBattleship.Location = new System.Drawing.Point(24, 350); + this.buttonAddBattleship.Name = "buttonAddBattleship"; + this.buttonAddBattleship.Size = new System.Drawing.Size(164, 30); + this.buttonAddBattleship.TabIndex = 1; + this.buttonAddBattleship.Text = "Добавить корабль"; + this.buttonAddBattleship.UseVisualStyleBackColor = true; + this.buttonAddBattleship.Click += new System.EventHandler(this.ButtonAddBattleship_Click); + // // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Battleship.Properties.Resources.BattleshipUp; - this.buttonUp.Location = new System.Drawing.Point(77, 413); + this.buttonUp.Location = new System.Drawing.Point(77, 531); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 7; @@ -101,7 +186,7 @@ // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Battleship.Properties.Resources.BattleshipRight; - this.buttonRight.Location = new System.Drawing.Point(113, 449); + this.buttonRight.Location = new System.Drawing.Point(113, 567); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 6; @@ -111,9 +196,9 @@ // buttonShowOnMap // this.buttonShowOnMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 367); + this.buttonShowOnMap.Location = new System.Drawing.Point(24, 486); this.buttonShowOnMap.Name = "buttonShowOnMap"; - this.buttonShowOnMap.Size = new System.Drawing.Size(188, 30); + this.buttonShowOnMap.Size = new System.Drawing.Size(164, 30); this.buttonShowOnMap.TabIndex = 5; this.buttonShowOnMap.Text = "Посмотреть карту"; this.buttonShowOnMap.UseVisualStyleBackColor = true; @@ -122,9 +207,9 @@ // buttonShowStorage // this.buttonShowStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonShowStorage.Location = new System.Drawing.Point(6, 290); + this.buttonShowStorage.Location = new System.Drawing.Point(24, 450); this.buttonShowStorage.Name = "buttonShowStorage"; - this.buttonShowStorage.Size = new System.Drawing.Size(188, 30); + this.buttonShowStorage.Size = new System.Drawing.Size(164, 30); this.buttonShowStorage.TabIndex = 4; this.buttonShowStorage.Text = "Посмотреть хранилище"; this.buttonShowStorage.UseVisualStyleBackColor = true; @@ -132,10 +217,10 @@ // // buttonRemoveBattleship // - this.buttonRemoveBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRemoveBattleship.Location = new System.Drawing.Point(6, 195); + this.buttonRemoveBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRemoveBattleship.Location = new System.Drawing.Point(24, 415); this.buttonRemoveBattleship.Name = "buttonRemoveBattleship"; - this.buttonRemoveBattleship.Size = new System.Drawing.Size(188, 29); + this.buttonRemoveBattleship.Size = new System.Drawing.Size(164, 29); this.buttonRemoveBattleship.TabIndex = 3; this.buttonRemoveBattleship.Text = "Удалить корабль"; this.buttonRemoveBattleship.UseVisualStyleBackColor = true; @@ -143,43 +228,19 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 166); + this.maskedTextBoxPosition.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(24, 386); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(164, 23); this.maskedTextBoxPosition.TabIndex = 2; // - // buttonAddBattleship - // - this.buttonAddBattleship.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddBattleship.Location = new System.Drawing.Point(6, 112); - this.buttonAddBattleship.Name = "buttonAddBattleship"; - this.buttonAddBattleship.Size = new System.Drawing.Size(188, 29); - this.buttonAddBattleship.TabIndex = 1; - this.buttonAddBattleship.Text = "Добавить корабль"; - this.buttonAddBattleship.UseVisualStyleBackColor = true; - this.buttonAddBattleship.Click += new System.EventHandler(this.ButtonAddBattleship_Click); - // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Водная карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 32); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); - 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(632, 493); + this.pictureBox.Size = new System.Drawing.Size(744, 611); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // @@ -187,13 +248,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(832, 493); + this.ClientSize = new System.Drawing.Size(944, 611); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetBattleship"; this.Text = "FormMapWithSetBattleship"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -213,5 +276,10 @@ private Button buttonAddBattleship; private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; + private GroupBox groupBoxMaps; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Battleship/Battleship/FormMapWithSetBattleship.cs b/Battleship/Battleship/FormMapWithSetBattleship.cs index d569a75..04ed588 100644 --- a/Battleship/Battleship/FormMapWithSetBattleship.cs +++ b/Battleship/Battleship/FormMapWithSetBattleship.cs @@ -13,55 +13,45 @@ namespace Battleship { public partial class FormMapWithSetBattleship : Form { - /// - /// Объект от класса карты с набором объектов - /// - private MapWithSetBattleshipGeneric _mapBattleshipCollectionGeneric; - /// - /// Конструктор - /// + private readonly Dictionary _mapDict = new() + { + {"Первая карта",new SimpleMap() }, + {"Вторая карта",new WaterMap() } + }; + + private readonly MapsCollection _mapsCollection; + public FormMapWithSetBattleship() { 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() { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + int index = listBoxMaps.SelectedIndex; + for (int i = 0; i < _mapsCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; + listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - switch (comboBoxSelectorMap.Text) + if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { - case "Водная карта": - map = new WaterMap(); - break; + listBoxMaps.SelectedIndex = 0; } - if (map != null) + else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) { - _mapBattleshipCollectionGeneric = new MapWithSetBattleshipGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapBattleshipCollectionGeneric = null; + listBoxMaps.SelectedIndex = index; } } - /// - /// Добавление объекта - /// - /// - /// + private void ButtonAddBattleship_Click(object sender, EventArgs e) { - if (_mapBattleshipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -69,10 +59,10 @@ namespace Battleship if (form.ShowDialog() == DialogResult.OK) { DrawningObjectBattleship battleship = new(form.SelectedBattleship); - if (_mapBattleshipCollectionGeneric + battleship != null ) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + battleship != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -80,13 +70,13 @@ namespace Battleship } } } - /// - /// Удаление объекта - /// - /// - /// + private void ButtonRemoveBattleship_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -96,54 +86,41 @@ namespace Battleship return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapBattleshipCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); } } - /// - /// Вывод набора - /// - /// - /// + private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapBattleshipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBattleshipCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } - /// - /// Вывод карты - /// - /// - /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapBattleshipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBattleshipCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } - /// - /// Перемещение - /// - /// - /// + private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapBattleshipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - //получаем имя кнопки string name = ((Button)sender)?.Name ?? string.Empty; Direction dir = Direction.None; switch (name) @@ -161,7 +138,42 @@ namespace Battleship dir = Direction.Right; break; } - pictureBox.Image = _mapBattleshipCollectionGeneric.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; + } + listBoxMaps.Items.Clear(); + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + 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); + listBoxMaps.Items.Clear(); + ReloadMaps(); + } + } + + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } } } diff --git a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs index 81bdbd8..fa5202f 100644 --- a/Battleship/Battleship/MapWithSetBattleshipGeneric.cs +++ b/Battleship/Battleship/MapWithSetBattleshipGeneric.cs @@ -52,13 +52,9 @@ namespace Battleship public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setBattleship.Count; i++) + foreach (var battleship in _setBattleship.GetBattleship()) { - var battleship = _setBattleship.Get(i); - if (battleship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, battleship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, battleship); } return new(_pictureWidth, _pictureHeight); } @@ -77,11 +73,11 @@ namespace Battleship int j = _setBattleship.Count - 1; for (int i = 0; i < _setBattleship.Count; i++) { - if (_setBattleship.Get(i) == null) + if (_setBattleship[i] == null) { for (; j > i; j--) { - var battleship = _setBattleship.Get(j); + var battleship = _setBattleship[j]; if (battleship != null) { _setBattleship.Insert(battleship, i); @@ -115,14 +111,13 @@ namespace Battleship { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; + int i = 0; - for (int i = 0; i < _setBattleship.Count; i++) + foreach (var battleship in _setBattleship.GetBattleship()) { - if (_setBattleship.Get(i) != null) - { - _setBattleship.Get(i).SetObject(i % width * _placeSizeWidth + 15, (height - 1 - i / width) * _placeSizeHeight + 8, _pictureWidth, _pictureHeight); - _setBattleship.Get(i)?.DrawningObject(gr); - } + battleship.SetObject(i % width * _placeSizeWidth + 20, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth + 20, _pictureHeight + 10); + battleship.DrawningObject(gr); + i++; } } } diff --git a/Battleship/Battleship/MapsCollection.cs b/Battleship/Battleship/MapsCollection.cs new file mode 100644 index 0000000..8f176d2 --- /dev/null +++ b/Battleship/Battleship/MapsCollection.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Battleship +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorage; + + public List Keys => _mapStorage.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorage = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddMap(string name, AbstractMap map) + { + var NewElem = new MapWithSetBattleshipGeneric( + _pictureWidth, _pictureHeight, map); + _mapStorage.Add(name, NewElem); + } + public void DelMap(string name) + { + _mapStorage.Remove(name); + } + public MapWithSetBattleshipGeneric this[string ind] + { + get + { + return _mapStorage[ind]; + } + } + } +} \ No newline at end of file diff --git a/Battleship/Battleship/SetBattleshipGeneric.cs b/Battleship/Battleship/SetBattleshipGeneric.cs index 815ba49..bf9d43a 100644 --- a/Battleship/Battleship/SetBattleshipGeneric.cs +++ b/Battleship/Battleship/SetBattleshipGeneric.cs @@ -13,64 +13,72 @@ namespace Battleship internal class SetBattleshipGeneric where T : class { - private readonly T[] _places; + private readonly List _places; - public int Count => _places.Length; + public int Count => _places.Count; + private int BattleshipPlaces = 0; + + private readonly int _maxCount; public SetBattleshipGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T battleship) { - return Insert(battleship, 0); + if (_places.Count + 1 >= _maxCount) + return -1; + _places.Insert(0, battleship); + return 0; } public int Insert(T battleship, int position) { - if (position >= _places.Length) - { + if (position >= _maxCount || position < 0) return -1; - } - if (_places[position] != null) - { - int indexNull = -1; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - indexNull = i; - break; - } - } - if (indexNull == -1) return -1; - for (int i = indexNull; i > position; i--) - { - T tmp = _places[i]; - _places[i] = _places[i - 1]; - _places[i - 1] = tmp; - } - } - _places[position] = battleship; + if (_places.Count + 1 >= _maxCount) + return -1; + _places.Insert(position, battleship); return position; } + public T Remove(int position) { - if (position >= _places.Length) - { - return null; - } - T removedObject = _places[position]; - _places[position] = null; - return removedObject; + if (position < 0 || position >= _maxCount) return null; + T savedBattleship = _places[position]; + _places.RemoveAt(position); + return savedBattleship; } - public T Get(int position) - { - if (position >= Count || position < 0) - return null; - return _places[position]; + public T this[int position] + { + get + { + if (position < 0 || position >= _maxCount) return null; + return _places[position]; + } + set + { + Insert(value, position); + } + + } + + public IEnumerable GetBattleship() + { + foreach (var battleship in _places) + { + if (battleship != null) + { + yield return battleship; + } + else + { + yield break; + } + } } } }