From 62bae752aeaef65cc68c77fde73860f201028d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sat, 29 Oct 2022 21:11:34 +0400 Subject: [PATCH 1/7] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyShip/MapWithSetShipGeneric.cs | 14 +++--- WarmlyShip/WarmlyShip/SetShipGeneric.cs | 50 +++++++++++++++---- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 8b7019a..1de68b7 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -51,7 +51,7 @@ namespace WarmlyShip Shaking(); for (int i = 0; i < _setShips.Count; i++) { - var ship = _setShips.Get(i); + var ship = _setShips[i]; if (ship != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, ship); @@ -74,11 +74,11 @@ namespace WarmlyShip 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 car = _setShips.Get(j); + var car = _setShips[j]; if (car != null) { _setShips.Insert(car, i); @@ -113,12 +113,12 @@ namespace WarmlyShip { for (int i = 0; i < _setShips.Count; ++i) { - if (_setShips.Get(i) != null ) + if (_setShips[i] != null ) { int temp = 0; - if (_setShips.Get(i).GetCurrentPosition().Bottom - _setShips.Get(i).GetCurrentPosition().Top < 75) temp = (int)(_setShips.Get(i).GetCurrentPosition().Bottom - _setShips.Get(i).GetCurrentPosition().Top); - _setShips.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); - _setShips.Get(i).DrawningObject(g); + if (_setShips[i].GetCurrentPosition().Bottom - _setShips[i].GetCurrentPosition().Top < 75) temp = (int)(_setShips[i].GetCurrentPosition().Bottom - _setShips[i].GetCurrentPosition().Top); + _setShips[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); + _setShips[i].DrawningObject(g); } } } diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index e22a337..2740f09 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -9,17 +9,20 @@ namespace WarmlyShip internal class SetShipGeneric 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 SetShipGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } private bool CanInsert(int position) { - for (int i = position; i < _places.Length; ++i) + for (int i = position; i < Count; ++i) if (_places[i] == null) return true; return false; } @@ -28,7 +31,7 @@ namespace WarmlyShip { if (CanInsert(0)) { - for (int i = _places.Length - 1; i > 0; --i) + for (int i = Count - 1; i > 0; --i) { if (_places[i] == null) { @@ -44,10 +47,10 @@ namespace WarmlyShip public int Insert(T ship, int position) { - if (position < 0 || position > _places.Length) return 0; - if (_places[position] != null && CanInsert(position)) + if (position < 0 || position > Count || Count == _maxCount) return 0; + /*if (_places[position] != null && CanInsert(position)) { - for (int i = _places.Length - 1; i > position; --i) + for (int i = _places.Count - 1; i > position; --i) { if (_places[i] == null) { @@ -57,6 +60,8 @@ namespace WarmlyShip } } _places[position] = ship; + return 1;*/ + _places.Insert(position, ship); return 1; } @@ -66,10 +71,33 @@ namespace WarmlyShip return 1; } - 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 + { + Insert(value, position); + } + } + + + public IEnumerable GetShips() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } + } } } -- 2.25.1 From 0dc69f5f7d9e442906dbaa71256fde5fd8839e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sat, 29 Oct 2022 22:29:16 +0400 Subject: [PATCH 2/7] =?UTF-8?q?1=20=D1=8D=D1=82=D0=B0=D0=BF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyShip/MapWithSetShipGeneric.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 1de68b7..655f208 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -49,13 +49,9 @@ namespace WarmlyShip public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setShips.Count; i++) + foreach (var car in _setShips.GetShips()) { - var ship = _setShips[i]; - if (ship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, ship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, car); } return new(_pictureWidth, _pictureHeight); } @@ -111,7 +107,7 @@ namespace WarmlyShip private void DrawShips(Graphics g) { - for (int i = 0; i < _setShips.Count; ++i) + /*for (int i = 0; i < _setShips.Count; ++i) { if (_setShips[i] != null ) { @@ -120,6 +116,19 @@ namespace WarmlyShip _setShips[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); _setShips[i].DrawningObject(g); } + }*/ + + int i = 0; + foreach (var ship in _setShips.GetShips()) + { + if (ship != null) + { + int temp = 0; + if (ship.GetCurrentPosition().Bottom - ship.GetCurrentPosition().Top < 75) temp = (int)(ship.GetCurrentPosition().Bottom - ship.GetCurrentPosition().Top); + ship.SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); + ship.DrawningObject(g); + } + ++i; } } -- 2.25.1 From 783652be175ba190212be1261f57fb81122a422e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sat, 29 Oct 2022 23:14:46 +0400 Subject: [PATCH 3/7] =?UTF-8?q?2=20=D1=8D=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/MapsCollection.cs | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 WarmlyShip/WarmlyShip/MapsCollection.cs diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs new file mode 100644 index 0000000..13d66c0 --- /dev/null +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace WarmlyShip +{ + internal class MapsCollection + { + readonly Dictionary> _mapStorages; + + public List Keys => _mapStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddMap(string name, AbstractMap map) + { + _mapStorages.Add(name, new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); + } + public void DelMap(string name) + { + _mapStorages.Remove(name); + } + + public MapWithSetShipGeneric this[string ind] + { + get + { + foreach (var map in _mapStorages) + { + if(map.Key == ind) return map.Value; + } + return null; + } + } + + } +} -- 2.25.1 From f5435fbccec86921ab8dd5e3dc40b8f6d3b54d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 30 Oct 2022 00:10:36 +0400 Subject: [PATCH 4/7] =?UTF-8?q?3=20=D1=8D=D1=82=D0=B0=D0=BF=20-=20=D1=84?= =?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 --- .../WarmlyShip/FormMapWithSetShip.Designer.cs | 120 ++++++++++++++---- WarmlyShip/WarmlyShip/FormMapWithSetShip.cs | 94 ++++++++++++-- WarmlyShip/WarmlyShip/MapsCollection.cs | 9 ++ 3 files changed, 184 insertions(+), 39 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs index f522302..df0f2ea 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { this.groupBox1 = 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.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); @@ -38,14 +44,15 @@ this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonRemoveShip = new System.Windows.Forms.Button(); this.buttonAddShip = new System.Windows.Forms.Button(); - this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); + this.groupBoxMaps.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBoxMaps); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.buttonDown); this.groupBox1.Controls.Add(this.buttonLeft); @@ -55,18 +62,81 @@ this.groupBox1.Controls.Add(this.buttonShowStorage); this.groupBox1.Controls.Add(this.buttonRemoveShip); this.groupBox1.Controls.Add(this.buttonAddShip); - this.groupBox1.Controls.Add(this.comboBoxSelectorMap); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(600, 0); + this.groupBox1.Location = new System.Drawing.Point(815, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 450); + this.groupBox1.Size = new System.Drawing.Size(200, 648); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // + // groupBoxMaps + // + 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(6, 22); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(188, 244); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карта"; + // + // buttonDeleteMap + // + this.buttonDeleteMap.Location = new System.Drawing.Point(6, 209); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(176, 23); + this.buttonDeleteMap.TabIndex = 3; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); + // + // listBoxMaps + // + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 15; + this.listBoxMaps.Location = new System.Drawing.Point(6, 109); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(176, 94); + this.listBoxMaps.TabIndex = 2; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // buttonAddMap + // + this.buttonAddMap.Location = new System.Drawing.Point(6, 80); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(176, 23); + this.buttonAddMap.TabIndex = 1; + 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, 22); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(176, 23); + this.textBoxNewMapName.TabIndex = 0; + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Вторая карта", + "Последняя карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 51); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 116); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 345); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); @@ -77,7 +147,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::WarmlyShip.Properties.Resources.todown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(75, 391); + this.buttonDown.Location = new System.Drawing.Point(75, 589); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); this.buttonDown.TabIndex = 10; @@ -89,7 +159,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::WarmlyShip.Properties.Resources.toleft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(19, 391); + this.buttonLeft.Location = new System.Drawing.Point(19, 589); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); this.buttonLeft.TabIndex = 9; @@ -101,7 +171,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::WarmlyShip.Properties.Resources.totop; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(75, 335); + this.buttonUp.Location = new System.Drawing.Point(75, 533); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); this.buttonUp.TabIndex = 8; @@ -113,7 +183,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::WarmlyShip.Properties.Resources.toright; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(131, 391); + this.buttonRight.Location = new System.Drawing.Point(131, 589); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(50, 50); this.buttonRight.TabIndex = 7; @@ -122,7 +192,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 281); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 432); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23); this.buttonShowOnMap.TabIndex = 5; @@ -132,7 +202,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 226); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 403); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(188, 23); this.buttonShowStorage.TabIndex = 4; @@ -142,7 +212,7 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(6, 170); + this.buttonRemoveShip.Location = new System.Drawing.Point(6, 374); this.buttonRemoveShip.Name = "buttonRemoveShip"; this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23); this.buttonRemoveShip.TabIndex = 3; @@ -152,7 +222,7 @@ // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(6, 65); + this.buttonAddShip.Location = new System.Drawing.Point(6, 316); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(188, 23); this.buttonAddShip.TabIndex = 1; @@ -160,25 +230,12 @@ this.buttonAddShip.UseVisualStyleBackColor = true; this.buttonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Вторая карта", - "Последняя карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22); - 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(600, 450); + this.pictureBox.Size = new System.Drawing.Size(815, 648); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -186,13 +243,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(1015, 648); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetShip"; this.Text = "FormMapWithSetShip"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -212,5 +271,10 @@ private Button buttonUp; private Button buttonRight; private MaskedTextBox maskedTextBoxPosition; + private GroupBox groupBoxMaps; + private Button buttonDeleteMap; + private ListBox listBoxMaps; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index 3df2a6b..3992e60 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -16,11 +16,80 @@ namespace WarmlyShip private MapWithSetShipGeneric _mapShipCollectionGeneric; public bool ShipOnMap = false; + private readonly Dictionary _mapsDict = new() { + { "Простая карта", new SimpleMap() }, + { "Вторая карта", new SecondMap() }, + { "Последняя карта", new LastMap() } + }; + + private readonly MapsCollection _mapsCollection; + public FormMapWithSetShip() { InitializeComponent(); + _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); + comboBoxSelectorMap.Items.Clear(); + foreach (var elem in _mapsDict) + { + 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 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(); + } + } + + private void ButtonAddMap_Click(object sender, EventArgs e) + { + if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) + { + MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + ReloadMaps(); + } + + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { AbstractMap map = null; @@ -47,7 +116,7 @@ namespace WarmlyShip } private void ButtonAddShip_Click(object sender, EventArgs e) { - if (_mapShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -69,6 +138,10 @@ namespace WarmlyShip private void ButtonRemoveShip_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -78,40 +151,39 @@ namespace WarmlyShip return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapShipCollectionGeneric - pos == 1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos == 1) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapShipCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); } + } private void ButtonShowStorage_Click(object sender, EventArgs e) { - if (_mapShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipCollectionGeneric.ShowSet(); - ShipOnMap = false; + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } - pictureBox.Image = _mapShipCollectionGeneric.ShowOnMap(); - ShipOnMap = true; + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipCollectionGeneric == null || !ShipOnMap) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -133,7 +205,7 @@ namespace WarmlyShip dir = Direction.Right; break; } - pictureBox.Image = _mapShipCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); } } } diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 13d66c0..c1e4535 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -24,12 +24,21 @@ namespace WarmlyShip _pictureHeight = pictureHeight; } + private bool inDir(string name) + { + foreach(var key in Keys) + if (key == name) return true; + return false; + } + public void AddMap(string name, AbstractMap map) { + if (inDir(name)) return; _mapStorages.Add(name, new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); } public void DelMap(string name) { + if (!inDir(name)) return; _mapStorages.Remove(name); } -- 2.25.1 From 6e509b6d07f2a95f5e2c4440d35647cdfde1ad6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 30 Oct 2022 00:49:41 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyShip/FormMapWithSetShip.Designer.cs | 2 +- WarmlyShip/WarmlyShip/FormMapWithSetShip.cs | 67 ++++++------------- WarmlyShip/WarmlyShip/MapsCollection.cs | 17 ++--- 3 files changed, 25 insertions(+), 61 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs index df0f2ea..21eb0cf 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.Designer.cs @@ -132,7 +132,7 @@ this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + //this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // maskedTextBoxPosition // diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs index 3992e60..ee6dcad 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShip.cs @@ -8,15 +8,14 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WarmlyShip; +using static System.Windows.Forms.DataFormats; namespace WarmlyShip { public partial class FormMapWithSetShip : Form { - private MapWithSetShipGeneric _mapShipCollectionGeneric; - public bool ShipOnMap = false; - - private readonly Dictionary _mapsDict = new() { + private readonly Dictionary _mapsDict = new() + { { "Простая карта", new SimpleMap() }, { "Вторая карта", new SecondMap() }, { "Последняя карта", new LastMap() } @@ -33,7 +32,6 @@ namespace WarmlyShip { comboBoxSelectorMap.Items.Add(elem.Key); } - } private void ReloadMaps() @@ -54,25 +52,6 @@ namespace WarmlyShip } } - 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(); - } - } - private void ButtonAddMap_Click(object sender, EventArgs e) { if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) @@ -89,31 +68,25 @@ namespace WarmlyShip ReloadMaps(); } - - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { - AbstractMap map = null; - switch (comboBoxSelectorMap.Text) + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + private void ButtonDeleteMap_Click(object sender, EventArgs e) + { + if (listBoxMaps.SelectedIndex == -1) { - case "Простая карта": - map = new SimpleMap(); - break; - case "Вторая карта": - map = new SecondMap(); - break; - case "Последняя карта": - map = new LastMap(); - break; + return; } - if (map != null) + if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _mapShipCollectionGeneric = new MapWithSetShipGeneric(pictureBox.Width, pictureBox.Height, map); - } - else - { - _mapShipCollectionGeneric = null; + _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? + string.Empty); + ReloadMaps(); } } + private void ButtonAddShip_Click(object sender, EventArgs e) { if (listBoxMaps.SelectedIndex == -1) @@ -124,10 +97,10 @@ namespace WarmlyShip if (form.ShowDialog() == DialogResult.OK) { DrawningObjectShip ship = new(form.SelectedShip); - if (_mapShipCollectionGeneric + ship == 1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship == 1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapShipCollectionGeneric.ShowSet(); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -154,13 +127,13 @@ namespace WarmlyShip if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos == 1) { MessageBox.Show("Объект удален"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); } - } private void ButtonShowStorage_Click(object sender, EventArgs e) diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index c1e4535..5e82195 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -24,21 +24,15 @@ namespace WarmlyShip _pictureHeight = pictureHeight; } - private bool inDir(string name) - { - foreach(var key in Keys) - if (key == name) return true; - return false; - } - public void AddMap(string name, AbstractMap map) { - if (inDir(name)) return; + if (_mapStorages.ContainsKey(name)) return; _mapStorages.Add(name, new MapWithSetShipGeneric(_pictureWidth, _pictureHeight, map)); } + public void DelMap(string name) { - if (!inDir(name)) return; + if (_mapStorages.ContainsKey(name)) return; _mapStorages.Remove(name); } @@ -46,10 +40,7 @@ namespace WarmlyShip { get { - foreach (var map in _mapStorages) - { - if(map.Key == ind) return map.Value; - } + if(_mapStorages.ContainsKey(ind)) return _mapStorages[ind]; return null; } } -- 2.25.1 From f5df18ed712381727d47ce4b705780ff6bd80eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 30 Oct 2022 01:09:29 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyShip/MapWithSetShipGeneric.cs | 4 +-- WarmlyShip/WarmlyShip/SetShipGeneric.cs | 36 ++----------------- 2 files changed, 4 insertions(+), 36 deletions(-) diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs index 655f208..3efe986 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipGeneric.cs @@ -49,9 +49,9 @@ namespace WarmlyShip public Bitmap ShowOnMap() { Shaking(); - foreach (var car in _setShips.GetShips()) + foreach (var ship in _setShips.GetShips()) { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } return new(_pictureWidth, _pictureHeight); } diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index 2740f09..d5d72d1 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -20,47 +20,15 @@ namespace WarmlyShip _places = new List(); } - private bool CanInsert(int position) - { - for (int i = position; i < Count; ++i) - if (_places[i] == null) return true; - return false; - } - public int Insert(T ship) { - if (CanInsert(0)) - { - for (int i = Count - 1; i > 0; --i) - { - if (_places[i] == null) - { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - _places[0] = ship; - return 1; - } - return 0; + _places.Insert(0, ship); + return 1; } public int Insert(T ship, int position) { if (position < 0 || position > Count || Count == _maxCount) return 0; - /*if (_places[position] != null && CanInsert(position)) - { - for (int i = _places.Count - 1; i > position; --i) - { - if (_places[i] == null) - { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = ship; - return 1;*/ _places.Insert(position, ship); return 1; } -- 2.25.1 From c82b33ed471d33c1af22a10515da9cc793d31fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 30 Oct 2022 12:20:44 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/MapsCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 5e82195..badab5b 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -32,7 +32,7 @@ namespace WarmlyShip public void DelMap(string name) { - if (_mapStorages.ContainsKey(name)) return; + if (!_mapStorages.ContainsKey(name)) return; _mapStorages.Remove(name); } -- 2.25.1