diff --git a/Boats/Boats/FormMapWithSetBoats.Designer.cs b/Boats/Boats/FormMapWithSetBoats.Designer.cs index ab44094..0a0e537 100644 --- a/Boats/Boats/FormMapWithSetBoats.Designer.cs +++ b/Boats/Boats/FormMapWithSetBoats.Designer.cs @@ -28,7 +28,7 @@ /// private void InitializeComponent() { - this.groupBox = new System.Windows.Forms.GroupBox(); + this.groupBoxInstruments = new System.Windows.Forms.GroupBox(); this.ButtonDown = new System.Windows.Forms.Button(); this.ButtonRight = new System.Windows.Forms.Button(); this.ButtonLeft = new System.Windows.Forms.Button(); @@ -38,38 +38,43 @@ this.ButtonShowStorage = new System.Windows.Forms.Button(); this.ButtonRemoveBoat = new System.Windows.Forms.Button(); this.ButtonAddBoat = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); - this.groupBox.SuspendLayout(); + 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.groupBoxInstruments.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.groupBoxMaps.SuspendLayout(); this.SuspendLayout(); // - // groupBox + // groupBoxInstruments // - this.groupBox.Controls.Add(this.ButtonDown); - this.groupBox.Controls.Add(this.ButtonRight); - this.groupBox.Controls.Add(this.ButtonLeft); - this.groupBox.Controls.Add(this.ButtonUp); - this.groupBox.Controls.Add(this.maskedTextBoxPosition); - this.groupBox.Controls.Add(this.ButtonShowOnMap); - this.groupBox.Controls.Add(this.ButtonShowStorage); - this.groupBox.Controls.Add(this.ButtonRemoveBoat); - this.groupBox.Controls.Add(this.ButtonAddBoat); - this.groupBox.Controls.Add(this.comboBoxSelectorMap); - this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(901, 0); - this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(250, 589); - this.groupBox.TabIndex = 0; - this.groupBox.TabStop = false; - this.groupBox.Text = "Инструменты"; + this.groupBoxInstruments.Controls.Add(this.ButtonDown); + this.groupBoxInstruments.Controls.Add(this.ButtonRight); + this.groupBoxInstruments.Controls.Add(this.ButtonLeft); + this.groupBoxInstruments.Controls.Add(this.ButtonUp); + this.groupBoxInstruments.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxInstruments.Controls.Add(this.ButtonShowOnMap); + this.groupBoxInstruments.Controls.Add(this.ButtonShowStorage); + this.groupBoxInstruments.Controls.Add(this.ButtonRemoveBoat); + this.groupBoxInstruments.Controls.Add(this.ButtonAddBoat); + this.groupBoxInstruments.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxInstruments.Location = new System.Drawing.Point(901, 0); + this.groupBoxInstruments.Name = "groupBoxInstruments"; + this.groupBoxInstruments.Size = new System.Drawing.Size(250, 768); + this.groupBoxInstruments.TabIndex = 0; + this.groupBoxInstruments.TabStop = false; + this.groupBoxInstruments.Text = "Инструменты"; // // ButtonDown // - this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonDown.BackgroundImage = global::Boats.Properties.Resources.arrow_down; this.ButtonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonDown.Location = new System.Drawing.Point(99, 537); + this.ButtonDown.Location = new System.Drawing.Point(99, 716); this.ButtonDown.Name = "ButtonDown"; this.ButtonDown.Size = new System.Drawing.Size(30, 30); this.ButtonDown.TabIndex = 10; @@ -78,10 +83,10 @@ // // ButtonRight // - this.ButtonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonRight.BackgroundImage = global::Boats.Properties.Resources.arrow_right; this.ButtonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonRight.Location = new System.Drawing.Point(135, 537); + this.ButtonRight.Location = new System.Drawing.Point(135, 716); this.ButtonRight.Name = "ButtonRight"; this.ButtonRight.Size = new System.Drawing.Size(30, 30); this.ButtonRight.TabIndex = 9; @@ -90,10 +95,10 @@ // // ButtonLeft // - this.ButtonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonLeft.BackgroundImage = global::Boats.Properties.Resources.arrow_left; this.ButtonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonLeft.Location = new System.Drawing.Point(63, 537); + this.ButtonLeft.Location = new System.Drawing.Point(63, 716); this.ButtonLeft.Name = "ButtonLeft"; this.ButtonLeft.Size = new System.Drawing.Size(30, 30); this.ButtonLeft.TabIndex = 8; @@ -102,10 +107,10 @@ // // ButtonUp // - this.ButtonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.ButtonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.ButtonUp.BackgroundImage = global::Boats.Properties.Resources.arrow_up; this.ButtonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.ButtonUp.Location = new System.Drawing.Point(99, 501); + this.ButtonUp.Location = new System.Drawing.Point(99, 680); this.ButtonUp.Name = "ButtonUp"; this.ButtonUp.Size = new System.Drawing.Size(30, 30); this.ButtonUp.TabIndex = 7; @@ -114,7 +119,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 168); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 449); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(232, 27); @@ -122,7 +127,7 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(6, 389); + this.ButtonShowOnMap.Location = new System.Drawing.Point(6, 613); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; this.ButtonShowOnMap.Size = new System.Drawing.Size(232, 40); this.ButtonShowOnMap.TabIndex = 4; @@ -132,7 +137,7 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(6, 303); + this.ButtonShowStorage.Location = new System.Drawing.Point(6, 555); this.ButtonShowStorage.Name = "ButtonShowStorage"; this.ButtonShowStorage.Size = new System.Drawing.Size(232, 40); this.ButtonShowStorage.TabIndex = 3; @@ -142,7 +147,7 @@ // // ButtonRemoveBoat // - this.ButtonRemoveBoat.Location = new System.Drawing.Point(6, 212); + this.ButtonRemoveBoat.Location = new System.Drawing.Point(6, 496); this.ButtonRemoveBoat.Name = "ButtonRemoveBoat"; this.ButtonRemoveBoat.Size = new System.Drawing.Size(232, 40); this.ButtonRemoveBoat.TabIndex = 2; @@ -152,7 +157,7 @@ // // ButtonAddBoat // - this.ButtonAddBoat.Location = new System.Drawing.Point(6, 97); + this.ButtonAddBoat.Location = new System.Drawing.Point(6, 391); this.ButtonAddBoat.Name = "ButtonAddBoat"; this.ButtonAddBoat.Size = new System.Drawing.Size(232, 40); this.ButtonAddBoat.TabIndex = 1; @@ -160,58 +165,118 @@ this.ButtonAddBoat.UseVisualStyleBackColor = true; this.ButtonAddBoat.Click += new System.EventHandler(this.ButtonAddBoat_Click); // - // comboBoxSelectorMap + // ComboBoxSelectorMap // - this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { + 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(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); + this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 59); + this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(220, 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(901, 589); + this.pictureBox.Size = new System.Drawing.Size(901, 768); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // + // 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(907, 26); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(232, 318); + this.groupBoxMaps.TabIndex = 11; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(6, 272); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(220, 40); + 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 = 20; + this.listBoxMaps.Location = new System.Drawing.Point(6, 153); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(220, 104); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(6, 93); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(220, 40); + this.ButtonAddMap.TabIndex = 2; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(6, 26); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(220, 27); + this.textBoxNewMapName.TabIndex = 0; + // // FormMapWithSetBoats // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1151, 589); + this.ClientSize = new System.Drawing.Size(1151, 768); + this.Controls.Add(this.groupBoxMaps); this.Controls.Add(this.pictureBox); - this.Controls.Add(this.groupBox); + this.Controls.Add(this.groupBoxInstruments); this.Name = "FormMapWithSetBoats"; this.Text = "Карта с набором элементов"; - this.groupBox.ResumeLayout(false); - this.groupBox.PerformLayout(); + this.groupBoxInstruments.ResumeLayout(false); + this.groupBoxInstruments.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); this.ResumeLayout(false); } #endregion - private GroupBox groupBox; + private GroupBox groupBoxInstruments; private MaskedTextBox maskedTextBoxPosition; private Button ButtonShowOnMap; private Button ButtonShowStorage; private Button ButtonRemoveBoat; private Button ButtonAddBoat; - private ComboBox comboBoxSelectorMap; + private ComboBox ComboBoxSelectorMap; private PictureBox pictureBox; private Button ButtonDown; private Button ButtonRight; private Button ButtonLeft; private Button ButtonUp; + private GroupBox groupBoxMaps; + private Button ButtonDeleteMap; + private ListBox listBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Boats/Boats/FormMapWithSetBoats.cs b/Boats/Boats/FormMapWithSetBoats.cs index f45b130..718f797 100644 --- a/Boats/Boats/FormMapWithSetBoats.cs +++ b/Boats/Boats/FormMapWithSetBoats.cs @@ -12,6 +12,19 @@ namespace Boats { public partial class FormMapWithSetBoats : Form { + /// + /// Словарь для выпадающего списка + /// + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Линии карта", new LineMap() }, + { "Океан карта", new OceanMap() }, + }; + /// + /// Объект от коллекции карт + /// + private readonly MapsCollection _mapsCollection; /// /// Объект от класса карты с набором объектов /// @@ -22,6 +35,32 @@ namespace Boats public FormMapWithSetBoats() { 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; + } } /// /// Выбор карты @@ -31,7 +70,7 @@ namespace Boats private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + switch (ComboBoxSelectorMap.Text) { case "Простая карта": map = new SimpleMap(); @@ -60,25 +99,20 @@ namespace Boats /// private void ButtonAddBoat_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } FormBoat form = new(); if (form.ShowDialog() == DialogResult.OK) { - bool added = false; - if (form.SelectedBoat != null) + DrawingObjectBoat boat = new(form.SelectedBoat); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat != -1) { - DrawingObjectBoat boat = new(form.SelectedBoat); - if (_mapBoatsCollectionGeneric + boat != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); - added = true; - } + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } - if (!added) + else { MessageBox.Show("Не удалось добавить объект"); } @@ -91,6 +125,10 @@ namespace Boats /// private void ButtonRemoveBoat_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -102,10 +140,10 @@ namespace Boats } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); pos -= 1; - if (_mapBoatsCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -119,11 +157,11 @@ namespace Boats /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } /// /// Вывод карты @@ -132,11 +170,11 @@ namespace Boats /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -145,11 +183,11 @@ namespace Boats /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapBoatsCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - // Получаем имя кнопки + //получаем имя кнопки string name = ((Button)sender)?.Name ?? string.Empty; Direction dir = Direction.None; switch (name) @@ -167,7 +205,55 @@ namespace Boats dir = Direction.Right; break; } - pictureBox.Image = _mapBoatsCollectionGeneric.MoveObject(dir); + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + /// + /// Добавление карты + /// + /// + /// + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (ComboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(ComboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[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); + ReloadMaps(); + } + } + /// + /// Выбор карты + /// + /// + /// + private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } } } diff --git a/Boats/Boats/MapWithSetBoatsGeneric.cs b/Boats/Boats/MapWithSetBoatsGeneric.cs index d52da8f..826dfdc 100644 --- a/Boats/Boats/MapWithSetBoatsGeneric.cs +++ b/Boats/Boats/MapWithSetBoatsGeneric.cs @@ -43,7 +43,7 @@ namespace Boats /// Массив точек установки лодок в гавани /// private Point[]? _placesPoints; - private readonly int _placesCount = 14; + private readonly int _placesCount = 18; /// /// Конструктор /// @@ -101,13 +101,9 @@ namespace Boats public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setBoats.Count; i++) + foreach (var boat in _setBoats.GetBoats()) { - var boat = _setBoats.Get(i); - if (boat != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, boat); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, boat); } return new(_pictureWidth, _pictureHeight); } @@ -132,11 +128,11 @@ namespace Boats int j = _setBoats.Count - 1; for (int i = 0; i < _setBoats.Count; i++) { - if (_setBoats.Get(i) == null) + if (_setBoats[i] == null) { for (; j > i; j--) { - var boat = _setBoats.Get(j); + var boat = _setBoats[j]; if (boat != null) { _setBoats.Insert(boat, i); @@ -158,7 +154,7 @@ namespace Boats private void DrawBackground(Graphics g) { bool pointsInit = false; - // Если массив точек null, значит рисуем фон первый раз и + // если массив точек null, то рисуем фон первый раз // инициализируем массив для его заполнения if (_placesPoints == null) { @@ -195,16 +191,16 @@ namespace Boats g.FillRectangle(Brushes.Brown, _pictureWidth - x - w, y, w, pirsSize); if (pointsInit) { - _placesPoints[9 + i] = new Point(x + 5, y - _placeSizeHeight + 5); - _placesPoints[4 - i] = new Point(_pictureWidth - x - w + 5, y - _placeSizeHeight + 5); + _placesPoints[11 + i] = new Point(x + 5, y - _placeSizeHeight + 5); + _placesPoints[6 - i] = new Point(_pictureWidth - x - w + 5, y - _placeSizeHeight + 5); } y += h + pirsSize; i++; } if (pointsInit) { - _placesPoints[9 + i] = new Point(x + 5, y - _placeSizeHeight + 5); - _placesPoints[4 - i] = new Point(_pictureWidth - x - w + 5, y - _placeSizeHeight + 5); + _placesPoints[11 + i] = new Point(x + 5, y - _placeSizeHeight + 5); + _placesPoints[6 - i] = new Point(_pictureWidth - x - w + 5, y - _placeSizeHeight + 5); } // вертикальные @@ -217,14 +213,14 @@ namespace Boats g.FillRectangle(Brushes.Brown, x, y, pirsSize, h); if (pointsInit) { - _placesPoints[8 - i] = new Point(x - w + 5, y + 5); + _placesPoints[10 - i] = new Point(x - w + 5, y + 5); } x += w + pirsSize; i++; } if (pointsInit) { - _placesPoints[8 - i] = new Point(x - w + 5, y + 5); + _placesPoints[10 - i] = new Point(x - w + 5, y + 5); } } /// @@ -233,12 +229,14 @@ namespace Boats /// private void DrawBoats(Graphics g) { - for (int i = 0; i < _setBoats.Count; i++) + int i = 0; + foreach (var boat in _setBoats.GetBoats()) { // Установка позиции - _setBoats.Get(i)?.SetObject(_placesPoints[i].X, _placesPoints[i].Y, + boat.SetObject(_placesPoints[i].X, _placesPoints[i].Y, _pictureWidth, _pictureHeight); - _setBoats.Get(i)?.DrawingObject(g); + boat.DrawingObject(g); + i++; } } } diff --git a/Boats/Boats/MapsCollection.cs b/Boats/Boats/MapsCollection.cs new file mode 100644 index 0000000..dd761e7 --- /dev/null +++ b/Boats/Boats/MapsCollection.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Boats +{ + /// + /// Класс для хранения коллекции карт + /// + 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) + { + // Добавление карты + MapWithSetBoatsGeneric newMap = new(_pictureWidth, _pictureHeight, map); + _mapStorages.Add(name, newMap); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // Удаление карты + if (!_mapStorages.ContainsKey(name)) + { + return; + } + _mapStorages.Remove(name); + } + /// + /// Доступ к гавани + /// + /// + /// + public MapWithSetBoatsGeneric this[string index] + { + get + { + // Получение объекта + if (_mapStorages.ContainsKey(index)) + { + return _mapStorages[index]; + } + return null; + } + } + } +} diff --git a/Boats/Boats/SetBoatsGeneric.cs b/Boats/Boats/SetBoatsGeneric.cs index b43a073..4097ce3 100644 --- a/Boats/Boats/SetBoatsGeneric.cs +++ b/Boats/Boats/SetBoatsGeneric.cs @@ -16,18 +16,23 @@ namespace Boats /// /// Массив объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество элементов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetBoatsGeneric(int count) { - _places = new T[count]; + _places = new List(); + _maxCount = count; } /// /// Добавление объекта в набор @@ -36,6 +41,9 @@ namespace Boats /// public int Insert(T boat) { + // Проверка на _maxCount + if (Count == _maxCount) + return -1; // Вставка в начало набора return Insert(boat, 0); } @@ -48,42 +56,9 @@ namespace Boats public int Insert(T boat, int position) { // Проверка позиции - if (position < 0 || position >= _places.Length) + if (position < 0 || position >= _maxCount - 1) return -1; - // Проверка, что элемент массива по этой позиции пустой - if (_places[position] != null) - { - // Если нет, проверим, что после вставляемого элемента в массиве есть пустой элемент - int i = position + 1; - int nullIndex = -1; - while (i < _places.Length) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - i++; - } - // Если свободной нет, то выходим - if (nullIndex < 0) - { - return -1; - } - else - { - // Если есть, сдвигаем все объекты, находящиеся - // справа от позиции до первого пустого элемента - i = nullIndex - 1; - while (i >= position) - { - _places[i + 1] = _places[i]; - i--; - } - } - } - // Вставка по позиции - _places[position] = boat; + _places.Insert(position, boat); return position; } /// @@ -94,28 +69,47 @@ namespace Boats public T Remove(int position) { // Проверка позиции - if (position < 0 || position >= _places.Length) + if (Count == 0 || position < 0 || position >= _maxCount) return null; - if (_places[position] == null) - { - return null; - } - // Удаление объекта из массива, присовив элементу массива значение null T boat = _places[position]; _places[position] = null; return boat; } - /// - /// Получение объекта из набора по позиции - /// - /// - /// Возвращает объект по позиции - public T Get(int position) + public T this[int position] { - // Проверка позиции - if (position < 0 || position >= _places.Length) - return null; - return _places[position]; + get + { + // Проверка позиции + if (position < 0 || position >= Count) + return null; + return _places[position]; + } + set + { + // Проверка позиции + if (position < 0 || position >= Count) + return; + // Вставка по позиции + _places[position] = value; + } + } + /// + /// Проход по набору до первого пустого + /// + /// + public IEnumerable GetBoats() + { + foreach (var boat in _places) + { + if (boat != null) + { + yield return boat; + } + else + { + yield break; + } + } } } }