From a23192133f2373c89372c00663e88619a1e154f5 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Wed, 19 Oct 2022 17:20:36 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=201.=20=D0=A1?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/Liner/MapWithSetShipsGeneric.cs | 18 +++--- Liner/Liner/SetShipsGeneric.cs | 79 +++++++++++++++------------ 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/Liner/Liner/MapWithSetShipsGeneric.cs b/Liner/Liner/MapWithSetShipsGeneric.cs index b8e16b9..5d9931f 100644 --- a/Liner/Liner/MapWithSetShipsGeneric.cs +++ b/Liner/Liner/MapWithSetShipsGeneric.cs @@ -44,13 +44,9 @@ namespace Liner public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - var ship = _setShips.Get(i); - if (ship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, ship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } return new(_pictureWidth, _pictureHeight); } @@ -67,11 +63,11 @@ namespace Liner int j = _setShips.Count - 1; for (int i = 0; i < _setShips.Count; i++) { - if (_setShips.Get(i) == null) + if (_setShips[i] == null) { for (; j > i; j--) { - var ship = _setShips.Get(j); + var ship = _setShips[j]; if (ship != null) { _setShips.Insert(ship, i); @@ -107,11 +103,11 @@ namespace Liner int heightEl = _pictureHeight / _placeSizeHeight; int curWidth = 1; int curHeight = 0; - for (int i = 0; i < _setShips.Count; i++) + foreach (var ship in _setShips.GetShips()) { - _setShips.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - (_pictureWidth / 7), + ship?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 10, curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); - _setShips.Get(i)?.DrawningObject(g); + ship?.DrawningObject(g); if (curWidth < widthEl) curWidth++; diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 0f4b0f0..5c9bfad 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -9,64 +9,71 @@ namespace Liner internal class SetShipsGeneric where T : class { - private readonly T[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; public SetShipsGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } public int Insert(T ship) { + if (_places.Count > _maxCount) + { + return -1; + } return Insert(ship, 0); } public int Insert(T ship, int position) { - if (position >= _places.Length || position < 0) - { - return -1; - } - if (_places[position] == null) - { - _places[position] = ship; - return position; - } - int emptyIndex = -1; - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - emptyIndex = i; - break; - } - } - if (emptyIndex < 0) - { - return -1; - } - for (int i = emptyIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = ship; + if (position >= _maxCount || position < 0) return -1; + _places.Insert(position, ship); return position; } public T Remove(int position) { - if (position >= _places.Length || position < 0) + if (position >= _maxCount || position < 0) { return null; } T ship = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return ship; } - public T Get(int position) + public T this[int position] { - if (position >= _places.Length || position < 0) + get { - return null; + if (position >= _places.Count || position < 0) + { + return null; + } + return _places[position]; + } + set + { + if (position >= _places.Count || position < 0) + { + return; + } + Insert(value, position); + + } + } + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } } } \ No newline at end of file -- 2.25.1 From b61cfee9fb575a9785c8f9bc709724bb1697b283 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Wed, 19 Oct 2022 17:25:11 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=202.=20=D0=9A?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=20MapsCollection.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/Liner/MapsCollection.cs | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Liner/Liner/MapsCollection.cs diff --git a/Liner/Liner/MapsCollection.cs b/Liner/Liner/MapsCollection.cs new file mode 100644 index 0000000..5dd14d7 --- /dev/null +++ b/Liner/Liner/MapsCollection.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Liner +{ + 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) + { + if (!_mapStorages.ContainsKey(name)) + { + _mapStorages.Add(name, new MapWithSetShipsGeneric(_pictureWidth, _pictureHeight, map)); + } + } + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name); + } + public MapWithSetShipsGeneric this[string ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +} -- 2.25.1 From dd17a5a4e902d550e0b43a7eab26fc5e552270ef Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Wed, 19 Oct 2022 17:55:30 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=203.=20=D0=A4?= =?UTF-8?q?=D0=BE=D1=80=D0=BC=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/Liner/FormMapWithSetShips.Designer.cs | 132 +++++++++++++++----- Liner/Liner/FormMapWithSetShips.cs | 92 ++++++++++++-- 2 files changed, 179 insertions(+), 45 deletions(-) diff --git a/Liner/Liner/FormMapWithSetShips.Designer.cs b/Liner/Liner/FormMapWithSetShips.Designer.cs index b2e1d3e..40d145a 100644 --- a/Liner/Liner/FormMapWithSetShips.Designer.cs +++ b/Liner/Liner/FormMapWithSetShips.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBox = new System.Windows.Forms.GroupBox(); + this.groupBox1 = 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.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.ButtonRemoveShip = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.ButtonAddShip = new System.Windows.Forms.Button(); - this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox.SuspendLayout(); + this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox // + this.groupBox.Controls.Add(this.groupBox1); this.groupBox.Controls.Add(this.buttonRight); this.groupBox.Controls.Add(this.buttonDown); this.groupBox.Controls.Add(this.buttonUp); @@ -55,21 +62,85 @@ this.groupBox.Controls.Add(this.ButtonRemoveShip); this.groupBox.Controls.Add(this.maskedTextBoxPosition); this.groupBox.Controls.Add(this.ButtonAddShip); - this.groupBox.Controls.Add(this.ComboBoxSelectorMap); this.groupBox.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox.Location = new System.Drawing.Point(627, 0); + this.groupBox.Location = new System.Drawing.Point(696, 0); this.groupBox.Name = "groupBox"; - this.groupBox.Size = new System.Drawing.Size(250, 450); + this.groupBox.Size = new System.Drawing.Size(279, 668); this.groupBox.TabIndex = 0; this.groupBox.TabStop = false; this.groupBox.Text = "Инструменты"; // + // groupBox1 + // + this.groupBox1.Controls.Add(this.ButtonDeleteMap); + this.groupBox1.Controls.Add(this.ListBoxMaps); + this.groupBox1.Controls.Add(this.ButtonAddMap); + this.groupBox1.Controls.Add(this.textBoxNewMapName); + this.groupBox1.Controls.Add(this.ComboBoxSelectorMap); + this.groupBox1.Location = new System.Drawing.Point(6, 26); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(250, 300); + this.groupBox1.TabIndex = 18; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Карты"; + // + // ButtonDeleteMap + // + this.ButtonDeleteMap.Location = new System.Drawing.Point(10, 255); + this.ButtonDeleteMap.Name = "ButtonDeleteMap"; + this.ButtonDeleteMap.Size = new System.Drawing.Size(222, 29); + this.ButtonDeleteMap.TabIndex = 13; + 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(10, 136); + this.ListBoxMaps.Name = "ListBoxMaps"; + this.ListBoxMaps.Size = new System.Drawing.Size(222, 104); + this.ListBoxMaps.TabIndex = 12; + this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // ButtonAddMap + // + this.ButtonAddMap.Location = new System.Drawing.Point(10, 101); + this.ButtonAddMap.Name = "ButtonAddMap"; + this.ButtonAddMap.Size = new System.Drawing.Size(222, 29); + this.ButtonAddMap.TabIndex = 11; + this.ButtonAddMap.Text = "Добавить карту"; + this.ButtonAddMap.UseVisualStyleBackColor = true; + this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); + // + // textBoxNewMapName + // + this.textBoxNewMapName.Location = new System.Drawing.Point(10, 34); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(222, 27); + this.textBoxNewMapName.TabIndex = 10; + // + // 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(10, 67); + this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(222, 28); + this.ComboBoxSelectorMap.TabIndex = 9; + this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // // buttonRight // this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::Liner.Properties.Resources._3; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(204, 408); + this.buttonRight.Location = new System.Drawing.Point(233, 626); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 17; @@ -81,7 +152,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::Liner.Properties.Resources._2; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(168, 408); + this.buttonDown.Location = new System.Drawing.Point(197, 626); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 16; @@ -93,7 +164,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::Liner.Properties.Resources._4; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(168, 372); + this.buttonUp.Location = new System.Drawing.Point(197, 590); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 15; @@ -105,7 +176,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::Liner.Properties.Resources._1; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(132, 408); + this.buttonLeft.Location = new System.Drawing.Point(161, 626); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 14; @@ -114,9 +185,9 @@ // // ButtonShowOnMap // - this.ButtonShowOnMap.Location = new System.Drawing.Point(28, 323); + this.ButtonShowOnMap.Location = new System.Drawing.Point(16, 558); this.ButtonShowOnMap.Name = "ButtonShowOnMap"; - this.ButtonShowOnMap.Size = new System.Drawing.Size(200, 29); + this.ButtonShowOnMap.Size = new System.Drawing.Size(222, 29); this.ButtonShowOnMap.TabIndex = 13; this.ButtonShowOnMap.Text = "Посмотреть карту"; this.ButtonShowOnMap.UseVisualStyleBackColor = true; @@ -124,9 +195,9 @@ // // ButtonShowStorage // - this.ButtonShowStorage.Location = new System.Drawing.Point(28, 258); + this.ButtonShowStorage.Location = new System.Drawing.Point(16, 505); this.ButtonShowStorage.Name = "ButtonShowStorage"; - this.ButtonShowStorage.Size = new System.Drawing.Size(200, 29); + this.ButtonShowStorage.Size = new System.Drawing.Size(222, 29); this.ButtonShowStorage.TabIndex = 12; this.ButtonShowStorage.Text = "Посмотреть хранилище"; this.ButtonShowStorage.UseVisualStyleBackColor = true; @@ -134,9 +205,9 @@ // // ButtonRemoveShip // - this.ButtonRemoveShip.Location = new System.Drawing.Point(28, 191); + this.ButtonRemoveShip.Location = new System.Drawing.Point(16, 452); this.ButtonRemoveShip.Name = "ButtonRemoveShip"; - this.ButtonRemoveShip.Size = new System.Drawing.Size(200, 29); + this.ButtonRemoveShip.Size = new System.Drawing.Size(222, 29); this.ButtonRemoveShip.TabIndex = 11; this.ButtonRemoveShip.Text = "Удалить корабль"; this.ButtonRemoveShip.UseVisualStyleBackColor = true; @@ -144,42 +215,28 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(28, 158); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(16, 419); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(200, 27); + this.maskedTextBoxPosition.Size = new System.Drawing.Size(222, 27); this.maskedTextBoxPosition.TabIndex = 10; // // ButtonAddShip // - this.ButtonAddShip.Location = new System.Drawing.Point(28, 110); + this.ButtonAddShip.Location = new System.Drawing.Point(16, 368); this.ButtonAddShip.Name = "ButtonAddShip"; - this.ButtonAddShip.Size = new System.Drawing.Size(200, 29); + this.ButtonAddShip.Size = new System.Drawing.Size(222, 29); this.ButtonAddShip.TabIndex = 2; this.ButtonAddShip.Text = "Добавить корабль"; this.ButtonAddShip.UseVisualStyleBackColor = true; this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_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(28, 38); - this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; - this.ComboBoxSelectorMap.Size = new System.Drawing.Size(200, 28); - this.ComboBoxSelectorMap.TabIndex = 9; - 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(627, 450); + this.pictureBox.Size = new System.Drawing.Size(696, 668); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -187,13 +244,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(877, 450); + this.ClientSize = new System.Drawing.Size(975, 668); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox); this.Name = "FormMapWithSetShips"; this.Text = "FormMapWithSetShips"; this.groupBox.ResumeLayout(false); this.groupBox.PerformLayout(); + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -213,5 +272,10 @@ private Button buttonDown; private Button buttonUp; private Button buttonLeft; + private GroupBox groupBox1; + private Button ButtonDeleteMap; + private ListBox ListBoxMaps; + private Button ButtonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/Liner/Liner/FormMapWithSetShips.cs b/Liner/Liner/FormMapWithSetShips.cs index 500b974..8e10dc0 100644 --- a/Liner/Liner/FormMapWithSetShips.cs +++ b/Liner/Liner/FormMapWithSetShips.cs @@ -14,9 +14,41 @@ namespace Liner public partial class FormMapWithSetShips : Form { private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + private readonly Dictionary _mapDict = new() + { + {"Простая карта", new SimpleMap() }, + {"Море и айсберги", new SeaMap() }, + {"Болото",new SwampMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetShips() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + ComboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapDict) + { + ComboBoxSelectorMap.Items.Add(elem.Key); + } + } + private void ReloadMaps() + { + int index = ListBoxMaps.SelectedIndex; + + ListBoxMaps.Items.Clear(); + for (int i = 0; i < _mapsCollection.Keys.Count; i++) + { + ListBoxMaps.Items.Add(_mapsCollection.Keys[i]); + } + + if (ListBoxMaps.Items.Count > 0 && (index == -1 || index >= ListBoxMaps.Items.Count)) + { + ListBoxMaps.SelectedIndex = 0; + } + else if (ListBoxMaps.Items.Count > 0 && index > -1 && index < ListBoxMaps.Items.Count) + { + ListBoxMaps.SelectedIndex = index; + } } private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { @@ -45,7 +77,7 @@ namespace Liner } private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -53,10 +85,10 @@ namespace Liner if (form.ShowDialog() == DialogResult.OK) { DrawingObjectShip ship = new(form.SelectedShip); - if (_mapShipsCollectionGeneric + ship != -1) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -66,6 +98,10 @@ namespace Liner } private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -75,10 +111,10 @@ namespace Liner return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapShipsCollectionGeneric - pos != null) + if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -87,23 +123,23 @@ namespace Liner } private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap(); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipsCollectionGeneric == null) + if (ListBoxMaps.SelectedIndex == -1) { return; } @@ -124,7 +160,41 @@ namespace Liner dir = Direction.Right; break; } - pictureBox.Image = _mapShipsCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (ComboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapDict.ContainsKey(ComboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[ComboBoxSelectorMap.Text]); + ReloadMaps(); + } + + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (ListBoxMaps.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить карту {ListBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _mapsCollection.DelMap(ListBoxMaps.SelectedItem?.ToString() ?? string.Empty); + ReloadMaps(); + } } } } -- 2.25.1 From 0449822d9eb20216179b345caf5b8ae47c341b46 Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Wed, 19 Oct 2022 18:03:48 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9B=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=BE=D1=87=D0=BA=D0=B8=20=D0=BA=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=20=D1=83=D0=B1=D1=80=D0=B0=D0=BD=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/Liner/FormMapWithSetShips.Designer.cs | 1 - Liner/Liner/FormMapWithSetShips.cs | 29 --------------------- 2 files changed, 30 deletions(-) diff --git a/Liner/Liner/FormMapWithSetShips.Designer.cs b/Liner/Liner/FormMapWithSetShips.Designer.cs index 40d145a..064deb1 100644 --- a/Liner/Liner/FormMapWithSetShips.Designer.cs +++ b/Liner/Liner/FormMapWithSetShips.Designer.cs @@ -133,7 +133,6 @@ this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; this.ComboBoxSelectorMap.Size = new System.Drawing.Size(222, 28); this.ComboBoxSelectorMap.TabIndex = 9; - this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // buttonRight // diff --git a/Liner/Liner/FormMapWithSetShips.cs b/Liner/Liner/FormMapWithSetShips.cs index 8e10dc0..340096c 100644 --- a/Liner/Liner/FormMapWithSetShips.cs +++ b/Liner/Liner/FormMapWithSetShips.cs @@ -13,7 +13,6 @@ namespace Liner { public partial class FormMapWithSetShips : Form { - private MapWithSetShipsGeneric _mapShipsCollectionGeneric; private readonly Dictionary _mapDict = new() { {"Простая карта", new SimpleMap() }, @@ -50,31 +49,6 @@ namespace Liner ListBoxMaps.SelectedIndex = index; } } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - AbstractMap map = null; - switch (ComboBoxSelectorMap.Text) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Море и айсберги": - map = new SeaMap(); - break; - case "Болото": - map = new SwampMap(); - break; - } - if (map != null) - { - _mapShipsCollectionGeneric = new MapWithSetShipsGeneric( - pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapShipsCollectionGeneric = null; - } - } private void ButtonAddShip_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) @@ -162,7 +136,6 @@ namespace Liner } 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)) @@ -178,12 +151,10 @@ namespace Liner _mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[ComboBoxSelectorMap.Text]); ReloadMaps(); } - private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } - private void ButtonDeleteMap_Click(object sender, EventArgs e) { if (ListBoxMaps.SelectedIndex == -1) -- 2.25.1 From 2d1622a1b6f032bc1cd25c8180343111ad57246e Mon Sep 17 00:00:00 2001 From: Pavel_Sorokin Date: Wed, 26 Oct 2022 15:21:35 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D1=87=D0=BA=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Liner/Liner/SetShipsGeneric.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Liner/Liner/SetShipsGeneric.cs b/Liner/Liner/SetShipsGeneric.cs index 5c9bfad..6e6c3b5 100644 --- a/Liner/Liner/SetShipsGeneric.cs +++ b/Liner/Liner/SetShipsGeneric.cs @@ -19,21 +19,17 @@ namespace Liner } public int Insert(T ship) { - if (_places.Count > _maxCount) - { - return -1; - } return Insert(ship, 0); } public int Insert(T ship, int position) { - if (position >= _maxCount || position < 0) return -1; + if (position < 0 || position > Count || _maxCount == Count) return -1; _places.Insert(position, ship); return position; } public T Remove(int position) { - if (position >= _maxCount || position < 0) + if (position >= Count || position < 0) { return null; } @@ -45,7 +41,7 @@ namespace Liner { get { - if (position >= _places.Count || position < 0) + if (position >= Count || position < 0) { return null; } @@ -53,7 +49,7 @@ namespace Liner } set { - if (position >= _places.Count || position < 0) + if (position >= Count || position < 0) { return; } -- 2.25.1