diff --git a/AircraftCarrier/AircraftCarrier/AbstractMap.cs b/AircraftCarrier/AircraftCarrier/AbstractMap.cs index a7ac427..c42d91e 100644 --- a/AircraftCarrier/AircraftCarrier/AbstractMap.cs +++ b/AircraftCarrier/AircraftCarrier/AbstractMap.cs @@ -38,7 +38,6 @@ namespace AircraftCarrier int yNumOfCells; int xObjOffset; int yObjOffset; - switch (direction) { case Direction.Up: @@ -149,6 +148,7 @@ namespace AircraftCarrier } break; } + if (roadIsClear) { _drawningObject.MoveObject(direction); diff --git a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs index d0edec2..cbc032f 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs @@ -35,7 +35,6 @@ namespace AircraftCarrier // /// Ширина отрисовки самолета /// - protected readonly int _aircraftcarrierWidth = 230; protected readonly int _aircraftcarrierHeight = 80; public DrawningAircraftCarrier(int speed, float weight, Color bodyColor) @@ -53,6 +52,7 @@ namespace AircraftCarrier { _startPosX = x; _startPosY = y; + _pictureWidth = width; _pictureHeigth = height; } diff --git a/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs index fbc7d11..b820f0f 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningModernAircraftCarrier.cs @@ -22,7 +22,7 @@ dopColor, bool flightDeck, bool hangarDeck, bool route) : } Pen pen = new(Color.Black); Brush dopBrush = new SolidBrush(modernAircraftCarrier.DopColor); - + if (modernAircraftCarrier.FlightDeck) { g.DrawRectangle(pen, _startPosX + 10, _startPosY, 160, 10); diff --git a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs index 3ae43c3..286fd02 100644 --- a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs @@ -9,13 +9,9 @@ namespace AircraftCarrier public class EntityAircraftCarrier { public int Speed { get; private set; } - public float Weight { get; private set; } - public Color BodyColor { get; private set; } - public float Step => Speed * 100 / Weight; - public EntityAircraftCarrier(int speed, float weight, Color bodyColor) { Random rnd = new Random(); @@ -23,6 +19,5 @@ namespace AircraftCarrier Weight = weight <= 0 ? rnd.Next(40, 70) : weight; BodyColor = bodyColor; } - } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index 99d1e9e..671d27f 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -25,7 +25,6 @@ namespace AircraftCarrier _aircraftcarrier?.DrawTransport(gr); pictureBoxAircraftCarrier.Image = bmp; } - private void SetData() { Random rnd = new(); diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs index e8d1b4d..443e6a7 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.Designer.cs @@ -30,82 +30,152 @@ private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); + this.groupBoxMaps = new System.Windows.Forms.GroupBox(); + this.listBoxMaps = new System.Windows.Forms.ListBox(); + this.buttonDeleteMap = new System.Windows.Forms.Button(); + this.buttonAddMap = new System.Windows.Forms.Button(); + this.textBoxNewMapName = new System.Windows.Forms.TextBox(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.button1 = new System.Windows.Forms.Button(); + this.button4 = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonRemoveAircraftCarrier = new System.Windows.Forms.Button(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.buttonAddAircraftCarrier = 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.buttonLeft); - this.groupBox1.Controls.Add(this.buttonDown); - this.groupBox1.Controls.Add(this.buttonUp); - this.groupBox1.Controls.Add(this.buttonRight); + this.groupBox1.Controls.Add(this.groupBoxMaps); + this.groupBox1.Controls.Add(this.button1); + this.groupBox1.Controls.Add(this.button4); + this.groupBox1.Controls.Add(this.button3); + this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.buttonShowOnMap); this.groupBox1.Controls.Add(this.buttonShowStorage); this.groupBox1.Controls.Add(this.buttonRemoveAircraftCarrier); this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.buttonAddAircraftCarrier); - this.groupBox1.Controls.Add(this.comboBoxSelectorMap); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; - this.groupBox1.Location = new System.Drawing.Point(550, 0); + this.groupBox1.Location = new System.Drawing.Point(593, 0); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(250, 450); + this.groupBox1.Size = new System.Drawing.Size(250, 519); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Инструменты"; // - // buttonLeft + // groupBoxMaps // - //this.buttonLeft.BackgroundImage = global::AircraftCarrier.Properties.Resources.Left; - this.buttonLeft.Location = new System.Drawing.Point(61, 402); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 30); - this.buttonLeft.TabIndex = 11; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + this.groupBoxMaps.Controls.Add(this.listBoxMaps); + this.groupBoxMaps.Controls.Add(this.buttonDeleteMap); + 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, 29); + this.groupBoxMaps.Name = "groupBoxMaps"; + this.groupBoxMaps.Size = new System.Drawing.Size(232, 239); + this.groupBoxMaps.TabIndex = 12; + this.groupBoxMaps.TabStop = false; + this.groupBoxMaps.Text = "Карты"; + //this.groupBoxMaps.Enter += new System.EventHandler(this.groupBoxMaps_Enter); // - // buttonDown + // listBoxMaps // - //this.buttonDown.BackgroundImage = global::AircraftCarrier.Properties.Resources.Down; - this.buttonDown.Location = new System.Drawing.Point(97, 402); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 30); - this.buttonDown.TabIndex = 10; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + this.listBoxMaps.FormattingEnabled = true; + this.listBoxMaps.ItemHeight = 20; + this.listBoxMaps.Location = new System.Drawing.Point(19, 137); + this.listBoxMaps.Name = "listBoxMaps"; + this.listBoxMaps.Size = new System.Drawing.Size(184, 64); + this.listBoxMaps.TabIndex = 3; + this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged); // - // buttonUp + // buttonDeleteMap // - this.buttonUp.Location = new System.Drawing.Point(97, 366); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 30); - this.buttonUp.TabIndex = 9; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + this.buttonDeleteMap.Location = new System.Drawing.Point(19, 208); + this.buttonDeleteMap.Name = "buttonDeleteMap"; + this.buttonDeleteMap.Size = new System.Drawing.Size(184, 29); + this.buttonDeleteMap.TabIndex = 2; + this.buttonDeleteMap.Text = "Удалить карту"; + this.buttonDeleteMap.UseVisualStyleBackColor = true; + this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click); // - // buttonRight + // buttonAddMap // - this.buttonRight.Location = new System.Drawing.Point(133, 402); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 30); - this.buttonRight.TabIndex = 8; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + this.buttonAddMap.Location = new System.Drawing.Point(19, 102); + this.buttonAddMap.Name = "buttonAddMap"; + this.buttonAddMap.Size = new System.Drawing.Size(184, 29); + 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(19, 26); + this.textBoxNewMapName.Name = "textBoxNewMapName"; + this.textBoxNewMapName.Size = new System.Drawing.Size(184, 27); + this.textBoxNewMapName.TabIndex = 0; + // + // 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(19, 68); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(184, 28); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + this.comboBoxSelectorMap.Click += new System.EventHandler(this.ButtonMove_Click); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(71, 483); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(30, 30); + this.button1.TabIndex = 11; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.ButtonMove_Click); + // + // button4 + // + this.button4.Location = new System.Drawing.Point(107, 483); + this.button4.Name = "button4"; + this.button4.Size = new System.Drawing.Size(30, 30); + this.button4.TabIndex = 10; + this.button4.UseVisualStyleBackColor = true; + this.button4.Click += new System.EventHandler(this.ButtonMove_Click); + // + // button3 + // + this.button3.Location = new System.Drawing.Point(107, 447); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(30, 30); + this.button3.TabIndex = 9; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.ButtonMove_Click); + // + // button2 + // + this.button2.Location = new System.Drawing.Point(143, 483); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(30, 30); + this.button2.TabIndex = 8; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonShowOnMap // - this.buttonShowOnMap.Location = new System.Drawing.Point(25, 321); + this.buttonShowOnMap.Location = new System.Drawing.Point(25, 412); this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Size = new System.Drawing.Size(184, 29); this.buttonShowOnMap.TabIndex = 6; @@ -115,7 +185,7 @@ // // buttonShowStorage // - this.buttonShowStorage.Location = new System.Drawing.Point(25, 280); + this.buttonShowStorage.Location = new System.Drawing.Point(25, 377); this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Size = new System.Drawing.Size(184, 29); this.buttonShowStorage.TabIndex = 5; @@ -125,7 +195,7 @@ // // buttonRemoveAircraftCarrier // - this.buttonRemoveAircraftCarrier.Location = new System.Drawing.Point(25, 230); + this.buttonRemoveAircraftCarrier.Location = new System.Drawing.Point(25, 342); this.buttonRemoveAircraftCarrier.Name = "buttonRemoveAircraftCarrier"; this.buttonRemoveAircraftCarrier.Size = new System.Drawing.Size(184, 29); this.buttonRemoveAircraftCarrier.TabIndex = 4; @@ -135,7 +205,7 @@ // // maskedTextBoxPosition // - this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 168); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(25, 309); this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Size = new System.Drawing.Size(184, 27); @@ -143,7 +213,7 @@ // // buttonAddAircraftCarrier // - this.buttonAddAircraftCarrier.Location = new System.Drawing.Point(25, 110); + this.buttonAddAircraftCarrier.Location = new System.Drawing.Point(25, 274); this.buttonAddAircraftCarrier.Name = "buttonAddAircraftCarrier"; this.buttonAddAircraftCarrier.Size = new System.Drawing.Size(184, 29); this.buttonAddAircraftCarrier.TabIndex = 2; @@ -151,25 +221,12 @@ this.buttonAddAircraftCarrier.UseVisualStyleBackColor = true; this.buttonAddAircraftCarrier.Click += new System.EventHandler(this.ButtonAddAircraftCarrier_Click); // - // comboBoxSelectorMap - // - this.comboBoxSelectorMap.FormattingEnabled = true; - this.comboBoxSelectorMap.Items.AddRange(new object[] { - "Простая карта", - "Сложная карта"}); - this.comboBoxSelectorMap.Location = new System.Drawing.Point(25, 61); - this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(184, 28); - this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); - this.comboBoxSelectorMap.Click += new System.EventHandler(this.ButtonMove_Click); - // // 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(550, 450); + this.pictureBox.Size = new System.Drawing.Size(593, 519); this.pictureBox.TabIndex = 1; this.pictureBox.TabStop = false; //this.pictureBox.Click += new System.EventHandler(this.pictureBox1_Click); @@ -178,13 +235,15 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(843, 519); this.Controls.Add(this.pictureBox); this.Controls.Add(this.groupBox1); this.Name = "FormMapWithSetAircraftCarriers"; this.Text = "Карта с набором объектов"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); + this.groupBoxMaps.ResumeLayout(false); + this.groupBoxMaps.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); @@ -193,10 +252,10 @@ #endregion private GroupBox groupBox1; - private Button buttonLeft; - private Button buttonDown; - private Button buttonUp; - private Button buttonRight; + private Button button1; + private Button button4; + private Button button3; + private Button button2; private Button buttonShowOnMap; private Button buttonShowStorage; private Button buttonRemoveAircraftCarrier; @@ -204,5 +263,10 @@ private Button buttonAddAircraftCarrier; private ComboBox comboBoxSelectorMap; private PictureBox pictureBox; + private GroupBox groupBoxMaps; + private ListBox listBoxMaps; + private Button buttonDeleteMap; + private Button buttonAddMap; + private TextBox textBoxNewMapName; } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs index 37d36e2..f0c5486 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetAircraftCarriers.cs @@ -18,9 +18,79 @@ _mapAircraftCarriersCollectionGeneric; /// /// Конструктор /// + private readonly Dictionary _mapsDict = new() + { + { "Простая карта", new SimpleMap() }, + { "Сложная карта", new ComplexMap() } + }; + private readonly MapsCollection _mapsCollection; public FormMapWithSetAircraftCarriers() { 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 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 ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) { @@ -47,7 +117,7 @@ _mapAircraftCarriersCollectionGeneric; } private void ButtonAddAircraftCarrier_Click(object sender, EventArgs e) { - if (_mapAircraftCarriersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -55,10 +125,12 @@ _mapAircraftCarriersCollectionGeneric; if (form.ShowDialog() == DialogResult.OK) { DrawningObjectAircraftCarrier aircraftcarrier = new(form.SelectedAircraftCarrier); - if ((_mapAircraftCarriersCollectionGeneric + aircraftcarrier) != -1) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? +string.Empty] + aircraftcarrier != -1) { MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapAircraftCarriersCollectionGeneric.ShowSet(); + pictureBox.Image = + _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { @@ -68,6 +140,10 @@ _mapAircraftCarriersCollectionGeneric; } private void buttonRemoveAircraftCarrier_Click(object sender, EventArgs e) { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) { return; @@ -78,7 +154,8 @@ _mapAircraftCarriersCollectionGeneric; return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapAircraftCarriersCollectionGeneric - pos != null) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? +string.Empty] - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapAircraftCarriersCollectionGeneric.ShowSet(); @@ -90,7 +167,7 @@ _mapAircraftCarriersCollectionGeneric; } private void buttonShowStorage_Click(object sender, EventArgs e) { - if (_mapAircraftCarriersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -98,7 +175,7 @@ _mapAircraftCarriersCollectionGeneric; } private void buttonShowOnMap_Click(object sender, EventArgs e) { - if (_mapAircraftCarriersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } @@ -106,7 +183,7 @@ _mapAircraftCarriersCollectionGeneric; } private void ButtonMove_Click(object sender, EventArgs e) { - if (_mapAircraftCarriersCollectionGeneric == null) + if (listBoxMaps.SelectedIndex == -1) { return; } diff --git a/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs index 379f1c9..b2e97c1 100644 --- a/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/MapWithSetAircraftCarriersGeneric.cs @@ -90,7 +90,7 @@ namespace AircraftCarrier Shaking(); for (int i = 0; i < _setAircraftCarriers.Count; i++) { - var aircraftcarrier = _setAircraftCarriers.Get(i); + var aircraftcarrier = _setAircraftCarriers[i]; if (aircraftcarrier != null) { return _map.CreateMap(_pictureWidth, _pictureHeight, aircraftcarrier); @@ -119,11 +119,11 @@ namespace AircraftCarrier int j = _setAircraftCarriers.Count - 1; for (int i = 0; i < _setAircraftCarriers.Count; i++) { - if (_setAircraftCarriers.Get(i) == null) + if (_setAircraftCarriers[i] == null) { for (; j > i; j--) { - var aircraftcarrier = _setAircraftCarriers.Get(j); + var aircraftcarrier = _setAircraftCarriers[j]; if (aircraftcarrier != null) { _setAircraftCarriers.Insert(aircraftcarrier, i); @@ -148,7 +148,7 @@ namespace AircraftCarrier for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - {//линия рамзетки места + {//линия разметки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } @@ -166,14 +166,13 @@ namespace AircraftCarrier int xNumOfPlaces = _pictureWidth / _placeSizeWidth; int rowNum = yNumOfPlaces - 1; int columnNum = 0; - for (int i = 0; i < _setAircraftCarriers.Count; i++) { - if (_setAircraftCarriers.Get(i) != null) + if (_setAircraftCarriers[i] != null) { - (float Left, float Top, float Right, float Bottom) = _setAircraftCarriers.Get(i).GetCurrentPosition(); - _setAircraftCarriers.Get(i).SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight); - _setAircraftCarriers.Get(i).DrawningObject(g); + (float Left, float Top, float Right, float Bottom) = _setAircraftCarriers[i].GetCurrentPosition(); + _setAircraftCarriers[i].SetObject(columnNum * _placeSizeWidth, rowNum * _placeSizeHeight + (_placeSizeHeight - (int)(Bottom - Top)), _pictureWidth, _pictureHeight); + _setAircraftCarriers[i].DrawningObject(g); } if (columnNum == xNumOfPlaces - 1) { diff --git a/AircraftCarrier/AircraftCarrier/MapsCollection.cs b/AircraftCarrier/AircraftCarrier/MapsCollection.cs new file mode 100644 index 0000000..3050853 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MapsCollection.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftCarrier +{ + 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) + { + // TODO Прописать логику для добавления + if (Keys.Contains(name)) + return; + _mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map)); + } + /// + /// Удаление карты + /// + /// Название карты + public void DelMap(string name) + { + // TODO Прописать логику для удаления + _mapStorages.Remove(name); + } + /// + /// Доступ к парковке + /// + /// + /// + public MapWithSetAircraftCarriersGeneric this[string + ind] + { + get + { + _mapStorages.TryGetValue(ind, out var mapWithSetAircraftCarriersGeneric); + return mapWithSetAircraftCarriersGeneric; + } + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs index 017e06b..6521afa 100644 --- a/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetAircraftCarriersGeneric.cs @@ -9,18 +9,20 @@ namespace AircraftCarrier internal class SetAircraftCarriersGeneric where T : class { - private readonly T[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + private readonly int _maxCount; /// /// Конструктор /// /// public SetAircraftCarriersGeneric(int count) { - _places = new T[count]; + _maxCount = count; + _places = new List(); } /// /// Добавление объекта в набор @@ -29,8 +31,13 @@ namespace AircraftCarrier /// public int Insert(T AircraftCarrier) { + // TODO вставка в начало набора return Insert(AircraftCarrier, 0); } + private bool CorrectPos(int pos) + { + return 0 <= pos && pos < _maxCount; + } /// /// Добавление объекта в набор на конкретную позицию /// @@ -39,37 +46,15 @@ namespace AircraftCarrier /// public int Insert(T AircraftCarrier, int position) { - // TODO проверка позиции - if (position >= _places.Length || position < 0) + // проверка позиции + if (!CorrectPos(position)) + { return -1; - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - if (_places[position] == null) - { - _places[position] = AircraftCarrier; - return position; } - // проверка, что после вставляемого элемента в массиве есть пустой элемент - int findEmptyPos = -1; - - for (int i = position + 1; i < Count; i++) - { - if (_places[i] == null) - { - findEmptyPos = i; - break; - } - } - if (findEmptyPos < 0) return -1; - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - for (int i = findEmptyPos; i > position; i--) - { - _places[i] = _places[i - 1]; - } - // TODO вставка по позиции - _places[position] = AircraftCarrier; + // вставка по позиции + _places.Insert(position, AircraftCarrier); return position; } - /// /// Удаление объекта из набора с конкретной позиции /// @@ -77,11 +62,12 @@ namespace AircraftCarrier /// public T Remove(int position) { - // TODO проверка позиции - if (position >= _places.Length || position < 0) return null; - // TODO удаление объекта из массива, присовив элементу массива значение null + // проверка позиции + if (!CorrectPos(position)) + return null; + // удаление объекта из массива, присовив элементу массива значение null T temp = _places[position]; - _places[position] = null; + _places.RemoveAt(position); return temp; } /// @@ -89,11 +75,31 @@ namespace AircraftCarrier /// /// /// - public T Get(int position) + public T this[int position] { - if (position >= _places.Length || position < 0) - return null; - return _places[position]; + get + { + return CorrectPos(position) && position < Count ? _places[position] : null; + } + set + { + Insert(value, position); + } + + } + public IEnumerable GetAircraftCarriers() + { + foreach (var aircraftcarrier in _places) + { + if (aircraftcarrier != null) + { + yield return aircraftcarrier; + } + else + { + yield break; + } + } } } } \ No newline at end of file