From 54e29c6e9adb1c2b7190ac8b36cba2c7675d3ac2 Mon Sep 17 00:00:00 2001 From: just1valery Date: Sun, 9 Oct 2022 14:00:23 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20"T?= =?UTF-8?q?oDo"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetShips.Designer.cs | 11 +++-- WarmlyShip/WarmlyShip/FormMapWithSetShips.cs | 22 +++------- .../WarmlyShip/MapWithSetShipsGeneric.cs | 6 +-- WarmlyShip/WarmlyShip/MapsCollection.cs | 8 ++-- WarmlyShip/WarmlyShip/SetShipsGeneric.cs | 44 +++++++------------ 5 files changed, 34 insertions(+), 57 deletions(-) diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs index 7fa69ee..3e5154a 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.Designer.cs @@ -109,7 +109,7 @@ this.buttonAddMap.Location = new System.Drawing.Point(0, 90); this.buttonAddMap.Name = "buttonAddMap"; this.buttonAddMap.Size = new System.Drawing.Size(238, 29); - this.buttonAddMap.TabIndex = 1; + this.buttonAddMap.TabIndex = 2; this.buttonAddMap.Text = "Добавить карту"; this.buttonAddMap.UseVisualStyleBackColor = true; this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click); @@ -123,15 +123,14 @@ // // 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(3, 56); this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; - this.comboBoxSelectorMap.Size = new System.Drawing.Size(235, 28); - this.comboBoxSelectorMap.TabIndex = 0; - this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + this.comboBoxSelectorMap.Size = new System.Drawing.Size(232, 28); + this.comboBoxSelectorMap.TabIndex = 1; // // buttonLeft // diff --git a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs index f2326b3..3fac642 100644 --- a/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs +++ b/WarmlyShip/WarmlyShip/FormMapWithSetShips.cs @@ -17,7 +17,8 @@ namespace WarmlyShip /// private readonly Dictionary _mapsDict = new() { - { "Простая карта", new SimpleMap() } + { "Простая карта", new SimpleMap() }, + { "Океан", new OceanMap() } }; /// /// Объект от коллекции карт @@ -101,10 +102,6 @@ namespace WarmlyShip _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); ReloadMaps(); } - } - private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) - { - } /// /// Добавление объекта @@ -121,7 +118,7 @@ namespace WarmlyShip if (form.ShowDialog() == DialogResult.OK) { DrawningObjectShip ship = new(form.SelectedShip); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); @@ -139,20 +136,13 @@ namespace WarmlyShip /// private void ButtonRemoveShip_Click(object sender, EventArgs e) { - if (listBoxMaps.SelectedIndex == -1) - { - return; - } - if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) - { - return; - } - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxMaps.SelectedIndex == -1 || string.IsNullOrEmpty(maskedTextBoxPosition.Text) || + MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos) + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); diff --git a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs index bf56fd9..43ed7f8 100644 --- a/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/MapWithSetShipsGeneric.cs @@ -55,7 +55,7 @@ namespace WarmlyShip /// /// /// - public static bool operator +(MapWithSetShipsGeneric map, T ship) + public static int operator +(MapWithSetShipsGeneric map, T ship) { return map._setShips.Insert(ship); } @@ -65,7 +65,7 @@ namespace WarmlyShip /// /// /// - public static bool operator -(MapWithSetShipsGeneric map, int position) + public static T operator -(MapWithSetShipsGeneric map, int position) { return map._setShips.Remove(position); } @@ -166,7 +166,7 @@ namespace WarmlyShip { int countInLine = _pictureWidth / _placeSizeWidth; int maxLeft = (countInLine - 1) * _placeSizeWidth; - foreach (var ship in _setShips.GetShips()) + for (int i = 0; i < _setShips.Count; i++) { var ship = _setShips[i]; ship?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight); diff --git a/WarmlyShip/WarmlyShip/MapsCollection.cs b/WarmlyShip/WarmlyShip/MapsCollection.cs index 58deb99..d26ae01 100644 --- a/WarmlyShip/WarmlyShip/MapsCollection.cs +++ b/WarmlyShip/WarmlyShip/MapsCollection.cs @@ -42,7 +42,7 @@ namespace WarmlyShip /// Карта public void AddMap(string name, AbstractMap map) { - // TODO Прописать логику для добавления + _mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map)); } /// /// Удаление карты @@ -50,7 +50,7 @@ namespace WarmlyShip /// Название карты public void DelMap(string name) { - // TODO Прописать логику для удаления + _mapStorages.Remove(name); } /// /// Доступ к парковке @@ -61,8 +61,8 @@ namespace WarmlyShip { get { - // TODO Продумать логику получения объекта - return null; + _mapStorages.TryGetValue(ind, out var mapWithSetShipsGeneric); + return mapWithSetShipsGeneric; } } } diff --git a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs index 62df44e..f42f0da 100644 --- a/WarmlyShip/WarmlyShip/SetShipsGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipsGeneric.cs @@ -33,14 +33,14 @@ namespace WarmlyShip /// /// Добавляемый корабль /// - public bool Insert(T ship) + public int Insert(T ship) { - //проверка на макс каунт return Insert(ship, 0); } + private bool isCorrectPosition(int position) { - return 0 <= position && position < Count; + return 0 <= position && position < _maxCount; } /// /// Добавление объекта в набор на конкретную позицию @@ -48,37 +48,27 @@ namespace WarmlyShip /// Добавляемый корабль /// Позиция /// - public bool Insert(T ship, int position) + public int Insert(T airplane, int position) { - int positionNullElement = position; - while (Get(positionNullElement) != null) + if (!isCorrectPosition(position)) { - positionNullElement++; + return -1; } - // Если изначальная позиция была некорректной или пустых элементов справа не оказалось возвращаем false - if (!isCorrectPosition(positionNullElement)) - { - return false; - } - while (positionNullElement != position) // Смещение вправо - { - _places[positionNullElement] = _places[positionNullElement - 1]; - positionNullElement--; - } - _places[position] = ship; - return true; + _places.Insert(position, airplane); + return position; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) - { + public T Remove(int position) + { if (!isCorrectPosition(position)) - return false; - _places[position-1] = null; - return true; + return null; + var result = _places[position]; + _places.RemoveAt(position); + return result; } /// /// Получение объекта из набора по позиции @@ -89,13 +79,11 @@ namespace WarmlyShip { get { - // TODO проверка позиции - return _places[position]; + return isCorrectPosition(position) && position < Count ? _places[position] : null; } set { - // TODO проверка позиции - // TODO вставка в список по позиции + Insert(value, position); } } ///