From 0f81ca3cc131366a4764617b51e5acb932dac0a0 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 4 Nov 2022 23:05:28 +0400 Subject: [PATCH] lab4 --- .../ContainerShip/ContainerShip.csproj | 4 - .../FormMapWithSetShip.Designer.cs | 106 ++++++++++++++---- .../ContainerShip/FormMapWithSetShip.cs | 92 +++++++++++++-- .../ContainerShip/MapWithSetShipGeneric.cs | 24 ++-- ContainerShip/ContainerShip/MapsCollection.cs | 57 ++++++++++ ContainerShip/ContainerShip/SetShipGeneric.cs | 97 ++++++++-------- 6 files changed, 280 insertions(+), 100 deletions(-) create mode 100644 ContainerShip/ContainerShip/MapsCollection.cs diff --git a/ContainerShip/ContainerShip/ContainerShip.csproj b/ContainerShip/ContainerShip/ContainerShip.csproj index 86fa26e..13ee123 100644 --- a/ContainerShip/ContainerShip/ContainerShip.csproj +++ b/ContainerShip/ContainerShip/ContainerShip.csproj @@ -8,10 +8,6 @@ enable - - - - True diff --git a/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs index 7f68838..0d0124d 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.Designer.cs @@ -29,8 +29,13 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.groupBox2 = new System.Windows.Forms.GroupBox(); + this.button2 = new System.Windows.Forms.Button(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.buttonAddMap = new System.Windows.Forms.Button(); this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddShip = new System.Windows.Forms.Button(); this.buttonRemoveShip = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); @@ -41,13 +46,14 @@ this.buttonUp = new System.Windows.Forms.Button(); this.pictureBox = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); + this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // groupBox1 // + this.groupBox1.Controls.Add(this.groupBox2); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); - this.groupBox1.Controls.Add(this.comboBoxSelectorMap); this.groupBox1.Controls.Add(this.buttonAddShip); this.groupBox1.Controls.Add(this.buttonRemoveShip); this.groupBox1.Controls.Add(this.buttonShowStorage); @@ -59,18 +65,61 @@ this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Location = new System.Drawing.Point(600, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(200, 450); + this.groupBox1.Size = new System.Drawing.Size(200, 525); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // - // maskedTextBoxPosition + // groupBox2 // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 185); - this.maskedTextBoxPosition.Mask = "00"; - this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; - this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); - this.maskedTextBoxPosition.TabIndex = 19; + this.groupBox2.Controls.Add(this.button2); + this.groupBox2.Controls.Add(this.listBoxMaps); + this.groupBox2.Controls.Add(this.textBoxNewMapName); + this.groupBox2.Controls.Add(this.buttonAddMap); + this.groupBox2.Controls.Add(this.comboBoxSelectorMap); + this.groupBox2.Location = new System.Drawing.Point(6, 22); + this.groupBox2.Name = "groupBox2"; + this.groupBox2.Size = new System.Drawing.Size(188, 237); + this.groupBox2.TabIndex = 20; + this.groupBox2.TabStop = false; + this.groupBox2.Text = "Карты"; + // + // button2 + // + this.button2.Location = new System.Drawing.Point(6, 208); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(176, 23); + this.button2.TabIndex = 22; + this.button2.Text = "Удалить карту"; + this.button2.UseVisualStyleBackColor = true; + this.button2.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 = 21; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); + // + // 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 = 20; + // + // 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 = 19; + this.buttonAddMap.Text = "Добавить карту"; + this.buttonAddMap.UseVisualStyleBackColor = true; + this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); // // comboBoxSelectorMap // @@ -80,15 +129,23 @@ "Простая карта", "Острова", "Скалы"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 51); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23); + this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23); this.comboBoxSelectorMap.TabIndex = 18; this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 294); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23); + this.maskedTextBoxPosition.TabIndex = 19; + // // buttonAddShip // - this.buttonAddShip.Location = new System.Drawing.Point(6, 135); + this.buttonAddShip.Location = new System.Drawing.Point(6, 265); this.buttonAddShip.Name = "buttonAddShip"; this.buttonAddShip.Size = new System.Drawing.Size(188, 23); this.buttonAddShip.TabIndex = 17; @@ -98,7 +155,7 @@ // // buttonRemoveShip // - this.buttonRemoveShip.Location = new System.Drawing.Point(6, 214); + this.buttonRemoveShip.Location = new System.Drawing.Point(6, 323); this.buttonRemoveShip.Name = "buttonRemoveShip"; this.buttonRemoveShip.Size = new System.Drawing.Size(188, 23); this.buttonRemoveShip.TabIndex = 15; @@ -108,7 +165,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(6, 296); + this.buttonShowStorage.Location = new System.Drawing.Point(6, 376); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(188, 23); this.buttonShowStorage.TabIndex = 14; @@ -118,7 +175,7 @@ // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(6, 325); + this.buttonShowOnMap.Location = new System.Drawing.Point(6, 405); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(188, 23); this.buttonShowOnMap.TabIndex = 13; @@ -131,7 +188,7 @@ this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(89, 408); + this.buttonDown.Location = new System.Drawing.Point(89, 483); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.TabIndex = 12; @@ -143,7 +200,7 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(53, 408); + this.buttonLeft.Location = new System.Drawing.Point(53, 483); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.TabIndex = 11; @@ -155,7 +212,7 @@ this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(125, 408); + this.buttonRight.Location = new System.Drawing.Point(125, 483); this.buttonRight.Name = "buttonRight"; this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.TabIndex = 10; @@ -167,7 +224,7 @@ this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(89, 372); + this.buttonUp.Location = new System.Drawing.Point(89, 447); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.TabIndex = 9; @@ -179,7 +236,7 @@ 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, 525); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; // @@ -187,13 +244,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, 525); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetShip"; this.Text = "FormMapWithSetShip"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBox2.ResumeLayout(false); + this.groupBox2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -213,5 +272,10 @@ private Button buttonUp; private ComboBox comboBoxSelectorMap; private MaskedTextBox maskedTextBoxPosition; + private GroupBox groupBox2; + private Button button2; + private ListBox listBoxMaps; + private TextBox textBoxNewMapName; + private Button buttonAddMap; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShip.cs b/ContainerShip/ContainerShip/FormMapWithSetShip.cs index 47a4383..3edbbd1 100644 --- a/ContainerShip/ContainerShip/FormMapWithSetShip.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShip.cs @@ -15,13 +15,66 @@ namespace ContainerShip private MapWithSetShipGeneric _mapShipCollectionGeneric; - + + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap()}, + { "Острова", new IslandsMap()}, + { "Скалы", new RocksMap()} + }; + + 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 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; @@ -58,10 +111,10 @@ namespace ContainerShip if (form.ShowDialog() == DialogResult.OK) { DrawingObjectShip 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 { @@ -105,17 +158,17 @@ namespace ContainerShip private void ButtonShowOnMap_Click(object sender, EventArgs e) { - if (_mapShipCollectionGeneric == null) - { + if (listBoxMaps.SelectedIndex == -1) + { return; } - pictureBox.Image = _mapShipCollectionGeneric.ShowOnMap(); - + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapShipCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -137,7 +190,26 @@ namespace ContainerShip dir = Direction.Right; break; } - pictureBox.Image = _mapShipCollectionGeneric.MoveObject(dir); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + } + + 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(); + } } } } diff --git a/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs index 2b2bace..763a50c 100644 --- a/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs +++ b/ContainerShip/ContainerShip/MapWithSetShipGeneric.cs @@ -56,13 +56,9 @@ namespace ContainerShip public Bitmap ShowOnMap() { Shaking(); - for (int i = 0; i < _setShip.Count; i++) + foreach (var ship in _setShip.GetShip()) { - var ship = _setShip.Get(i); - if (ship != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, ship); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); } return new(_pictureWidth, _pictureHeight); } @@ -81,11 +77,11 @@ namespace ContainerShip int j = _setShip.Count - 1; for (int i = 0; i < _setShip.Count; i++) { - if (_setShip.Get(i) == null) + if (_setShip[i] == null) { for (; j > i; j--) { - var ship = _setShip.Get(j); + var ship = _setShip[j]; if (ship != null) { _setShip.Insert(ship, i); @@ -112,7 +108,7 @@ namespace ContainerShip for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + for (int j = 0; j < _pictureHeight / _placeSizeHeight; ++j) { g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight + _placeSizeHeight / 2); @@ -141,15 +137,15 @@ namespace ContainerShip int RowIndex = yNumOfPlaces - 1; int ColumnIndex = xNumOfPlaces - 1; - for (int i = 0; i < _setShip.Count; i++) + foreach (var ship in _setShip.GetShip()) { - if (_setShip.Get(i) != null) + if (ship != null) { - (float Left, float Top, float Right, float Bottom) = _setShip.Get(i).GetCurrentPosition(); - _setShip.Get(i).SetObject(ColumnIndex * _placeSizeWidth, + (float Left, float Top, float Right, float Bottom) = ship.GetCurrentPosition(); + ship.SetObject(ColumnIndex * _placeSizeWidth, RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight); - _setShip.Get(i).DrawingObject(g); + ship.DrawingObject(g); } if (ColumnIndex == 0) { diff --git a/ContainerShip/ContainerShip/MapsCollection.cs b/ContainerShip/ContainerShip/MapsCollection.cs new file mode 100644 index 0000000..18a3483 --- /dev/null +++ b/ContainerShip/ContainerShip/MapsCollection.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + 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)) + { + return; + } + MapWithSetShipGeneric newMap = new(_pictureWidth, _pictureHeight, map); + _mapStorages.Add(name, newMap); + } + + public void DelMap(string name) + { + if (_mapStorages.ContainsKey(name)) + { + _mapStorages.Remove(name); + } + } + + public MapWithSetShipGeneric this[string + ind] + { + get + { + if (_mapStorages.ContainsKey(ind)) + { + return _mapStorages[ind]; + } + return null; + } + } + } +} diff --git a/ContainerShip/ContainerShip/SetShipGeneric.cs b/ContainerShip/ContainerShip/SetShipGeneric.cs index 1913ff9..6cf3cf3 100644 --- a/ContainerShip/ContainerShip/SetShipGeneric.cs +++ b/ContainerShip/ContainerShip/SetShipGeneric.cs @@ -9,82 +9,77 @@ namespace ContainerShip 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(); } - + public int Insert(T ship) - { + { return Insert(ship, 0); } public int Insert(T ship, int position) { - if (position < 0 || position > _places.Length) + if (position < 0 || position > Count || Count == _maxCount) { return -1; } - int emptyCellIndex = -1; - if (_places[position] != null) - { - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - emptyCellIndex = i; - break; - } - } - if (emptyCellIndex != -1) - { - for (int i = emptyCellIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - } - } - else - { - _places[position] = ship; - return position; - } - - if (emptyCellIndex == -1) - { - return -1; - } - else - { - _places[position] = ship; - return position; - } + _places.Insert(position, ship); + return position; } - + public T Remove(int position) { if (position >= Count || position < 0) { return null; } - T removedObject = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return removedObject; } - - public T Get(int position) + + public T this[int position] { - if (position >= Count || position < 0) + get { - return null; + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + set + { + if (position < 0 || position >= Count) + { + return; + } + Insert(value, position); + } + } + public IEnumerable GetShip() + { + foreach (var ship in _places) + { + if (ship != null) + { + yield return ship; + } + else + { + yield break; + } } - return _places[position]; } } }