From e992d502d642673e76398ba55e65e00dfef12ea1 Mon Sep 17 00:00:00 2001 From: foxkerik6 Date: Mon, 28 Nov 2022 22:09:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetAirplane.Designer.cs | 123 +++++++++++++----- .../Stormtrooper/FormMapWithSetAirplane.cs | 104 ++++++++++----- Stormtrooper/Stormtrooper/MapCollection.cs | 81 ++++++++++++ .../Stormtrooper/MapWithSetAirplaneGeneric.cs | 29 ++--- .../Stormtrooper/SetAirplaneGeneric.cs | 91 ++++++------- 5 files changed, 300 insertions(+), 128 deletions(-) create mode 100644 Stormtrooper/Stormtrooper/MapCollection.cs diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs index a7582cb..67e78a5 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.groupBoxMap = new System.Windows.Forms.GroupBox(); + this.buttonRemoveMap = new System.Windows.Forms.Button(); + this.listBoxMap = new System.Windows.Forms.ListBox(); + this.textBoxMapName = new System.Windows.Forms.TextBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.buttonRemove = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonCreate = new System.Windows.Forms.Button(); - this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBoxTools.SuspendLayout(); + this.groupBoxMap.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxMap); this.groupBoxTools.Controls.Add(this.buttonRight); this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonDown); @@ -55,19 +62,82 @@ this.groupBoxTools.Controls.Add(this.buttonRemove); this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); this.groupBoxTools.Controls.Add(this.buttonCreate); - this.groupBoxTools.Controls.Add(this.comboBoxMapSelector); this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Location = new System.Drawing.Point(600, 0); this.groupBoxTools.Name = "groupBoxTools"; - this.groupBoxTools.Size = new System.Drawing.Size(200, 450); + this.groupBoxTools.Size = new System.Drawing.Size(200, 588); this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // groupBoxMap + // + this.groupBoxMap.Controls.Add(this.buttonRemoveMap); + this.groupBoxMap.Controls.Add(this.listBoxMap); + this.groupBoxMap.Controls.Add(this.textBoxMapName); + this.groupBoxMap.Controls.Add(this.buttonAddMap); + this.groupBoxMap.Controls.Add(this.comboBoxMapSelector); + this.groupBoxMap.Location = new System.Drawing.Point(6, 22); + this.groupBoxMap.Name = "groupBoxMap"; + this.groupBoxMap.Size = new System.Drawing.Size(182, 259); + this.groupBoxMap.TabIndex = 19; + this.groupBoxMap.TabStop = false; + this.groupBoxMap.Text = "Карты"; + // + // buttonRemoveMap + // + this.buttonRemoveMap.Location = new System.Drawing.Point(20, 207); + this.buttonRemoveMap.Name = "buttonRemoveMap"; + this.buttonRemoveMap.Size = new System.Drawing.Size(144, 23); + this.buttonRemoveMap.TabIndex = 13; + this.buttonRemoveMap.Text = "Удалить карту"; + this.buttonRemoveMap.UseVisualStyleBackColor = true; + this.buttonRemoveMap.Click += new System.EventHandler(this.ButtonRemoveMap_Click); + // + // listBoxMap + // + this.listBoxMap.FormattingEnabled = true; + this.listBoxMap.ItemHeight = 15; + this.listBoxMap.Location = new System.Drawing.Point(18, 104); + this.listBoxMap.Name = "listBoxMap"; + this.listBoxMap.Size = new System.Drawing.Size(146, 94); + this.listBoxMap.TabIndex = 12; + this.listBoxMap.SelectedIndexChanged += new System.EventHandler(this.listBoxMap_SelectedIndexChanged); + // + // textBoxMapName + // + this.textBoxMapName.Location = new System.Drawing.Point(18, 17); + this.textBoxMapName.Name = "textBoxMapName"; + this.textBoxMapName.Size = new System.Drawing.Size(146, 23); + this.textBoxMapName.TabIndex = 11; + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(18, 75); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(146, 23); + this.buttonAddMap.TabIndex = 10; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // comboBoxMapSelector + // + this.comboBoxMapSelector.BackColor = System.Drawing.SystemColors.Window; + this.comboBoxMapSelector.FormattingEnabled = true; + this.comboBoxMapSelector.Items.AddRange(new object[] { + "Простая карта", + "Опасная карта", + "Облачная карта"}); + this.comboBoxMapSelector.Location = new System.Drawing.Point(18, 46); + this.comboBoxMapSelector.Name = "comboBoxMapSelector"; + this.comboBoxMapSelector.Size = new System.Drawing.Size(146, 23); + this.comboBoxMapSelector.TabIndex = 9; + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.Location = new System.Drawing.Point(119, 402); + this.buttonRight.Location = new System.Drawing.Point(119, 540); this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(35, 35); @@ -78,7 +148,7 @@ // buttonLeft // this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.Location = new System.Drawing.Point(35, 402); + this.buttonLeft.Location = new System.Drawing.Point(35, 540); this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(35, 35); @@ -89,7 +159,7 @@ // buttonDown // this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.Location = new System.Drawing.Point(77, 403); + this.buttonDown.Location = new System.Drawing.Point(77, 541); this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(35, 35); @@ -100,7 +170,7 @@ // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.Location = new System.Drawing.Point(77, 361); + this.buttonUp.Location = new System.Drawing.Point(77, 499); this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(35, 35); @@ -110,7 +180,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(8, 323); + this.buttonShowOnMap.Location = new System.Drawing.Point(8, 453); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(174, 23); this.buttonShowOnMap.TabIndex = 14; @@ -120,7 +190,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(8, 275); + this.buttonShowStorage.Location = new System.Drawing.Point(8, 424); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(174, 23); this.buttonShowStorage.TabIndex = 13; @@ -130,17 +200,17 @@ // // buttonRemove // - this.buttonRemove.Location = new System.Drawing.Point(7, 172); + this.buttonRemove.Location = new System.Drawing.Point(7, 378); this.buttonRemove.Name = "buttonRemove"; this.buttonRemove.Size = new System.Drawing.Size(174, 23); this.buttonRemove.TabIndex = 12; - this.buttonRemove.Text = "Удалить"; + this.buttonRemove.Text = "Удалить самолёт"; this.buttonRemove.UseVisualStyleBackColor = true; this.buttonRemove.Click += new System.EventHandler(this.ButtonRemove_Click); // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 122); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(8, 347); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); @@ -149,34 +219,20 @@ // // buttonCreate // - this.buttonCreate.Location = new System.Drawing.Point(6, 74); + this.buttonCreate.Location = new System.Drawing.Point(8, 318); this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Size = new System.Drawing.Size(175, 23); this.buttonCreate.TabIndex = 10; - this.buttonCreate.Text = "Добавить"; + this.buttonCreate.Text = "Добавить самолёт"; this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.Click += new System.EventHandler(this.ButtonAdd_Click); // - // comboBoxMapSelector - // - this.comboBoxMapSelector.BackColor = System.Drawing.SystemColors.Window; - this.comboBoxMapSelector.FormattingEnabled = true; - this.comboBoxMapSelector.Items.AddRange(new object[] { - "Простая карта", - "Опасная карта", - "Облачная карта"}); - this.comboBoxMapSelector.Location = new System.Drawing.Point(6, 22); - this.comboBoxMapSelector.Name = "comboBoxMapSelector"; - this.comboBoxMapSelector.Size = new System.Drawing.Size(175, 23); - this.comboBoxMapSelector.TabIndex = 9; - this.comboBoxMapSelector.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(600, 450); + this.pictureBox.Size = new System.Drawing.Size(600, 588); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -184,13 +240,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(800, 588); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBoxTools); this.Name = "FormMapWithSetAirplane"; this.Text = "FormMapWithSetAirplane"; this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxMap.ResumeLayout(false); + this.groupBoxMap.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -210,5 +268,10 @@ private Button buttonLeft; private Button buttonDown; private Button buttonUp; + private GroupBox groupBoxMap; + private TextBox textBoxMapName; + private Button buttonAddMap; + private Button buttonRemoveMap; + private ListBox listBoxMap; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs index fc10273..2b8311b 100644 --- a/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs +++ b/Stormtrooper/Stormtrooper/FormMapWithSetAirplane.cs @@ -1,5 +1,4 @@ -using Cars; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -14,41 +13,64 @@ namespace Stormtrooper { public partial class FormMapWithSetAirplane : Form { + private readonly Dictionary _mapsDict = new() + { + {"Простая карта", new SimpleMap()}, + {"Опасная карта", new DangerMap()}, + {"Облачная карта", new CloudMap()} + }; + /// + /// Объект от коллекции карт + /// + private readonly MapCollection _mapCollection; private MapWithSetAirplaneGeneric _mapAirsCollectionGeneric; public FormMapWithSetAirplane() { InitializeComponent(); + _mapCollection = new MapCollection(pictureBox.Width, pictureBox.Height); + comboBoxMapSelector.Items.Clear(); + foreach(var map in _mapsDict) + { + comboBoxMapSelector.Items.Add(map.Key); + } } /// /// Выбор карты /// /// /// - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ReloadMaps() { - AbstractMap map = null; - switch (comboBoxMapSelector.Text) + int index = listBoxMap.SelectedIndex; + listBoxMap.Items.Clear(); + for (int i = 0; i < _mapCollection.Keys.Count; i++) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Опасная карта": - map = new DangerMap(); - break; - case "Облачная карта": - map = new CloudMap(); - break; + listBoxMap.Items.Add(_mapCollection.Keys[i]); } - if (map != null) + if (listBoxMap.Items.Count > 0 && (index == -1 || index >= listBoxMap.Items.Count)) { - _mapAirsCollectionGeneric = new MapWithSetAirplaneGeneric( - pictureBox.Width, pictureBox.Height, map); + listBoxMap.SelectedIndex = 0; } - else + else if (listBoxMap.Items.Count > 0 && index > -1 && index < listBoxMap.Items.Count) { - _mapAirsCollectionGeneric = null; + listBoxMap.SelectedIndex = index; } } + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxMapSelector.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxMapSelector.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapCollection.AddMap(textBoxMapName.Text, _mapsDict[comboBoxMapSelector.Text]); + ReloadMaps(); + } /// /// Добавление объекта /// @@ -56,7 +78,7 @@ namespace Stormtrooper /// private void ButtonAdd_Click(object sender, EventArgs e) { - if (_mapAirsCollectionGeneric == null) + if (listBoxMap.SelectedIndex == -1) { return; } @@ -66,10 +88,10 @@ namespace Stormtrooper if(form.SelectedAirplane != null) { DrawningObject airplane = new (form.SelectedAirplane); - if (_mapAirsCollectionGeneric + airplane != -1) + if (_mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty] + airplane != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -90,6 +112,10 @@ namespace Stormtrooper /// private void ButtonRemove_Click(object sender, EventArgs e) { + if (listBoxMap.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -99,10 +125,10 @@ namespace Stormtrooper return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapAirsCollectionGeneric - pos != null) + if (_mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -116,11 +142,11 @@ namespace Stormtrooper /// private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapAirsCollectionGeneric == null) + if (listBoxMap.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Вывод карты @@ -129,11 +155,11 @@ namespace Stormtrooper /// private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapAirsCollectionGeneric == null) + if (listBoxMap.SelectedIndex == -1) { return; } - pictureBox.Image = _mapAirsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } /// /// Перемещение @@ -142,7 +168,7 @@ namespace Stormtrooper /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapAirsCollectionGeneric == null) + if (listBoxMap.SelectedIndex == -1) { return; } @@ -164,7 +190,25 @@ namespace Stormtrooper dir = Direction.Right; break; } - pictureBox.Image = _mapAirsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + + private void listBoxMap_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapCollection[listBoxMap.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + private void ButtonRemoveMap_Click(object sender, EventArgs e) + { + if (listBoxMap.SelectedIndex == -1) + { + return; + } + + if (MessageBox.Show($"Удалить карту {listBoxMap.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapCollection.DelMap(listBoxMap.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); + } } } } diff --git a/Stormtrooper/Stormtrooper/MapCollection.cs b/Stormtrooper/Stormtrooper/MapCollection.cs new file mode 100644 index 0000000..07d9ffe --- /dev/null +++ b/Stormtrooper/Stormtrooper/MapCollection.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class MapCollection + { + /// + /// Словарь (хранилище) с картами + /// + readonly Dictionary> _mapStorages; + /// + /// Возвращение списка названий карт + /// + public List Keys => _mapStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public MapCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление карты + /// + /// Название карты + /// Карта + public void AddMap(string name, AbstractMap map) + { + // Добавление карты + MapWithSetAirplaneGeneric newMap = new(_pictureWidth, _pictureHeight, map); + if (!_mapStorages.ContainsKey(name)) + _mapStorages.Add(name, newMap); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // Удаление карты + if (!_mapStorages.ContainsKey(name)) + { + return; + } + _mapStorages.Remove(name); + } + /// + /// Доступ к гавани + /// + /// + /// + public MapWithSetAirplaneGeneric this[string index] + { + get + { + // Получение объекта + if (_mapStorages.ContainsKey(index)) + { + return _mapStorages[index]; + } + return null; + } + } + } +} diff --git a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs index a1c1d29..37c072f 100644 --- a/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/MapWithSetAirplaneGeneric.cs @@ -87,14 +87,9 @@ namespace Stormtrooper /// public Bitmap ShowOnMap() { - Shaking(); - for (int i = 0; i < _setAirs.Count; i++) + foreach (var air in _setAirs.GetBoats()) { - var air = _setAirs.Get(i); - if (air != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, air); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, air); } return new(_pictureWidth, _pictureHeight); } @@ -120,11 +115,11 @@ namespace Stormtrooper int j = _setAirs.Count - 1; for (int i = 0; i < _setAirs.Count; i++) { - if (_setAirs.Get(i) == null) + if (_setAirs[i] == null) { for (; j > i; j--) { - var air = _setAirs.Get(j); + var air = _setAirs[j]; if (air != null) { _setAirs.Insert(air, i); @@ -171,22 +166,16 @@ namespace Stormtrooper int currentWidth = width - 1; int currentHeight = 0; - for (int i = 0; i < _setAirs.Count; i++) + int i = 0; + foreach (var air in _setAirs.GetBoats()) { - _setAirs.Get(i)?.SetObject(currentWidth * _placeSizeWidth, + air.SetObject(currentWidth * _placeSizeWidth, currentHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); - _setAirs.Get(i)?.DrawningObject(g); + air.DrawningObject(g); - if (currentWidth > 0) - currentWidth--; - else - { - currentWidth = width - 1; - currentHeight++; - } - if (currentHeight > height) return; + i++; } } } diff --git a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs index 719eccc..1ef7e87 100644 --- a/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs +++ b/Stormtrooper/Stormtrooper/SetAirplaneGeneric.cs @@ -10,20 +10,22 @@ namespace Stormtrooper where T : class { /// - /// Массив объектов, которые храним + /// List объектов, которые храним /// - private readonly T[] _places; + private readonly List _places; /// /// Количество объектов в массиве /// - public int Count => _places.Length; + public int Count => _places.Count; + private readonly int _maxCount; /// /// Конструктор /// /// public SetAirplaneGeneric(int count) { - _places = new T[count]; + _places = new List(); + _maxCount = count; } /// /// Добавление объекта в набор @@ -32,6 +34,8 @@ namespace Stormtrooper /// public int Insert(T airplane) { + if (Count == _maxCount) + return -1; Insert(airplane, 0); return 0; } @@ -43,42 +47,10 @@ namespace Stormtrooper /// public int Insert(T airplane, int position) { - if (position < 0 || position >= Count) - { + if (position < 0 || position >= _maxCount - 1) return -1; - } - if (_places[position] == null || _places[position] == default(T)) - { - _places[position] = airplane; - 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] = airplane; - return position; - } - else - { - return -1; - } - } + _places.Insert(position, airplane); + return position; } /// @@ -88,26 +60,49 @@ namespace Stormtrooper /// public T Remove(int position) { - if (position < 0 || position >= Count) - { + if (Count == 0 || position < 0 || position >= _maxCount) return null; - } - T memoryObj = _places[position]; + T air = _places[position]; _places[position] = null; - return memoryObj; + return air; } /// /// Получение объекта из набора по позиции /// /// /// - public T Get(int position) + public T this[int position] { - if (position < 0 || position >= Count) + get { - return null; + // Проверка позиции + 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; + } } - return _places[position]; } } }